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 <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 aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
103 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPathWire, aBaseShape);
107 aPipeBuilder->Build();
110 if(!aPipeBuilder->IsDone() || aPipeBuilder->Shape().IsNull()) {
114 this->initialize(aPipeBuilder);
117 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
118 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
121 TopoDS_Shape aResult = aPipeBuilder->Shape();
122 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
123 GeomShapePtr aGeomSh(new GeomAPI_Shape());
124 aGeomSh->setImpl(new TopoDS_Shape(aResult));
125 this->setShape(aGeomSh);
129 //==================================================================================================
130 void GeomAlgoAPI_Pipe::build(const GeomShapePtr theBaseShape,
131 const GeomShapePtr thePathShape,
132 const GeomShapePtr theBiNormal)
134 // Getting base shape.
135 TopoDS_Shape aBaseShape;
136 TopAbs_ShapeEnum aBaseShapeType;
137 if(!getBase(aBaseShape, aBaseShapeType, theBaseShape)) {
142 TopoDS_Wire aPathWire;
143 if(!getPath(aPathWire, thePathShape)) {
147 aPathWire.Move(getPathToBaseTranslation(aBaseShape, aPathWire));
149 // Getting Bi-Normal.
150 if(!theBiNormal.get()) {
153 TopoDS_Shape aBiNormalShape = theBiNormal->impl<TopoDS_Shape>();
154 if(aBiNormalShape.IsNull() || aBiNormalShape.ShapeType() != TopAbs_EDGE) {
157 TopoDS_Edge aBiNormalEdge = TopoDS::Edge(aBiNormalShape);
158 Standard_Real aFirst, aLast;
159 Handle(Geom_Curve) aBiNormalCurve = BRep_Tool::Curve(aBiNormalEdge, aFirst, aLast);
160 Handle(Geom_Line) aBiNormalLine = Handle(Geom_Line)::DownCast(aBiNormalCurve);
161 if(aBiNormalLine.IsNull()) {
164 gp_Dir aBiNormalDir = aBiNormalLine->Lin().Direction();
167 BRepOffsetAPI_MakePipeShell* aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
171 aPipeBuilder->Add(aBaseShape);
172 aPipeBuilder->SetMode(aBiNormalDir);
173 if(!buildPipe(aPipeBuilder)) {
177 this->initialize(aPipeBuilder);
180 if(aBaseShapeType == TopAbs_FACE) {
181 if(aPipeBuilder->MakeSolid() == Standard_False) {
185 TopoDS_Shape aResult = aPipeBuilder->Shape();
186 if(aResult.IsNull()) {
191 this->setToShapes(getListFromShape(aPipeBuilder->LastShape()));
192 this->setFromShapes(getListFromShape(aPipeBuilder->FirstShape()));
195 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
196 GeomShapePtr aGeomSh(new GeomAPI_Shape());
197 aGeomSh->setImpl(new TopoDS_Shape(aResult));
198 this->setShape(aGeomSh);
202 //==================================================================================================
203 void GeomAlgoAPI_Pipe::build(const ListOfShape& theBaseShapes,
204 const ListOfShape& theLocations,
205 const GeomShapePtr thePathShape)
207 if(theBaseShapes.empty() ||
208 (!theLocations.empty() && theLocations.size() != theBaseShapes.size())) {
213 TopoDS_Shape aBaseShape;
214 TopAbs_ShapeEnum aBaseShapeType;
215 if (!getBase(aBaseShape, aBaseShapeType, theBaseShapes.front())) {
219 TopoDS_Wire aPathWire;
220 if(!getPath(aPathWire, thePathShape)) {
224 gp_Trsf aTrsf = getPathToBaseTranslation(aBaseShape, aPathWire);
225 aPathWire.Move(aTrsf);
227 // Get locations after moving path shape.
228 std::list<TopoDS_Vertex> aLocations;
229 for (ListOfShape::const_iterator aLocIt = theLocations.cbegin();
230 aLocIt != theLocations.cend();
233 GeomShapePtr aLocation = *aLocIt;
234 if (!aLocation.get() || aLocation->shapeType() != GeomAPI_Shape::VERTEX) {
238 TopoDS_Vertex aLocationVertex = aLocation->impl<TopoDS_Vertex>();
239 TopoDS_Vertex aMovedVertex;
240 for (TopExp_Explorer anExp(aPathWire, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
241 if (anExp.Current().IsPartner(aLocationVertex)) {
242 aMovedVertex = TopoDS::Vertex(anExp.Current());
243 aLocations.push_back(aMovedVertex);
247 if (aMovedVertex.IsNull()) {
252 if (theLocations.size() != aLocations.size()) {
256 bool aHasLocations = !aLocations.empty();
259 Standard_Boolean isDone = Standard_False;
260 bool anIsSolidNeeded = false;
261 BRepOffsetAPI_MakePipeShell* aPipeBuilder;
262 for(int i = 0; i < 2; ++i) {
263 aPipeBuilder = new BRepOffsetAPI_MakePipeShell(aPathWire);
267 ListOfShape::const_iterator aBaseIt = theBaseShapes.cbegin();
268 std::list<TopoDS_Vertex>::const_iterator aLocationsIt = aLocations.cbegin();
269 while(aBaseIt != theBaseShapes.cend()) {
270 GeomShapePtr aBase = *aBaseIt;
271 if(!getBase(aBaseShape, aBaseShapeType, aBase)) {
276 if(aBaseShapeType == TopAbs_FACE) {
277 anIsSolidNeeded = true;
281 aPipeBuilder->Add(aBaseShape, *aLocationsIt);
284 aPipeBuilder->Add(aBaseShape);
288 if(aPipeBuilder->IsReady() == Standard_False) {
294 // Try to use Descrete Trihedron mode.
295 aPipeBuilder->SetDiscreteMode();
297 aPipeBuilder->Build();
298 isDone = aPipeBuilder->IsDone();
311 this->initialize(aPipeBuilder);
314 if(anIsSolidNeeded) {
315 if(aPipeBuilder->MakeSolid() == Standard_False) {
319 TopoDS_Shape aResult = aPipeBuilder->Shape();
322 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
323 aFromShape->setImpl(new TopoDS_Shape(aPipeBuilder->FirstShape()));
324 aToShape->setImpl(new TopoDS_Shape(aPipeBuilder->LastShape()));
325 this->addFromShape(aFromShape);
326 this->addToShape(aToShape);
329 if(aResult.IsNull()) {
332 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
333 GeomShapePtr aGeomSh(new GeomAPI_Shape());
334 aGeomSh->setImpl(new TopoDS_Shape(aResult));
335 this->setShape(aGeomSh);
339 //==================================================================================================
340 void GeomAlgoAPI_Pipe::generated(const GeomShapePtr theShape,
341 ListOfShape& theHistory)
343 GeomAlgoAPI_MakeShape::generated(theShape, theHistory);
346 // Auxilary functions:
347 //==================================================================================================
348 bool getBase(TopoDS_Shape& theBaseOut,
349 TopAbs_ShapeEnum& theBaseTypeOut,
350 const GeomShapePtr theBaseShape)
352 if(!theBaseShape.get()) {
356 theBaseOut = theBaseShape->impl<TopoDS_Shape>();
357 if(theBaseOut.IsNull()) {
360 theBaseTypeOut = theBaseOut.ShapeType();
361 if(theBaseTypeOut == TopAbs_VERTEX) {
363 } else if(theBaseTypeOut == TopAbs_EDGE) {
364 theBaseOut = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseOut)).Shape();
365 } else if(theBaseTypeOut == TopAbs_WIRE) {
367 } else if(theBaseTypeOut == TopAbs_FACE) {
368 TopExp_Explorer anExp(theBaseOut, TopAbs_WIRE);
369 theBaseOut = anExp.Current();
377 //==================================================================================================
378 bool getPath(TopoDS_Wire& thePathOut, const GeomShapePtr thePathShape)
380 if(!thePathShape.get()) {
384 TopoDS_Shape aPathShape = thePathShape->impl<TopoDS_Shape>();
385 if(aPathShape.IsNull()) {
388 TopAbs_ShapeEnum aPathShapeType = aPathShape.ShapeType();
389 if(aPathShapeType == TopAbs_EDGE) {
390 TopoDS_Edge aPathEdge = TopoDS::Edge(aPathShape);
391 thePathOut = BRepBuilderAPI_MakeWire(aPathEdge).Wire();
392 } else if(aPathShapeType == TopAbs_WIRE) {
393 thePathOut = TopoDS::Wire(aPathShape);
401 //==================================================================================================
402 gp_Trsf getPathToBaseTranslation(const TopoDS_Shape& theBase, const TopoDS_Shape& thePath)
404 gp_Trsf aTranslation;
406 BRepExtrema_DistShapeShape aDist(theBase, thePath);
408 if (aDist.IsDone() && aDist.Value() > Precision::Confusion()) {
409 gp_Pnt aPntBase = aDist.PointOnShape1(1);
410 gp_Pnt aPntPath = aDist.PointOnShape2(1);
411 aTranslation.SetTranslation(aPntPath, aPntBase);
417 //==================================================================================================
418 bool buildPipe(BRepOffsetAPI_MakePipeShell* thePipeBuilder)
420 thePipeBuilder->Build();
422 Standard_Boolean isDone = thePipeBuilder->IsDone();
425 // Try to use Descrete Trihedron mode.
426 thePipeBuilder->SetDiscreteMode();
427 thePipeBuilder->Build();
428 isDone = thePipeBuilder->IsDone();
431 return isDone == Standard_True;
434 //==================================================================================================
435 ListOfShape getListFromShape(const TopoDS_Shape& theShape)
439 TopAbs_ShapeEnum aType = theShape.ShapeType();
440 if(aType == TopAbs_WIRE || aType == TopAbs_SHELL || aType == TopAbs_COMPOUND) {
441 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
442 GeomShapePtr aGeomShape(new GeomAPI_Shape());
443 aGeomShape->setImpl(new TopoDS_Shape(anIt.Value()));
444 aList.push_back(aGeomShape);
447 GeomShapePtr aGeomShape(new GeomAPI_Shape());
448 aGeomShape->setImpl(new TopoDS_Shape(theShape));
449 aList.push_back(aGeomShape);