1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-04-10 18:51:21 +03:00

0023134: Updating Tutorial sample source code

This commit is contained in:
dbv 2012-05-04 19:26:16 +04:00
parent 48a06a3056
commit c45bc3b03a

View File

@ -54,164 +54,138 @@
TopoDS_Shape TopoDS_Shape
MakeBottle(const Standard_Real myWidth , const Standard_Real myHeight , MakeBottle(const Standard_Real myWidth, const Standard_Real myHeight,
const Standard_Real myThickness) const Standard_Real myThickness)
{ {
//Profile : Define Support Points // Profile : Define Support Points
gp_Pnt aPnt1(-myWidth / 2. , 0 , 0); gp_Pnt aPnt1(-myWidth / 2., 0, 0);
gp_Pnt aPnt2(-myWidth / 2. , -myThickness / 4. , 0); gp_Pnt aPnt2(-myWidth / 2., -myThickness / 4., 0);
gp_Pnt aPnt3(0 , -myThickness / 2. , 0); gp_Pnt aPnt3(0, -myThickness / 2., 0);
gp_Pnt aPnt4(myWidth / 2. , -myThickness / 4. , 0); gp_Pnt aPnt4(myWidth / 2., -myThickness / 4., 0);
gp_Pnt aPnt5(myWidth / 2. , 0 , 0); gp_Pnt aPnt5(myWidth / 2., 0, 0);
//Profile : Define the Geometry // Profile : Define the Geometry
Handle(Geom_TrimmedCurve) aArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3 ,aPnt4); Handle(Geom_TrimmedCurve) anArcOfCircle = GC_MakeArcOfCircle(aPnt2,aPnt3,aPnt4);
Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1 , aPnt2); Handle(Geom_TrimmedCurve) aSegment1 = GC_MakeSegment(aPnt1, aPnt2);
Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4 , aPnt5); Handle(Geom_TrimmedCurve) aSegment2 = GC_MakeSegment(aPnt4, aPnt5);
//Profile : Define the Topology // Profile : Define the Topology
TopoDS_Edge aEdge1 = BRepBuilderAPI_MakeEdge(aSegment1); TopoDS_Edge anEdge1 = BRepBuilderAPI_MakeEdge(aSegment1);
TopoDS_Edge aEdge2 = BRepBuilderAPI_MakeEdge(aArcOfCircle); TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(anArcOfCircle);
TopoDS_Edge aEdge3 = BRepBuilderAPI_MakeEdge(aSegment2); TopoDS_Edge anEdge3 = BRepBuilderAPI_MakeEdge(aSegment2);
TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(aEdge1 , aEdge2 , aEdge3); TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge1, anEdge2, anEdge3);
//Complete Profile // Complete Profile
gp_Ax1 xAxis = gp::OX(); gp_Ax1 xAxis = gp::OX();
gp_Trsf aTrsf; gp_Trsf aTrsf;
aTrsf.SetMirror(xAxis); aTrsf.SetMirror(xAxis);
BRepBuilderAPI_Transform aBRepTrsf(aWire, aTrsf);
TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();
TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);
BRepBuilderAPI_Transform aBRepTrsf(aWire , aTrsf); BRepBuilderAPI_MakeWire mkWire;
TopoDS_Shape aMirroredShape = aBRepTrsf.Shape();
TopoDS_Wire aMirroredWire = TopoDS::Wire(aMirroredShape);
BRepBuilderAPI_MakeWire mkWire;
mkWire.Add(aWire); mkWire.Add(aWire);
mkWire.Add(aMirroredWire); mkWire.Add(aMirroredWire);
TopoDS_Wire myWireProfile = mkWire.Wire(); TopoDS_Wire myWireProfile = mkWire.Wire();
//Body : Prism the Profile // Body : Prism the Profile
TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile); TopoDS_Face myFaceProfile = BRepBuilderAPI_MakeFace(myWireProfile);
gp_Vec aPrismVec(0 , 0 , myHeight); gp_Vec aPrismVec(0, 0, myHeight);
TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile, aPrismVec);
TopoDS_Shape myBody = BRepPrimAPI_MakePrism(myFaceProfile , aPrismVec); // Body : Apply Fillets
BRepFilletAPI_MakeFillet mkFillet(myBody);
TopExp_Explorer anEdgeExplorer(myBody, TopAbs_EDGE);
while(anEdgeExplorer.More()){
TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExplorer.Current());
//Add edge to fillet algorithm
mkFillet.Add(myThickness / 12., anEdge);
anEdgeExplorer.Next();
}
//Body : Apply Fillets myBody = mkFillet.Shape();
BRepFilletAPI_MakeFillet mkFillet(myBody);
TopExp_Explorer aEdgeExplorer(myBody , TopAbs_EDGE);
while(aEdgeExplorer.More()){ // Body : Add the Neck
gp_Pnt neckLocation(0, 0, myHeight);
TopoDS_Edge aEdge = TopoDS::Edge(aEdgeExplorer.Current()); gp_Dir neckAxis = gp::DZ();
gp_Ax2 neckAx2(neckLocation, neckAxis);
//Add edge to fillet algorithm Standard_Real myNeckRadius = myThickness / 4.;
mkFillet.Add(myThickness / 12. , aEdge); Standard_Real myNeckHeight = myHeight / 10.;
aEdgeExplorer.Next(); BRepPrimAPI_MakeCylinder MKCylinder(neckAx2, myNeckRadius, myNeckHeight);
} TopoDS_Shape myNeck = MKCylinder.Shape();
myBody = mkFillet.Shape(); myBody = BRepAlgoAPI_Fuse(myBody, myNeck);
//Body : Add the Neck // Body : Create a Hollowed Solid
gp_Pnt neckLocation(0 , 0 , myHeight); TopoDS_Face faceToRemove;
gp_Dir neckNormal = gp::DZ(); Standard_Real zMax = -1;
gp_Ax2 neckAx2(neckLocation , neckNormal);
Standard_Real myNeckRadius = myThickness / 4.; for(TopExp_Explorer aFaceExplorer(myBody, TopAbs_FACE); aFaceExplorer.More(); aFaceExplorer.Next()){
Standard_Real myNeckHeight = myHeight / 10; TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());
// Check if <aFace> is the top face of the bottles neck
BRepPrimAPI_MakeCylinder MKCylinder(neckAx2 , myNeckRadius , myNeckHeight); Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
TopoDS_Shape myNeck = MKCylinder.Shape(); if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){
Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);
myBody = BRepAlgoAPI_Fuse(myBody , myNeck); gp_Pnt aPnt = aPlane->Location();
Standard_Real aZ = aPnt.Z();
//Body : Create a Hollowed Solid if(aZ > zMax){
TopoDS_Face faceToRemove; zMax = aZ;
Standard_Real zMax = -1;
for(TopExp_Explorer aFaceExplorer(myBody , TopAbs_FACE) ; aFaceExplorer.More() ; aFaceExplorer.Next()){
TopoDS_Face aFace = TopoDS::Face(aFaceExplorer.Current());
//Check if <aFace> is the top face of the bottle's neck
Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
if(aSurface->DynamicType() == STANDARD_TYPE(Geom_Plane)){
Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(aSurface);
gp_Pnt aPnt = aPlane->Location();
Standard_Real aZ = aPnt.Z();
if(aZ > zMax){
zMax = aZ;
faceToRemove = aFace; faceToRemove = aFace;
} }
} }
} }
TopTools_ListOfShape facesToRemove; TopTools_ListOfShape facesToRemove;
facesToRemove.Append(faceToRemove);
facesToRemove.Append(faceToRemove); myBody = BRepOffsetAPI_MakeThickSolid(myBody, facesToRemove, -myThickness / 50, 1.e-3);
// Threading : Create Surfaces
Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 0.99);
Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2, myNeckRadius * 1.05);
myBody = BRepOffsetAPI_MakeThickSolid(myBody , facesToRemove , -myThickness / 50 , 1.e-3); // Threading : Define 2D Curves
gp_Pnt2d aPnt(2. * M_PI, myNeckHeight / 2.);
gp_Dir2d aDir(2. * M_PI, myNeckHeight / 4.);
gp_Ax2d anAx2d(aPnt, aDir);
Standard_Real aMajor = 2. * M_PI;
//return myBody;
//Threading : Create Surfaces
Handle(Geom_CylindricalSurface) aCyl1 = new Geom_CylindricalSurface(neckAx2 , myNeckRadius * 0.99);
Handle(Geom_CylindricalSurface) aCyl2 = new Geom_CylindricalSurface(neckAx2 , myNeckRadius * 1.05);
//Threading : Define 2D Curves
gp_Pnt2d aPnt(2. * M_PI , myNeckHeight / 2.);
gp_Dir2d aDir(2. * M_PI , myNeckHeight / 4.);
gp_Ax2d aAx2d(aPnt , aDir);
Standard_Real aMajor = 2. * M_PI;
Standard_Real aMinor = myNeckHeight / 10; Standard_Real aMinor = myNeckHeight / 10;
Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(aAx2d , aMajor , aMinor); Handle(Geom2d_Ellipse) anEllipse1 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor);
Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(aAx2d , aMajor , aMinor / 4); Handle(Geom2d_Ellipse) anEllipse2 = new Geom2d_Ellipse(anAx2d, aMajor, aMinor / 4);
Handle(Geom2d_TrimmedCurve) anArc1 = new Geom2d_TrimmedCurve(anEllipse1, 0, M_PI);
Handle(Geom2d_TrimmedCurve) anArc2 = new Geom2d_TrimmedCurve(anEllipse2, 0, M_PI);
gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0);
gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(M_PI);
Handle(Geom2d_TrimmedCurve) aArc1 = new Geom2d_TrimmedCurve(anEllipse1 , 0 , M_PI); Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1, anEllipsePnt2);
Handle(Geom2d_TrimmedCurve) aArc2 = new Geom2d_TrimmedCurve(anEllipse2 , 0 , M_PI); // Threading : Build Edges and Wires
TopoDS_Edge anEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(anArc1, aCyl1);
TopoDS_Edge anEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment, aCyl1);
TopoDS_Edge anEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(anArc2, aCyl2);
TopoDS_Edge anEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment, aCyl2);
TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(anEdge1OnSurf1, anEdge2OnSurf1);
TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(anEdge1OnSurf2, anEdge2OnSurf2);
BRepLib::BuildCurves3d(threadingWire1);
BRepLib::BuildCurves3d(threadingWire2);
gp_Pnt2d anEllipsePnt1 = anEllipse1->Value(0); // Create Threading
gp_Pnt2d anEllipsePnt2 = anEllipse1->Value(M_PI); BRepOffsetAPI_ThruSections aTool(Standard_True);
aTool.AddWire(threadingWire1);
Handle(Geom2d_TrimmedCurve) aSegment = GCE2d_MakeSegment(anEllipsePnt1 , anEllipsePnt2); aTool.AddWire(threadingWire2);
aTool.CheckCompatibility(Standard_False);
//Threading : Build Edges and Wires TopoDS_Shape myThreading = aTool.Shape();
TopoDS_Edge aEdge1OnSurf1 = BRepBuilderAPI_MakeEdge(aArc1 , aCyl1);
TopoDS_Edge aEdge2OnSurf1 = BRepBuilderAPI_MakeEdge(aSegment , aCyl1);
TopoDS_Edge aEdge1OnSurf2 = BRepBuilderAPI_MakeEdge(aArc2 , aCyl2);
TopoDS_Edge aEdge2OnSurf2 = BRepBuilderAPI_MakeEdge(aSegment , aCyl2);
TopoDS_Wire threadingWire1 = BRepBuilderAPI_MakeWire(aEdge1OnSurf1 , aEdge2OnSurf1); // Building the Resulting Compound
TopoDS_Wire threadingWire2 = BRepBuilderAPI_MakeWire(aEdge1OnSurf2 , aEdge2OnSurf2); TopoDS_Compound aRes;
BRep_Builder aBuilder;
aBuilder.MakeCompound (aRes);
aBuilder.Add (aRes, myBody);
aBuilder.Add (aRes, myThreading);
BRepLib::BuildCurves3d(threadingWire1); return aRes;
BRepLib::BuildCurves3d(threadingWire2);
//Create Threading
BRepOffsetAPI_ThruSections aTool(Standard_True);
aTool.AddWire(threadingWire1);
aTool.AddWire(threadingWire2);
aTool.CheckCompatibility(Standard_False);
TopoDS_Shape myThreading = aTool.Shape();
//Building the resulting compound
TopoDS_Compound aRes;
BRep_Builder aBuilder;
aBuilder.MakeCompound (aRes);
aBuilder.Add (aRes, myBody);
aBuilder.Add (aRes, myThreading);
return aRes;
} }