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