1 // Copyright (C) 2007-2016 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(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 wether 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 #if OCC_VERSION_MAJOR < 7
260 log.SetTouched(Label());
262 log->SetTouched(Label());
268 //=======================================================================
269 //function : MakeScaledPrism
271 //=======================================================================
272 TopoDS_Shape GEOMImpl_PrismDriver::MakeScaledPrism (const TopoDS_Shape& theShapeBase,
273 const gp_Vec& theVector,
274 const Standard_Real theScaleFactor,
275 const gp_Pnt& theCDG,
281 // 1. aCDG = geompy.MakeCDG(theBase)
282 gp_Pnt aCDG = theCDG;
284 gp_Ax3 aPos = GEOMUtils::GetPosition(theShapeBase);
285 aCDG = aPos.Location();
287 TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
289 // Process case of several given shapes
290 if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
291 theShapeBase.ShapeType() == TopAbs_SHELL) {
293 TopoDS_Shape aShapeI;
294 TopoDS_Compound aCompound;
295 B.MakeCompound(aCompound);
296 TopoDS_Iterator It (theShapeBase, Standard_True, Standard_True);
297 for (; It.More(); It.Next()) {
299 aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
300 B.Add(aCompound, aShapeI);
305 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
309 // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
311 // Bug 6839: Check for standalone (not included in faces) degenerated edges
312 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
313 TopExp::MapShapesAndAncestors(theShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
314 Standard_Integer i, nbE = aEFMap.Extent();
315 for (i = 1; i <= nbE; i++) {
316 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
317 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
318 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
319 if (aFaces.IsEmpty())
320 Standard_ConstructionError::Raise
321 ("Scaling aborted : cannot scale standalone degenerated edge");
327 aTrsf.SetScale(aCDG, theScaleFactor);
328 BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
329 TopoDS_Shape aScale = aBRepTrsf.Shape();
331 // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
333 aTrsf3.SetTranslation(theVector);
334 TopLoc_Location aLocOrig = aScale.Location();
335 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
336 TopLoc_Location aLocRes (aTrsf3 * aTrsfOrig);
337 TopoDS_Shape aBase2 = aScale.Located(aLocRes);
339 // 4. aCDG_2 = geompy.MakeTranslationVectorDistance(aCDG, theVec, theH)
340 gp_Pnt aCDG_2 = aCDG.Translated(theVector);
341 TopoDS_Shape aShapeCDG_2 = BRepBuilderAPI_MakeVertex(aCDG_2).Shape();
343 // 5. Vector = geompy.MakeVector(aCDG, aCDG_2)
344 TopoDS_Shape aShapeVec = BRepBuilderAPI_MakeEdge(aCDG, aCDG_2).Shape();
345 TopoDS_Edge anEdge = TopoDS::Edge(aShapeVec);
346 TopoDS_Wire aWirePath = BRepBuilderAPI_MakeWire(anEdge);
348 // 6. aPrism = geompy.MakePipeWithDifferentSections([theBase, aBase2], [aCDG, aCDG_2], Vector, False, False)
349 Handle(TopTools_HSequenceOfShape) aBases = new TopTools_HSequenceOfShape;
350 aBases->Append(theShapeBase);
351 aBases->Append(aBase2);
353 Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
354 aLocs->Append(aShapeCDG_1);
355 aLocs->Append(aShapeCDG_2);
357 aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections
358 (aWirePath, aBases, aLocs, false, false, false);
360 // 7. Make a solid, if possible
361 if (theShapeBase.ShapeType() == TopAbs_FACE) {
362 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
363 TopExp_Explorer expF (aShape, TopAbs_FACE);
364 Standard_Integer ifa = 0;
365 for (; expF.More(); expF.Next()) {
366 aSewing.Add(expF.Current());
373 TopoDS_Shape sh = aSewing.SewedShape();
374 if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
375 // case for creation of shell from one face
382 TopExp_Explorer exp (sh, TopAbs_SHELL);
383 Standard_Integer ish = 0;
384 for (; exp.More(); exp.Next()) {
385 aShell = exp.Current();
391 BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
392 if (chkShell.Closed() == BRepCheck_NoError) {
396 BRepClass3d_SolidClassifier SC (Sol);
397 SC.PerformInfinitePoint(Precision::Confusion());
398 if (SC.State() == TopAbs_IN) {
400 B.Add(Sol, aShell.Reversed());
410 //=======================================================================
411 //function : MakeDraftPrism
413 //=======================================================================
414 TopoDS_Shape GEOMImpl_PrismDriver::MakeDraftPrism ( const TopoDS_Shape& theInitShape,
415 const TopoDS_Shape& theBaseShape,
416 const Standard_Real theHeight,
417 const Standard_Real theAngle,
419 const TopoDS_Shape& theSupport,
424 if (theInitShape.ShapeType() == TopAbs_COMPOUND)
426 TopExp_Explorer anExp(theInitShape, TopAbs_SOLID);
428 for(;anExp.More();anExp.Next())
432 Standard_ConstructionError::Raise("The input shape is a compound with more than one solid");
435 Standard_ConstructionError::Raise("The input shape is a compound without any solid");
438 TopoDS_Wire aWire = TopoDS_Wire();
440 if (theBaseShape.ShapeType() == TopAbs_EDGE)
442 aWire = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseShape));
444 else if (theBaseShape.ShapeType() == TopAbs_WIRE)
446 aWire = TopoDS::Wire(theBaseShape);
450 Standard_ConstructionError::Raise("The input profile is neither a wire, nor edge");
453 TopoDS_Vertex aV1, aV2;
454 TopExp::Vertices(aWire, aV1, aV2);
455 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
456 aWire.Closed( true );
459 Standard_ConstructionError::Raise("The input profile is not closed");
461 // Construction of the face if the wire hasn't any support face;
462 // the face must be planar for BRepFeat_MakeDPrism
463 TopoDS_Face aFaceBase = BRepBuilderAPI_MakeFace(aWire, /*OnlyPlane=*/true);
465 if(!theSupport.IsNull() && theSupport.ShapeType() == TopAbs_FACE) // If the wire has a support
467 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(theSupport));
468 TopoDS_Face aTempFace = BRepBuilderAPI_MakeFace(aSurf, aWire);
470 if(aTempFace.Orientation() != TopoDS::Face(theSupport).Orientation())
472 aFaceBase=TopoDS::Face(aTempFace.Reversed());
478 // Invert height and angle if the operation is an extruded cut
479 bool invert = isInvert? isProtrusion : !isProtrusion;
481 // If the face has a reversed orientation invert for extruded boss operations
482 if(aFaceBase.Orientation() == TopAbs_REVERSED)
485 Standard_Real anAngle = theAngle;
486 Standard_Real aHeight = theHeight;
489 anAngle = -theAngle; // Invert angle and height
490 aHeight = -theHeight;
493 BRepFeat_MakeDPrism aPrism(theInitShape, aFaceBase, aFaceBase,
494 anAngle*M_PI/180., isProtrusion, Standard_True);
496 aPrism.Perform(aHeight);
497 aPrism.Check(); // Raises NotDone if done is false
499 aShape = aPrism.Shape();
504 //================================================================================
506 * \brief Returns a name of creation operation and names and values of creation parameters
508 //================================================================================
510 bool GEOMImpl_PrismDriver::
511 GetCreationInformation(std::string& theOperationName,
512 std::vector<GEOM_Param>& theParams)
514 if (Label().IsNull()) return 0;
515 Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
517 GEOMImpl_IPrism aCI( function );
518 Standard_Integer aType = function->GetType();
520 theOperationName = "EXTRUSION";
523 case PRISM_BASE_VEC_H:
524 case PRISM_BASE_VEC_H_2WAYS:
525 AddParam( theParams, "Base", aCI.GetBase() );
526 AddParam( theParams, "Vector", aCI.GetVector() );
527 AddParam( theParams, "Height", aCI.GetH() );
528 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
529 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
531 case PRISM_BASE_TWO_PNT:
532 case PRISM_BASE_TWO_PNT_2WAYS:
533 AddParam( theParams, "Base", aCI.GetBase() );
534 AddParam( theParams, "Point 1", aCI.GetFirstPoint() );
535 AddParam( theParams, "Point 2", aCI.GetLastPoint() );
536 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
537 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
539 case PRISM_BASE_DXDYDZ:
540 case PRISM_BASE_DXDYDZ_2WAYS:
541 AddParam( theParams, "Base", aCI.GetBase() );
542 AddParam( theParams, "Dx", aCI.GetDX() );
543 AddParam( theParams, "Dy", aCI.GetDY() );
544 AddParam( theParams, "Dz", aCI.GetDZ() );
545 AddParam( theParams, "Both Directions", aType == PRISM_BASE_VEC_H_2WAYS );
546 AddParam( theParams, "Scale base-opposite face", aCI.GetScale() );
548 case DRAFT_PRISM_FEATURE:
549 theOperationName = aCI.GetFuseFlag() ? "EXTRUDED_BOSS" : "EXTRUDED_CUT";
550 AddParam( theParams, "Initial shape", aCI.GetInitShape() );
551 AddParam( theParams, "Profile", aCI.GetBase() );
552 AddParam( theParams, "Height", aCI.GetH() );
553 AddParam( theParams, "Draft angle", aCI.GetDraftAngle() );
562 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PrismDriver,GEOM_BaseDriver);