1 // Copyright (C) 2014-2020 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>
39 #include <TopExp_Explorer.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <Precision.hxx>
44 static bool getBase(TopoDS_Shape& theBaseOut,
45 TopAbs_ShapeEnum& theBaseTypeOut,
46 const GeomShapePtr theBaseShape);
47 static bool getPath(TopoDS_Wire& thePathOut,
48 const GeomShapePtr thePathShape);
49 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
50 const TopoDS_Shape& thePath);
51 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
52 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
54 //==================================================================================================
55 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
56 const GeomShapePtr thePathShape)
58 build(theBaseShape, thePathShape);
61 //==================================================================================================
62 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
63 const GeomShapePtr thePathShape,
64 const GeomShapePtr theBiNormal)
66 build(theBaseShape, thePathShape, theBiNormal);
69 //==================================================================================================
70 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
71 const ListOfShape& theLocations,
72 const GeomShapePtr thePathShape)
74 build(theBaseShapes, theLocations, thePathShape);
77 //==================================================================================================
78 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
79 const GeomShapePtr thePathShape)
81 // Getting base shape.
82 if(!theBaseShape.get()) {
85 TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
86 if(aBaseShape.IsNull()) {
89 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
90 if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
91 aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
92 aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
97 TopoDS_Wire aPathWire;
98 if(!getPath(aPathWire, thePathShape)) {
101 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
102 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
103 aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
104 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
105 if (!anOldPath->isSame(aNewPath))
106 addMovedPath(anOldPath, aNewPath);
109 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
113 aPipeBuilder->Build();
116 if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
120 this->initialize(aPipeBuilder);
123 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
124 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
127 TopoDS_Shape aResult = aPipeBuilder->Shape();
128 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
129 GeomShapePtr aGeomSh(new GeomAPI_Shape());
130 aGeomSh->setImpl(new TopoDS_Shape(aResult));
131 this->setShape(aGeomSh);
135 //==================================================================================================
136 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
137 const GeomShapePtr thePathShape,
138 const GeomShapePtr theBiNormal)
140 // Getting base shape and path.
141 TopoDS_Shape aBaseShape;
142 TopAbs_ShapeEnum aBaseShapeType;
143 TopoDS_Wire aPathWire;
144 if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
145 !getPath(aPathWire, thePathShape) ||
146 !theBiNormal.get()) {
150 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
151 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
152 aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
153 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
154 if (!anOldPath->isSame(aNewPath))
155 addMovedPath(anOldPath, aNewPath);
157 // Getting Bi-Normal.
158 TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
159 if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
162 TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
163 Standard_Real aFirst, aLast;
164 Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
165 Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
166 if(aBiNormalLine.IsNull()) {
169 gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
172 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
176 aPipeBuilder->Add(aBaseShape);
177 aPipeBuilder->SetMode(aBiNormalDir);
178 if(!buildPipe(aPipeBuilder)) {
182 this->initialize(aPipeBuilder);
185 if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
188 TopoDS_Shape aResult = aPipeBuilder->Shape();
189 if(aResult.IsNull()) {
194 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
195 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
198 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
199 GeomShapePtr aGeomSh(new GeomAPI_Shape());
200 aGeomSh->setImpl(new TopoDS_Shape(aResult));
201 this->setShape(aGeomSh);
205 //==================================================================================================
206 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
207 const ListOfShape& theLocations,
208 const GeomShapePtr thePathShape)
210 if(theBaseShapes.empty() ||
211 (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
215 // Getting base shape and path.
216 TopoDS_Shape aBaseShape;
217 TopAbs_ShapeEnum aBaseShapeType;
218 TopoDS_Wire aPathWire;
219 if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
220 !getPath(aPathWire, thePathShape)) {
224 TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
225 gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
227 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
228 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
229 aPathWire.Move(aTrsf);
230 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
231 if (!anOldPath->isSame(aNewPath))
232 addMovedPath(anOldPath, aNewPath);
234 // Get locations after moving path shape.
235 std::list<TopoDS_Vertex> aLocations;
236 for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
237 aLocIt != theLocations.cend();
240 GeomShapePtr aLocation = *aLocIt;
241 if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
245 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
246 TopoDS_Vertex aMovedVertex;
247 for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
248 if (anExp.Current().IsPartner(aLocationVertex)) {
249 aMovedVertex = TopoDS::Vertex(anExp.Current());
250 aLocations.push_back(aMovedVertex);
254 if (aMovedVertex.IsNull()) {
259 if (theLocations.size() != aLocations.size()) {
263 bool aHasLocations = !aLocations.empty();
266 Standard_Boolean isDone = Standard_False;
267 bool anIsSolidNeeded = false;
268 BRepOffsetAPI_MakePipeShell* aPipeBuilder;
269 for(int i = 0; i < 2; ++i) {
270 aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
274 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
275 std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
276 while(aBaseIt != theBaseShapes.cend()) {
277 GeomShapePtr aBase = *aBaseIt;
278 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
283 if(aBaseShapeType == TopAbs_FACE) {
284 anIsSolidNeeded = true;
288 aPipeBuilder->Add(aBaseShape, *aLocationsIt);
291 aPipeBuilder->Add(aBaseShape);
295 if(aPipeBuilder->IsReady() == Standard_False) {
301 // Try to use Descrete Trihedron mode.
302 aPipeBuilder->SetDiscreteMode();
304 aPipeBuilder->Build();
305 isDone = aPipeBuilder->IsDone();
318 this->initialize(aPipeBuilder);
321 if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
324 TopoDS_Shape aResult = aPipeBuilder->Shape();
327 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
328 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
329 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
330 fixOrientation(aFromShape);
331 fixOrientation(aToShape);
332 this->addFromShape(aFromShape);
333 this->addToShape(aToShape);
336 if(aResult.IsNull()) {
339 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
340 GeomShapePtr aGeomSh(new GeomAPI_Shape());
341 aGeomSh->setImpl(new TopoDS_Shape(aResult));
342 this->setShape(aGeomSh);
346 //==================================================================================================
347 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
348 ListOfShape& theHistory)
350 if (myMovedPath.isBound(theShape))
351 GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
353 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
356 // Auxilary functions:
357 //==================================================================================================
358 bool getBase(TopoDS_Shape& theBaseOut,
359 TopAbs_ShapeEnum& theBaseTypeOut,
360 const GeomShapePtr theBaseShape)
362 if(!theBaseShape.get()) {
366 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
367 if(theBaseOut.IsNull()) {
370 theBaseTypeOut = theBaseOut.ShapeType();
371 if(theBaseTypeOut == TopAbs_VERTEX) {
373 } else if(theBaseTypeOut == TopAbs_EDGE) {
374 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
375 } else if(theBaseTypeOut == TopAbs_WIRE) {
377 } else if(theBaseTypeOut == TopAbs_FACE) {
378 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
379 theBaseOut = anExp.Current();
387 //==================================================================================================
388 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
390 if(!thePathShape.get()) {
394 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
395 if(aPathShape.IsNull()) {
398 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
399 if(aPathShapeType == TopAbs_EDGE) {
400 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
401 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
402 } else if(aPathShapeType == TopAbs_WIRE) {
403 thePathOut = TopoDS::Wire(aPathShape);
411 //==================================================================================================
412 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
414 gp_Trsf aTranslation;
416 BRepExtrema_DistShapeShape aDist(theBase, thePath);
418 if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
419 gp_Pnt aPntBase = aDist.PointOnShape1(1);
420 gp_Pnt aPntPath = aDist.PointOnShape2(1);
421 // get the end point on the path nearest to the found point,
422 // because the path is almost usually started from the one of the sections
423 if (!BRep_Tool::IsClosed(thePath)) {
424 TopoDS_Wire aPathWire = TopoDS::Wire(thePath);
425 if (!aPathWire.IsNull()) {
426 TopoDS_Vertex aV1, aV2;
427 TopExp::Vertices(aPathWire, aV1, aV2);
429 gp_Pnt aStart = BRep_Tool::Pnt(aV1);
430 gp_Pnt anEnd = BRep_Tool::Pnt(aV2);
432 // compare 3 distances
433 double aDistToStart = aPntBase.Distance(aStart);
434 double aDistToEnd = aPntBase.Distance(anEnd);
435 double aMinDist = aPntBase.Distance(aPntPath);
437 static const double THE_THRESHOLD = 0.01; // threshold for distance ratio
438 double aDeltaStart = Abs(aDistToStart - aMinDist);
439 double aDeltaEnd = Abs(aDistToEnd - aMinDist);
440 if (aDeltaStart < THE_THRESHOLD * aDeltaEnd)
442 else if (aDeltaEnd < THE_THRESHOLD * aDeltaStart)
446 aTranslation.SetTranslation(aPntPath, aPntBase);
452 //==================================================================================================
453 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
455 thePipeBuilder->Build();
457 Standard_Boolean isDone = thePipeBuilder->IsDone();
460 // Try to use Descrete Trihedron mode.
461 thePipeBuilder->SetDiscreteMode();
462 thePipeBuilder->Build();
463 isDone = thePipeBuilder->IsDone();
466 return isDone == Standard_True;
469 //==================================================================================================
470 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
474 TopAbs_ShapeEnum aType = theShape.ShapeType();
475 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
476 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
477 GeomShapePtr aGeomShape(new GeomAPI_Shape());
478 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
479 aList.push_back(aGeomShape);
482 GeomShapePtr aGeomShape(new GeomAPI_Shape());
483 aGeomShape->setImpl(new TopoDS_Shape(theShape));
484 aList.push_back(aGeomShape);
490 //==================================================================================================
491 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
494 GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
495 GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
496 for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
497 myMovedPath.bind(anOldExp.current(), aNewExp.current());