Salome HOME
0020695: EDF 1076 GEOM: Add a new shape in GEOM: T-shape
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ShapeDriver.cxx
1 //  Copyright (C) 2007-2008  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 #include <GEOMImpl_ShapeDriver.hxx>
23
24 #include <GEOMImpl_IShapes.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_Block6Explorer.hxx>
27
28 #include <GEOM_Function.hxx>
29
30 // OCCT Includes
31 #include <ShapeFix_Wire.hxx>
32 #include <ShapeFix_Edge.hxx>
33
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepAlgo_FaceRestrictor.hxx>
37 #include <BRepBuilderAPI_Sewing.hxx>
38 #include <BRepBuilderAPI_Copy.hxx>
39 #include <BRepCheck.hxx>
40 #include <BRepCheck_Analyzer.hxx>
41 #include <BRepCheck_Shell.hxx>
42 #include <BRepClass3d_SolidClassifier.hxx>
43 #include <BRepBuilderAPI_MakeWire.hxx>
44 #include <BRepBuilderAPI_Sewing.hxx>
45
46 #include <ShapeAnalysis_FreeBounds.hxx>
47
48 #include <TopAbs.hxx>
49 #include <TopoDS.hxx>
50 #include <TopoDS_Shape.hxx>
51 #include <TopoDS_Edge.hxx>
52 #include <TopoDS_Wire.hxx>
53 #include <TopoDS_Shell.hxx>
54 #include <TopoDS_Solid.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
57 #include <TopExp.hxx>
58 #include <TopExp_Explorer.hxx>
59
60 #include <TopTools_MapOfShape.hxx>
61 #include <TopTools_HSequenceOfShape.hxx>
62
63 #include <TColStd_HSequenceOfTransient.hxx>
64
65 #include <Precision.hxx>
66 #include <Standard_NullObject.hxx>
67 #include <Standard_TypeMismatch.hxx>
68 #include <Standard_ConstructionError.hxx>
69
70 //=======================================================================
71 //function : GetID
72 //purpose  :
73 //=======================================================================
74 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
75 {
76   static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
77   return aShapeDriver;
78 }
79
80
81 //=======================================================================
82 //function : GEOMImpl_ShapeDriver
83 //purpose  :
84 //=======================================================================
85 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
86 {
87 }
88
89 //=======================================================================
90 //function : Execute
91 //purpose  :
92 //=======================================================================
93 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
94 {
95   if (Label().IsNull()) return 0;
96   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
97
98   GEOMImpl_IShapes aCI (aFunction);
99   Standard_Integer aType = aFunction->GetType();
100
101   TopoDS_Shape aShape;
102   BRep_Builder B;
103
104   if (aType == WIRE_EDGES) {
105     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
106     TopoDS_Wire aWire;
107     B.MakeWire(aWire);
108
109     // add edges
110     for (unsigned int ind = 1; ind <= aShapes->Length(); ind++) {
111       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
112       TopoDS_Shape aShape_i = aRefShape->GetValue();
113       if (aShape_i.IsNull()) {
114         Standard_NullObject::Raise("Shape for wire construction is null");
115       }
116      if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
117        TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
118        for (; exp.More(); exp.Next())
119          B.Add(aWire, TopoDS::Edge(exp.Current()));
120      } else {
121        Standard_TypeMismatch::Raise
122          ("Shape for wire construction is neither an edge nor a wire");
123      }
124     }
125
126     // fix edges order
127     Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
128     aFW->Load(aWire);
129     aFW->FixReorder();
130     
131     if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
132       Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
133     } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
134       Standard_ConstructionError::Raise("Wire construction failed");
135     } else {
136     }
137
138     // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
139     Standard_Real aTolerance = aCI.GetTolerance();
140     if (aTolerance < Precision::Confusion())
141       aTolerance = Precision::Confusion();
142     
143     aFW->ClosedWireMode() = Standard_False;
144     aFW->FixConnected(aTolerance);
145     if (aFW->StatusConnected(ShapeExtend_FAIL)) {
146       Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
147     }
148     // IMP 0019766
149     if (aFW->StatusConnected(ShapeExtend_DONE3)) {
150       // Confused with <prec> but not Analyzer.Precision(), set the same
151       aFW->FixGapsByRangesMode() = Standard_True;
152       if (aFW->FixGaps3d()) {
153         Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
154         Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
155         for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
156           TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
157           aFe->FixVertexTolerance(aEdge);
158           aFe->FixSameParameter(aEdge);
159         }
160       }
161       else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
162         Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
163       }
164     }
165       aShape = aFW->WireAPIMake();
166   }
167   else if (aType == FACE_WIRE) {
168     Handle(GEOM_Function) aRefBase = aCI.GetBase();
169     TopoDS_Shape aShapeBase = aRefBase->GetValue();
170     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
171     TopoDS_Wire W;
172     if (aShapeBase.ShapeType() == TopAbs_WIRE) {
173       W = TopoDS::Wire(aShapeBase);
174       // check the wire is closed
175       TopoDS_Vertex aV1, aV2;
176       TopExp::Vertices(W, aV1, aV2);
177       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
178         aShapeBase.Closed(true);
179       else
180         Standard_NullObject::Raise
181           ("Shape for face construction is not closed");
182     }
183     else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
184       BRepBuilderAPI_MakeWire MW;
185       MW.Add(TopoDS::Edge(aShapeBase));
186       if (!MW.IsDone()) {
187         Standard_ConstructionError::Raise("Wire construction failed");
188       }
189           W = MW;
190     }
191     else {
192       Standard_NullObject::Raise
193         ("Shape for face construction is neither a wire nor a closed edge");
194     }
195     GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
196     if (aShape.IsNull()) {
197       Standard_ConstructionError::Raise("Face construction failed");
198     }
199   }
200   else if (aType == FACE_WIRES) {
201     // Try to build a face from a set of wires and edges
202     int ind;
203
204     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
205     int nbshapes = aShapes->Length();
206     if (nbshapes < 1) {
207       Standard_ConstructionError::Raise("No wires or edges given");
208     }
209
210     // 1. Extract all edges from the given arguments
211     TopTools_MapOfShape aMapEdges;
212     Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
213
214     for (ind = 1; ind <= nbshapes; ind++) {
215       Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
216       TopoDS_Shape aSh_i = aRefSh_i->GetValue();
217
218       TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
219       for (; anExpE_i.More(); anExpE_i.Next()) {
220         if (aMapEdges.Add(anExpE_i.Current())) {
221           aSeqEdgesIn->Append(anExpE_i.Current());
222         }
223       }
224     }
225
226     // 2. Connect edges to wires of maximum length
227     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
228     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
229                                                   /*shared*/Standard_False, aSeqWiresOut);
230
231     // 3. Separate closed wires
232     Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
233     Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
234     for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
235       if (aSeqWiresOut->Value(ind).Closed())
236         aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
237       else
238         aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
239     }
240
241     if (aSeqClosedWires->Length() < 1) {
242       Standard_ConstructionError::Raise
243         ("There is no closed contour can be built from the given arguments");
244     }
245
246     // 4. Build a face / list of faces from all the obtained closed wires
247
248     // 4.a. Basic face
249     TopoDS_Shape aFFace;
250     TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
251     GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
252     if (aFFace.IsNull()) {
253       Standard_ConstructionError::Raise("Face construction failed");
254     }
255
256     // 4.b. Add other wires
257     if (aSeqClosedWires->Length() == 1) {
258       aShape = aFFace;
259     }
260     else {
261       TopoDS_Compound C;
262       BRep_Builder aBuilder;
263       aBuilder.MakeCompound(C);
264       BRepAlgo_FaceRestrictor FR;
265
266       TopAbs_Orientation OriF = aFFace.Orientation();
267       TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
268       FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
269
270       for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
271         TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
272         FR.Add(aW);
273       }
274
275       FR.Perform();
276
277       if (FR.IsDone()) {
278         int k = 0;
279         TopoDS_Shape aFace;
280         for (; FR.More(); FR.Next()) {
281           aFace = FR.Current().Oriented(OriF);
282           aBuilder.Add(C, aFace);
283           k++;
284         }
285         if (k == 1) {
286           aShape = aFace;
287         } else {
288           aShape = C;
289         }
290       }
291     }
292
293     // 5. Add all open wires to the result
294     if (aSeqOpenWires->Length() > 0) {
295       //Standard_ConstructionError::Raise("There are some open wires");
296       TopoDS_Compound C;
297       BRep_Builder aBuilder;
298       if (aSeqClosedWires->Length() == 1) {
299         aBuilder.MakeCompound(C);
300         aBuilder.Add(C, aShape);
301       }
302       else {
303         C = TopoDS::Compound(aShape);
304       }
305
306       for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
307         aBuilder.Add(C, aSeqOpenWires->Value(ind));
308       }
309
310       aShape = C;
311     }
312   }
313   else if (aType == SHELL_FACES) {
314     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
315     unsigned int ind, nbshapes = aShapes->Length();
316
317     // add faces
318     BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
319     for (ind = 1; ind <= nbshapes; ind++) {
320       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
321       TopoDS_Shape aShape_i = aRefShape->GetValue();
322       if (aShape_i.IsNull()) {
323         Standard_NullObject::Raise("Face for shell construction is null");
324       }
325       aSewing.Add(aShape_i);
326     }
327
328     aSewing.Perform();
329
330     TopoDS_Shape sh = aSewing.SewedShape();
331     if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
332       // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
333       TopoDS_Shell ss;
334       B.MakeShell(ss);
335       B.Add(ss,sh);
336       aShape = ss;
337     }
338     else {
339       //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
340       TopExp_Explorer exp (sh, TopAbs_SHELL);
341       Standard_Integer ish = 0;
342       for (; exp.More(); exp.Next()) {
343         aShape = exp.Current();
344         ish++;
345       }
346
347       if (ish != 1)
348         aShape = aSewing.SewedShape();
349     }
350
351   }
352   else if (aType == SOLID_SHELL) {
353     Handle(GEOM_Function) aRefShell = aCI.GetBase();
354     TopoDS_Shape aShapeShell = aRefShell->GetValue();
355     if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
356       Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
357     }
358
359     BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
360     if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
361
362     TopoDS_Solid Sol;
363     B.MakeSolid(Sol);
364     B.Add(Sol, aShapeShell);
365     BRepClass3d_SolidClassifier SC (Sol);
366     SC.PerformInfinitePoint(Precision::Confusion());
367     if (SC.State() == TopAbs_IN) {
368       B.MakeSolid(Sol);
369       B.Add(Sol, aShapeShell.Reversed());
370     }
371
372     aShape = Sol;
373
374   }
375   else if (aType == SOLID_SHELLS) {
376     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
377     unsigned int ind, nbshapes = aShapes->Length();
378     Standard_Integer ish = 0;
379     TopoDS_Solid Sol;
380     B.MakeSolid(Sol);
381
382     // add shapes
383     for (ind = 1; ind <= nbshapes; ind++) {
384       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
385       TopoDS_Shape aShapeShell = aRefShape->GetValue();
386       if (aShapeShell.IsNull()) {
387         Standard_NullObject::Raise("Shell for solid construction is null");
388       }
389       if (aShapeShell.ShapeType() == TopAbs_SHELL) {
390         B.Add(Sol, aShapeShell);
391         ish++;
392       }
393     }
394     if ( ish == 0 ) return 0;
395     BRepClass3d_SolidClassifier SC (Sol);
396     SC.PerformInfinitePoint(Precision::Confusion());
397     if (SC.State() == TopAbs_IN)
398       aShape = Sol.Reversed();
399     else
400       aShape = Sol;
401   }
402   else if (aType == COMPOUND_SHAPES) {
403     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
404     unsigned int ind, nbshapes = aShapes->Length();
405
406     // add shapes
407     TopoDS_Compound C;
408     B.MakeCompound(C);
409     for (ind = 1; ind <= nbshapes; ind++) {
410       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
411       TopoDS_Shape aShape_i = aRefShape->GetValue();
412       if (aShape_i.IsNull()) {
413         Standard_NullObject::Raise("Shape for compound construction is null");
414       }
415       B.Add(C, aShape_i);
416     }
417
418     aShape = C;
419
420   }
421   else if (aType == REVERSE_ORIENTATION) {
422     Handle(GEOM_Function) aRefShape = aCI.GetBase();
423     TopoDS_Shape aShape_i = aRefShape->GetValue();
424     if (aShape_i.IsNull()) {
425        Standard_NullObject::Raise("Shape for reverse is null");
426     }
427
428     BRepBuilderAPI_Copy Copy(aShape_i);
429     if( Copy.IsDone() ) {
430       TopoDS_Shape tds = Copy.Shape();
431       if( tds.IsNull() ) {
432         Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
433       }
434
435       if( tds.Orientation() == TopAbs_FORWARD)
436         tds.Orientation(TopAbs_REVERSED);
437       else
438         tds.Orientation(TopAbs_FORWARD);
439
440       aShape = tds;
441     }
442   }
443
444   if (aShape.IsNull()) return 0;
445
446   // Check shape validity
447   BRepCheck_Analyzer ana (aShape, false);
448   if (!ana.IsValid()) {
449     //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
450   }
451
452   aFunction->SetValue(aShape);
453
454   log.SetTouched(Label());
455
456   return 1;
457 }
458
459
460 //=======================================================================
461 //function :  GEOMImpl_ShapeDriver_Type_
462 //purpose  :
463 //=======================================================================
464 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
465 {
466
467   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
468   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
469   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
470   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
471   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
472   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
473
474
475   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
476   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
477                                                          sizeof(GEOMImpl_ShapeDriver),
478                                                          1,
479                                                          (Standard_Address)_Ancestors,
480                                                          (Standard_Address)NULL);
481
482   return _aType;
483 }
484
485 //=======================================================================
486 //function : DownCast
487 //purpose  :
488 //=======================================================================
489 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
490 {
491   Handle(GEOMImpl_ShapeDriver) _anOtherObject;
492
493   if (!AnObject.IsNull()) {
494      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
495        _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
496      }
497   }
498
499   return _anOtherObject;
500 }