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