1 // Copyright (C) 2007-2011 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.
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_IShapesOperations.hxx>
27 #include <GEOMImpl_IMeasureOperations.hxx>
28 #include <GEOMImpl_GlueDriver.hxx>
29 #include <GEOMImpl_PipeDriver.hxx>
30 #include <GEOMImpl_Types.hxx>
31 #include <GEOM_Function.hxx>
33 #include <BRepPrimAPI_MakePrism.hxx>
34 #include <BRepFeat_MakeDPrism.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRepBuilderAPI_MakeEdge.hxx>
38 #include <BRepBuilderAPI_MakeWire.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <BRepBuilderAPI_MakeVertex.hxx>
41 #include <BRepBuilderAPI_Sewing.hxx>
42 #include <BRepBuilderAPI_Transform.hxx>
43 #include <BRepCheck_Shell.hxx>
44 #include <BRepClass3d_SolidClassifier.hxx>
45 #include <BRep_Tool.hxx>
46 #include <BRepTools.hxx>
50 #include <TopExp_Explorer.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Edge.hxx>
54 #include <TopoDS_Shape.hxx>
55 #include <TopoDS_Shell.hxx>
56 #include <TopoDS_Solid.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <TopTools_HSequenceOfShape.hxx>
59 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
61 #include <Precision.hxx>
65 #include <gp_Trsf.hxx>
67 #include <Standard_Stream.hxx>
69 #include <Standard_ConstructionError.hxx>
71 #include "utilities.h"
73 //=======================================================================
76 //=======================================================================
77 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
79 static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
84 //=======================================================================
85 //function : GEOMImpl_PrismDriver
87 //=======================================================================
88 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver()
92 //=======================================================================
95 //=======================================================================
96 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
98 if (Label().IsNull()) return 0;
99 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
101 GEOMImpl_IPrism aCI (aFunction);
102 Standard_Integer aType = aFunction->GetType();
106 if (aType == PRISM_BASE_VEC_H || aType == PRISM_BASE_VEC_H_2WAYS) {
107 Handle(GEOM_Function) aRefBase = aCI.GetBase();
108 Handle(GEOM_Function) aRefVector = aCI.GetVector();
109 TopoDS_Shape aShapeBase = aRefBase->GetValue();
110 TopoDS_Shape aShapeVec = aRefVector->GetValue();
111 if (aShapeVec.ShapeType() == TopAbs_EDGE) {
112 TopoDS_Edge anE = TopoDS::Edge(aShapeVec);
113 TopoDS_Vertex V1, V2;
114 TopExp::Vertices(anE, V1, V2, Standard_True);
115 if (!V1.IsNull() && !V2.IsNull()) {
116 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
117 if (Abs(aCI.GetH()) < Precision::Confusion()) {
118 Standard_ConstructionError::Raise("Absolute value of prism height is too small");
120 if (aV.Magnitude() > Precision::Confusion()) {
122 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
123 aShape = MakeScaledPrism(aShapeBase, aV * aCI.GetH(), aCI.GetScale());
126 if (aType == PRISM_BASE_VEC_H_2WAYS) {
128 aTrsf.SetTranslation((-aV) * aCI.GetH());
129 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
130 aShapeBase = aTransformation.Shape();
131 aCI.SetH(aCI.GetH()*2);
133 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
138 } else if (aType == PRISM_BASE_TWO_PNT || aType == PRISM_BASE_TWO_PNT_2WAYS) {
139 Handle(GEOM_Function) aRefBase = aCI.GetBase();
140 Handle(GEOM_Function) aRefPnt1 = aCI.GetFirstPoint();
141 Handle(GEOM_Function) aRefPnt2 = aCI.GetLastPoint();
142 TopoDS_Shape aShapeBase = aRefBase->GetValue();
143 TopoDS_Shape aShapePnt1 = aRefPnt1->GetValue();
144 TopoDS_Shape aShapePnt2 = aRefPnt2->GetValue();
145 if (aShapePnt1.ShapeType() == TopAbs_VERTEX &&
146 aShapePnt2.ShapeType() == TopAbs_VERTEX) {
147 TopoDS_Vertex V1 = TopoDS::Vertex(aShapePnt1);
148 TopoDS_Vertex V2 = TopoDS::Vertex(aShapePnt2);
149 if (!V1.IsNull() && !V2.IsNull()) {
150 gp_Vec aV (BRep_Tool::Pnt(V1), BRep_Tool::Pnt(V2));
151 if (aV.Magnitude() > gp::Resolution()) {
152 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
153 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
156 if (aType == PRISM_BASE_TWO_PNT_2WAYS) {
158 aTrsf.SetTranslation(-aV);
159 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
160 aShapeBase = aTransformation.Shape();
163 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
168 } else if (aType == PRISM_BASE_DXDYDZ || aType == PRISM_BASE_DXDYDZ_2WAYS) {
169 Handle(GEOM_Function) aRefBase = aCI.GetBase();
170 TopoDS_Shape aShapeBase = aRefBase->GetValue();
171 gp_Vec aV (aCI.GetDX(), aCI.GetDY(), aCI.GetDZ());
172 if (aV.Magnitude() > gp::Resolution()) {
173 if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
174 aShape = MakeScaledPrism(aShapeBase, aV, aCI.GetScale());
177 if (aType == PRISM_BASE_DXDYDZ_2WAYS) {
179 aTrsf.SetTranslation(-aV);
180 BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
181 aShapeBase = aTransformation.Shape();
184 aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
189 else if (aType == DRAFT_PRISM_FEATURE) {
190 Handle(GEOM_Function) aRefInit = aCI.GetInitShape();
191 Handle(GEOM_Function) aRefBase = aCI.GetBase();
192 TopoDS_Shape anInitShape = aRefInit->GetValue(); // Initial shape
193 TopoDS_Shape aSketch = aRefBase->GetValue();
194 Standard_Real aHeight = aCI.GetH(); // Height of the extrusion
195 Standard_Real anAngle = aCI.GetDraftAngle(); // Draft angle
196 Standard_Boolean isProtrusion = (aCI.GetFuseFlag()==1);
197 // Flag to know wether the feature is a protrusion (fuse) or a depression (cut)
199 // history of the Base wire (RefBase)
200 Handle(GEOM_Object) aSuppObj;
201 TDF_LabelSequence aLabelSeq;
202 aRefBase->GetDependency(aLabelSeq);
204 // If the base wire has only one dependency we use it
205 // to determine the right normal of the face which
206 // must be oriented towards outside of the solid (like the support face)
207 if (aLabelSeq.Length()==1)
209 TDF_Label anArgumentRefLabel = aLabelSeq.Value(1);
210 aSuppObj = GEOM_Object::GetReferencedObject(anArgumentRefLabel);
213 TopoDS_Shape aSupport;
215 if(!aSuppObj.IsNull()) // If the wire has a support
216 aSupport = aSuppObj->GetValue();
218 aShape = MakeDraftPrism(anInitShape, aSketch, aHeight, anAngle, isProtrusion, aSupport);
221 if (aShape.IsNull()) return 0;
224 if (aType == DRAFT_PRISM_FEATURE)
226 TopoDS_Shape aRes = aShape;
228 // If the result is a compound with only one solid,
230 if (aShape.ShapeType() == TopAbs_COMPOUND)
232 TopExp_Explorer anExp(aShape, TopAbs_SOLID);
237 for(;anExp.More();anExp.Next())
239 aSolid = TopoDS::Solid(anExp.Current());
248 aFunction->SetValue(aRes);
252 TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
253 aFunction->SetValue(aRes);
257 log.SetTouched(Label());
262 //=======================================================================
263 //function : MakeScaledPrism
265 //=======================================================================
266 TopoDS_Shape GEOMImpl_PrismDriver::MakeScaledPrism (const TopoDS_Shape& theShapeBase,
267 const gp_Vec& theVector,
268 const Standard_Real theScaleFactor,
269 const gp_Pnt& theCDG,
275 // 1. aCDG = geompy.MakeCDG(theBase)
276 gp_Pnt aCDG = theCDG;
278 gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(theShapeBase);
279 aCDG = aPos.Location();
281 TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
283 // Process case of several given shapes
284 if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
285 theShapeBase.ShapeType() == TopAbs_SHELL) {
287 TopoDS_Shape aShapeI;
288 TopoDS_Compound aCompound;
289 B.MakeCompound(aCompound);
290 TopoDS_Iterator It (theShapeBase, Standard_True, Standard_True);
291 for (; It.More(); It.Next()) {
293 aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
294 B.Add(aCompound, aShapeI);
299 aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
303 // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
305 // Bug 6839: Check for standalone (not included in faces) degenerated edges
306 TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
307 TopExp::MapShapesAndAncestors(theShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
308 Standard_Integer i, nbE = aEFMap.Extent();
309 for (i = 1; i <= nbE; i++) {
310 TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
311 if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
312 const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
313 if (aFaces.IsEmpty())
314 Standard_ConstructionError::Raise
315 ("Scaling aborted : cannot scale standalone degenerated edge");
321 aTrsf.SetScale(aCDG, theScaleFactor);
322 BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
323 TopoDS_Shape aScale = aBRepTrsf.Shape();
325 // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
327 aTrsf3.SetTranslation(theVector);
328 TopLoc_Location aLocOrig = aScale.Location();
329 gp_Trsf aTrsfOrig = aLocOrig.Transformation();
330 TopLoc_Location aLocRes (aTrsf3 * aTrsfOrig);
331 TopoDS_Shape aBase2 = aScale.Located(aLocRes);
333 // 4. aCDG_2 = geompy.MakeTranslationVectorDistance(aCDG, theVec, theH)
334 gp_Pnt aCDG_2 = aCDG.Translated(theVector);
335 TopoDS_Shape aShapeCDG_2 = BRepBuilderAPI_MakeVertex(aCDG_2).Shape();
337 // 5. Vector = geompy.MakeVector(aCDG, aCDG_2)
338 TopoDS_Shape aShapeVec = BRepBuilderAPI_MakeEdge(aCDG, aCDG_2).Shape();
339 TopoDS_Edge anEdge = TopoDS::Edge(aShapeVec);
340 TopoDS_Wire aWirePath = BRepBuilderAPI_MakeWire(anEdge);
342 // 6. aPrism = geompy.MakePipeWithDifferentSections([theBase, aBase2], [aCDG, aCDG_2], Vector, False, False)
343 Handle(TopTools_HSequenceOfShape) aBases = new TopTools_HSequenceOfShape;
344 aBases->Append(theShapeBase);
345 aBases->Append(aBase2);
347 Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
348 aLocs->Append(aShapeCDG_1);
349 aLocs->Append(aShapeCDG_2);
351 aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections(aWirePath, aBases, aLocs, false, false);
353 // 7. Make a solid, if possible
354 if (theShapeBase.ShapeType() == TopAbs_FACE) {
355 BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
356 TopExp_Explorer expF (aShape, TopAbs_FACE);
357 Standard_Integer ifa = 0;
358 for (; expF.More(); expF.Next()) {
359 aSewing.Add(expF.Current());
366 TopoDS_Shape sh = aSewing.SewedShape();
367 if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
368 // case for creation of shell from one face
375 TopExp_Explorer exp (sh, TopAbs_SHELL);
376 Standard_Integer ish = 0;
377 for (; exp.More(); exp.Next()) {
378 aShell = exp.Current();
384 BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
385 if (chkShell.Closed() == BRepCheck_NoError) {
389 BRepClass3d_SolidClassifier SC (Sol);
390 SC.PerformInfinitePoint(Precision::Confusion());
391 if (SC.State() == TopAbs_IN) {
393 B.Add(Sol, aShell.Reversed());
403 //=======================================================================
404 //function : MakeDraftPrism
406 //=======================================================================
407 TopoDS_Shape GEOMImpl_PrismDriver::MakeDraftPrism ( const TopoDS_Shape& theInitShape,
408 const TopoDS_Shape& theBaseShape,
409 const Standard_Real theHeight,
410 const Standard_Real theAngle,
412 const TopoDS_Shape& theSupport)
416 if (theInitShape.ShapeType() == TopAbs_COMPOUND)
418 TopExp_Explorer anExp(theInitShape, TopAbs_SOLID);
420 for(;anExp.More();anExp.Next())
424 Standard_ConstructionError::Raise("The input shape is a compound with more than one solid");
427 Standard_ConstructionError::Raise("The input shape is a compound without any solid");
430 TopoDS_Wire aWire = TopoDS_Wire();
432 if (theBaseShape.ShapeType() == TopAbs_EDGE)
434 aWire = BRepBuilderAPI_MakeWire(TopoDS::Edge(theBaseShape));
436 else if (theBaseShape.ShapeType() == TopAbs_WIRE)
438 aWire = TopoDS::Wire(theBaseShape);
442 Standard_ConstructionError::Raise("The input profile is neither a wire, nor edge");
445 TopoDS_Vertex aV1, aV2;
446 TopExp::Vertices(aWire, aV1, aV2);
447 if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
448 aWire.Closed( true );
451 Standard_ConstructionError::Raise("The input profile is not closed");
453 // Construction of the face if the wire hasn't any support face
454 TopoDS_Face aFaceBase = BRepBuilderAPI_MakeFace(aWire);
456 if(!theSupport.IsNull() && theSupport.ShapeType() == TopAbs_FACE) // If the wire has a support
458 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(theSupport));
459 TopoDS_Face aTempFace = BRepBuilderAPI_MakeFace(aSurf, aWire);
461 if(aTempFace.Orientation() != TopoDS::Face(theSupport).Orientation())
463 aFaceBase=TopoDS::Face(aTempFace.Reversed());
469 // Invert height and angle if the operation is an extruded cut
470 bool invert = !isProtrusion;
472 // If the face has a reversed orientation invert for extruded boss operations
473 if(aFaceBase.Orientation() == TopAbs_REVERSED)
474 invert = isProtrusion;
476 Standard_Real anAngle = theAngle;
477 Standard_Real aHeight = theHeight;
480 anAngle = -theAngle; // Invert angle and height
481 aHeight = -theHeight;
484 BRepFeat_MakeDPrism aPrism(theInitShape, aFaceBase, TopoDS_Face(),
485 anAngle*PI180, isProtrusion, Standard_True);
487 aPrism.Perform(aHeight);
489 aShape = aPrism.Shape();
495 //=======================================================================
496 //function : GEOMImpl_PrismDriver_Type_
498 //=======================================================================
499 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
502 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
503 if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
504 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
505 if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
506 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
507 if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
509 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
510 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PrismDriver",
511 sizeof(GEOMImpl_PrismDriver),
513 (Standard_Address)_Ancestors,
514 (Standard_Address)NULL);
519 //=======================================================================
520 //function : DownCast
522 //=======================================================================
523 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
525 Handle(GEOMImpl_PrismDriver) _anOtherObject;
527 if (!AnObject.IsNull()) {
528 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
529 _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
533 return _anOtherObject;