1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_Prism.cpp
5 // Author: Dmitry Bobylev
7 #include <GeomAlgoAPI_Prism.h>
9 #include <GeomAPI_Face.h>
10 #include <GeomAPI_Pln.h>
11 #include <GeomAPI_Pnt.h>
12 #include <GeomAPI_ShapeExplorer.h>
13 #include <GeomAPI_XYZ.h>
14 #include <GeomAlgoAPI_DFLoader.h>
15 #include <GeomAlgoAPI_FaceBuilder.h>
16 #include <GeomAlgoAPI_MakeShapeList.h>
17 #include <GeomAlgoAPI_ShapeTools.h>
19 #include <Bnd_Box.hxx>
20 #include <BRep_Builder.hxx>
21 #include <BRep_Tool.hxx>
22 #include <BRepAlgoAPI_Cut.hxx>
23 #include <BRepBndLib.hxx>
24 #include <BRepBuilderAPI_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeShape.hxx>
26 #include <BRepBuilderAPI_MakeWire.hxx>
27 #include <BRepCheck_Analyzer.hxx>
28 #include <BRepExtrema_ExtCF.hxx>
29 #include <BRepGProp.hxx>
30 #include <BRepOffsetAPI_MakePipe.hxx>
31 #include <BRepTools.hxx>
32 #include <Geom_Plane.hxx>
34 #include <GProp_GProps.hxx>
35 #include <TCollection_AsciiString.hxx>
36 #include <TopExp_Explorer.hxx>
38 #include <TopoDS_Edge.hxx>
39 #include <TopoDS_Shell.hxx>
40 #include <TopoDS_Solid.hxx>
41 #include <TopoDS_Wire.hxx>
42 #include <TopTools_ListIteratorOfListOfShape.hxx>
44 //=================================================================================================
45 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
50 build(theBasis, std::shared_ptr<GeomAPI_Shape>(), theToSize, std::shared_ptr<GeomAPI_Shape>(), theFromSize);
53 //=================================================================================================
54 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(std::shared_ptr<GeomAPI_Shape> theBasis,
55 std::shared_ptr<GeomAPI_Shape> theToShape,
57 std::shared_ptr<GeomAPI_Shape> theFromShape,
61 build(theBasis, theToShape, theToSize, theFromShape, theFromSize);
64 //=================================================================================================
65 void GeomAlgoAPI_Prism::build(const std::shared_ptr<GeomAPI_Shape>& theBasis,
66 const std::shared_ptr<GeomAPI_Shape>& theToShape,
68 const std::shared_ptr<GeomAPI_Shape>& theFromShape,
72 (((!theFromShape && !theToShape) || (theFromShape && theToShape && theFromShape->isEqual(theToShape)))
73 && (theFromSize == -theToSize))) {
77 // If bounding faces was not set creating them.
78 std::shared_ptr<GeomAPI_Face> aBaseFace;
79 if(theBasis->shapeType() == GeomAPI_Shape::SHELL) {
80 GeomAPI_ShapeExplorer anExp(theBasis, GeomAPI_Shape::FACE);
82 std::shared_ptr<GeomAPI_Shape> aFaceOnShell = anExp.current();
83 aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(aFaceOnShell));
86 aBaseFace = std::shared_ptr<GeomAPI_Face>(new GeomAPI_Face(theBasis));
88 if(!aBaseFace.get()) {
91 std::shared_ptr<GeomAPI_Pln> aBasePln = aBaseFace->getPlane();
92 std::shared_ptr<GeomAPI_Dir> aBaseDir = aBasePln->direction();
93 std::shared_ptr<GeomAPI_Pnt> aBaseLoc = aBasePln->location();
94 std::shared_ptr<GeomAPI_Shape> aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
96 std::shared_ptr<GeomAPI_Shape> aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
97 std::shared_ptr<GeomAPI_Shape> aBoundingToShape = theToShape ? theToShape : aBasePlane;
99 // Moving bounding faces according to "from" and "to" sizes.
100 std::shared_ptr<GeomAPI_Face> aFromFace(new GeomAPI_Face(aBoundingFromShape));
101 std::shared_ptr<GeomAPI_Pln> aFromPln = aFromFace->getPlane();
102 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
103 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
105 std::shared_ptr<GeomAPI_Face> aToFace(new GeomAPI_Face(aBoundingToShape));
106 std::shared_ptr<GeomAPI_Pln> aToPln = aToFace->getPlane();
107 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
108 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
110 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
112 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
113 aSign ? theFromSize : -theFromSize))));
114 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
116 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
117 aSign ? -theToSize : theToSize))));
118 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
120 // Getting bounding box for base shape.
121 const TopoDS_Shape& aBasisShape = theBasis->impl<TopoDS_Shape>();
123 BRepBndLib::Add(aBasisShape, aBndBox);
124 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
125 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
126 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
129 for(int i = 0; i < 2; i++) {
130 for(int j = 0; j < 2; j++) {
131 for(int k = 0; k < 2; k++) {
132 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
138 // Project points to bounding planes. Search max distance to them.
139 const TopoDS_Shape& aBndToShape = aBoundingToShape->impl<TopoDS_Shape>();
140 const TopoDS_Shape& aBndFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
141 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
142 gp_Vec aNormal(aBaseDir->impl<gp_Dir>());
143 for(int i = 0; i < 8; i++) {
144 gp_Lin aLine(aPoints[i], aNormal);
145 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
146 BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
147 BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
148 if(aToExt.NbExt() == 0 || aFromExt.NbExt() == 0) {
151 const gp_Pnt& aPntOnToFace = aToExt.PointOnFace(1);
152 const gp_Pnt& aPntOnFromFace = aFromExt.PointOnFace(1);
153 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
154 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
156 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
157 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
160 // We added 1 just to be sure that pipe is long enough for boolean operation.
161 Standard_Real aPipeLength = aMaxToDist + aMaxFromDist + 1;
163 // Making wire for pipe.
164 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis);
165 const gp_Pnt aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
166 TopoDS_Face aFace = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
167 gp_Pnt aPipeStartPnt = aCentrePnt.Translated(aNormal.Scaled(aPipeLength));
168 gp_Pnt aPipeEndPnt = aCentrePnt.Translated(aNormal.Scaled(-aPipeLength));
169 TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aPipeStartPnt, aPipeEndPnt);
170 TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
173 ListOfMakeShape aListOfMakeShape;
174 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
178 std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
179 std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
180 aWire->setImpl(new TopoDS_Shape(aPipeWire));
181 aBShape->setImpl(new TopoDS_Shape(aBasisShape));
182 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
183 TopoDS_Shape aResult = aPipeBuilder->Shape();
185 // Orienting bounding planes.
186 gp_Lin aLine(aCentrePnt, aNormal);
187 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
188 BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
189 BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
190 Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
191 Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
192 if(aToParameter > aFromParameter) {
193 gp_Vec aVec = aToDir->impl<gp_Dir>();
194 if((aVec * aNormal) > 0) {
195 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
196 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
198 aVec = aFromDir->impl<gp_Dir>();
199 if((aVec * aNormal) < 0) {
200 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
201 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
204 gp_Vec aVec = aToDir->impl<gp_Dir>();
205 if((aVec * aNormal) < 0) {
206 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
207 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
209 aVec = aFromDir->impl<gp_Dir>();
210 if((aVec * aNormal) > 0) {
211 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
212 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
216 // Making solids from bounding planes.
217 TopoDS_Shell aToShell, aFromShell;
218 TopoDS_Solid aToSolid, aFromSolid;
219 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
220 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
221 BRep_Builder aBoundingBuilder;
222 aBoundingBuilder.MakeShell(aToShell);
223 aBoundingBuilder.MakeShell(aFromShell);
224 aBoundingBuilder.Add(aToShell, aToShape);
225 aBoundingBuilder.Add(aFromShell, aFromShape);
226 aBoundingBuilder.MakeSolid(aToSolid);
227 aBoundingBuilder.MakeSolid(aFromSolid);
228 aBoundingBuilder.Add(aToSolid, aToShell);
229 aBoundingBuilder.Add(aFromSolid, aFromShell);
231 // Cutting with to plane.
232 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
233 aToCutBuilder->Build();
234 if(!aToCutBuilder->IsDone()) {
237 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
238 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
239 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
240 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
241 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
242 myToFaces.push_back(aShape);
244 aResult = aToCutBuilder->Shape();
246 // Cutting with from plane.
247 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
248 aFromCutBuilder->Build();
249 if(!aFromCutBuilder->IsDone()) {
252 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
253 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
254 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
255 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
256 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
257 myFromFaces.push_back(aShape);
259 aResult = aFromCutBuilder->Shape();
261 TopExp_Explorer anExp(aResult, TopAbs_SOLID);
265 if(aResult.ShapeType() == TopAbs_COMPOUND) {
266 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
268 if(aResult.ShapeType() == TopAbs_COMPOUND) {
269 std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
270 aCompound->setImpl(new TopoDS_Shape(aResult));
271 ListOfShape aCompSolids, aFreeSolids;
272 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
273 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
274 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
275 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
276 TopoDS_Compound aResultComp;
277 TopoDS_Builder aBuilder;
278 aBuilder.MakeCompound(aResultComp);
279 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
280 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
282 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
283 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
285 aResult = aResultComp;
289 // Fill data map to keep correct orientation of sub-shapes.
290 myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
291 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
292 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
293 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
294 myMap->bind(aCurrentShape, aCurrentShape);
296 myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
297 myShape->setImpl(new TopoDS_Shape(aResult));
298 myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
302 //=================================================================================================
303 bool GeomAlgoAPI_Prism::isDone() const
308 //=================================================================================================
309 bool GeomAlgoAPI_Prism::isValid() const
311 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
312 return (aChecker.IsValid() == Standard_True);
315 //=================================================================================================
316 bool GeomAlgoAPI_Prism::hasVolume() const
318 bool hasVolume(false);
320 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
322 BRepGProp::VolumeProperties(aRShape, aGProp);
323 if(aGProp.Mass() > Precision::Confusion())
329 //=================================================================================================
330 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
335 //=================================================================================================
336 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
341 //=================================================================================================
342 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
347 //=================================================================================================
348 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
353 //=================================================================================================
354 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const