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