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