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