2 #include <Standard_Stream.hxx>
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>
10 #include <GEOM_Function.hxx>
13 #include <ShapeFix_Wire.hxx>
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>
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>
40 #include <Precision.hxx>
41 #include <Standard_NullObject.hxx>
42 #include <Standard_TypeMismatch.hxx>
43 #include <Standard_ConstructionError.hxx>
45 //=======================================================================
48 //=======================================================================
49 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
51 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
56 //=======================================================================
57 //function : GEOMImpl_ShapeDriver
59 //=======================================================================
60 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
64 //=======================================================================
67 //=======================================================================
68 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
70 if (Label().IsNull()) return 0;
71 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
73 GEOMImpl_IShapes aCI (aFunction);
74 Standard_Integer aType = aFunction->GetType();
79 if (aType == WIRE_EDGES) {
80 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
81 unsigned int ind, nbshapes = aShapes->Length();
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");
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));
98 Standard_TypeMismatch::Raise
99 ("Shape for wire construction is neither an edge nor a wire");
103 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
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");
115 aShape = aFW->Wire();
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");
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");
129 //aShape = MF.Shape();
130 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
131 if (aShape.IsNull()) {
132 Standard_ConstructionError::Raise("Face construction failed");
135 } else if (aType == FACE_WIRES) {
136 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
137 int nbshapes = aShapes->Length();
139 Standard_ConstructionError::Raise("No wires given");
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");
148 TopoDS_Wire W = TopoDS::Wire(aWire);
151 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
152 //if (!MF.IsDone()) {
153 // Standard_ConstructionError::Raise("Face construction failed");
155 //TopoDS_Shape FFace = MF.Shape();
157 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
158 if (FFace.IsNull()) {
159 Standard_ConstructionError::Raise("Face construction failed");
167 BRep_Builder aBuilder;
168 aBuilder.MakeCompound(C);
169 BRepAlgo_FaceRestrictor FR;
171 TopAbs_Orientation OriF = FFace.Orientation();
172 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
173 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
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");
183 FR.Add(TopoDS::Wire(aWire_i));
191 for (; FR.More(); FR.Next()) {
192 aFace = FR.Current().Oriented(OriF);
193 aBuilder.Add(C, aFace);
203 } else if (aType == SHELL_FACES) {
204 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
205 unsigned int ind, nbshapes = aShapes->Length();
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");
215 aSewing.Add(aShape_i);
220 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
221 Standard_Integer ish = 0;
222 for (; exp.More(); exp.Next()) {
223 aShape = exp.Current();
228 aShape = aSewing.SewedShape();
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");
237 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
238 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
242 B.Add(Sol, aShapeShell);
243 BRepClass3d_SolidClassifier SC (Sol);
244 SC.PerformInfinitePoint(Precision::Confusion());
245 if (SC.State() == TopAbs_IN) {
247 B.Add(Sol, aShapeShell.Reversed());
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;
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");
266 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
267 B.Add(Sol, aShapeShell);
271 if ( ish == 0 ) return 0;
272 BRepClass3d_SolidClassifier SC (Sol);
273 SC.PerformInfinitePoint(Precision::Confusion());
274 switch (SC.State()) {
276 aShape = Sol.Reversed(); break;
279 default: // not closed shell?
283 } else if (aType == COMPOUND_SHAPES) {
284 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
285 unsigned int ind, nbshapes = aShapes->Length();
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");
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");
308 BRepBuilderAPI_Copy Copy(aShape_i);
309 if( Copy.IsDone() ) {
310 TopoDS_Shape tds = Copy.Shape();
312 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
315 if( tds.Orientation() == TopAbs_FORWARD)
316 tds.Orientation(TopAbs_REVERSED) ;
318 tds.Orientation(TopAbs_FORWARD) ;
324 if (aShape.IsNull()) return 0;
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");
332 aFunction->SetValue(aShape);
334 log.SetTouched(Label());
340 //=======================================================================
341 //function : GEOMImpl_ShapeDriver_Type_
343 //=======================================================================
344 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
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);
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),
359 (Standard_Address)_Ancestors,
360 (Standard_Address)NULL);
365 //=======================================================================
366 //function : DownCast
368 //=======================================================================
369 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
371 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
373 if (!AnObject.IsNull()) {
374 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
375 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
379 return _anOtherObject ;