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_ShapeTools.h>
18 #include <Bnd_Box.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepAlgoAPI_Cut.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_Transform.hxx>
24 #include <BRepTools.hxx>
25 #include <Geom_Curve.hxx>
26 #include <Geom2d_Curve.hxx>
27 #include <BRepLib_CheckCurveOnSurface.hxx>
28 #include <BRepPrimAPI_MakePrism.hxx>
29 #include <Geom_Plane.hxx>
31 #include <IntAna_IntConicQuad.hxx>
32 #include <IntAna_Quadric.hxx>
33 #include <IntTools_Context.hxx>
34 #include <TopExp_Explorer.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Shell.hxx>
38 #include <TopoDS_Solid.hxx>
39 #include <TopTools_ListIteratorOfListOfShape.hxx>
41 //=================================================================================================
42 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
43 const double theToSize,
44 const double theFromSize)
46 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
49 //=================================================================================================
50 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
51 const std::shared_ptr<GeomAPI_Dir> theDirection,
52 const double theToSize,
53 const double theFromSize)
55 build(theBaseShape, theDirection, GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
58 //=================================================================================================
59 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
60 const GeomShapePtr theToShape,
61 const double theToSize,
62 const GeomShapePtr theFromShape,
63 const double theFromSize)
65 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), theToShape, theToSize, theFromShape, theFromSize);
68 //=================================================================================================
69 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
70 const std::shared_ptr<GeomAPI_Dir> theDirection,
71 const GeomShapePtr theToShape,
72 const double theToSize,
73 const GeomShapePtr theFromShape,
74 const double theFromSize)
76 build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
79 //=================================================================================================
80 void GeomAlgoAPI_Prism::build(const GeomShapePtr& theBaseShape,
81 const std::shared_ptr<GeomAPI_Dir> theDirection,
82 const GeomShapePtr& theToShape,
83 const double theToSize,
84 const GeomShapePtr& theFromShape,
85 const double theFromSize)
87 if(!theBaseShape.get() ||
88 (((!theFromShape.get() && !theToShape.get()) || (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
89 && (theFromSize == -theToSize))) {
93 // Getting base shape.
94 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
95 TopAbs_ShapeEnum aShapeTypeToExp;
96 switch(aBaseShape.ShapeType()) {
98 aShapeTypeToExp = TopAbs_VERTEX;
102 aShapeTypeToExp = TopAbs_EDGE;
106 aShapeTypeToExp = TopAbs_FACE;
112 // Getting direction.
115 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
116 std::shared_ptr<GeomAPI_Dir> aBaseDir;
117 GeomShapePtr aBasePlane;
118 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
119 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
120 if(aBaseShape.ShapeType() == TopAbs_VERTEX || aBaseShape.ShapeType() == TopAbs_EDGE ||
121 aFindPlane.Found() != Standard_True) {
122 // Direction should be set.
123 if(!theDirection.get()) {
127 aBaseDir = theDirection;
128 aDirVec = theDirection->impl<gp_Dir>();
129 gp_XYZ aDirXYZ = aDirVec.XYZ();
130 Standard_Real aMinParam = Precision::Infinite();
132 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
133 const TopoDS_Shape& aVertex = anExp.Current();
134 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
135 double aParam = aDirXYZ.Dot(aPnt.XYZ());
136 if(aParam < aMinParam) {
142 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
143 aLoc = aPlane->Axis().Location();
144 aDirVec = aPlane->Axis().Direction();
146 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
148 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
149 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
151 TopoDS_Shape aResult;
152 if(!isBoundingShapesSet) {
153 // Moving base shape.
155 aTrsf.SetTranslation(aDirVec * -theFromSize);
156 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
157 if(!aTransformBuilder) {
160 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
161 if(!aTransformBuilder->IsDone()) {
164 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
167 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
171 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
172 if(!aPrismBuilder->IsDone()) {
175 aResult = aPrismBuilder->Shape();
178 for(TopExp_Explorer anExp(aMovedBase, aShapeTypeToExp); anExp.More(); anExp.Next()) {
179 const TopoDS_Shape& aShape = anExp.Current();
180 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
181 aFromShape->setImpl(new TopoDS_Shape(aPrismBuilder->FirstShape(aShape)));
182 aToShape->setImpl(new TopoDS_Shape(aPrismBuilder->LastShape(aShape)));
183 this->addFromShape(aFromShape);
184 this->addToShape(aToShape);
187 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
188 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
190 // Moving prism bounding faces according to "from" and "to" sizes.
191 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
192 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
193 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
195 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
196 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
197 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
199 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
201 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
202 aSign ? theFromSize : -theFromSize))));
203 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
205 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
206 aSign ? -theToSize : theToSize))));
207 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
209 // Getting bounding box for base shape.
211 BRepBndLib::Add(aBaseShape, aBndBox);
212 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
213 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
214 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
217 for(int i = 0; i < 2; i++) {
218 for(int j = 0; j < 2; j++) {
219 for(int k = 0; k < 2; k++) {
220 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
226 // Project points to bounding planes. Search max distance to them.
227 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
228 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
229 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
230 for(int i = 0; i < 8; i++) {
231 gp_Lin aLine(aPoints[i], aDirVec);
232 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
233 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
234 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
237 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
238 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
239 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
240 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
242 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
243 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
247 // We added 1 just to be sure that prism is long enough for boolean operation.
248 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
250 // Moving base shape.
252 aTrsf.SetTranslation(aDirVec * -aPrismLength);
253 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
254 if(!aTransformBuilder) {
257 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
258 if(!aTransformBuilder->IsDone()) {
261 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
264 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
268 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
269 if(!aPrismBuilder->IsDone()) {
272 aResult = aPrismBuilder->Shape();
274 // Orienting bounding planes.
275 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
276 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
277 gp_Lin aLine(aCentrePnt, aDirVec);
278 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
279 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
280 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
281 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
282 if(aToParameter > aFromParameter) {
283 gp_Vec aVec = aToDir->impl<gp_Dir>();
284 if((aVec * aDirVec) > 0) {
285 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
286 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
288 aVec = aFromDir->impl<gp_Dir>();
289 if((aVec * aDirVec) < 0) {
290 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
291 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
294 gp_Vec aVec = aToDir->impl<gp_Dir>();
295 if((aVec * aDirVec) < 0) {
296 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
297 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
299 aVec = aFromDir->impl<gp_Dir>();
300 if((aVec * aDirVec) > 0) {
301 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
302 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
306 // Making solids from bounding planes.
307 TopoDS_Shell aToShell, aFromShell;
308 TopoDS_Solid aToSolid, aFromSolid;
309 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
310 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
311 TopoDS_Face aToFace = TopoDS::Face(aToShape);
312 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
313 BRep_Builder aBoundingBuilder;
314 aBoundingBuilder.MakeShell(aToShell);
315 aBoundingBuilder.Add(aToShell, aToShape);
316 aBoundingBuilder.MakeShell(aFromShell);
317 aBoundingBuilder.Add(aFromShell, aFromShape);
318 aBoundingBuilder.MakeSolid(aToSolid);
319 aBoundingBuilder.Add(aToSolid, aToShell);
320 aBoundingBuilder.MakeSolid(aFromSolid);
321 aBoundingBuilder.Add(aFromSolid, aFromShell);
323 // Cutting with to plane.
324 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
325 aToCutBuilder->Build();
326 if(!aToCutBuilder->IsDone()) {
329 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
330 aResult = aToCutBuilder->Shape();
331 if(aResult.ShapeType() == TopAbs_COMPOUND) {
332 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
334 if(aShapeTypeToExp == TopAbs_FACE) {
335 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
336 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
337 GeomShapePtr aGeomSh(new GeomAPI_Shape());
338 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
339 this->addToShape(aGeomSh);
343 // Cutting with from plane.
344 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
345 aFromCutBuilder->Build();
346 if(!aFromCutBuilder->IsDone()) {
349 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
350 aResult = aFromCutBuilder->Shape();
351 TopoDS_Iterator aCheckIt(aResult);
352 if(!aCheckIt.More()) {
355 if(aResult.ShapeType() == TopAbs_COMPOUND) {
356 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
358 if(aShapeTypeToExp == TopAbs_FACE) {
359 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
360 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
361 GeomShapePtr aGeomSh(new GeomAPI_Shape());
362 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
363 this->addFromShape(aGeomSh);
367 // Naming for extrusion from vertex, edge.
368 for(TopExp_Explorer anExp(aResult, aShapeTypeToExp); anExp.More(); anExp.Next()) {
369 const TopoDS_Shape& aShape = anExp.Current();
370 if(aShapeTypeToExp == TopAbs_VERTEX) {
371 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
372 IntTools_Context anIntTools;
373 if(anIntTools.IsValidPointForFace(aPnt, aToFace, Precision::Confusion()) == Standard_True) {
374 GeomShapePtr aGeomSh(new GeomAPI_Shape());
375 aGeomSh->setImpl(new TopoDS_Shape(aShape));
376 this->addToShape(aGeomSh);
378 if(anIntTools.IsValidPointForFace(aPnt, aFromFace, Precision::Confusion()) == Standard_True) {
379 GeomShapePtr aGeomSh(new GeomAPI_Shape());
380 aGeomSh->setImpl(new TopoDS_Shape(aShape));
381 this->addFromShape(aGeomSh);
383 } else if(aShapeTypeToExp == TopAbs_EDGE) {
384 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
385 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, aToFace);
386 anEdgeCheck.Perform();
387 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
388 GeomShapePtr aGeomSh(new GeomAPI_Shape());
389 aGeomSh->setImpl(new TopoDS_Shape(aShape));
390 this->addToShape(aGeomSh);
392 anEdgeCheck.Init(anEdge, aFromFace);
393 anEdgeCheck.Perform();
394 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
395 GeomShapePtr aGeomSh(new GeomAPI_Shape());
396 aGeomSh->setImpl(new TopoDS_Shape(aShape));
397 this->addFromShape(aGeomSh);
404 if(aResult.ShapeType() == TopAbs_COMPOUND) {
405 GeomShapePtr aCompound(new GeomAPI_Shape);
406 aCompound->setImpl(new TopoDS_Shape(aResult));
407 ListOfShape aCompSolids, aFreeSolids;
408 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
409 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
410 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
411 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
412 TopoDS_Compound aResultComp;
413 TopoDS_Builder aBuilder;
414 aBuilder.MakeCompound(aResultComp);
415 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
416 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
418 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
419 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
421 aResult = aResultComp;
427 if(aResult.IsNull()) {
430 GeomShapePtr aGeomSh(new GeomAPI_Shape());
431 aGeomSh->setImpl(new TopoDS_Shape(aResult));
432 this->setShape(aGeomSh);