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 <Geom_Curve.hxx>
25 #include <Geom2d_Curve.hxx>
26 #include <BRepLib_CheckCurveOnSurface.hxx>
27 #include <BRepPrimAPI_MakePrism.hxx>
28 #include <Geom_Plane.hxx>
30 #include <IntAna_IntConicQuad.hxx>
31 #include <IntAna_Quadric.hxx>
32 #include <IntTools_Context.hxx>
33 #include <TopExp_Explorer.hxx>
35 #include <TopoDS_Edge.hxx>
36 #include <TopoDS_Shell.hxx>
37 #include <TopoDS_Solid.hxx>
38 #include <TopTools_ListIteratorOfListOfShape.hxx>
40 #include <BRepTools.hxx>
42 //=================================================================================================
43 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
44 const double theToSize,
45 const double theFromSize)
47 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
50 //=================================================================================================
51 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
52 const std::shared_ptr<GeomAPI_Dir> theDirection,
53 const double theToSize,
54 const double theFromSize)
56 build(theBaseShape, theDirection, GeomShapePtr(), theToSize, GeomShapePtr(), theFromSize);
59 //=================================================================================================
60 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
61 const GeomShapePtr theToShape,
62 const double theToSize,
63 const GeomShapePtr theFromShape,
64 const double theFromSize)
66 build(theBaseShape, std::shared_ptr<GeomAPI_Dir>(), theToShape, theToSize, theFromShape, theFromSize);
69 //=================================================================================================
70 GeomAlgoAPI_Prism::GeomAlgoAPI_Prism(const GeomShapePtr theBaseShape,
71 const std::shared_ptr<GeomAPI_Dir> theDirection,
72 const GeomShapePtr theToShape,
73 const double theToSize,
74 const GeomShapePtr theFromShape,
75 const double theFromSize)
77 build(theBaseShape, theDirection, theToShape, theToSize, theFromShape, theFromSize);
80 //=================================================================================================
81 void GeomAlgoAPI_Prism::build(const GeomShapePtr& theBaseShape,
82 const std::shared_ptr<GeomAPI_Dir> theDirection,
83 const GeomShapePtr& theToShape,
84 const double theToSize,
85 const GeomShapePtr& theFromShape,
86 const double theFromSize)
88 if(!theBaseShape.get() ||
89 (((!theFromShape.get() && !theToShape.get()) || (theFromShape.get() && theToShape.get() && theFromShape->isEqual(theToShape)))
90 && (theFromSize == -theToSize))) {
94 // Getting base shape.
95 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
96 TopAbs_ShapeEnum aShapeTypeToExp;
97 switch(aBaseShape.ShapeType()) {
99 aShapeTypeToExp = TopAbs_VERTEX;
103 aShapeTypeToExp = TopAbs_EDGE;
107 aShapeTypeToExp = TopAbs_FACE;
113 // Getting direction.
116 std::shared_ptr<GeomAPI_Pnt> aBaseLoc;
117 std::shared_ptr<GeomAPI_Dir> aBaseDir;
118 GeomShapePtr aBasePlane;
119 const bool isBoundingShapesSet = theFromShape.get() || theToShape.get();
120 BRepBuilderAPI_FindPlane aFindPlane(aBaseShape);
121 if(aBaseShape.ShapeType() == TopAbs_VERTEX || aBaseShape.ShapeType() == TopAbs_EDGE ||
122 aFindPlane.Found() != Standard_True) {
123 // Direction and both bounding planes should be set or empty.
124 if(!theDirection.get()) {
128 aBaseDir = theDirection;
129 aDirVec = theDirection->impl<gp_Dir>();
130 gp_XYZ aDirXYZ = aDirVec.XYZ();
131 Standard_Real aMinParam = std::numeric_limits<double>::max();
133 for(TopExp_Explorer anExp(aBaseShape, TopAbs_VERTEX); anExp.More(); anExp.Next()) {
134 const TopoDS_Shape& aVertex = anExp.Current();
135 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
136 double aParam = aDirXYZ.Dot(aPnt.XYZ());
137 if(aParam < aMinParam) {
143 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
144 aLoc = aPlane->Axis().Location();
145 aDirVec = aPlane->Axis().Direction();
147 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
149 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
150 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
152 TopoDS_Shape aResult;
153 if(!isBoundingShapesSet) {
154 // Moving base shape.
156 aTrsf.SetTranslation(aDirVec * -theFromSize);
157 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
158 if(!aTransformBuilder) {
161 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
162 if(!aTransformBuilder->IsDone()) {
165 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
168 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
172 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
173 if(!aPrismBuilder->IsDone()) {
176 aResult = aPrismBuilder->Shape();
179 for(TopExp_Explorer anExp(aMovedBase, aShapeTypeToExp); anExp.More(); anExp.Next()) {
180 const TopoDS_Shape& aShape = anExp.Current();
181 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
182 aFromShape->setImpl(new TopoDS_Shape(aPrismBuilder->FirstShape(aShape)));
183 aToShape->setImpl(new TopoDS_Shape(aPrismBuilder->LastShape(aShape)));
184 this->addFromShape(aFromShape);
185 this->addToShape(aToShape);
188 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
189 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
191 // Moving prism bounding faces according to "from" and "to" sizes.
192 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
193 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
194 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
196 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
197 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
198 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
200 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
202 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
203 aSign ? theFromSize : -theFromSize))));
204 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
206 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
207 aSign ? -theToSize : theToSize))));
208 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
210 // Getting bounding box for base shape.
212 BRepBndLib::Add(aBaseShape, aBndBox);
213 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
214 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
215 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
218 for(int i = 0; i < 2; i++) {
219 for(int j = 0; j < 2; j++) {
220 for(int k = 0; k < 2; k++) {
221 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
227 // Project points to bounding planes. Search max distance to them.
228 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
229 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
230 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
231 for(int i = 0; i < 8; i++) {
232 gp_Lin aLine(aPoints[i], aDirVec);
233 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
234 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
235 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
238 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
239 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
240 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
241 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
243 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
244 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
248 // We added 1 just to be sure that prism is long enough for boolean operation.
249 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
251 // Moving base shape.
253 aTrsf.SetTranslation(aDirVec * -aPrismLength);
254 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
255 if(!aTransformBuilder) {
258 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
259 if(!aTransformBuilder->IsDone()) {
262 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
265 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
269 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
270 if(!aPrismBuilder->IsDone()) {
273 aResult = aPrismBuilder->Shape();
275 // Orienting bounding planes.
276 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
277 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
278 gp_Lin aLine(aCentrePnt, aDirVec);
279 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
280 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
281 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
282 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
283 if(aToParameter > aFromParameter) {
284 gp_Vec aVec = aToDir->impl<gp_Dir>();
285 if((aVec * aDirVec) > 0) {
286 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
287 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
289 aVec = aFromDir->impl<gp_Dir>();
290 if((aVec * aDirVec) < 0) {
291 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
292 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
295 gp_Vec aVec = aToDir->impl<gp_Dir>();
296 if((aVec * aDirVec) < 0) {
297 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
298 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
300 aVec = aFromDir->impl<gp_Dir>();
301 if((aVec * aDirVec) > 0) {
302 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
303 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
307 // Making solids from bounding planes.
308 TopoDS_Shell aToShell, aFromShell;
309 TopoDS_Solid aToSolid, aFromSolid;
310 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
311 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
312 TopoDS_Face aToFace = TopoDS::Face(aToShape);
313 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
314 BRep_Builder aBoundingBuilder;
315 aBoundingBuilder.MakeShell(aToShell);
316 aBoundingBuilder.Add(aToShell, aToShape);
317 aBoundingBuilder.MakeShell(aFromShell);
318 aBoundingBuilder.Add(aFromShell, aFromShape);
319 aBoundingBuilder.MakeSolid(aToSolid);
320 aBoundingBuilder.Add(aToSolid, aToShell);
321 aBoundingBuilder.MakeSolid(aFromSolid);
322 aBoundingBuilder.Add(aFromSolid, aFromShell);
324 // Cutting with to plane.
325 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
326 aToCutBuilder->Build();
327 if(!aToCutBuilder->IsDone()) {
330 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
331 aResult = aToCutBuilder->Shape();
332 if(aResult.ShapeType() == TopAbs_COMPOUND) {
333 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
335 if(aShapeTypeToExp == TopAbs_FACE) {
336 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
337 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
338 GeomShapePtr aGeomSh(new GeomAPI_Shape());
339 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
340 this->addToShape(aGeomSh);
344 // Cutting with from plane.
345 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
346 aFromCutBuilder->Build();
347 if(!aFromCutBuilder->IsDone()) {
350 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
351 aResult = aFromCutBuilder->Shape();
352 TopoDS_Iterator aCheckIt(aResult);
353 if(!aCheckIt.More()) {
356 if(aResult.ShapeType() == TopAbs_COMPOUND) {
357 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
359 if(aShapeTypeToExp == TopAbs_FACE) {
360 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
361 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
362 GeomShapePtr aGeomSh(new GeomAPI_Shape());
363 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
364 this->addFromShape(aGeomSh);
368 // Naming for extrusion from vertex, edge.
369 for(TopExp_Explorer anExp(aResult, aShapeTypeToExp); anExp.More(); anExp.Next()) {
370 const TopoDS_Shape& aShape = anExp.Current();
371 if(aShapeTypeToExp == TopAbs_VERTEX) {
372 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
373 IntTools_Context anIntTools;
374 if(anIntTools.IsValidPointForFace(aPnt, aToFace, Precision::Confusion()) == Standard_True) {
375 GeomShapePtr aGeomSh(new GeomAPI_Shape());
376 aGeomSh->setImpl(new TopoDS_Shape(aShape));
377 this->addToShape(aGeomSh);
379 if(anIntTools.IsValidPointForFace(aPnt, aFromFace, Precision::Confusion()) == Standard_True) {
380 GeomShapePtr aGeomSh(new GeomAPI_Shape());
381 aGeomSh->setImpl(new TopoDS_Shape(aShape));
382 this->addFromShape(aGeomSh);
384 } else if(aShapeTypeToExp == TopAbs_EDGE) {
385 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
386 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, aToFace);
387 anEdgeCheck.Perform();
388 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
389 GeomShapePtr aGeomSh(new GeomAPI_Shape());
390 aGeomSh->setImpl(new TopoDS_Shape(aShape));
391 this->addToShape(aGeomSh);
393 anEdgeCheck.Init(anEdge, aFromFace);
394 anEdgeCheck.Perform();
395 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
396 GeomShapePtr aGeomSh(new GeomAPI_Shape());
397 aGeomSh->setImpl(new TopoDS_Shape(aShape));
398 this->addFromShape(aGeomSh);
405 if(aResult.ShapeType() == TopAbs_COMPOUND) {
406 GeomShapePtr aCompound(new GeomAPI_Shape);
407 aCompound->setImpl(new TopoDS_Shape(aResult));
408 ListOfShape aCompSolids, aFreeSolids;
409 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
410 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
411 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
412 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
413 TopoDS_Compound aResultComp;
414 TopoDS_Builder aBuilder;
415 aBuilder.MakeCompound(aResultComp);
416 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
417 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
419 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
420 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
422 aResult = aResultComp;
428 if(aResult.IsNull()) {
431 GeomShapePtr aGeomSh(new GeomAPI_Shape());
432 aGeomSh->setImpl(new TopoDS_Shape(aResult));
433 this->setShape(aGeomSh);