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.
114 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
115 std::shared_ptr<GeomAPI_Dir> aBaseDir;
116 GeomShapePtr aBasePlane;
117 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
118 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
119 if(theDirection.get()) {
120 aBaseDir = theDirection;
121 aDirVec = theDirection->impl<gp_Dir>();
122 } else if(aBaseShape.ShapeType() != TopAbs_VERTEX &&
123 aBaseShape.ShapeType() != TopAbs_EDGE &&
124 aFindPlane.Found() == Standard_True){
125 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
126 gp_Pnt aLoc = aPlane->Axis().Location();
127 aDirVec = aPlane->Axis().Direction();
128 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
129 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
133 if(!aBaseLoc.get()) {
135 gp_XYZ aDirXYZ = aDirVec.XYZ();
136 Standard_Real aMinParam = Precision::Infinite();
137 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
138 const TopoDS_Shape& aVertex = anExp.Current();
139 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
140 double aParam = aDirXYZ.Dot(aPnt.XYZ());
141 if(aParam < aMinParam) {
146 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
148 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
150 TopoDS_Shape aResult;
151 if(!isBoundingShapesSet) {
152 // Moving base shape.
154 aTrsf.SetTranslation(aDirVec * -theFromSize);
155 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
156 if(!aTransformBuilder) {
159 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
160 if(!aTransformBuilder->IsDone()) {
163 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
166 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
170 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
171 if(!aPrismBuilder->IsDone()) {
174 aResult = aPrismBuilder->Shape();
177 for(TopExp_Explorer anExp(aMovedBase, aShapeTypeToExp); anExp.More(); anExp.Next()) {
178 const TopoDS_Shape& aShape = anExp.Current();
179 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
180 aFromShape->setImpl(new TopoDS_Shape(aPrismBuilder->FirstShape(aShape)));
181 aToShape->setImpl(new TopoDS_Shape(aPrismBuilder->LastShape(aShape)));
182 this->addFromShape(aFromShape);
183 this->addToShape(aToShape);
186 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
187 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
189 // Moving prism bounding faces according to "from" and "to" sizes.
190 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
191 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
192 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
194 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
195 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
196 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
198 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
200 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
201 aSign ? theFromSize : -theFromSize))));
202 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
204 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
205 aSign ? -theToSize : theToSize))));
206 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
208 // Getting bounding box for base shape.
210 BRepBndLib::Add(aBaseShape, aBndBox);
211 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
212 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
213 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
216 for(int i = 0; i < 2; i++) {
217 for(int j = 0; j < 2; j++) {
218 for(int k = 0; k < 2; k++) {
219 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
225 // Project points to bounding planes. Search max distance to them.
226 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
227 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
228 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
229 for(int i = 0; i < 8; i++) {
230 gp_Lin aLine(aPoints[i], aDirVec);
231 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
232 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
233 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
236 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
237 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
238 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
239 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
241 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
242 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
246 // We added 1 just to be sure that prism is long enough for boolean operation.
247 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
249 // Moving base shape.
251 aTrsf.SetTranslation(aDirVec * -aPrismLength);
252 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
253 if(!aTransformBuilder) {
256 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
257 if(!aTransformBuilder->IsDone()) {
260 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
263 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
267 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
268 if(!aPrismBuilder->IsDone()) {
271 aResult = aPrismBuilder->Shape();
273 // Orienting bounding planes.
274 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
275 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
276 gp_Lin aLine(aCentrePnt, aDirVec);
277 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
278 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
279 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
280 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
281 if(aToParameter > aFromParameter) {
282 gp_Vec aVec = aToDir->impl<gp_Dir>();
283 if((aVec * aDirVec) > 0) {
284 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
285 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
287 aVec = aFromDir->impl<gp_Dir>();
288 if((aVec * aDirVec) < 0) {
289 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
290 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
293 gp_Vec aVec = aToDir->impl<gp_Dir>();
294 if((aVec * aDirVec) < 0) {
295 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
296 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
298 aVec = aFromDir->impl<gp_Dir>();
299 if((aVec * aDirVec) > 0) {
300 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
301 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
305 // Making solids from bounding planes.
306 TopoDS_Shell aToShell, aFromShell;
307 TopoDS_Solid aToSolid, aFromSolid;
308 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
309 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
310 TopoDS_Face aToFace = TopoDS::Face(aToShape);
311 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
312 BRep_Builder aBoundingBuilder;
313 aBoundingBuilder.MakeShell(aToShell);
314 aBoundingBuilder.Add(aToShell, aToShape);
315 aBoundingBuilder.MakeShell(aFromShell);
316 aBoundingBuilder.Add(aFromShell, aFromShape);
317 aBoundingBuilder.MakeSolid(aToSolid);
318 aBoundingBuilder.Add(aToSolid, aToShell);
319 aBoundingBuilder.MakeSolid(aFromSolid);
320 aBoundingBuilder.Add(aFromSolid, aFromShell);
322 // Cutting with to plane.
323 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
324 aToCutBuilder->Build();
325 if(!aToCutBuilder->IsDone()) {
328 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
329 aResult = aToCutBuilder->Shape();
330 if(aResult.ShapeType() == TopAbs_COMPOUND) {
331 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
333 if(aShapeTypeToExp == TopAbs_FACE) {
334 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
335 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
336 GeomShapePtr aGeomSh(new GeomAPI_Shape());
337 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
338 this->addToShape(aGeomSh);
342 // Cutting with from plane.
343 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
344 aFromCutBuilder->Build();
345 if(!aFromCutBuilder->IsDone()) {
348 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
349 aResult = aFromCutBuilder->Shape();
350 TopoDS_Iterator aCheckIt(aResult);
351 if(!aCheckIt.More()) {
354 if(aResult.ShapeType() == TopAbs_COMPOUND) {
355 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
357 if(aShapeTypeToExp == TopAbs_FACE) {
358 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
359 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
360 GeomShapePtr aGeomSh(new GeomAPI_Shape());
361 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
362 this->addFromShape(aGeomSh);
366 // Naming for extrusion from vertex, edge.
367 for(TopExp_Explorer anExp(aResult, aShapeTypeToExp); anExp.More(); anExp.Next()) {
368 const TopoDS_Shape& aShape = anExp.Current();
369 GeomShapePtr aGeomSh(new GeomAPI_Shape());
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 aGeomSh->setImpl(new TopoDS_Shape(aShape));
375 this->addToShape(aGeomSh);
377 if(anIntTools.IsValidPointForFace(aPnt, aFromFace, Precision::Confusion()) == Standard_True) {
378 aGeomSh->setImpl(new TopoDS_Shape(aShape));
379 this->addFromShape(aGeomSh);
381 } else if(aShapeTypeToExp == TopAbs_EDGE) {
382 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
383 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, aToFace);
384 anEdgeCheck.Perform();
385 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
386 aGeomSh->setImpl(new TopoDS_Shape(aShape));
387 this->addToShape(aGeomSh);
389 anEdgeCheck.Init(anEdge, aFromFace);
390 anEdgeCheck.Perform();
391 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
392 aGeomSh->setImpl(new TopoDS_Shape(aShape));
393 this->addFromShape(aGeomSh);
400 if(aResult.ShapeType() == TopAbs_COMPOUND) {
401 GeomShapePtr aCompound(new GeomAPI_Shape);
402 aCompound->setImpl(new TopoDS_Shape(aResult));
403 ListOfShape aCompSolids, aFreeSolids;
404 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
405 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
406 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
407 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
408 TopoDS_Compound aResultComp;
409 TopoDS_Builder aBuilder;
410 aBuilder.MakeCompound(aResultComp);
411 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
412 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
414 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
415 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
417 aResult = aResultComp;
423 if(aResult.IsNull()) {
426 GeomShapePtr aGeomSh(new GeomAPI_Shape());
427 aGeomSh->setImpl(new TopoDS_Shape(aResult));
428 this->setShape(aGeomSh);