1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_Pipe.h"
23 #include "GeomAlgoAPI_DFLoader.h"
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
29 #include <BRep_Tool.hxx>
30 #include <BRepOffsetAPI_MakePipe.hxx>
31 #include <BRepOffsetAPI_MakePipeShell.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <Geom_Curve.hxx>
34 #include <Geom_Line.hxx>
36 #include <NCollection_List.hxx>
37 #include <TopExp_Explorer.hxx>
39 #include <TopoDS_Shape.hxx>
41 static bool getBase(TopoDS_Shape& theBaseOut,
42 TopAbs_ShapeEnum& theBaseTypeOut,
43 const GeomShapePtr theBaseShape);
44 static bool getPath(TopoDS_Wire& thePathOut,
45 const GeomShapePtr thePathShape);
46 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
47 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
49 //==================================================================================================
50 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
51 const GeomShapePtr thePathShape)
53 build(theBaseShape, thePathShape);
56 //==================================================================================================
57 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
58 const GeomShapePtr thePathShape,
59 const GeomShapePtr theBiNormal)
61 build(theBaseShape, thePathShape, theBiNormal);
64 //==================================================================================================
65 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
66 const ListOfShape& theLocations,
67 const GeomShapePtr thePathShape)
69 build(theBaseShapes, theLocations, thePathShape);
72 //==================================================================================================
73 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
74 const GeomShapePtr thePathShape)
76 // Getting base shape.
77 if(!theBaseShape.get()) {
80 TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
81 if(aBaseShape.IsNull()) {
84 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
85 if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
86 aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
87 aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
92 TopoDS_Wire aPathWire;
93 if(!getPath(aPathWire, thePathShape)) {
98 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
102 aPipeBuilder->Build();
105 if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
109 this->initialize(aPipeBuilder);
112 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
113 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
116 TopoDS_Shape aResult = aPipeBuilder->Shape();
117 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
118 GeomShapePtr aGeomSh(new GeomAPI_Shape());
119 aGeomSh->setImpl(new TopoDS_Shape(aResult));
120 this->setShape(aGeomSh);
124 //==================================================================================================
125 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
126 const GeomShapePtr thePathShape,
127 const GeomShapePtr theBiNormal)
129 // Getting base shape.
130 TopoDS_Shape aBaseShape;
131 TopAbs_ShapeEnum aBaseShapeType;
132 if(!getBase(aBaseShape, aBaseShapeType, theBaseShape)) {
137 TopoDS_Wire aPathWire;
138 if(!getPath(aPathWire, thePathShape)) {
142 // Getting Bi-Normal.
143 if(!theBiNormal.get()) {
146 TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
147 if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
150 TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
151 Standard_Real aFirst, aLast;
152 Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
153 Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
154 if(aBiNormalLine.IsNull()) {
157 gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
160 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
164 aPipeBuilder->Add(aBaseShape);
165 aPipeBuilder->SetMode(aBiNormalDir);
166 if(!buildPipe(aPipeBuilder)) {
170 this->initialize(aPipeBuilder);
173 if(aBaseShapeType == TopAbs_FACE) {
174 if(aPipeBuilder->MakeSolid() == Standard_False) {
178 TopoDS_Shape aResult = aPipeBuilder->Shape();
179 if(aResult.IsNull()) {
184 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
185 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
188 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
189 GeomShapePtr aGeomSh(new GeomAPI_Shape());
190 aGeomSh->setImpl(new TopoDS_Shape(aResult));
191 this->setShape(aGeomSh);
195 //==================================================================================================
196 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
197 const ListOfShape& theLocations,
198 const GeomShapePtr thePathShape)
200 if(theBaseShapes.empty() ||
201 (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
205 bool aHasLocations = false;
206 if(!theLocations.empty()) {
207 aHasLocations = true;
211 TopoDS_Wire aPathWire;
212 if(!getPath(aPathWire, thePathShape)) {
217 Standard_Boolean isDone = Standard_False;
218 bool anIsSolidNeeded = false;
219 BRepOffsetAPI_MakePipeShell* aPipeBuilder;
220 for(int i = 0; i < 2; ++i) {
221 aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
225 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
226 ListOfShape::const_iterator aLocIt = theLocations.cbegin();
227 while(aBaseIt != theBaseShapes.cend()) {
228 GeomShapePtr aBase = *aBaseIt;
229 TopoDS_Shape aBaseShape;
230 TopAbs_ShapeEnum aBaseShapeType;
231 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
236 if(aBaseShapeType == TopAbs_FACE) {
237 anIsSolidNeeded = true;
241 GeomShapePtr aLocation = *aLocIt;
242 if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
246 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
248 aPipeBuilder->Add(aBaseShape, aLocationVertex);
250 aPipeBuilder->Add(aBaseShape);
254 if(aPipeBuilder->IsReady() == Standard_False) {
260 // Try to use Descrete Trihedron mode.
261 aPipeBuilder->SetDiscreteMode();
263 aPipeBuilder->Build();
264 isDone = aPipeBuilder->IsDone();
277 this->initialize(aPipeBuilder);
280 if(anIsSolidNeeded) {
281 if(aPipeBuilder->MakeSolid() == Standard_False) {
285 TopoDS_Shape aResult = aPipeBuilder->Shape();
288 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
289 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
290 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
291 this->addFromShape(aFromShape);
292 this->addToShape(aToShape);
295 if(aResult.IsNull()) {
298 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
299 GeomShapePtr aGeomSh(new GeomAPI_Shape());
300 aGeomSh->setImpl(new TopoDS_Shape(aResult));
301 this->setShape(aGeomSh);
305 //==================================================================================================
306 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
307 ListOfShape& theHistory)
309 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
312 // Auxilary functions:
313 //==================================================================================================
314 bool getBase(TopoDS_Shape& theBaseOut,
315 TopAbs_ShapeEnum& theBaseTypeOut,
316 const GeomShapePtr theBaseShape)
318 if(!theBaseShape.get()) {
322 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
323 if(theBaseOut.IsNull()) {
326 theBaseTypeOut = theBaseOut.ShapeType();
327 if(theBaseTypeOut == TopAbs_VERTEX) {
329 } else if(theBaseTypeOut == TopAbs_EDGE) {
330 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
331 } else if(theBaseTypeOut == TopAbs_WIRE) {
333 } else if(theBaseTypeOut == TopAbs_FACE) {
334 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
335 theBaseOut = anExp.Current();
343 //==================================================================================================
344 bool getPath(TopoDS_Wire& thePathOut,
345 const GeomShapePtr thePathShape)
347 if(!thePathShape.get()) {
351 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
352 if(aPathShape.IsNull()) {
355 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
356 if(aPathShapeType == TopAbs_EDGE) {
357 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
358 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
359 } else if(aPathShapeType == TopAbs_WIRE) {
360 thePathOut = TopoDS::Wire(aPathShape);
368 //==================================================================================================
369 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
371 thePipeBuilder->Build();
373 Standard_Boolean isDone = thePipeBuilder->IsDone();
376 // Try to use Descrete Trihedron mode.
377 thePipeBuilder->SetDiscreteMode();
378 thePipeBuilder->Build();
379 isDone = thePipeBuilder->IsDone();
382 return isDone == Standard_True;
385 //==================================================================================================
386 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
390 TopAbs_ShapeEnum aType = theShape.ShapeType();
391 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
392 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
393 GeomShapePtr aGeomShape(new GeomAPI_Shape());
394 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
395 aList.push_back(aGeomShape);
398 GeomShapePtr aGeomShape(new GeomAPI_Shape());
399 aGeomShape->setImpl(new TopoDS_Shape(theShape));
400 aList.push_back(aGeomShape);