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