Salome HOME
1e7887b700fb01d7f867e392ed112b7cb7dd0051
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_Tube.cpp
1 // Copyright (C) 2014-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <GeomAlgoAPI_Tube.h>
21
22 #include <BRepAlgo_FaceRestrictor.hxx>
23
24 #include <BRepBuilderAPI_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeFace.hxx>
26 #include <BRepBuilderAPI_MakeWire.hxx>
27 #include <BRepBuilderAPI_Transform.hxx>
28
29 #include <BRepPrimAPI_MakePrism.hxx>
30
31 #include <gp_Circ.hxx>
32
33 #include <TopoDS_Wire.hxx>
34 #include <TopoDS_Face.hxx>
35
36 //=================================================================================================
37 GeomAlgoAPI_Tube::GeomAlgoAPI_Tube()
38 {
39 }
40
41 //=================================================================================================
42 GeomAlgoAPI_Tube::GeomAlgoAPI_Tube(const double theRMin, const double theRMax, const double theZ)
43 {
44   myRMin = theRMin;
45   myRMax = theRMax;
46   myZ = theZ;
47 }
48
49 //=================================================================================================
50 bool GeomAlgoAPI_Tube::check()
51 {
52   if ((myRMax -myRMin) < Precision::Confusion()) {
53     myError = "Tube builder :: rmin is greater then or equal to rmax.";
54     return false;
55   } else if (myRMin < 0.) {
56     myError = "Tube builder :: rmin is negative.";
57     return false;
58   } else if (myZ < Precision::Confusion()) {
59     myError = "Tube builder :: z is negative or null.";
60     return false;
61   }
62
63   return true;
64 }
65
66 //=================================================================================================
67 void GeomAlgoAPI_Tube::build()
68 {
69   buildTube();
70 }
71
72 //=================================================================================================
73 void GeomAlgoAPI_Tube::buildTube()
74 {
75   myCreatedFaces.clear();
76
77   // Construct the inner and outer circles
78   gp_Pnt anOrigin(0., 0., 0.);
79   gp_Dir aNormal(0., 0., 1.);
80   gp_Circ anInnerCircle(gp_Ax2(anOrigin, aNormal), myRMin);
81   gp_Circ anOuterCircle(gp_Ax2(anOrigin, aNormal), myRMax);
82
83   // Construct the inner wire
84   BRepBuilderAPI_MakeEdge anInnerCircleBuilder(anInnerCircle);
85   anInnerCircleBuilder.Build();
86   BRepBuilderAPI_MakeWire anInnerWireBuilder;
87   anInnerWireBuilder.Add(anInnerCircleBuilder.Edge());
88   anInnerWireBuilder.Build();
89   TopoDS_Wire anInnerWire(anInnerWireBuilder.Wire());
90
91   // Construct the outer wire
92   BRepBuilderAPI_MakeEdge anOuterCircleBuilder(anOuterCircle);
93   anOuterCircleBuilder.Build();
94   BRepBuilderAPI_MakeWire anOuterWireBuilder;
95   anOuterWireBuilder.Add(anOuterCircleBuilder.Edge());
96   anOuterWireBuilder.Build();
97   TopoDS_Wire anOuterWire(anOuterWireBuilder.Wire());
98
99   // Construct the face withe the outer wire
100   BRepBuilderAPI_MakeFace aFaceBuilder(anOuterWire);
101   aFaceBuilder.Build();
102   TopoDS_Face aFace(aFaceBuilder.Face());
103
104   // Construct the hole face
105   BRepAlgo_FaceRestrictor aFaceRestrictor;
106   aFaceRestrictor.Init(aFace, Standard_False, Standard_True);
107   aFaceRestrictor.Add(anInnerWire);
108   aFaceRestrictor.Add(anOuterWire);
109   aFaceRestrictor.Perform();
110   aFace = TopoDS_Face(aFaceRestrictor.Current());
111
112   // Construct the tube
113   gp_Vec aVec(aNormal);
114   gp_Trsf aTrsf;
115   aTrsf.SetTranslation(aVec * -myZ/2);
116   BRepBuilderAPI_Transform *aTranformBuilder = new BRepBuilderAPI_Transform(aFace, aTrsf);
117   if (!aTranformBuilder || !aTranformBuilder->IsDone()) {
118    myError = "Tube builder :: algorithm failed";
119    return;
120   }
121   TopoDS_Shape aMovedBase = aTranformBuilder->Shape();
122   BRepPrimAPI_MakePrism *aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aVec * myZ);
123
124   std::shared_ptr<GeomAPI_Shape> aShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
125   aShape->setImpl(new TopoDS_Shape(aPrismBuilder->Shape()));
126   setShape(aShape);
127
128   setImpl(aPrismBuilder);
129   setBuilderType(OCCT_BRepBuilderAPI_MakeShape);
130
131   setDone(true);
132 }