qoccmakebottle.cpp

Go to the documentation of this file.
00001 /************************************************************************************
00002 **
00003 ** This file is part of the QtOPENCASCADE Toolkit.
00004 **
00005 ** Copyright (C) 2006, 2007, 2008 QtOCC Team Members
00006 **               Peter Dolbey, Marc Britten, Stephane Routelous
00007 **               Stefan Boeykens, Pawel Dobrolowski, Walter Steffe
00008 **               Álvaro Castro Castilla, Dirk Burski, Fotis Sioutis
00009 **
00010 ** This library is free software; you can redistribute it and/or
00011 ** modify it under the terms of the GNU Lesser General Public
00012 ** License as published by the Free Software Foundation; either
00013 ** version 2.1 of the License, or (at your option) any later version.
00014 ** 
00015 ** This library is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 ** Lesser General Public License for more details.
00019 ** 
00020 ** You should have received a copy of the GNU Lesser General Public
00021 ** License along with this library; if not, write to the Free Software
00022 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023 **
00024 ***********************************************************************************/
00025 
00026 #include <BRep_Tool.hxx>
00027 #include <BRepAlgoAPI_Fuse.hxx>
00028 
00029 #include <BRepBuilderAPI_MakeVertex.hxx>
00030 #include <BRepBuilderAPI_MakeEdge.hxx>
00031 #include <BRepBuilderAPI_MakeFace.hxx>
00032 #include <BRepBuilderAPI_MakeWire.hxx>
00033 #include <BRepBuilderAPI_Transform.hxx>
00034 
00035 #include <BRepFilletAPI_MakeFillet.hxx>
00036 
00037 #include <BRepLib.hxx>
00038 
00039 #include <BRepOffsetAPI_MakeThickSolid.hxx>
00040 #include <BRepOffsetAPI_ThruSections.hxx>
00041 
00042 #include <BRepPrimAPI_MakeCylinder.hxx>
00043 #include <BRepPrimAPI_MakePrism.hxx>
00044 
00045 #include <GC_Root.hxx>
00046 #include <GC_MakeArcOfCircle.hxx>
00047 #include <GC_MakeSegment.hxx>
00048 
00049 #include <GCE2d_MakeSegment.hxx>
00050 
00051 #include <gp.hxx>
00052 #include <gp_Ax1.hxx>
00053 #include <gp_Ax2.hxx>
00054 #include <gp_Ax2d.hxx>
00055 #include <gp_Dir.hxx>
00056 #include <gp_Dir2d.hxx>
00057 #include <gp_Pnt.hxx>
00058 #include <gp_Pnt2d.hxx>
00059 #include <gp_Trsf.hxx>
00060 #include <gp_Vec.hxx>
00061 
00062 #include <Geom_CylindricalSurface.hxx>
00063 #include <Geom_Plane.hxx>
00064 #include <Geom_Surface.hxx>
00065 #include <Geom_TrimmedCurve.hxx>
00066 
00067 #include <Geom2d_Ellipse.hxx>
00068 #include <Geom2d_TrimmedCurve.hxx>
00069 
00070 #include <TopExp_Explorer.hxx>
00071 
00072 #include <TopoDS.hxx>
00073 #include <TopoDS_Vertex.hxx>
00074 #include <TopoDS_Edge.hxx>
00075 #include <TopoDS_Face.hxx>
00076 #include <TopoDS_Wire.hxx>
00077 #include <TopoDS_Shape.hxx>
00078 #include <TopoDS_Compound.hxx>
00079 
00080 #include <TopTools_ListOfShape.hxx>
00081 #include <AIS_InteractiveContext.hxx>
00082 #include <AIS_Shape.hxx>
00083 
00084 #include "qoccinternal.h"
00085 
00086 TopoDS_Shape
00087 MakeBottle(const Standard_Real myWidth , const Standard_Real myHeight ,
00088                    const Standard_Real myThickness)
00089 {
00090         //Profile : Define Support Points
00091         gp_Pnt aPnt1(-myWidth / 2. , 0 , 0);
00092     gp_Pnt aPnt2(-myWidth / 2. , -myThickness / 4. , 0);
00093     gp_Pnt aPnt3(0 , -myThickness / 2. , 0);
00094     gp_Pnt aPnt4(myWidth / 2. , -myThickness / 4. , 0);
00095     gp_Pnt aPnt5(myWidth / 2. , 0 , 0);
00096 
00097         //Profile : Define the Geometry
00098         Handle(Geom_TrimmedCurve) aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4);
00099     Handle(Geom_TrimmedCurve) aSegment1    = GC_MakeSegment(aPnt1 , aPnt2);
00100     Handle(Geom_TrimmedCurve) aSegment2    = GC_MakeSegment(aPnt4 , aPnt5);
00101 
00102         //Profile : Define the Topology
00103         TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
00104         TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle);
00105     TopoDS_Edge aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);
00106         TopoDS_Wire aWire  = BRepBuilderAPI_MakeWire(aEdge1 , aEdge2 , aEdge3);
00107 
00108         //Complete Profile
00109         gp_Ax1 xAxis = gp::OX();
00110         gp_Trsf aTrsf;
00111 
00112     aTrsf.SetMirror(xAxis);
00113 
00114         BRepBuilderAPI_Transform aBRepTrsf(aWire , aTrsf);
00115         TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();
00116         TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);
00117 
00118         BRepBuilderAPI_MakeWire mkWire;
00119 
00120     mkWire.Add(aWire);
00121     mkWire.Add(aMirroredWire);
00122 
00123     TopoDS_Wire myWireProfile = mkWire.Wire();
00124 
00125         //Body : Prism the Profile
00126         TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
00127         gp_Vec          aPrismVec(0 , 0 , myHeight);
00128 
00129         TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile , aPrismVec);
00130 
00131         //Body : Apply Fillets
00132         BRepFilletAPI_MakeFillet mkFillet(myBody);
00133         TopExp_Explorer                  aEdgeExplorer(myBody , TopAbs_EDGE);
00134 
00135         while(aEdgeExplorer.More()){
00136                 
00137                 TopoDS_Edge aEdge = TopoDS::Edge(aEdgeExplorer.Current());
00138 
00139         //Add edge to fillet algorithm
00140                 mkFillet.Add(myThickness / 12. , aEdge);
00141 
00142         aEdgeExplorer.Next();
00143         }
00144 
00145         myBody = mkFillet.Shape();
00146 
00147         //Body : Add the Neck   
00148         gp_Pnt neckLocation(0 , 0 , myHeight);
00149         gp_Dir neckNormal = gp::DZ();
00150     gp_Ax2 neckAx2(neckLocation , neckNormal);
00151 
00152         Standard_Real myNeckRadius = myThickness / 4.;
00153         Standard_Real myNeckHeight = myHeight / 10;
00154 
00155         BRepPrimAPI_MakeCylinder MKCylinder(neckAx2 , myNeckRadius , myNeckHeight);
00156         TopoDS_Shape myNeck = MKCylinder.Shape();
00157 
00158         myBody = BRepAlgoAPI_Fuse(myBody , myNeck);
00159 
00160         //Body : Create a Hollowed Solid
00161         TopoDS_Face   faceToRemove;
00162         Standard_Real zMax = -1;
00163 
00164         for(TopExp_Explorer aFaceExplorer(myBody , TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
00165                 
00166                 TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());
00167 
00168         //Check if <aFace> is the top face of the bottle's neck
00169                 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
00170 
00171                 if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){
00172 
00173                         Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);
00174 
00175                         gp_Pnt            aPnt = aPlane->Location();
00176                         Standard_Real aZ   = aPnt.Z();
00177                         
00178                         if(aZ > zMax){
00179                                 
00180                                 zMax             = aZ;
00181                 faceToRemove = aFace;
00182                         }
00183                 }
00184         }
00185 
00186         TopTools_ListOfShape facesToRemove;
00187         
00188         facesToRemove.Append(faceToRemove);
00189 
00190         myBody = BRepOffsetAPI_MakeThickSolid(myBody , facesToRemove , -myThickness / 50 , 1.e-3);
00191 
00192 
00193         //return myBody;
00194         //Threading : Create Surfaces
00195         Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2 , myNeckRadius * 0.99);
00196         Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2 , myNeckRadius * 1.05);
00197 
00198         //Threading : Define 2D Curves
00199         gp_Pnt2d aPnt(2. * PI , myNeckHeight / 2.);
00200     gp_Dir2d aDir(2. * PI , myNeckHeight / 4.);
00201     gp_Ax2d aAx2d(aPnt , aDir);
00202 
00203         Standard_Real aMajor = 2. * PI;
00204     Standard_Real aMinor = myNeckHeight / 10;
00205 
00206     Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(aAx2d , aMajor , aMinor);
00207     Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4);
00208 
00209         Handle(Geom2d_TrimmedCurve) aArc1 = new Geom2d_TrimmedCurve(anEllipse1 , 0 , PI);
00210         Handle(Geom2d_TrimmedCurve) aArc2 = new Geom2d_TrimmedCurve(anEllipse2 , 0 , PI);
00211 
00212         gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0);
00213         gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(PI);
00214         
00215         Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2);
00216 
00217         //Threading : Build Edges and Wires
00218         TopoDS_Edge aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(aArc1 , aCyl1);
00219         TopoDS_Edge aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment , aCyl1);
00220         TopoDS_Edge aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(aArc2 , aCyl2);
00221         TopoDS_Edge aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment , aCyl2);
00222 
00223         TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1 , aEdge2OnSurf1);
00224         TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2 , aEdge2OnSurf2);
00225 
00226         BRepLib::BuildCurves3d(threadingWire1);
00227         BRepLib::BuildCurves3d(threadingWire2);
00228 
00229         //Create Threading
00230         BRepOffsetAPI_ThruSections aTool(Standard_True);
00231         
00232         aTool.AddWire(threadingWire1);
00233         aTool.AddWire(threadingWire2);
00234         aTool.CheckCompatibility(Standard_False);
00235 
00236         TopoDS_Shape myThreading = aTool.Shape();
00237 
00238         //Building the resulting compound
00239         TopoDS_Compound aRes;
00240         BRep_Builder aBuilder;
00241         aBuilder.MakeCompound (aRes);
00242 
00243         aBuilder.Add (aRes, myBody);
00244         aBuilder.Add (aRes, myThreading);
00245 
00246         return aRes;
00247 }
00248 
00249 void LoadBottle ( Handle(AIS_InteractiveContext) theContext )
00250 {
00251         TopoDS_Shape aBottle=MakeBottle(50,70,30);
00252 
00253     Handle(AIS_Shape) AISBottle=new AIS_Shape(aBottle);
00254     theContext->SetMaterial(AISBottle,Graphic3d_NOM_GOLD);
00255     theContext->SetColor(AISBottle, Quantity_NOC_RED);
00256     theContext->SetDisplayMode(AISBottle,1,Standard_False);
00257         //theContext->SetTransparency(AISBottle,0.6);
00258     theContext->Display(AISBottle, Standard_False);
00259         //theContext->SetCurrentObject(AISBottle, Standard_False);
00260 
00261 }
00262 
00263 
00264 void AddVertex (double x, double y, double z, Handle_AIS_InteractiveContext theContext)
00265 {
00266         TopoDS_Vertex aVertex=BRepBuilderAPI_MakeVertex( gp_Pnt(x,y,z) );
00267         Handle(AIS_Shape) AISVertex = new AIS_Shape(aVertex);
00268         // context is the handle to an AIS_InteractiveContext object.
00269         theContext->Display(AISVertex); 
00270 }
00271 
00272 void ShowOrigin ( Handle_AIS_InteractiveContext theContext )
00273 {
00274         AddVertex ( 0.0, 0.0, 0.0, theContext); 
00275 }

Generated on Sat Feb 23 21:22:18 2008 for QtGEOM by  doxygen 1.4.7