Salome HOME
0020598: EDF 1191 GEOM : Creation of hexa block from two faces
[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       // IMP 0019766
172       if (aFW->StatusConnected(ShapeExtend_DONE3)) {
173         // Confused with <prec> but not Analyzer.Precision(), set the same
174         aFW->FixGapsByRangesMode() = Standard_True;
175         if (aFW->FixGaps3d()) {
176           Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
177           Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
178           for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
179             TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
180             aFe->FixVertexTolerance(aEdge);
181             aFe->FixSameParameter(aEdge);
182           }
183         }
184         else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
185           Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
186         }
187       }
188
189       aShape = aFW->WireAPIMake();
190     }
191   }
192   else if (aType == FACE_WIRE) {
193     Handle(GEOM_Function) aRefBase = aCI.GetBase();
194     TopoDS_Shape aShapeBase = aRefBase->GetValue();
195     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
196     TopoDS_Wire W;
197     if (aShapeBase.ShapeType() == TopAbs_WIRE) {
198       W = TopoDS::Wire(aShapeBase);
199     }
200     else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
201       BRepBuilderAPI_MakeWire MW;
202       MW.Add(TopoDS::Edge(aShapeBase));
203       if (!MW.IsDone()) {
204         Standard_ConstructionError::Raise("Wire construction failed");
205       }
206           W = MW;
207     }
208     else {
209       Standard_NullObject::Raise
210         ("Shape for face construction is neither a wire nor a closed edge");
211     }
212     GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
213     if (aShape.IsNull()) {
214       Standard_ConstructionError::Raise("Face construction failed");
215     }
216   }
217   else if (aType == FACE_WIRES) {
218     // Try to build a face from a set of wires and edges
219     int ind;
220
221     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
222     int nbshapes = aShapes->Length();
223     if (nbshapes < 1) {
224       Standard_ConstructionError::Raise("No wires or edges given");
225     }
226
227     // 1. Extract all edges from the given arguments
228     TopTools_MapOfShape aMapEdges;
229     Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
230
231     BRep_Builder B;
232     for (ind = 1; ind <= nbshapes; ind++) {
233       Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
234       TopoDS_Shape aSh_i = aRefSh_i->GetValue();
235
236       TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
237       for (; anExpE_i.More(); anExpE_i.Next()) {
238         if (aMapEdges.Add(anExpE_i.Current())) {
239           aSeqEdgesIn->Append(anExpE_i.Current());
240         }
241       }
242     }
243
244     // 2. Connect edges to wires of maximum length
245     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
246     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
247                                                   /*shared*/Standard_False, aSeqWiresOut);
248
249     // 3. Separate closed wires
250     Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
251     Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
252     for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
253       if (aSeqWiresOut->Value(ind).Closed())
254         aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
255       else
256         aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
257     }
258
259     if (aSeqClosedWires->Length() < 1) {
260       Standard_ConstructionError::Raise
261         ("There is no closed contour can be built from the given arguments");
262     }
263
264     // 4. Build a face / list of faces from all the obtained closed wires
265
266     // 4.a. Basic face
267     TopoDS_Shape aFFace;
268     TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
269     GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
270     if (aFFace.IsNull()) {
271       Standard_ConstructionError::Raise("Face construction failed");
272     }
273
274     // 4.b. Add other wires
275     if (aSeqClosedWires->Length() == 1) {
276       aShape = aFFace;
277     }
278     else {
279       TopoDS_Compound C;
280       BRep_Builder aBuilder;
281       aBuilder.MakeCompound(C);
282       BRepAlgo_FaceRestrictor FR;
283
284       TopAbs_Orientation OriF = aFFace.Orientation();
285       TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
286       FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
287
288       for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
289         TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
290         FR.Add(aW);
291       }
292
293       FR.Perform();
294
295       if (FR.IsDone()) {
296         int k = 0;
297         TopoDS_Shape aFace;
298         for (; FR.More(); FR.Next()) {
299           aFace = FR.Current().Oriented(OriF);
300           aBuilder.Add(C, aFace);
301           k++;
302         }
303         if (k == 1) {
304           aShape = aFace;
305         } else {
306           aShape = C;
307         }
308       }
309     }
310
311     // 5. Add all open wires to the result
312     if (aSeqOpenWires->Length() > 0) {
313       //Standard_ConstructionError::Raise("There are some open wires");
314       TopoDS_Compound C;
315       BRep_Builder aBuilder;
316       if (aSeqClosedWires->Length() == 1) {
317         aBuilder.MakeCompound(C);
318         aBuilder.Add(C, aShape);
319       }
320       else {
321         C = TopoDS::Compound(aShape);
322       }
323
324       for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
325         aBuilder.Add(C, aSeqOpenWires->Value(ind));
326       }
327
328       aShape = C;
329     }
330   }
331   else if (aType == SHELL_FACES) {
332     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
333     unsigned int ind, nbshapes = aShapes->Length();
334
335     // add faces
336     BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
337     for (ind = 1; ind <= nbshapes; ind++) {
338       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
339       TopoDS_Shape aShape_i = aRefShape->GetValue();
340       if (aShape_i.IsNull()) {
341         Standard_NullObject::Raise("Face for shell construction is null");
342       }
343       aSewing.Add(aShape_i);
344     }
345
346     aSewing.Perform();
347
348     TopoDS_Shape sh = aSewing.SewedShape();
349     if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
350       // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
351       TopoDS_Shell ss;
352       B.MakeShell(ss);
353       B.Add(ss,sh);
354       aShape = ss;
355     }
356     else {
357       //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
358       TopExp_Explorer exp (sh, TopAbs_SHELL);
359       Standard_Integer ish = 0;
360       for (; exp.More(); exp.Next()) {
361         aShape = exp.Current();
362         ish++;
363       }
364
365       if (ish != 1)
366         aShape = aSewing.SewedShape();
367     }
368
369   }
370   else if (aType == SOLID_SHELL) {
371     Handle(GEOM_Function) aRefShell = aCI.GetBase();
372     TopoDS_Shape aShapeShell = aRefShell->GetValue();
373     if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
374       Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
375     }
376
377     BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
378     if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
379
380     TopoDS_Solid Sol;
381     B.MakeSolid(Sol);
382     B.Add(Sol, aShapeShell);
383     BRepClass3d_SolidClassifier SC (Sol);
384     SC.PerformInfinitePoint(Precision::Confusion());
385     if (SC.State() == TopAbs_IN) {
386       B.MakeSolid(Sol);
387       B.Add(Sol, aShapeShell.Reversed());
388     }
389
390     aShape = Sol;
391
392   }
393   else if (aType == SOLID_SHELLS) {
394     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
395     unsigned int ind, nbshapes = aShapes->Length();
396     Standard_Integer ish = 0;
397     TopoDS_Solid Sol;
398     B.MakeSolid(Sol);
399
400     // add shapes
401     for (ind = 1; ind <= nbshapes; ind++) {
402       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
403       TopoDS_Shape aShapeShell = aRefShape->GetValue();
404       if (aShapeShell.IsNull()) {
405         Standard_NullObject::Raise("Shell for solid construction is null");
406       }
407       if (aShapeShell.ShapeType() == TopAbs_SHELL) {
408         B.Add(Sol, aShapeShell);
409         ish++;
410       }
411     }
412     if ( ish == 0 ) return 0;
413     BRepClass3d_SolidClassifier SC (Sol);
414     SC.PerformInfinitePoint(Precision::Confusion());
415     if (SC.State() == TopAbs_IN)
416       aShape = Sol.Reversed();
417     else
418       aShape = Sol;
419   }
420   else if (aType == COMPOUND_SHAPES) {
421     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
422     unsigned int ind, nbshapes = aShapes->Length();
423
424     // add shapes
425     TopoDS_Compound C;
426     B.MakeCompound(C);
427     for (ind = 1; ind <= nbshapes; ind++) {
428       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
429       TopoDS_Shape aShape_i = aRefShape->GetValue();
430       if (aShape_i.IsNull()) {
431         Standard_NullObject::Raise("Shape for compound construction is null");
432       }
433       B.Add(C, aShape_i);
434     }
435
436     aShape = C;
437
438   }
439   else if (aType == REVERSE_ORIENTATION) {
440     Handle(GEOM_Function) aRefShape = aCI.GetBase();
441     TopoDS_Shape aShape_i = aRefShape->GetValue();
442     if (aShape_i.IsNull()) {
443        Standard_NullObject::Raise("Shape for reverse is null");
444     }
445
446     BRepBuilderAPI_Copy Copy(aShape_i);
447     if( Copy.IsDone() ) {
448       TopoDS_Shape tds = Copy.Shape();
449       if( tds.IsNull() ) {
450         Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
451       }
452
453       if( tds.Orientation() == TopAbs_FORWARD)
454         tds.Orientation(TopAbs_REVERSED);
455       else
456         tds.Orientation(TopAbs_FORWARD);
457
458       aShape = tds;
459     }
460   }
461
462   if (aShape.IsNull()) return 0;
463
464   // Check shape validity
465   BRepCheck_Analyzer ana (aShape, false);
466   if (!ana.IsValid()) {
467     //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
468   }
469
470   aFunction->SetValue(aShape);
471
472   log.SetTouched(Label());
473
474   return 1;
475 }
476
477
478 //=======================================================================
479 //function :  GEOMImpl_ShapeDriver_Type_
480 //purpose  :
481 //=======================================================================
482 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
483 {
484
485   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
486   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
487   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
488   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
489   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
490   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
491
492
493   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
494   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
495                                                          sizeof(GEOMImpl_ShapeDriver),
496                                                          1,
497                                                          (Standard_Address)_Ancestors,
498                                                          (Standard_Address)NULL);
499
500   return _aType;
501 }
502
503 //=======================================================================
504 //function : DownCast
505 //purpose  :
506 //=======================================================================
507 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
508 {
509   Handle(GEOMImpl_ShapeDriver) _anOtherObject;
510
511   if (!AnObject.IsNull()) {
512      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
513        _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
514      }
515   }
516
517   return _anOtherObject;
518 }