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 aWire->setImpl(new TopoDS_Shape(aPipeWire));
179 aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aPipeBuilder, aWire));
180 TopoDS_Shape aResult = aPipeBuilder->Shape();
182 // Orienting bounding planes.
183 gp_Lin aLine(aCentrePnt, aNormal);
184 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aLine).Edge();
185 BRepExtrema_ExtCF aToExt(anEdge, TopoDS::Face(aBndToShape));
186 BRepExtrema_ExtCF aFromExt(anEdge, TopoDS::Face(aBndFromShape));
187 Standard_Real aToParameter = aToExt.ParameterOnEdge(1);
188 Standard_Real aFromParameter = aFromExt.ParameterOnEdge(1);
189 if(aToParameter > aFromParameter) {
190 gp_Vec aVec = aToDir->impl<gp_Dir>();
191 if((aVec * aNormal) > 0) {
192 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
193 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
195 aVec = aFromDir->impl<gp_Dir>();
196 if((aVec * aNormal) < 0) {
197 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
198 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
201 gp_Vec aVec = aToDir->impl<gp_Dir>();
202 if((aVec * aNormal) < 0) {
203 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
204 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
206 aVec = aFromDir->impl<gp_Dir>();
207 if((aVec * aNormal) > 0) {
208 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
209 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
213 // Making solids from bounding planes.
214 TopoDS_Shell aToShell, aFromShell;
215 TopoDS_Solid aToSolid, aFromSolid;
216 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
217 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
218 BRep_Builder aBoundingBuilder;
219 aBoundingBuilder.MakeShell(aToShell);
220 aBoundingBuilder.MakeShell(aFromShell);
221 aBoundingBuilder.Add(aToShell, aToShape);
222 aBoundingBuilder.Add(aFromShell, aFromShape);
223 aBoundingBuilder.MakeSolid(aToSolid);
224 aBoundingBuilder.MakeSolid(aFromSolid);
225 aBoundingBuilder.Add(aToSolid, aToShell);
226 aBoundingBuilder.Add(aFromSolid, aFromShell);
228 // Cutting with to plane.
229 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
230 aToCutBuilder->Build();
231 if(!aToCutBuilder->IsDone()) {
234 aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aToCutBuilder));
235 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
236 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
237 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
238 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
239 myToFaces.push_back(aShape);
241 aResult = aToCutBuilder->Shape();
243 // Cutting with from plane.
244 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
245 aFromCutBuilder->Build();
246 if(!aFromCutBuilder->IsDone()) {
249 aListOfMakeShape.push_back(std::make_shared<GeomAlgoAPI_MakeShape>(aFromCutBuilder));
250 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
251 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
252 std::shared_ptr<GeomAPI_Shape> aShape(new GeomAPI_Shape());
253 aShape->setImpl(new TopoDS_Shape(anIt.Value()));
254 myFromFaces.push_back(aShape);
256 aResult = aFromCutBuilder->Shape();
258 TopExp_Explorer anExp(aResult, TopAbs_SOLID);
262 if(aResult.ShapeType() == TopAbs_COMPOUND) {
263 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
266 myShape = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
267 myShape->setImpl(new TopoDS_Shape(aResult));
269 // Fill data map to keep correct orientation of sub-shapes.
270 myMap = std::shared_ptr<GeomAPI_DataMapOfShapeShape>(new GeomAPI_DataMapOfShapeShape);
271 for (TopExp_Explorer Exp(aResult,TopAbs_FACE); Exp.More(); Exp.Next()) {
272 std::shared_ptr<GeomAPI_Shape> aCurrentShape(new GeomAPI_Shape());
273 aCurrentShape->setImpl(new TopoDS_Shape(Exp.Current()));
274 myMap->bind(aCurrentShape, aCurrentShape);
277 myMkShape = std::shared_ptr<GeomAlgoAPI_MakeShapeList>(new GeomAlgoAPI_MakeShapeList(aListOfMakeShape));
281 //=================================================================================================
282 bool GeomAlgoAPI_Prism::isDone() const
287 //=================================================================================================
288 bool GeomAlgoAPI_Prism::isValid() const
290 BRepCheck_Analyzer aChecker(myShape->impl<TopoDS_Shape>());
291 return (aChecker.IsValid() == Standard_True);
294 //=================================================================================================
295 bool GeomAlgoAPI_Prism::hasVolume() const
297 bool hasVolume(false);
299 const TopoDS_Shape& aRShape = myShape->impl<TopoDS_Shape>();
301 BRepGProp::VolumeProperties(aRShape, aGProp);
302 if(aGProp.Mass() > Precision::Confusion())
308 //=================================================================================================
309 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_Prism::shape() const
314 //=================================================================================================
315 const ListOfShape& GeomAlgoAPI_Prism::fromFaces() const
320 //=================================================================================================
321 const ListOfShape& GeomAlgoAPI_Prism::toFaces() const
326 //=================================================================================================
327 std::shared_ptr<GeomAPI_DataMapOfShapeShape> GeomAlgoAPI_Prism::mapOfShapes() const
332 //=================================================================================================
333 std::shared_ptr<GeomAlgoAPI_MakeShape> GeomAlgoAPI_Prism::makeShape() const