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