]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_PrismDriver.cxx
Salome HOME
52e9ac1c666bcc68131d49f36b23e6dc9ebf35a4
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PrismDriver.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <GEOMImpl_PrismDriver.hxx>
24
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>
32
33 #include <BRepPrimAPI_MakePrism.hxx>
34 #include <BRepFeat_MakeDPrism.hxx>
35
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>
47
48 #include <TopAbs.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopoDS.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>
60
61 #include <Precision.hxx>
62 #include <gp_Ax3.hxx>
63 #include <gp_Pnt.hxx>
64 #include <gp_Vec.hxx>
65 #include <gp_Trsf.hxx>
66
67 #include <Standard_Stream.hxx>
68
69 #include <Standard_ConstructionError.hxx>
70
71 #include "utilities.h"
72
73 //=======================================================================
74 //function : GetID
75 //purpose  :
76 //=======================================================================
77 const Standard_GUID& GEOMImpl_PrismDriver::GetID()
78 {
79   static Standard_GUID aPrismDriver("FF1BBB17-5D14-4df2-980B-3A668264EA16");
80   return aPrismDriver;
81 }
82
83
84 //=======================================================================
85 //function : GEOMImpl_PrismDriver
86 //purpose  :
87 //=======================================================================
88 GEOMImpl_PrismDriver::GEOMImpl_PrismDriver()
89 {
90 }
91
92 //=======================================================================
93 //function : Execute
94 //purpose  :
95 //=======================================================================
96 Standard_Integer GEOMImpl_PrismDriver::Execute(TFunction_Logbook& log) const
97 {
98   if (Label().IsNull()) return 0;
99   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
100
101   GEOMImpl_IPrism aCI (aFunction);
102   Standard_Integer aType = aFunction->GetType();
103
104   TopoDS_Shape aShape;
105
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");
119         }
120         if (aV.Magnitude() > Precision::Confusion()) {
121           aV.Normalize();
122           if (aType != PRISM_BASE_DXDYDZ_2WAYS && aCI.GetScale() > Precision::Confusion()) {
123             aShape = MakeScaledPrism(aShapeBase, aV * aCI.GetH(), aCI.GetScale());
124           }
125           else {
126             if (aType == PRISM_BASE_VEC_H_2WAYS) {
127               gp_Trsf aTrsf;
128               aTrsf.SetTranslation((-aV) * aCI.GetH());
129               BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
130               aShapeBase = aTransformation.Shape();
131               aCI.SetH(aCI.GetH()*2);
132             }
133             aShape = BRepPrimAPI_MakePrism(aShapeBase, aV * aCI.GetH(), Standard_False).Shape();
134           }
135         }
136       }
137     }
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());
154           }
155           else {
156             if (aType == PRISM_BASE_TWO_PNT_2WAYS) {
157               gp_Trsf aTrsf;
158               aTrsf.SetTranslation(-aV);
159               BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
160               aShapeBase = aTransformation.Shape();
161               aV = aV * 2;
162             }
163             aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
164           }
165         }
166       }
167     }
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());
175       }
176       else {
177         if (aType == PRISM_BASE_DXDYDZ_2WAYS) {
178           gp_Trsf aTrsf;
179           aTrsf.SetTranslation(-aV);
180           BRepBuilderAPI_Transform aTransformation(aShapeBase, aTrsf, Standard_False);
181           aShapeBase = aTransformation.Shape();
182           aV = aV * 2;
183         }
184         aShape = BRepPrimAPI_MakePrism(aShapeBase, aV, Standard_False).Shape();
185       }
186     }
187   }
188   
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)
198     
199     if (anInitShape.ShapeType() == TopAbs_COMPOUND)
200     {
201       TopExp_Explorer anExp(anInitShape, TopAbs_SOLID);
202       int solidCount = 0;
203       for(;anExp.More();anExp.Next())
204       {
205         solidCount++;
206       }
207       if (solidCount == 0)
208       {
209         Standard_ConstructionError::Raise("The input shape is a compound without any solid");
210       }
211       else if (solidCount > 1)
212       {
213         Standard_ConstructionError::Raise("The input shape is a compound with more than one solid");
214       }
215     }
216      
217     TopoDS_Face aFaceBase;
218     
219     if (aSketch.ShapeType() == TopAbs_FACE)
220     {
221       aFaceBase = TopoDS::Face(aSketch); 
222     }
223     else
224     {
225       TopoDS_Wire aWire = TopoDS_Wire();
226       
227       if (aSketch.ShapeType() == TopAbs_EDGE)
228       {
229         aWire = BRepBuilderAPI_MakeWire(TopoDS::Edge(aSketch));
230       }
231       else if (aSketch.ShapeType() == TopAbs_WIRE)
232       {
233         aWire = TopoDS::Wire(aSketch);
234       }
235       else
236       {
237         Standard_ConstructionError::Raise("The input sketch is not a wire, an edge or a face");
238       }
239       
240       TopoDS_Vertex aV1, aV2;
241       TopExp::Vertices(aWire, aV1, aV2);
242       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
243         aWire.Closed( true );
244       
245       if (!aWire.Closed())
246         Standard_ConstructionError::Raise("The edge or wire is not closed");
247         
248       // history of the Base wire (RefBase)
249       Handle(GEOM_Object) aSuppObj;
250       TDF_LabelSequence aLabelSeq;
251       aRefBase->GetDependency(aLabelSeq);
252       
253       // If the base wire has only one dependency we use it
254       // to determine the right normal of the face which
255       // must be oriented towards outside of the solid (like the support face)
256       if (aLabelSeq.Length()==1)  
257       {
258         TDF_Label anArgumentRefLabel = aLabelSeq.Value(1);
259         aSuppObj = GEOM_Object::GetReferencedObject(anArgumentRefLabel);   
260       }
261       
262       // Construction of the face if the wire hasn't any support face
263       aFaceBase = BRepBuilderAPI_MakeFace(aWire);
264   
265       if(!aSuppObj.IsNull())      // If the wire has a support
266       {
267         TopoDS_Shape aSupport = aSuppObj->GetValue();
268         if (aSupport.ShapeType() == TopAbs_FACE)
269         {
270           Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aSupport));
271           TopoDS_Face aTempFace = BRepBuilderAPI_MakeFace(aSurf, aWire);
272           
273           if(aTempFace.Orientation() != TopoDS::Face(aSupport).Orientation())
274           {
275             aFaceBase=TopoDS::Face(aTempFace.Reversed());
276           }
277           else
278             aFaceBase=aTempFace;
279         }
280       }
281       
282       // Invert height and angle if the operation is an extruded cut
283       bool invert = !isProtrusion; 
284       
285       // If the face has a reverse orientation invert for extruded boss operations
286       if(aFaceBase.Orientation() == TopAbs_REVERSED)
287         invert = isProtrusion;
288
289       if(invert)
290       {
291         anAngle = -anAngle;  // Invert angle and height
292         aHeight = -aHeight;
293       }
294     
295     }
296     
297     BRepFeat_MakeDPrism thePrism(anInitShape, aFaceBase, TopoDS_Face(),
298                                  anAngle*PI180, isProtrusion, Standard_True); 
299     
300     thePrism.Perform(aHeight);
301     
302     aShape = thePrism.Shape();
303   }
304
305   if (aShape.IsNull()) return 0;
306   
307   
308   if (aType == DRAFT_PRISM_FEATURE)
309   {
310     TopoDS_Shape aRes = aShape;
311     
312     // If the result is a compound with only one solid,
313     // return the solid
314     if (aShape.ShapeType() == TopAbs_COMPOUND)  
315     {
316       TopExp_Explorer anExp(aShape, TopAbs_SOLID);
317       
318       int solidNb = 0;
319       TopoDS_Solid aSolid;
320       
321       for(;anExp.More();anExp.Next())
322       {
323         aSolid = TopoDS::Solid(anExp.Current());
324         solidNb++;
325       }
326       if (solidNb == 1)
327       {
328         aRes = aSolid;
329       } 
330     } 
331     
332     aFunction->SetValue(aRes);
333   }
334   else
335   {
336     TopoDS_Shape aRes = GEOMImpl_IShapesOperations::CompsolidToCompound(aShape);
337     aFunction->SetValue(aRes);
338   }
339   
340
341   log.SetTouched(Label());
342
343   return 1;
344 }
345
346 //=======================================================================
347 //function : MakeScaledPrism
348 //purpose  :
349 //=======================================================================
350 TopoDS_Shape GEOMImpl_PrismDriver::MakeScaledPrism (const TopoDS_Shape& theShapeBase,
351                                                     const gp_Vec&       theVector,
352                                                     const Standard_Real theScaleFactor,
353                                                     const gp_Pnt&       theCDG,
354                                                     bool                isCDG)
355 {
356   TopoDS_Shape aShape;
357   BRep_Builder B;
358
359   // 1. aCDG = geompy.MakeCDG(theBase)
360   gp_Pnt aCDG = theCDG;
361   if (!isCDG) {
362     gp_Ax3 aPos = GEOMImpl_IMeasureOperations::GetPosition(theShapeBase);
363     aCDG = aPos.Location();
364   }
365   TopoDS_Shape aShapeCDG_1 = BRepBuilderAPI_MakeVertex(aCDG).Shape();
366
367   // Process case of several given shapes
368   if (theShapeBase.ShapeType() == TopAbs_COMPOUND ||
369       theShapeBase.ShapeType() == TopAbs_SHELL) {
370     int nbSub = 0;
371     TopoDS_Shape aShapeI;
372     TopoDS_Compound aCompound;
373     B.MakeCompound(aCompound);
374     TopoDS_Iterator It (theShapeBase, Standard_True, Standard_True);
375     for (; It.More(); It.Next()) {
376       nbSub++;
377       aShapeI = MakeScaledPrism(It.Value(), theVector, theScaleFactor, aCDG, true);
378       B.Add(aCompound, aShapeI);
379     }
380     if (nbSub == 1)
381       aShape = aShapeI;
382     else if (nbSub > 1)
383       aShape = GEOMImpl_GlueDriver::GlueFaces(aCompound, Precision::Confusion(), Standard_True);
384     return aShape;
385   }
386
387   // 2. Scale = geompy.MakeScaleTransform(theBase, aCDG, theScaleFactor)
388
389   // Bug 6839: Check for standalone (not included in faces) degenerated edges
390   TopTools_IndexedDataMapOfShapeListOfShape aEFMap;
391   TopExp::MapShapesAndAncestors(theShapeBase, TopAbs_EDGE, TopAbs_FACE, aEFMap);
392   Standard_Integer i, nbE = aEFMap.Extent();
393   for (i = 1; i <= nbE; i++) {
394     TopoDS_Shape anEdgeSh = aEFMap.FindKey(i);
395     if (BRep_Tool::Degenerated(TopoDS::Edge(anEdgeSh))) {
396       const TopTools_ListOfShape& aFaces = aEFMap.FindFromIndex(i);
397       if (aFaces.IsEmpty())
398         Standard_ConstructionError::Raise
399           ("Scaling aborted : cannot scale standalone degenerated edge");
400     }
401   }
402
403   // Perform Scaling
404   gp_Trsf aTrsf;
405   aTrsf.SetScale(aCDG, theScaleFactor);
406   BRepBuilderAPI_Transform aBRepTrsf (theShapeBase, aTrsf, Standard_False);
407   TopoDS_Shape aScale = aBRepTrsf.Shape();
408
409   // 3. aBase2 = geompy.MakeTranslationVectorDistance(Scale, theVec, theH)
410   gp_Trsf aTrsf3;
411   aTrsf3.SetTranslation(theVector);
412   TopLoc_Location aLocOrig = aScale.Location();
413   gp_Trsf aTrsfOrig = aLocOrig.Transformation();
414   TopLoc_Location aLocRes (aTrsf3 * aTrsfOrig);
415   TopoDS_Shape aBase2 = aScale.Located(aLocRes);
416
417   // 4. aCDG_2 = geompy.MakeTranslationVectorDistance(aCDG, theVec, theH)
418   gp_Pnt aCDG_2 = aCDG.Translated(theVector);
419   TopoDS_Shape aShapeCDG_2 = BRepBuilderAPI_MakeVertex(aCDG_2).Shape();
420
421   // 5. Vector = geompy.MakeVector(aCDG, aCDG_2)
422   TopoDS_Shape aShapeVec = BRepBuilderAPI_MakeEdge(aCDG, aCDG_2).Shape();
423   TopoDS_Edge anEdge = TopoDS::Edge(aShapeVec);
424   TopoDS_Wire aWirePath = BRepBuilderAPI_MakeWire(anEdge);
425
426   // 6. aPrism = geompy.MakePipeWithDifferentSections([theBase, aBase2], [aCDG, aCDG_2], Vector, False, False)
427   Handle(TopTools_HSequenceOfShape) aBases = new TopTools_HSequenceOfShape;
428   aBases->Append(theShapeBase);
429   aBases->Append(aBase2);
430
431   Handle(TopTools_HSequenceOfShape) aLocs = new TopTools_HSequenceOfShape;
432   aLocs->Append(aShapeCDG_1);
433   aLocs->Append(aShapeCDG_2);
434
435   aShape = GEOMImpl_PipeDriver::CreatePipeWithDifferentSections(aWirePath, aBases, aLocs, false, false);
436
437   // 7. Make a solid, if possible
438   if (theShapeBase.ShapeType() == TopAbs_FACE) {
439     BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
440     TopExp_Explorer expF (aShape, TopAbs_FACE);
441     Standard_Integer ifa = 0;
442     for (; expF.More(); expF.Next()) {
443       aSewing.Add(expF.Current());
444       ifa++;
445     }
446     if (ifa > 0) {
447       aSewing.Perform();
448       TopoDS_Shape aShell;
449
450       TopoDS_Shape sh = aSewing.SewedShape();
451       if (sh.ShapeType() == TopAbs_FACE && ifa == 1) {
452         // case for creation of shell from one face
453         TopoDS_Shell ss;
454         B.MakeShell(ss);
455         B.Add(ss,sh);
456         aShell = ss;
457       }
458       else {
459         TopExp_Explorer exp (sh, TopAbs_SHELL);
460         Standard_Integer ish = 0;
461         for (; exp.More(); exp.Next()) {
462           aShell = exp.Current();
463           ish++;
464         }
465         if (ish != 1)
466           aShell = sh;
467       }
468       BRepCheck_Shell chkShell (TopoDS::Shell(aShell));
469       if (chkShell.Closed() == BRepCheck_NoError) {
470         TopoDS_Solid Sol;
471         B.MakeSolid(Sol);
472         B.Add(Sol, aShell);
473         BRepClass3d_SolidClassifier SC (Sol);
474         SC.PerformInfinitePoint(Precision::Confusion());
475         if (SC.State() == TopAbs_IN) {
476           B.MakeSolid(Sol);
477           B.Add(Sol, aShell.Reversed());
478         }
479         aShape = Sol;
480       }
481     }
482   }
483
484   return aShape;
485 }
486
487 //=======================================================================
488 //function : GEOMImpl_PrismDriver_Type_
489 //purpose  :
490 //=======================================================================
491 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PrismDriver_Type_()
492 {
493
494   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
495   if (aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
496   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
497   if (aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
498   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
499   if (aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
500
501   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
502   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PrismDriver",
503                                                          sizeof(GEOMImpl_PrismDriver),
504                                                          1,
505                                                          (Standard_Address)_Ancestors,
506                                                          (Standard_Address)NULL);
507
508   return _aType;
509 }
510
511 //=======================================================================
512 //function : DownCast
513 //purpose  :
514 //=======================================================================
515 const Handle(GEOMImpl_PrismDriver) Handle(GEOMImpl_PrismDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
516 {
517   Handle(GEOMImpl_PrismDriver) _anOtherObject;
518
519   if (!AnObject.IsNull()) {
520      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PrismDriver))) {
521        _anOtherObject = Handle(GEOMImpl_PrismDriver)((Handle(GEOMImpl_PrismDriver)&)AnObject);
522      }
523   }
524
525   return _anOtherObject;
526 }