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 Standard_Real aPipeLength = aMaxToDist + aMaxFromDist;
162 // Making wire for pipe.
163 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBasis);
164 const gp_Pnt aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
165 TopoDS_Face aFace = TopoDS::Face(aBaseFace->impl<TopoDS_Shape>());
166 gp_Pnt aPipeStartPnt = aCentrePnt.Translated(aNormal.Scaled(aPipeLength));
167 gp_Pnt aPipeEndPnt = aCentrePnt.Translated(aNormal.Scaled(-aPipeLength));
168 TopoDS_Edge aPipeEdge = BRepBuilderAPI_MakeEdge(aPipeStartPnt, aPipeEndPnt);
169 TopoDS_Wire aPipeWire = BRepBuilderAPI_MakeWire(aPipeEdge).Wire();
172 ListOfMakeShape aListOfMakeShape;
173 BRepOffsetAPI_MakePipe* aPipeBuilder = new BRepOffsetAPI_MakePipe(aPipeWire, aBasisShape);
177 std::shared_ptr<GeomAPI_Shape> aWire(new GeomAPI_Shape);
178 std::shared_ptr<GeomAPI_Shape> aBShape(new GeomAPI_Shape);
179 aWire->setImpl(new TopoDS_Shape(aPipeWire));
180 aBShape->setImpl(new TopoDS_Shape(aBasisShape));
181 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPipeBuilder, aWire, aBShape)));
182 TopoDS_Shape aResult = aPipeBuilder->Shape();
184 // Orienting bounding planes.
185 gp_Lin aLine(aCentrePnt, aNormal);
186 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
187 BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
188 BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
189 Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
190 Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
191 if(aToParameter > aFromParameter) {
192 gp_Vec aVec = aToDir->impl<gp_Dir>();
193 if((aVec * aNormal) > 0) {
194 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
195 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
197 aVec = aFromDir->impl<gp_Dir>();
198 if((aVec * aNormal) < 0) {
199 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
200 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
203 gp_Vec aVec = aToDir->impl<gp_Dir>();
204 if((aVec * aNormal) < 0) {
205 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
206 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
208 aVec = aFromDir->impl<gp_Dir>();
209 if((aVec * aNormal) > 0) {
210 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
211 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
215 // Making solids from bounding planes.
216 TopoDS_Shell aToShell, aFromShell;
217 TopoDS_Solid aToSolid, aFromSolid;
218 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
219 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
220 BRep_Builder aBoundingBuilder;
221 aBoundingBuilder.MakeShell(aToShell);
222 aBoundingBuilder.MakeShell(aFromShell);
223 aBoundingBuilder.Add(aToShell, aToShape);
224 aBoundingBuilder.Add(aFromShell, aFromShape);
225 aBoundingBuilder.MakeSolid(aToSolid);
226 aBoundingBuilder.MakeSolid(aFromSolid);
227 aBoundingBuilder.Add(aToSolid, aToShell);
228 aBoundingBuilder.Add(aFromSolid, aFromShell);
230 // Cutting with to plane.
231 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
232 aToCutBuilder->Build();
233 if(!aToCutBuilder->IsDone()) {
236 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
237 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
238 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
239 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
240 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
241 myToFaces.push_back(aShape);
243 aResult = aToCutBuilder->Shape();
245 // Cutting with from plane.
246 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
247 aFromCutBuilder->Build();
248 if(!aFromCutBuilder->IsDone()) {
251 aListOfMakeShape.push_back(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
252 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
253 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
254 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
255 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
256 myFromFaces.push_back(aShape);
258 aResult = aFromCutBuilder->Shape();
260 TopExp_Explorer anExp(aResult, TopAbs_SOLID);
264 if(aResult.ShapeType() == TopAbs_COMPOUND) {
265 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
267 if(aResult.ShapeType() == TopAbs_COMPOUND) {
268 std::shared_ptr<GeomAPI_Shape> aCompound(new GeomAPI_Shape);
269 aCompound->setImpl(new TopoDS_Shape(aResult));
270 ListOfShape aCompSolids, aFreeSolids;
271 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
272 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
273 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
274 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
275 TopoDS_Compound aResultComp;
276 TopoDS_Builder aBuilder;
277 aBuilder.MakeCompound(aResultComp);
278 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
279 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
281 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
282 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
284 aResult = aResultComp;
288 // Fill data map to keep correct orientation of sub-shapes.
289 myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
290 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
291 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
292 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
293 myMap->bind(aCurrentShape, aCurrentShape);
295 myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
296 myShape->setImpl(new TopoDS_Shape(aResult));
297 myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
301 //=================================================================================================
302 bool GeomAlgoAPI_Prism::isDone() const
307 //=================================================================================================
308 bool GeomAlgoAPI_Prism::isValid() const
310 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
311 return (aChecker.IsValid() == Standard_True);
314 //=================================================================================================
315 bool GeomAlgoAPI_Prism::hasVolume() const
317 bool hasVolume(false);
319 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
321 BRepGProp::VolumeProperties(aRShape, aGProp);
322 if(aGProp.Mass() > Precision::Confusion())
328 //=================================================================================================
329 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
334 //=================================================================================================
335 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
340 //=================================================================================================
341 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
346 //=================================================================================================
347 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
352 //=================================================================================================
353 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const