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