1 // Copyright (C) 2014-2022 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 <BOPAlgo_ArgumentAnalyzer.hxx>
35 #include <Geom_Curve.hxx>
36 #include <Geom_Line.hxx>
38 #include <NCollection_List.hxx>
40 #include <TopExp_Explorer.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <Precision.hxx>
45 static bool getBase(TopoDS_Shape& theBaseOut,
46 TopAbs_ShapeEnum& theBaseTypeOut,
47 const GeomShapePtr theBaseShape);
48 static bool getPath(TopoDS_Wire& thePathOut,
49 const GeomShapePtr thePathShape);
50 static gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase,
51 const TopoDS_Shape& thePath);
52 static bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder);
53 static ListOfShape getListFromShape(const TopoDS_Shape& theShape);
55 //==================================================================================================
56 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
57 const GeomShapePtr thePathShape)
59 build(theBaseShape, thePathShape);
62 //==================================================================================================
63 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const GeomShapePtr theBaseShape,
64 const GeomShapePtr thePathShape,
65 const GeomShapePtr theBiNormal)
67 build(theBaseShape, thePathShape, theBiNormal);
70 //==================================================================================================
71 GeomAlgoAPI_Pipe::GeomAlgoAPI_Pipe(const ListOfShape& theBaseShapes,
72 const ListOfShape& theLocations,
73 const GeomShapePtr thePathShape)
75 build(theBaseShapes, theLocations, thePathShape);
78 //==================================================================================================
79 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
80 const GeomShapePtr thePathShape)
82 // Getting base shape.
83 if(!theBaseShape.get()) {
86 TopoDS_Shape aBaseShape = theBaseShape->impl<TopoDS_Shape>();
87 if(aBaseShape.IsNull()) {
90 TopAbs_ShapeEnum aBaseShapeType = aBaseShape.ShapeType();
91 if(aBaseShapeType != TopAbs_VERTEX && aBaseShapeType != TopAbs_EDGE &&
92 aBaseShapeType != TopAbs_WIRE && aBaseShapeType != TopAbs_FACE &&
93 aBaseShapeType != TopAbs_SHELL && aBaseShapeType != TopAbs_COMPOUND) {
98 TopoDS_Wire aPathWire;
99 if(!getPath(aPathWire, thePathShape)) {
102 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
103 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
104 aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
105 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
106 if (!anOldPath->isSame(aNewPath))
107 addMovedPath(anOldPath, aNewPath);
110 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
114 aPipeBuilder->Build();
117 if (!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
122 // Check for self-interfering result
123 BOPAlgo_ArgumentAnalyzer aChecker;
124 aChecker.SetShape1(aPipeBuilder->Shape());
125 aChecker.SelfInterMode() = Standard_True;
126 aChecker.StopOnFirstFaulty() = Standard_True;
128 if (aChecker.HasFaulty()) {
129 myError = "Self-interfering result.";
134 this->initialize(aPipeBuilder);
137 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
138 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
141 TopoDS_Shape aResult = aPipeBuilder->Shape();
142 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
143 GeomShapePtr aGeomSh(new GeomAPI_Shape());
144 aGeomSh->setImpl(new TopoDS_Shape(aResult));
145 this->setShape(aGeomSh);
149 //==================================================================================================
150 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
151 const GeomShapePtr thePathShape,
152 const GeomShapePtr theBiNormal)
154 // Getting base shape and path.
155 TopoDS_Shape aBaseShape;
156 TopAbs_ShapeEnum aBaseShapeType;
157 TopoDS_Wire aPathWire;
158 if (!getBase(aBaseShape, aBaseShapeType, theBaseShape) ||
159 !getPath(aPathWire, thePathShape) ||
160 !theBiNormal.get()) {
164 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
165 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
166 aPathWire.Move(getPathToBaseTranslation(theBaseShape->impl<TopoDS_Shape>(), aPathWire));
167 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
168 if (!anOldPath->isSame(aNewPath))
169 addMovedPath(anOldPath, aNewPath);
171 // Getting Bi-Normal.
172 TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
173 if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
176 TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
177 Standard_Real aFirst, aLast;
178 Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
179 Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
180 if(aBiNormalLine.IsNull()) {
183 gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
186 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
190 aPipeBuilder->Add(aBaseShape);
191 aPipeBuilder->SetMode(aBiNormalDir);
192 if(!buildPipe(aPipeBuilder)) {
196 this->initialize(aPipeBuilder);
199 if(aBaseShapeType == TopAbs_FACE && !aPipeBuilder->MakeSolid()) {
202 TopoDS_Shape aResult = aPipeBuilder->Shape();
203 if(aResult.IsNull()) {
208 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
209 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
212 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
213 GeomShapePtr aGeomSh(new GeomAPI_Shape());
214 aGeomSh->setImpl(new TopoDS_Shape(aResult));
215 this->setShape(aGeomSh);
219 //==================================================================================================
220 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
221 const ListOfShape& theLocations,
222 const GeomShapePtr thePathShape)
224 if(theBaseShapes.empty() ||
225 (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
229 // Getting base shape and path.
230 TopoDS_Shape aBaseShape;
231 TopAbs_ShapeEnum aBaseShapeType;
232 TopoDS_Wire aPathWire;
233 if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front()) ||
234 !getPath(aPathWire, thePathShape)) {
238 TopoDS_Shape aReallyBase = theBaseShapes.front()->impl<TopoDS_Shape>();
239 gp_Trsf aTrsf = getPathToBaseTranslation(aReallyBase, aPathWire);
241 GeomShapePtr anOldPath(new GeomAPI_Shape), aNewPath(new GeomAPI_Shape);
242 anOldPath->setImpl(new TopoDS_Shape(aPathWire));
243 aPathWire.Move(aTrsf);
244 aNewPath->setImpl(new TopoDS_Shape(aPathWire));
245 if (!anOldPath->isSame(aNewPath))
246 addMovedPath(anOldPath, aNewPath);
248 // Get locations after moving path shape.
249 std::list<TopoDS_Vertex> aLocations;
250 for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
251 aLocIt != theLocations.cend();
254 GeomShapePtr aLocation = *aLocIt;
255 if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
259 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
260 TopoDS_Vertex aMovedVertex;
261 for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
262 if (anExp.Current().IsPartner(aLocationVertex)) {
263 aMovedVertex = TopoDS::Vertex(anExp.Current());
264 aLocations.push_back(aMovedVertex);
268 if (aMovedVertex.IsNull()) {
273 if (theLocations.size() != aLocations.size()) {
277 bool aHasLocations = !aLocations.empty();
280 Standard_Boolean isDone = Standard_False;
281 bool anIsSolidNeeded = false;
282 BRepOffsetAPI_MakePipeShell* aPipeBuilder;
283 for(int i = 0; i < 2; ++i) {
284 aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
288 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
289 std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
290 while(aBaseIt != theBaseShapes.cend()) {
291 GeomShapePtr aBase = *aBaseIt;
292 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
297 if(aBaseShapeType == TopAbs_FACE) {
298 anIsSolidNeeded = true;
302 aPipeBuilder->Add(aBaseShape, *aLocationsIt);
305 aPipeBuilder->Add(aBaseShape);
309 if(aPipeBuilder->IsReady() == Standard_False) {
315 // Try to use Descrete Trihedron mode.
316 aPipeBuilder->SetDiscreteMode();
318 aPipeBuilder->Build();
319 isDone = aPipeBuilder->IsDone();
332 this->initialize(aPipeBuilder);
335 if(anIsSolidNeeded && !aPipeBuilder->MakeSolid()) {
338 TopoDS_Shape aResult = aPipeBuilder->Shape();
341 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
342 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
343 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
344 fixOrientation(aFromShape);
345 fixOrientation(aToShape);
346 this->addFromShape(aFromShape);
347 this->addToShape(aToShape);
350 if(aResult.IsNull()) {
353 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
354 GeomShapePtr aGeomSh(new GeomAPI_Shape());
355 aGeomSh->setImpl(new TopoDS_Shape(aResult));
356 this->setShape(aGeomSh);
360 //==================================================================================================
361 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
362 ListOfShape& theHistory)
364 if (myMovedPath.isBound(theShape))
365 GeomAlgoAPI_MakeShape::generated(myMovedPath.find(theShape), theHistory);
367 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
370 // Auxilary functions:
371 //==================================================================================================
372 bool getBase(TopoDS_Shape& theBaseOut,
373 TopAbs_ShapeEnum& theBaseTypeOut,
374 const GeomShapePtr theBaseShape)
376 if(!theBaseShape.get()) {
380 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
381 if(theBaseOut.IsNull()) {
384 theBaseTypeOut = theBaseOut.ShapeType();
385 if(theBaseTypeOut == TopAbs_VERTEX) {
387 } else if(theBaseTypeOut == TopAbs_EDGE) {
388 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
389 } else if(theBaseTypeOut == TopAbs_WIRE) {
391 } else if(theBaseTypeOut == TopAbs_FACE) {
392 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
393 theBaseOut = anExp.Current();
401 //==================================================================================================
402 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
404 if(!thePathShape.get()) {
408 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
409 if(aPathShape.IsNull()) {
412 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
413 if(aPathShapeType == TopAbs_EDGE) {
414 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
415 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
416 } else if(aPathShapeType == TopAbs_WIRE) {
417 thePathOut = TopoDS::Wire(aPathShape);
425 //==================================================================================================
426 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
428 gp_Trsf aTranslation;
430 BRepExtrema_DistShapeShape aDist(theBase, thePath);
432 if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
433 gp_Pnt aPntBase = aDist.PointOnShape1(1);
434 gp_Pnt aPntPath = aDist.PointOnShape2(1);
435 // get the end point on the path nearest to the found point,
436 // because the path is almost usually started from the one of the sections
437 if (!BRep_Tool::IsClosed(thePath)) {
438 TopoDS_Wire aPathWire = TopoDS::Wire(thePath);
439 if (!aPathWire.IsNull()) {
440 TopoDS_Vertex aV1, aV2;
441 TopExp::Vertices(aPathWire, aV1, aV2);
443 gp_Pnt aStart = BRep_Tool::Pnt(aV1);
444 gp_Pnt anEnd = BRep_Tool::Pnt(aV2);
446 // compare 3 distances
447 double aDistToStart = aPntBase.Distance(aStart);
448 double aDistToEnd = aPntBase.Distance(anEnd);
449 double aMinDist = aPntBase.Distance(aPntPath);
451 static const double THE_THRESHOLD = 0.01; // threshold for distance ratio
452 double aDeltaStart = Abs(aDistToStart - aMinDist);
453 double aDeltaEnd = Abs(aDistToEnd - aMinDist);
454 if (aDeltaStart < THE_THRESHOLD * aDeltaEnd)
456 else if (aDeltaEnd < THE_THRESHOLD * aDeltaStart)
459 aPntPath = aPntBase; // no translation
462 aTranslation.SetTranslation(aPntPath, aPntBase);
468 //==================================================================================================
469 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
471 thePipeBuilder->Build();
473 Standard_Boolean isDone = thePipeBuilder->IsDone();
476 // Try to use Descrete Trihedron mode.
477 thePipeBuilder->SetDiscreteMode();
478 thePipeBuilder->Build();
479 isDone = thePipeBuilder->IsDone();
482 return isDone == Standard_True;
485 //==================================================================================================
486 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
490 TopAbs_ShapeEnum aType = theShape.ShapeType();
491 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
492 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
493 GeomShapePtr aGeomShape(new GeomAPI_Shape());
494 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
495 aList.push_back(aGeomShape);
498 GeomShapePtr aGeomShape(new GeomAPI_Shape());
499 aGeomShape->setImpl(new TopoDS_Shape(theShape));
500 aList.push_back(aGeomShape);
506 //==================================================================================================
507 void GeomAlgoAPI_Pipe::addMovedPath(GeomShapePtr thePath, GeomShapePtr theMoved)
510 GeomAPI_ShapeExplorer anOldExp(thePath, GeomAPI_Shape::EDGE);
511 GeomAPI_ShapeExplorer aNewExp(theMoved, GeomAPI_Shape::EDGE);
512 for(; anOldExp.more(); anOldExp.next(), aNewExp.next()) {
513 myMovedPath.bind(anOldExp.current(), aNewExp.current());