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.
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 and both bounding planes should be set or empty.
123 if(!theDirection.get() || (isBoundingShapesSet && (!theToShape.get() || !theFromShape.get()))) {
127 aBaseDir = theDirection;
128 aDirVec = theDirection->impl<gp_Dir>();
130 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
131 gp_Pnt aLoc = aPlane->Axis().Location();
132 aDirVec = aPlane->Axis().Direction();
134 aBaseLoc.reset(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
135 aBaseDir.reset(new GeomAPI_Dir(aDirVec.X(), aDirVec.Y(), aDirVec.Z()));
136 aBasePlane = GeomAlgoAPI_FaceBuilder::planarFace(aBaseLoc, aBaseDir);
139 TopoDS_Shape aResult;
140 if(!isBoundingShapesSet) {
141 // Moving base shape.
143 aTrsf.SetTranslation(aDirVec * -theFromSize);
144 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
145 if(!aTransformBuilder) {
148 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
149 if(!aTransformBuilder->IsDone()) {
152 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
155 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * (theFromSize + theToSize));
159 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
160 if(!aPrismBuilder->IsDone()) {
163 aResult = aPrismBuilder->Shape();
166 for(TopExp_Explorer anExp(aMovedBase, aShapeTypeToExp); anExp.More(); anExp.Next()) {
167 const TopoDS_Shape& aShape = anExp.Current();
168 GeomShapePtr aFromShape(new GeomAPI_Shape), aToShape(new GeomAPI_Shape);
169 aFromShape->setImpl(new TopoDS_Shape(aPrismBuilder->FirstShape(aShape)));
170 aToShape->setImpl(new TopoDS_Shape(aPrismBuilder->LastShape(aShape)));
171 this->addFromShape(aFromShape);
172 this->addToShape(aToShape);
175 GeomShapePtr aBoundingFromShape = theFromShape ? theFromShape : aBasePlane;
176 GeomShapePtr aBoundingToShape = theToShape ? theToShape : aBasePlane;
178 // Moving prism bounding faces according to "from" and "to" sizes.
179 std::shared_ptr<GeomAPI_Pln> aFromPln = GeomAPI_Face(aBoundingFromShape).getPlane();
180 std::shared_ptr<GeomAPI_Pnt> aFromLoc = aFromPln->location();
181 std::shared_ptr<GeomAPI_Dir> aFromDir = aFromPln->direction();
183 std::shared_ptr<GeomAPI_Pln> aToPln = GeomAPI_Face(aBoundingToShape).getPlane();
184 std::shared_ptr<GeomAPI_Pnt> aToLoc = aToPln->location();
185 std::shared_ptr<GeomAPI_Dir> aToDir = aToPln->direction();
187 bool aSign = aFromLoc->xyz()->dot(aBaseDir->xyz()) > aToLoc->xyz()->dot(aBaseDir->xyz());
189 std::shared_ptr<GeomAPI_Pnt> aFromPnt(new GeomAPI_Pnt(aFromLoc->xyz()->added(aBaseDir->xyz()->multiplied(
190 aSign ? theFromSize : -theFromSize))));
191 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
193 std::shared_ptr<GeomAPI_Pnt> aToPnt(new GeomAPI_Pnt(aToLoc->xyz()->added(aBaseDir->xyz()->multiplied(
194 aSign ? -theToSize : theToSize))));
195 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
197 // Getting bounding box for base shape.
199 BRepBndLib::Add(aBaseShape, aBndBox);
200 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
201 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
202 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
205 for(int i = 0; i < 2; i++) {
206 for(int j = 0; j < 2; j++) {
207 for(int k = 0; k < 2; k++) {
208 aPoints[aNum] = gp_Pnt(aXArr[i], aYArr[j], aZArr[k]);
214 // Project points to bounding planes. Search max distance to them.
215 IntAna_Quadric aBndToQuadric(gp_Pln(aToPnt->impl<gp_Pnt>(), aToDir->impl<gp_Dir>()));
216 IntAna_Quadric aBndFromQuadric(gp_Pln(aFromPnt->impl<gp_Pnt>(), aFromDir->impl<gp_Dir>()));
217 Standard_Real aMaxToDist = 0, aMaxFromDist = 0;
218 for(int i = 0; i < 8; i++) {
219 gp_Lin aLine(aPoints[i], aDirVec);
220 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
221 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
222 if(aToIntAna.NbPoints() == 0 || aFromIntAna.NbPoints() == 0) {
225 const gp_Pnt& aPntOnToFace = aToIntAna.Point(1);
226 const gp_Pnt& aPntOnFromFace = aFromIntAna.Point(1);
227 if(aPoints[i].Distance(aPntOnToFace) > aMaxToDist) {
228 aMaxToDist = aPoints[i].Distance(aPntOnToFace);
230 if(aPoints[i].Distance(aPntOnFromFace) > aMaxFromDist) {
231 aMaxFromDist = aPoints[i].Distance(aPntOnFromFace);
235 // We added 1 just to be sure that prism is long enough for boolean operation.
236 double aPrismLength = aMaxToDist + aMaxFromDist + 1;
238 // Moving base shape.
240 aTrsf.SetTranslation(aDirVec * -aPrismLength);
241 BRepBuilderAPI_Transform* aTransformBuilder = new BRepBuilderAPI_Transform(aBaseShape, aTrsf);
242 if(!aTransformBuilder) {
245 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aTransformBuilder)));
246 if(!aTransformBuilder->IsDone()) {
249 TopoDS_Shape aMovedBase = aTransformBuilder->Shape();
252 BRepPrimAPI_MakePrism* aPrismBuilder = new BRepPrimAPI_MakePrism(aMovedBase, aDirVec * 2 * aPrismLength);
256 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aPrismBuilder)));
257 if(!aPrismBuilder->IsDone()) {
260 aResult = aPrismBuilder->Shape();
262 // Orienting bounding planes.
263 std::shared_ptr<GeomAPI_Pnt> aCentreOfMass = GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape);
264 const gp_Pnt& aCentrePnt = aCentreOfMass->impl<gp_Pnt>();
265 gp_Lin aLine(aCentrePnt, aDirVec);
266 IntAna_IntConicQuad aToIntAna(aLine, aBndToQuadric);
267 IntAna_IntConicQuad aFromIntAna(aLine, aBndFromQuadric);
268 Standard_Real aToParameter = aToIntAna.ParamOnConic(1);
269 Standard_Real aFromParameter = aFromIntAna.ParamOnConic(1);
270 if(aToParameter > aFromParameter) {
271 gp_Vec aVec = aToDir->impl<gp_Dir>();
272 if((aVec * aDirVec) > 0) {
273 aToDir->setImpl(new gp_Dir(aVec.Reversed()));
274 aBoundingToShape = GeomAlgoAPI_FaceBuilder::planarFace(aToPnt, aToDir);
276 aVec = aFromDir->impl<gp_Dir>();
277 if((aVec * aDirVec) < 0) {
278 aFromDir->setImpl(new gp_Dir(aVec.Reversed()));
279 aBoundingFromShape = GeomAlgoAPI_FaceBuilder::planarFace(aFromPnt, aFromDir);
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);
294 // Making solids from bounding planes.
295 TopoDS_Shell aToShell, aFromShell;
296 TopoDS_Solid aToSolid, aFromSolid;
297 const TopoDS_Shape& aToShape = aBoundingToShape->impl<TopoDS_Shape>();
298 const TopoDS_Shape& aFromShape = aBoundingFromShape->impl<TopoDS_Shape>();
299 TopoDS_Face aToFace = TopoDS::Face(aToShape);
300 TopoDS_Face aFromFace = TopoDS::Face(aFromShape);
301 BRep_Builder aBoundingBuilder;
302 aBoundingBuilder.MakeShell(aToShell);
303 aBoundingBuilder.Add(aToShell, aToShape);
304 aBoundingBuilder.MakeShell(aFromShell);
305 aBoundingBuilder.Add(aFromShell, aFromShape);
306 aBoundingBuilder.MakeSolid(aToSolid);
307 aBoundingBuilder.Add(aToSolid, aToShell);
308 aBoundingBuilder.MakeSolid(aFromSolid);
309 aBoundingBuilder.Add(aFromSolid, aFromShell);
311 // Cutting with to plane.
312 BRepAlgoAPI_Cut* aToCutBuilder = new BRepAlgoAPI_Cut(aResult, aToSolid);
313 aToCutBuilder->Build();
314 if(!aToCutBuilder->IsDone()) {
317 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aToCutBuilder)));
318 aResult = aToCutBuilder->Shape();
319 if(aResult.ShapeType() == TopAbs_COMPOUND) {
320 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
322 if(aShapeTypeToExp == TopAbs_FACE) {
323 const TopTools_ListOfShape& aToShapes = aToCutBuilder->Modified(aToShape);
324 for(TopTools_ListIteratorOfListOfShape anIt(aToShapes); anIt.More(); anIt.Next()) {
325 GeomShapePtr aGeomSh(new GeomAPI_Shape());
326 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
327 this->addToShape(aGeomSh);
331 // Cutting with from plane.
332 BRepAlgoAPI_Cut* aFromCutBuilder = new BRepAlgoAPI_Cut(aResult, aFromSolid);
333 aFromCutBuilder->Build();
334 if(!aFromCutBuilder->IsDone()) {
337 this->appendAlgo(std::shared_ptr<GeomAlgoAPI_MakeShape>(new GeomAlgoAPI_MakeShape(aFromCutBuilder)));
338 aResult = aFromCutBuilder->Shape();
339 TopoDS_Iterator aCheckIt(aResult);
340 if(!aCheckIt.More()) {
343 if(aResult.ShapeType() == TopAbs_COMPOUND) {
344 aResult = GeomAlgoAPI_DFLoader::refineResult(aResult);
346 if(aShapeTypeToExp == TopAbs_FACE) {
347 const TopTools_ListOfShape& aFromShapes = aFromCutBuilder->Modified(aFromShape);
348 for(TopTools_ListIteratorOfListOfShape anIt(aFromShapes); anIt.More(); anIt.Next()) {
349 GeomShapePtr aGeomSh(new GeomAPI_Shape());
350 aGeomSh->setImpl(new TopoDS_Shape(anIt.Value()));
351 this->addFromShape(aGeomSh);
355 // Naming for extrusion from vertex, edge.
356 for(TopExp_Explorer anExp(aResult, aShapeTypeToExp); anExp.More(); anExp.Next()) {
357 const TopoDS_Shape& aShape = anExp.Current();
358 if(aShapeTypeToExp == TopAbs_VERTEX) {
359 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
360 IntTools_Context anIntTools;
361 if(anIntTools.IsValidPointForFace(aPnt, aToFace, Precision::Confusion()) == Standard_True) {
362 GeomShapePtr aGeomSh(new GeomAPI_Shape());
363 aGeomSh->setImpl(new TopoDS_Shape(aShape));
364 this->addToShape(aGeomSh);
366 if(anIntTools.IsValidPointForFace(aPnt, aFromFace, Precision::Confusion()) == Standard_True) {
367 GeomShapePtr aGeomSh(new GeomAPI_Shape());
368 aGeomSh->setImpl(new TopoDS_Shape(aShape));
369 this->addFromShape(aGeomSh);
371 } else if(aShapeTypeToExp == TopAbs_EDGE) {
372 TopoDS_Edge anEdge = TopoDS::Edge(aShape);
373 BRepLib_CheckCurveOnSurface anEdgeCheck(anEdge, aToFace);
374 anEdgeCheck.Perform();
375 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
376 GeomShapePtr aGeomSh(new GeomAPI_Shape());
377 aGeomSh->setImpl(new TopoDS_Shape(aShape));
378 this->addToShape(aGeomSh);
380 anEdgeCheck.Init(anEdge, aFromFace);
381 anEdgeCheck.Perform();
382 if(anEdgeCheck.MaxDistance() < Precision::Confusion()) {
383 GeomShapePtr aGeomSh(new GeomAPI_Shape());
384 aGeomSh->setImpl(new TopoDS_Shape(aShape));
385 this->addFromShape(aGeomSh);
392 if(aResult.ShapeType() == TopAbs_COMPOUND) {
393 GeomShapePtr aCompound(new GeomAPI_Shape);
394 aCompound->setImpl(new TopoDS_Shape(aResult));
395 ListOfShape aCompSolids, aFreeSolids;
396 GeomAlgoAPI_ShapeTools::combineShapes(aCompound, GeomAPI_Shape::COMPSOLID, aCompSolids, aFreeSolids);
397 if(aCompSolids.size() == 1 && aFreeSolids.size() == 0) {
398 aResult = aCompSolids.front()->impl<TopoDS_Shape>();
399 } else if (aCompSolids.size() > 1 || (aCompSolids.size() >= 1 && aFreeSolids.size() >= 1)) {
400 TopoDS_Compound aResultComp;
401 TopoDS_Builder aBuilder;
402 aBuilder.MakeCompound(aResultComp);
403 for(ListOfShape::const_iterator anIter = aCompSolids.cbegin(); anIter != aCompSolids.cend(); anIter++) {
404 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
406 for(ListOfShape::const_iterator anIter = aFreeSolids.cbegin(); anIter != aFreeSolids.cend(); anIter++) {
407 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
409 aResult = aResultComp;
415 if(aResult.IsNull()) {
418 GeomShapePtr aGeomSh(new GeomAPI_Shape());
419 aGeomSh->setImpl(new TopoDS_Shape(aResult));
420 this->setShape(aGeomSh);