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