1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "GeomAlgoAPI_Pipe.h"
22 #include "GeomAlgoAPI_DFLoader.h"
24 #include <GeomAPI_Dir.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Lin.h>
27 #include <GeomAPI_ShapeExplorer.h>
29 #include <BRep_Tool.hxx>
30 #include <BRepExtrema_DistShapeShape.hxx>
31 #include <BRepOffsetAPI_MakePipe.hxx>
32 #include <BRepOffsetAPI_MakePipeShell.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom_Line.hxx>
37 #include <NCollection_List.hxx>
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Shape.hxx>
41 #include <Precision.hxx>
43 static bool getBase(TopoDS_Shape& theBaseOut,
44 TopAbs_ShapeEnum& theBaseTypeOut,
45 const GeomShapePtr theBaseShape);
46 static bool getPath(TopoDS_Wire& thePathOut,
47 const GeomShapePtr thePathShape);
48 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
49 const TopoDS_Shape& thePath);
50 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
51 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
53 //==================================================================================================
54 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
55 const GeomShapePtr thePathShape)
57 build(theBaseShape, thePathShape);
60 //==================================================================================================
61 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
62 const GeomShapePtr thePathShape,
63 const GeomShapePtr theBiNormal)
65 build(theBaseShape, thePathShape, theBiNormal);
68 //==================================================================================================
69 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
70 const ListOfShape& theLocations,
71 const GeomShapePtr thePathShape)
73 build(theBaseShapes, theLocations, thePathShape);
76 //==================================================================================================
77 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
78 const GeomShapePtr thePathShape)
80 // Getting base shape.
81 if(!theBaseShape.get()) {
84 TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
85 if(aBaseShape.IsNull()) {
88 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
89 if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
90 aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
91 aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
96 TopoDS_Wire aPathWire;
97 if(!getPath(aPathWire, thePathShape)) {
100 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
101 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
102 aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
103 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
104 if (!anOldPath->isSame(aNewPath))
105 addMovedPath(anOldPath, aNewPath);
108 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
112 aPipeBuilder->Build();
115 if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
119 this->initialize(aPipeBuilder);
122 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
123 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
126 TopoDS_Shape aResult = aPipeBuilder->Shape();
127 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
128 GeomShapePtr aGeomSh(new GeomAPI_Shape());
129 aGeomSh->setImpl(new TopoDS_Shape(aResult));
130 this->setShape(aGeomSh);
134 //==================================================================================================
135 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
136 const GeomShapePtr thePathShape,
137 const GeomShapePtr theBiNormal)
139 // Getting base shape and path.
140 TopoDS_Shape aBaseShape;
141 TopAbs_ShapeEnum aBaseShapeType;
142 TopoDS_Wire aPathWire;
143 if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
144 !getPath(aPathWire, thePathShape) ||
145 !theBiNormal.get()) {
149 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
150 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
151 aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
152 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
153 if (!anOldPath->isSame(aNewPath))
154 addMovedPath(anOldPath, aNewPath);
156 // Getting Bi-Normal.
157 TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
158 if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
161 TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
162 Standard_Real aFirst, aLast;
163 Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
164 Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
165 if(aBiNormalLine.IsNull()) {
168 gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
171 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
175 aPipeBuilder->Add(aBaseShape);
176 aPipeBuilder->SetMode(aBiNormalDir);
177 if(!buildPipe(aPipeBuilder)) {
181 this->initialize(aPipeBuilder);
184 if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
187 TopoDS_Shape aResult = aPipeBuilder->Shape();
188 if(aResult.IsNull()) {
193 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
194 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
197 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
198 GeomShapePtr aGeomSh(new GeomAPI_Shape());
199 aGeomSh->setImpl(new TopoDS_Shape(aResult));
200 this->setShape(aGeomSh);
204 //==================================================================================================
205 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
206 const ListOfShape& theLocations,
207 const GeomShapePtr thePathShape)
209 if(theBaseShapes.empty() ||
210 (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
214 // Getting base shape and path.
215 TopoDS_Shape aBaseShape;
216 TopAbs_ShapeEnum aBaseShapeType;
217 TopoDS_Wire aPathWire;
218 if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
219 !getPath(aPathWire, thePathShape)) {
223 TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
224 gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
226 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
227 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
228 aPathWire.Move(aTrsf);
229 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
230 if (!anOldPath->isSame(aNewPath))
231 addMovedPath(anOldPath, aNewPath);
233 // Get locations after moving path shape.
234 std::list<TopoDS_Vertex> aLocations;
235 for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
236 aLocIt != theLocations.cend();
239 GeomShapePtr aLocation = *aLocIt;
240 if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
244 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
245 TopoDS_Vertex aMovedVertex;
246 for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
247 if (anExp.Current().IsPartner(aLocationVertex)) {
248 aMovedVertex = TopoDS::Vertex(anExp.Current());
249 aLocations.push_back(aMovedVertex);
253 if (aMovedVertex.IsNull()) {
258 if (theLocations.size() != aLocations.size()) {
262 bool aHasLocations = !aLocations.empty();
265 Standard_Boolean isDone = Standard_False;
266 bool anIsSolidNeeded = false;
267 BRepOffsetAPI_MakePipeShell* aPipeBuilder;
268 for(int i = 0; i < 2; ++i) {
269 aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
273 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
274 std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
275 while(aBaseIt != theBaseShapes.cend()) {
276 GeomShapePtr aBase = *aBaseIt;
277 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
282 if(aBaseShapeType == TopAbs_FACE) {
283 anIsSolidNeeded = true;
287 aPipeBuilder->Add(aBaseShape, *aLocationsIt);
290 aPipeBuilder->Add(aBaseShape);
294 if(aPipeBuilder->IsReady() == Standard_False) {
300 // Try to use Descrete Trihedron mode.
301 aPipeBuilder->SetDiscreteMode();
303 aPipeBuilder->Build();
304 isDone = aPipeBuilder->IsDone();
317 this->initialize(aPipeBuilder);
320 if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
323 TopoDS_Shape aResult = aPipeBuilder->Shape();
326 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
327 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
328 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
329 fixOrientation(aFromShape);
330 fixOrientation(aToShape);
331 this->addFromShape(aFromShape);
332 this->addToShape(aToShape);
335 if(aResult.IsNull()) {
338 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
339 GeomShapePtr aGeomSh(new GeomAPI_Shape());
340 aGeomSh->setImpl(new TopoDS_Shape(aResult));
341 this->setShape(aGeomSh);
345 //==================================================================================================
346 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
347 ListOfShape& theHistory)
349 if (myMovedPath.isBound(theShape))
350 GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
352 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
355 // Auxilary functions:
356 //==================================================================================================
357 bool getBase(TopoDS_Shape& theBaseOut,
358 TopAbs_ShapeEnum& theBaseTypeOut,
359 const GeomShapePtr theBaseShape)
361 if(!theBaseShape.get()) {
365 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
366 if(theBaseOut.IsNull()) {
369 theBaseTypeOut = theBaseOut.ShapeType();
370 if(theBaseTypeOut == TopAbs_VERTEX) {
372 } else if(theBaseTypeOut == TopAbs_EDGE) {
373 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
374 } else if(theBaseTypeOut == TopAbs_WIRE) {
376 } else if(theBaseTypeOut == TopAbs_FACE) {
377 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
378 theBaseOut = anExp.Current();
386 //==================================================================================================
387 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
389 if(!thePathShape.get()) {
393 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
394 if(aPathShape.IsNull()) {
397 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
398 if(aPathShapeType == TopAbs_EDGE) {
399 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
400 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
401 } else if(aPathShapeType == TopAbs_WIRE) {
402 thePathOut = TopoDS::Wire(aPathShape);
410 //==================================================================================================
411 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
413 gp_Trsf aTranslation;
415 BRepExtrema_DistShapeShape aDist(theBase, thePath);
417 if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
418 gp_Pnt aPntBase = aDist.PointOnShape1(1);
419 gp_Pnt aPntPath = aDist.PointOnShape2(1);
420 aTranslation.SetTranslation(aPntPath, aPntBase);
426 //==================================================================================================
427 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
429 thePipeBuilder->Build();
431 Standard_Boolean isDone = thePipeBuilder->IsDone();
434 // Try to use Descrete Trihedron mode.
435 thePipeBuilder->SetDiscreteMode();
436 thePipeBuilder->Build();
437 isDone = thePipeBuilder->IsDone();
440 return isDone == Standard_True;
443 //==================================================================================================
444 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
448 TopAbs_ShapeEnum aType = theShape.ShapeType();
449 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
450 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
451 GeomShapePtr aGeomShape(new GeomAPI_Shape());
452 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
453 aList.push_back(aGeomShape);
456 GeomShapePtr aGeomShape(new GeomAPI_Shape());
457 aGeomShape->setImpl(new TopoDS_Shape(theShape));
458 aList.push_back(aGeomShape);
464 //==================================================================================================
465 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
468 GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
469 GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
470 for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
471 myMovedPath.bind(anOldExp.current(), aNewExp.current());