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 "GeomAPI_Shape.h"
23 #include <GeomAPI_Vertex.h>
24 #include <GeomAPI_Edge.h>
25 #include <GeomAPI_Wire.h>
26 #include <GeomAPI_Face.h>
27 #include <GeomAPI_Shell.h>
28 #include <GeomAPI_Solid.h>
30 #include <BRep_Tool.hxx>
31 #include <BRepAlgoAPI_Section.hxx>
32 #include <BRepBndLib.hxx>
33 #include <BRepBuilderAPI_FindPlane.hxx>
34 #include <BRepExtrema_DistShapeShape.hxx>
35 #include <BRepTools.hxx>
36 #include <Bnd_Box.hxx>
37 #include <Geom_Circle.hxx>
38 #include <Geom_Conic.hxx>
39 #include <Geom_Curve.hxx>
40 #include <Geom_Ellipse.hxx>
41 #include <Geom_Hyperbola.hxx>
42 #include <Geom_Line.hxx>
43 #include <Geom_Parabola.hxx>
44 #include <Geom_Plane.hxx>
45 #include <Geom_RectangularTrimmedSurface.hxx>
46 #include <Geom_TrimmedCurve.hxx>
47 #include <TopExp_Explorer.hxx>
49 #include <TopoDS_Iterator.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <NCollection_List.hxx>
53 #include <BOPAlgo_CheckerSI.hxx>
54 #include <BOPDS_DS.hxx>
57 #include <algorithm> // for std::transform
59 #include <BRepTools.hxx>
61 #define MY_SHAPE implPtr<TopoDS_Shape>()
63 GeomAPI_Shape::GeomAPI_Shape()
64 : GeomAPI_Interface(new TopoDS_Shape())
68 std::shared_ptr<GeomAPI_Shape> GeomAPI_Shape::emptyCopied() const
70 GeomShapePtr aShape(new GeomAPI_Shape());
71 aShape->setImpl(new TopoDS_Shape(MY_SHAPE->EmptyCopied()));
75 bool GeomAPI_Shape::isNull() const
77 return MY_SHAPE->IsNull() == Standard_True;
80 bool GeomAPI_Shape::isEqual(const std::shared_ptr<GeomAPI_Shape> theShape) const
85 return theShape->isNull();
86 if (theShape->isNull())
89 return MY_SHAPE->IsEqual(theShape->impl<TopoDS_Shape>()) == Standard_True;
92 bool GeomAPI_Shape::isSame(const std::shared_ptr<GeomAPI_Shape> theShape) const
97 return theShape->isNull();
98 if (theShape->isNull())
101 return MY_SHAPE->IsSame(theShape->impl<TopoDS_Shape>()) == Standard_True;
104 bool GeomAPI_Shape::isVertex() const
106 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
107 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_VERTEX;
110 bool GeomAPI_Shape::isEdge() const
112 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
113 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_EDGE;
116 bool GeomAPI_Shape::isWire() const
118 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
119 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_WIRE;
122 bool GeomAPI_Shape::isFace() const
124 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
125 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_FACE;
128 bool GeomAPI_Shape::isShell() const
130 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
131 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SHELL;
134 bool GeomAPI_Shape::isCompound() const
136 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
137 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND;
140 bool GeomAPI_Shape::isCompoundOfSolids() const
142 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
143 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
145 bool isAtLeastOne = false;
146 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
147 if (aSubs.Value().IsNull() || aSubs.Value().ShapeType() != TopAbs_SOLID)
154 GeomAPI_Shape::ShapeType GeomAPI_Shape::typeOfCompoundShapes() const
156 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
157 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
160 for(TopoDS_Iterator aSubs(aShape); aSubs.More(); aSubs.Next()) {
161 if (!aSubs.Value().IsNull()) {
163 aType = aSubs.Value().ShapeType();
164 else if (aSubs.Value().ShapeType() != aType)
168 return (GeomAPI_Shape::ShapeType) aType;
171 // adds the nopt-compound elements recursively to the list
172 static void addSimpleToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
174 if (!theShape.IsNull()) {
175 if (theShape.ShapeType() == TopAbs_COMPOUND) {
176 for(TopoDS_Iterator aSubs(theShape); aSubs.More(); aSubs.Next()) {
177 addSimpleToList(aSubs.Value(), theList);
180 theList.Append(theShape);
185 bool GeomAPI_Shape::isConnectedTopology() const
187 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
188 if (aShape.IsNull() || aShape.ShapeType() != TopAbs_COMPOUND)
190 // list of simple elements that are not detected in connection to others
191 NCollection_List<TopoDS_Shape> aNotConnected;
192 addSimpleToList(aShape, aNotConnected);
193 if (aNotConnected.IsEmpty()) // an empty compound
196 // collect here the group of connected subs, starting with one first element
197 NCollection_List<TopoDS_Shape> aNewConnected;
198 aNewConnected.Append(aNotConnected.First());
199 aNotConnected.RemoveFirst();
200 // iterate until some new element become connected
201 while(!aNewConnected.IsEmpty() && !aNotConnected.IsEmpty()) {
202 NCollection_List<TopoDS_Shape> aNew; // very new connected to new connected
203 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotConnected);
204 while(aNotIter.More()) {
205 // optimization to avoid TopExp_Explorer double-cycle, collect all vertices in the list first
206 NCollection_List<TopoDS_Shape> aNotVertices;
207 for(TopExp_Explorer anExp1(aNotIter.Value(), TopAbs_VERTEX); anExp1.More(); anExp1.Next()) {
208 aNotVertices.Append(anExp1.Current());
211 bool aConnected = false;
212 NCollection_List<TopoDS_Shape>::Iterator aNewIter(aNewConnected);
213 for(; !aConnected && aNewIter.More(); aNewIter.Next()) {
214 // checking topological connecion of aNotIter and aNewIter
215 // (if shapes are connected, vertices are connected for sure)
216 TopExp_Explorer anExp2(aNewIter.Value(), TopAbs_VERTEX);
217 for(; !aConnected && anExp2.More(); anExp2.Next()) {
218 NCollection_List<TopoDS_Shape>::Iterator aNotIter(aNotVertices);
219 for(; aNotIter.More(); aNotIter.Next()) {
220 if (aNotIter.Value().IsSame(anExp2.Current())) {
228 aNew.Append(aNotIter.Value());
229 aNotConnected.Remove(aNotIter);
234 // remove all new connected and put to this list very new connected
235 aNewConnected.Clear();
236 aNewConnected.Append(aNew);
238 return aNotConnected.IsEmpty() == Standard_True;
241 bool GeomAPI_Shape::isSolid() const
243 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
244 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_SOLID;
247 bool GeomAPI_Shape::isCompSolid() const
249 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
250 return !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPSOLID;
253 bool GeomAPI_Shape::isPlanar() const
255 TopoDS_Shape aShape = impl<TopoDS_Shape>();
257 if(aShape.IsNull()) {
261 TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
262 if(aShapeType == TopAbs_COMPOUND) {
263 TopoDS_Iterator anIt(aShape);
265 for(; anIt.More(); anIt.Next()) {
269 anIt.Initialize(aShape);
270 aShape = anIt.Value();
274 aShapeType = aShape.ShapeType();
275 if(aShapeType == TopAbs_VERTEX) {
277 } else if(aShapeType == TopAbs_FACE) {
278 const Handle(Geom_Surface)& aSurface = BRep_Tool::Surface(TopoDS::Face(aShape));
279 Handle(Standard_Type) aType = aSurface->DynamicType();
281 if(aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
282 Handle(Geom_RectangularTrimmedSurface) aTrimSurface =
283 Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
284 aType = aTrimSurface->BasisSurface()->DynamicType();
286 return (aType == STANDARD_TYPE(Geom_Plane)) == Standard_True;
288 BRepBuilderAPI_FindPlane aFindPlane(aShape);
289 bool isFound = aFindPlane.Found() == Standard_True;
291 if(!isFound && aShapeType == TopAbs_EDGE) {
292 Standard_Real aFirst, aLast;
293 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFirst, aLast);
294 Handle(Standard_Type) aType = aCurve->DynamicType();
296 if(aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
297 Handle(Geom_TrimmedCurve) aTrimCurve = Handle(Geom_TrimmedCurve)::DownCast(aCurve);
298 aType = aTrimCurve->BasisCurve()->DynamicType();
301 if(aType == STANDARD_TYPE(Geom_Line)
302 || aType == STANDARD_TYPE(Geom_Conic)
303 || aType == STANDARD_TYPE(Geom_Circle)
304 || aType == STANDARD_TYPE(Geom_Ellipse)
305 || aType == STANDARD_TYPE(Geom_Hyperbola)
306 || aType == STANDARD_TYPE(Geom_Parabola)) {
317 std::shared_ptr<GeomAPI_Vertex> GeomAPI_Shape::vertex() const
319 GeomVertexPtr aVertex;
321 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
322 aVertex = GeomVertexPtr(new GeomAPI_Vertex);
323 aVertex->setImpl(new TopoDS_Shape(aShape));
328 std::shared_ptr<GeomAPI_Edge> GeomAPI_Shape::edge() const
332 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
333 anEdge = GeomEdgePtr(new GeomAPI_Edge);
334 anEdge->setImpl(new TopoDS_Shape(aShape));
339 std::shared_ptr<GeomAPI_Wire> GeomAPI_Shape::wire() const
343 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
344 aWire = GeomWirePtr(new GeomAPI_Wire);
345 aWire->setImpl(new TopoDS_Shape(aShape));
350 std::shared_ptr<GeomAPI_Face> GeomAPI_Shape::face() const
354 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
355 aFace = GeomFacePtr(new GeomAPI_Face);
356 aFace->setImpl(new TopoDS_Shape(aShape));
361 std::shared_ptr<GeomAPI_Shell> GeomAPI_Shape::shell() const
365 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
366 aShell = GeomShellPtr(new GeomAPI_Shell);
367 aShell->setImpl(new TopoDS_Shape(aShape));
372 std::shared_ptr<GeomAPI_Solid> GeomAPI_Shape::solid() const
376 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
377 aSolid = GeomSolidPtr(new GeomAPI_Solid);
378 aSolid->setImpl(new TopoDS_Shape(aShape));
383 std::list<std::shared_ptr<GeomAPI_Shape> >
384 GeomAPI_Shape::subShapes(ShapeType theSubShapeType) const
387 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
391 for (TopExp_Explorer anExp(aShape, (TopAbs_ShapeEnum)theSubShapeType);
392 anExp.More(); anExp.Next()) {
393 GeomShapePtr aSub(new GeomAPI_Shape);
394 aSub->setImpl(new TopoDS_Shape(anExp.Current()));
395 aSubs.push_back(aSub);
400 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeType() const
402 const TopoDS_Shape& aShape = impl<TopoDS_Shape>();
404 return GeomAPI_Shape::SHAPE;
406 ShapeType aST = GeomAPI_Shape::SHAPE;
408 switch(aShape.ShapeType()) {
409 case TopAbs_COMPOUND:
410 aST = GeomAPI_Shape::COMPOUND;
412 case TopAbs_COMPSOLID:
413 aST = GeomAPI_Shape::COMPSOLID;
416 aST = GeomAPI_Shape::SOLID;
419 aST = GeomAPI_Shape::SHELL;
422 aST = GeomAPI_Shape::FACE;
425 aST = GeomAPI_Shape::WIRE;
428 aST = GeomAPI_Shape::EDGE;
431 aST = GeomAPI_Shape::VERTEX;
434 aST = GeomAPI_Shape::SHAPE;
441 GeomAPI_Shape::ShapeType GeomAPI_Shape::shapeTypeByStr(std::string theType)
443 std::transform(theType.begin(), theType.end(), theType.begin(), ::toupper);
444 if (theType == "COMPOUND")
446 if (theType == "COMPSOLID")
448 if (theType == "SOLID")
450 if (theType == "SHELL")
452 if (theType == "FACE")
454 if (theType == "WIRE")
456 if (theType == "EDGE")
458 if (theType == "VERTEX")
460 return SHAPE; // default
463 std::string GeomAPI_Shape::shapeTypeStr() const
465 ShapeType aShapeType = shapeType();
466 std::string aShapeTypeStr;
470 aShapeTypeStr = "COMPOUND";
474 aShapeTypeStr = "COMPSOLID";
478 aShapeTypeStr = "SOLID";
482 aShapeTypeStr = "SHELL";
486 aShapeTypeStr = "FACE";
490 aShapeTypeStr = "WIRE";
494 aShapeTypeStr = "EDGE";
498 aShapeTypeStr = "VERTEX";
502 aShapeTypeStr = "SHAPE";
507 return aShapeTypeStr;
510 GeomAPI_Shape::Orientation GeomAPI_Shape::orientation() const
512 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
514 switch(anOrientation) {
515 case TopAbs_FORWARD: return FORWARD;
516 case TopAbs_REVERSED: return REVERSED;
517 case TopAbs_INTERNAL: return INTERNAL;
518 case TopAbs_EXTERNAL: return EXTERNAL;
519 default: return FORWARD;
523 void GeomAPI_Shape::setOrientation(const GeomAPI_Shape::Orientation theOrientation)
525 TopAbs_Orientation anOrientation = MY_SHAPE->Orientation();
527 switch(theOrientation) {
528 case FORWARD: MY_SHAPE->Orientation(TopAbs_FORWARD); break;
529 case REVERSED: MY_SHAPE->Orientation(TopAbs_REVERSED); break;
530 case INTERNAL: MY_SHAPE->Orientation(TopAbs_INTERNAL); break;
531 case EXTERNAL: MY_SHAPE->Orientation(TopAbs_EXTERNAL); break;
535 void GeomAPI_Shape::reverse()
540 bool GeomAPI_Shape::isSubShape(const std::shared_ptr<GeomAPI_Shape> theShape,
541 const bool theCheckOrientation) const
543 if(!theShape.get()) {
547 const TopoDS_Shape& aShapeToSearch = theShape->impl<TopoDS_Shape>();
548 if(aShapeToSearch.IsNull()) {
552 for(TopExp_Explorer anExp(*MY_SHAPE, aShapeToSearch.ShapeType()); anExp.More(); anExp.Next()) {
553 if(theCheckOrientation ?
554 aShapeToSearch.IsEqual(anExp.Current()) : aShapeToSearch.IsSame(anExp.Current())) {
562 bool GeomAPI_Shape::computeSize(double& theXmin, double& theYmin, double& theZmin,
563 double& theXmax, double& theYmax, double& theZmax) const
565 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
569 BRepBndLib::Add(aShape, aBndBox);
570 aBndBox.Get(theXmin, theYmin, theZmin, theXmax, theYmax, theZmax);
574 std::string GeomAPI_Shape::getShapeStream() const
576 std::ostringstream aStream;
577 const TopoDS_Shape& aShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
578 BRepTools::Write(aShape, aStream);
579 return aStream.str();
582 GeomShapePtr GeomAPI_Shape::intersect(const GeomShapePtr theShape) const
584 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
585 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
587 BRepAlgoAPI_Section aCommon(aShape1, aShape2);
588 if (!aCommon.IsDone())
589 return GeomShapePtr();
591 TopoDS_Shape aResult = aCommon.Shape();
592 if (aResult.ShapeType() == TopAbs_COMPOUND) {
593 NCollection_List<TopoDS_Shape> aSubs;
594 addSimpleToList(aResult, aSubs);
595 if(aSubs.Size() == 1) {
596 aResult = aSubs.First();
597 } else if(aSubs.Size() == 0) {
598 return GeomShapePtr();
602 GeomShapePtr aResShape(new GeomAPI_Shape);
603 aResShape->setImpl(new TopoDS_Shape(aResult));
607 bool GeomAPI_Shape::isIntersect(const GeomShapePtr theShape) const
609 if(!theShape.get()) {
613 const TopoDS_Shape& aShape1 = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
614 const TopoDS_Shape& aShape2 = theShape->impl<TopoDS_Shape>();
616 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
618 if(aDist.IsDone() && aDist.Value() < Precision::Confusion()) {
625 void GeomAPI_Shape::translate(const std::shared_ptr<GeomAPI_Dir> theDir, const double theOffset)
627 gp_Dir aDir = theDir->impl<gp_Dir>();
628 gp_Vec aTrsfVec(aDir.XYZ() * theOffset);
629 gp_Trsf aTranslation;
630 aTranslation.SetTranslation(aTrsfVec);
631 TopoDS_Shape aResult = MY_SHAPE->Moved(aTranslation);
632 setImpl(new TopoDS_Shape(aResult));
635 bool GeomAPI_Shape::isSelfIntersected(const int theLevelOfCheck) const
637 BOPAlgo_CheckerSI aCSI; // checker of self-interferences
638 aCSI.SetLevelOfCheck(theLevelOfCheck);
639 TopTools_ListOfShape aList;
640 const TopoDS_Shape& aThisShape = const_cast<GeomAPI_Shape*>(this)->impl<TopoDS_Shape>();
641 aList.Append(aThisShape);
642 aCSI.SetArguments(aList);
644 if (aCSI.HasErrors() || aCSI.DS().Interferences().Extent() > 0) {