1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include <GEOMImpl_PrismDriver.hxx>
25 #include <GEOMImpl_IPrism.hxx>
26 #include <GEOMImpl_GlueDriver.hxx>
27 #include <GEOMImpl_PipeDriver.hxx>
28 #include <GEOMImpl_Types.hxx>
30 #include <GEOM_Function.hxx>
31 #include <GEOM_Object.hxx>
33 #include <GEOMUtils.hxx>
35 #include <BRepPrimAPI_MakePrism.hxx>
36 #include <BRepFeat_MakeDPrism.hxx>
38 #include <BRep_Builder.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeWire.hxx>
41 #include <BRepBuilderAPI_MakeFace.hxx>
42 #include <BRepBuilderAPI_MakeVertex.hxx>
43 #include <BRepBuilderAPI_Sewing.hxx>
44 #include <BRepBuilderAPI_Transform.hxx>
45 #include <BRepCheck_Shell.hxx>
46 #include <BRepClass3d_SolidClassifier.hxx>
47 #include <BRep_Tool.hxx>
48 #include <BRepTools.hxx>
52 #include <TopExp_Explorer.hxx>
54 #include <TopoDS_Compound.hxx>
55 #include <TopoDS_Edge.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Shell.hxx>
58 #include <TopoDS_Solid.hxx>
59 #include <TopoDS_Vertex.hxx>
60 #include <TopTools_HSequenceOfShape.hxx>
61 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
63 #include <Precision.hxx>
67 #include <gp_Trsf.hxx>
69 #include <Standard_Stream.hxx>
71 #include <Standard_ConstructionError.hxx>
73 #include "utilities.h"
75 //=======================================================================
78 //=======================================================================
79 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
81 static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
86 //=======================================================================
87 //function : GEOMImpl_PrismDriver
89 //=======================================================================
90 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver()
94 //=======================================================================
97 //=======================================================================
98 Standard_Integer GEOMImpl_PrismDriver::Execute(Handle(TFunction_Logbook)& log) const
100 if (Label().IsNull()) return 0;
101 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
103 GEOMImpl_IPrism aCI (aFunction);
104 Standard_Integer aType = aFunction->GetType();
108 if (aType == PRISM_BASE_VEC_H || aType == PRISM_BASE_VEC_H_2WAYS) {
109 Handle(GEOM_Function) aRefBase = aCI.GetBase();
110 Handle(GEOM_Function) aRefVector = aCI.GetVector();
111 TopoDS_Shape aShapeBase = aRefBase->GetValue();
112 TopoDS_Shape aShapeVec = aRefVector->GetValue();
113 if (aShapeVec.ShapeType() == TopAbs_EDGE) {
114 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
115 TopoDS_Vertex V1, V2;
116 TopExp::Vertices(anE, V1, V2, Standard_True);
117 if (!V1.IsNull() && !V2.IsNull()) {
118 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
119 if (Abs(aCI.GetH()) < Precision::Confusion()) {
120 Standard_ConstructionError::Raise("Absolute value of prism height is too small");
122 if (aV.Magnitude() > Precision::Confusion()) {
124 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
125 aShape = MakeScaledPrism(aShapeBase, aV * aCI.GetH(), aCI.GetScale());
128 if (aType == PRISM_BASE_VEC_H_2WAYS) {
130 aTrsf.SetTranslation((-aV) * aCI.GetH());
131 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
132 aShapeBase = aTransformation.Shape();
133 aCI.SetH(aCI.GetH()*2);
135 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
140 } else if (aType == PRISM_BASE_TWO_PNT || aType == PRISM_BASE_TWO_PNT_2WAYS) {
141 Handle(GEOM_Function) aRefBase = aCI.GetBase();
142 Handle(GEOM_Function) aRefPnt1 = aCI.GetFirstPoint();
143 Handle(GEOM_Function) aRefPnt2 = aCI.GetLastPoint();
144 TopoDS_Shape aShapeBase = aRefBase->GetValue();
145 TopoDS_Shape aShapePnt1 = aRefPnt1->GetValue();
146 TopoDS_Shape aShapePnt2 = aRefPnt2->GetValue();
147 if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
148 aShapePnt2.ShapeType() == TopAbs_VERTEX) {
149 TopoDS_Vertex V1 = TopoDS::Vertex(aShapePnt1);
150 TopoDS_Vertex V2 = TopoDS::Vertex(aShapePnt2);
151 if (!V1.IsNull() && !V2.IsNull()) {
152 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
153 if (aV.Magnitude() > gp::Resolution()) {
154 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
155 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
158 if (aType == PRISM_BASE_TWO_PNT_2WAYS) {
160 aTrsf.SetTranslation(-aV);
161 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
162 aShapeBase = aTransformation.Shape();
165 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
170 } else if (aType == PRISM_BASE_DXDYDZ || aType == PRISM_BASE_DXDYDZ_2WAYS) {
171 Handle(GEOM_Function) aRefBase = aCI.GetBase();
172 TopoDS_Shape aShapeBase = aRefBase->GetValue();
173 gp_Vec aV (aCI.GetDX(), aCI.GetDY(), aCI.GetDZ());
174 if (aV.Magnitude() > gp::Resolution()) {
175 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
176 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
179 if (aType == PRISM_BASE_DXDYDZ_2WAYS) {
181 aTrsf.SetTranslation(-aV);
182 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
183 aShapeBase = aTransformation.Shape();
186 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
191 else if (aType == DRAFT_PRISM_FEATURE) {
192 Handle(GEOM_Function) aRefInit = aCI.GetInitShape();
193 Handle(GEOM_Function) aRefBase = aCI.GetBase();
194 TopoDS_Shape anInitShape = aRefInit->GetValue(); // Initial shape
195 TopoDS_Shape aSketch = aRefBase->GetValue();
196 Standard_Real aHeight = aCI.GetH(); // Height of the extrusion
197 Standard_Real anAngle = aCI.GetDraftAngle(); // Draft angle
198 Standard_Boolean isProtrusion = (aCI.GetFuseFlag()==1);
199 Standard_Boolean isInvert = aCI.GetInvertFlag();
200 // Flag to know whether the feature is a protrusion (fuse) or a depression (cut)
202 // history of the Base wire (RefBase)
203 Handle(GEOM_Object) aSuppObj;
204 TDF_LabelSequence aLabelSeq;
205 aRefBase->GetDependency(aLabelSeq);
207 // If the base wire has only one dependency we use it
208 // to determine the right normal of the face which
209 // must be oriented towards outside of the solid (like the support face)
210 if (aLabelSeq.Length()==1)
212 TDF_Label anArgumentRefLabel = aLabelSeq.Value(1);
213 aSuppObj = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
216 TopoDS_Shape aSupport;
218 if(!aSuppObj.IsNull()) // If the wire has a support
219 aSupport = aSuppObj->GetValue();
221 aShape = MakeDraftPrism(anInitShape, aSketch, aHeight, anAngle, isProtrusion, aSupport, isInvert);
224 if (aShape.IsNull()) return 0;
227 if (aType == DRAFT_PRISM_FEATURE)
229 TopoDS_Shape aRes = aShape;
231 // If the result is a compound with only one solid,
233 if (aShape.ShapeType() == TopAbs_COMPOUND)
235 TopExp_Explorer anExp(aShape, TopAbs_SOLID);
240 for(;anExp.More();anExp.Next())
242 aSolid = TopoDS::Solid(anExp.Current());
251 aFunction->SetValue(aRes);
255 TopoDS_Shape aRes = GEOMUtils::CompsolidToCompound(aShape);
256 aFunction->SetValue(aRes);
259 log->SetTouched(Label());
264 //=======================================================================
265 //function : MakeScaledPrism
267 //=======================================================================
268 TopoDS_Shape GEOMImpl_PrismDriver::MakeScaledPrism (const TopoDS_Shape& theShapeBase,
269 const gp_Vec& theVector,
270 const Standard_Real theScaleFactor,
271 const gp_Pnt& theCDG,
277 // 1. aCDG = geompy.MakeCDG(theBase)
278 gp_Pnt aCDG = theCDG;
280 gp_Ax3 aPos = GEOMUtils::GetPosition(theShapeBase);
281 aCDG = aPos.Location();
283 TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
285 // Process case of several given shapes
286 if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
287 theShapeBase.ShapeType() == TopAbs_SHELL) {
289 TopoDS_Shape aShapeI;
290 TopoDS_Compound aCompound;
291 B.MakeCompound(aCompound);
292 TopoDS_Iterator It (theShapeBase, Standard_True, Standard_True);
293 for (; It.More(); It.Next()) {
295 aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
296 B.Add(aCompound, aShapeI);
301 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
305 // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
307 // Bug 6839: Check for standalone (not included in faces) degenerated edges
308 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
309 TopExp::MapShapesAndAncestors(theShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
310 Standard_Integer i, nbE = aEFMap.Extent();
311 for (i = 1; i <= nbE; i++) {
312 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
313 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
314 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
315 if (aFaces.IsEmpty())
316 Standard_ConstructionError::Raise
317 ("Scaling aborted : cannot scale standalone degenerated edge");
323 aTrsf.SetScale(aCDG, theScaleFactor);
324 BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
325 TopoDS_Shape aScale = aBRepTrsf.Shape();
327 // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
329 aTrsf3.SetTranslation(theVector);
330 TopLoc_Location aLocOrig = aScale.Location();
331 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
332 TopLoc_Location aLocRes (aTrsf3 * aTrsfOrig);
333 TopoDS_Shape aBase2 = aScale.Located(aLocRes);
335 // 4. aCDG_2 = geompy.MakeTranslationVectorDistance(aCDG, theVec, theH)
336 gp_Pnt aCDG_2 = aCDG.Translated(theVector);
337 TopoDS_Shape aShapeCDG_2 = BRepBuilderAPI_MakeVertex(aCDG_2).Shape();
339 // 5. Vector = geompy.MakeVector(aCDG, aCDG_2)
340 TopoDS_Shape aShapeVec = BRepBuilderAPI_MakeEdge(aCDG, aCDG_2).Shape();
341 TopoDS_Edge anEdge = TopoDS::Edge(aShapeVec);
342 TopoDS_Wire aWirePath = BRepBuilderAPI_MakeWire(anEdge);
344 // 6. aPrism = geompy.MakePipeWithDifferentSections([theBase, aBase2], [aCDG, aCDG_2], Vector, False, False)
345 Handle(TopTools_HSequenceOfShape) aBases = new TopTools_HSequenceOfShape;
346 aBases->Append(theShapeBase);
347 aBases->Append(aBase2);
349 Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
350 aLocs->Append(aShapeCDG_1);
351 aLocs->Append(aShapeCDG_2);
353 aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
354 (aWirePath, aBases, aLocs, false, false, false);
356 // 7. Make a solid, if possible
357 if (theShapeBase.ShapeType() == TopAbs_FACE) {
358 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
359 TopExp_Explorer expF (aShape, TopAbs_FACE);
360 Standard_Integer ifa = 0;
361 for (; expF.More(); expF.Next()) {
362 aSewing.Add(expF.Current());
369 TopoDS_Shape sh = aSewing.SewedShape();
370 if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
371 // case for creation of shell from one face
378 TopExp_Explorer exp (sh, TopAbs_SHELL);
379 Standard_Integer ish = 0;
380 for (; exp.More(); exp.Next()) {
381 aShell = exp.Current();
387 BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
388 if (chkShell.Closed() == BRepCheck_NoError) {
392 BRepClass3d_SolidClassifier SC (Sol);
393 SC.PerformInfinitePoint(Precision::Confusion());
394 if (SC.State() == TopAbs_IN) {
396 B.Add(Sol, aShell.Reversed());
406 //=======================================================================
407 //function : MakeDraftPrism
409 //=======================================================================
410 TopoDS_Shape GEOMImpl_PrismDriver::MakeDraftPrism ( const TopoDS_Shape& theInitShape,
411 const TopoDS_Shape& theBaseShape,
412 const Standard_Real theHeight,
413 const Standard_Real theAngle,
415 const TopoDS_Shape& theSupport,
420 if (theInitShape.ShapeType() == TopAbs_COMPOUND)
422 TopExp_Explorer anExp(theInitShape, TopAbs_SOLID);
424 for(;anExp.More();anExp.Next())
428 Standard_ConstructionError::Raise("The input shape is a compound with more than one solid");
431 Standard_ConstructionError::Raise("The input shape is a compound without any solid");
434 TopoDS_Wire aWire = TopoDS_Wire();
436 if (theBaseShape.ShapeType() == TopAbs_EDGE)
438 aWire = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseShape));
440 else if (theBaseShape.ShapeType() == TopAbs_WIRE)
442 aWire = TopoDS::Wire(theBaseShape);
446 Standard_ConstructionError::Raise("The input profile is neither a wire, nor edge");
449 TopoDS_Vertex aV1, aV2;
450 TopExp::Vertices(aWire, aV1, aV2);
451 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
452 aWire.Closed( true );
455 Standard_ConstructionError::Raise("The input profile is not closed");
457 // Construction of the face if the wire hasn't any support face;
458 // the face must be planar for BRepFeat_MakeDPrism
459 TopoDS_Face aFaceBase = BRepBuilderAPI_MakeFace(aWire, /*OnlyPlane=*/true);
461 if(!theSupport.IsNull() && theSupport.ShapeType() == TopAbs_FACE) // If the wire has a support
463 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(theSupport));
464 TopoDS_Face aTempFace = BRepBuilderAPI_MakeFace(aSurf, aWire);
466 if(aTempFace.Orientation() != TopoDS::Face(theSupport).Orientation())
468 aFaceBase=TopoDS::Face(aTempFace.Reversed());
474 // Invert height and angle if the operation is an extruded cut
475 bool invert = isInvert? isProtrusion : !isProtrusion;
477 // If the face has a reversed orientation invert for extruded boss operations
478 if(aFaceBase.Orientation() == TopAbs_REVERSED)
481 Standard_Real anAngle = theAngle;
482 Standard_Real aHeight = theHeight;
485 anAngle = -theAngle; // Invert angle and height
486 aHeight = -theHeight;
489 BRepFeat_MakeDPrism aPrism(theInitShape, aFaceBase, aFaceBase,
490 anAngle*M_PI/180., isProtrusion, Standard_True);
492 aPrism.Perform(aHeight);
493 aPrism.Check(); // Raises NotDone if done is false
495 aShape = aPrism.Shape();
500 //================================================================================
502 * \brief Returns a name of creation operation and names and values of creation parameters
504 //================================================================================
506 bool GEOMImpl_PrismDriver::
507 GetCreationInformation(std::string& theOperationName,
508 std::vector<GEOM_Param>& theParams)
510 if (Label().IsNull()) return 0;
511 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
513 GEOMImpl_IPrism aCI( function );
514 Standard_Integer aType = function->GetType();
516 theOperationName = "EXTRUSION";
519 case PRISM_BASE_VEC_H:
520 case PRISM_BASE_VEC_H_2WAYS:
521 AddParam( theParams, "Base", aCI.GetBase() );
522 AddParam( theParams, "Vector", aCI.GetVector() );
523 AddParam( theParams, "Height", aCI.GetH() );
524 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
525 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
527 case PRISM_BASE_TWO_PNT:
528 case PRISM_BASE_TWO_PNT_2WAYS:
529 AddParam( theParams, "Base", aCI.GetBase() );
530 AddParam( theParams, "Point 1", aCI.GetFirstPoint() );
531 AddParam( theParams, "Point 2", aCI.GetLastPoint() );
532 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
533 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
535 case PRISM_BASE_DXDYDZ:
536 case PRISM_BASE_DXDYDZ_2WAYS:
537 AddParam( theParams, "Base", aCI.GetBase() );
538 AddParam( theParams, "Dx", aCI.GetDX() );
539 AddParam( theParams, "Dy", aCI.GetDY() );
540 AddParam( theParams, "Dz", aCI.GetDZ() );
541 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
542 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
544 case DRAFT_PRISM_FEATURE:
545 theOperationName = aCI.GetFuseFlag() ? "EXTRUDED_BOSS" : "EXTRUDED_CUT";
546 AddParam( theParams, "Initial shape", aCI.GetInitShape() );
547 AddParam( theParams, "Profile", aCI.GetBase() );
548 AddParam( theParams, "Height", aCI.GetH() );
549 AddParam( theParams, "Draft angle", aCI.GetDraftAngle() );
558 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PrismDriver,GEOM_BaseDriver)