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