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 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
221 bool anIsSolidNeeded = false;
222 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
223 ListOfShape::const_iterator aLocIt = theLocations.cbegin();
224 while(aBaseIt != theBaseShapes.cend()) {
225 GeomShapePtr aBase = *aBaseIt;
226 TopoDS_Shape aBaseShape;
227 TopAbs_ShapeEnum aBaseShapeType;
228 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
233 if(aBaseShapeType == TopAbs_FACE) {
234 anIsSolidNeeded = true;
238 GeomShapePtr aLocation = *aLocIt;
239 if(!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
243 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
245 aPipeBuilder->Add(aBaseShape, aLocationVertex);
247 aPipeBuilder->Add(aBaseShape);
251 if(aPipeBuilder->IsReady() == Standard_False) {
256 if(!buildPipe(aPipeBuilder)) {
260 this->initialize(aPipeBuilder);
263 if(anIsSolidNeeded) {
264 if(aPipeBuilder->MakeSolid() == Standard_False) {
268 TopoDS_Shape aResult = aPipeBuilder->Shape();
271 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
272 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
273 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
274 this->addFromShape(aFromShape);
275 this->addToShape(aToShape);
278 if(aResult.IsNull()) {
281 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
282 GeomShapePtr aGeomSh(new GeomAPI_Shape());
283 aGeomSh->setImpl(new TopoDS_Shape(aResult));
284 this->setShape(aGeomSh);
288 //==================================================================================================
289 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
290 ListOfShape& theHistory)
292 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
295 // Auxilary functions:
296 //==================================================================================================
297 bool getBase(TopoDS_Shape& theBaseOut,
298 TopAbs_ShapeEnum& theBaseTypeOut,
299 const GeomShapePtr theBaseShape)
301 if(!theBaseShape.get()) {
305 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
306 if(theBaseOut.IsNull()) {
309 theBaseTypeOut = theBaseOut.ShapeType();
310 if(theBaseTypeOut == TopAbs_VERTEX) {
312 } else if(theBaseTypeOut == TopAbs_EDGE) {
313 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
314 } else if(theBaseTypeOut == TopAbs_WIRE) {
316 } else if(theBaseTypeOut == TopAbs_FACE) {
317 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
318 theBaseOut = anExp.Current();
326 //==================================================================================================
327 bool getPath(TopoDS_Wire& thePathOut,
328 const GeomShapePtr thePathShape)
330 if(!thePathShape.get()) {
334 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
335 if(aPathShape.IsNull()) {
338 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
339 if(aPathShapeType == TopAbs_EDGE) {
340 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
341 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
342 } else if(aPathShapeType == TopAbs_WIRE) {
343 thePathOut = TopoDS::Wire(aPathShape);
351 //==================================================================================================
352 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
354 thePipeBuilder->Build();
356 Standard_Boolean isDone = thePipeBuilder->IsDone();
359 // Try to use Descrete Trihedron mode.
360 thePipeBuilder->SetDiscreteMode();
361 thePipeBuilder->Build();
362 isDone = thePipeBuilder->IsDone();
365 return isDone == Standard_True;
368 //==================================================================================================
369 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
373 TopAbs_ShapeEnum aType = theShape.ShapeType();
374 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
375 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
376 GeomShapePtr aGeomShape(new GeomAPI_Shape());
377 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
378 aList.push_back(aGeomShape);
381 GeomShapePtr aGeomShape(new GeomAPI_Shape());
382 aGeomShape->setImpl(new TopoDS_Shape(theShape));
383 aList.push_back(aGeomShape);