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