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();
85 BRepBuilderAPI_MakeWire MW;
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");
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));
102 Standard_TypeMismatch::Raise
103 ("Shape for wire construction is neither an edge nor a wire");
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.
116 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
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");
128 aShape = aFW->WireAPIMake();
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");
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");
143 //aShape = MF.Shape();
144 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
145 if (aShape.IsNull()) {
146 Standard_ConstructionError::Raise("Face construction failed");
149 } else if (aType == FACE_WIRES) {
150 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
151 int nbshapes = aShapes->Length();
153 Standard_ConstructionError::Raise("No wires given");
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");
162 TopoDS_Wire W = TopoDS::Wire(aWire);
165 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
166 //if (!MF.IsDone()) {
167 // Standard_ConstructionError::Raise("Face construction failed");
169 //TopoDS_Shape FFace = MF.Shape();
171 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
172 if (FFace.IsNull()) {
173 Standard_ConstructionError::Raise("Face construction failed");
181 BRep_Builder aBuilder;
182 aBuilder.MakeCompound(C);
183 BRepAlgo_FaceRestrictor FR;
185 TopAbs_Orientation OriF = FFace.Orientation();
186 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
187 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
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");
197 FR.Add(TopoDS::Wire(aWire_i));
205 for (; FR.More(); FR.Next()) {
206 aFace = FR.Current().Oriented(OriF);
207 aBuilder.Add(C, aFace);
217 } else if (aType == SHELL_FACES) {
218 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
219 unsigned int ind, nbshapes = aShapes->Length();
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");
229 aSewing.Add(aShape_i);
234 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
235 Standard_Integer ish = 0;
236 for (; exp.More(); exp.Next()) {
237 aShape = exp.Current();
242 aShape = aSewing.SewedShape();
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");
251 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
252 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
256 B.Add(Sol, aShapeShell);
257 BRepClass3d_SolidClassifier SC (Sol);
258 SC.PerformInfinitePoint(Precision::Confusion());
259 if (SC.State() == TopAbs_IN) {
261 B.Add(Sol, aShapeShell.Reversed());
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;
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");
280 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
281 B.Add(Sol, aShapeShell);
285 if ( ish == 0 ) return 0;
286 BRepClass3d_SolidClassifier SC (Sol);
287 SC.PerformInfinitePoint(Precision::Confusion());
288 switch (SC.State()) {
290 aShape = Sol.Reversed(); break;
293 default: // not closed shell?
297 } else if (aType == COMPOUND_SHAPES) {
298 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
299 unsigned int ind, nbshapes = aShapes->Length();
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");
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");
322 BRepBuilderAPI_Copy Copy(aShape_i);
323 if( Copy.IsDone() ) {
324 TopoDS_Shape tds = Copy.Shape();
326 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
329 if( tds.Orientation() == TopAbs_FORWARD)
330 tds.Orientation(TopAbs_REVERSED) ;
332 tds.Orientation(TopAbs_FORWARD) ;
338 if (aShape.IsNull()) return 0;
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");
346 aFunction->SetValue(aShape);
348 log.SetTouched(Label());
354 //=======================================================================
355 //function : GEOMImpl_ShapeDriver_Type_
357 //=======================================================================
358 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
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);
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),
373 (Standard_Address)_Ancestors,
374 (Standard_Address)NULL);
379 //=======================================================================
380 //function : DownCast
382 //=======================================================================
383 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
385 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
387 if (!AnObject.IsNull()) {
388 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
389 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
393 return _anOtherObject ;