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->WireAPIMake();
117 Standard_ConstructionError::Raise("Wire construction failed");
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");
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");
131 //aShape = MF.Shape();
132 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
133 if (aShape.IsNull()) {
134 Standard_ConstructionError::Raise("Face construction failed");
137 } else if (aType == FACE_WIRES) {
138 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
139 int nbshapes = aShapes->Length();
141 Standard_ConstructionError::Raise("No wires given");
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");
150 TopoDS_Wire W = TopoDS::Wire(aWire);
153 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
154 //if (!MF.IsDone()) {
155 // Standard_ConstructionError::Raise("Face construction failed");
157 //TopoDS_Shape FFace = MF.Shape();
159 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
160 if (FFace.IsNull()) {
161 Standard_ConstructionError::Raise("Face construction failed");
169 BRep_Builder aBuilder;
170 aBuilder.MakeCompound(C);
171 BRepAlgo_FaceRestrictor FR;
173 TopAbs_Orientation OriF = FFace.Orientation();
174 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
175 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
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");
185 FR.Add(TopoDS::Wire(aWire_i));
193 for (; FR.More(); FR.Next()) {
194 aFace = FR.Current().Oriented(OriF);
195 aBuilder.Add(C, aFace);
205 } else if (aType == SHELL_FACES) {
206 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
207 unsigned int ind, nbshapes = aShapes->Length();
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");
217 aSewing.Add(aShape_i);
222 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
223 Standard_Integer ish = 0;
224 for (; exp.More(); exp.Next()) {
225 aShape = exp.Current();
230 aShape = aSewing.SewedShape();
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");
239 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
240 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
244 B.Add(Sol, aShapeShell);
245 BRepClass3d_SolidClassifier SC (Sol);
246 SC.PerformInfinitePoint(Precision::Confusion());
247 if (SC.State() == TopAbs_IN) {
249 B.Add(Sol, aShapeShell.Reversed());
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;
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");
268 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
269 B.Add(Sol, aShapeShell);
273 if ( ish == 0 ) return 0;
274 BRepClass3d_SolidClassifier SC (Sol);
275 SC.PerformInfinitePoint(Precision::Confusion());
276 switch (SC.State()) {
278 aShape = Sol.Reversed(); break;
281 default: // not closed shell?
285 } else if (aType == COMPOUND_SHAPES) {
286 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
287 unsigned int ind, nbshapes = aShapes->Length();
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");
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");
310 BRepBuilderAPI_Copy Copy(aShape_i);
311 if( Copy.IsDone() ) {
312 TopoDS_Shape tds = Copy.Shape();
314 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
317 if( tds.Orientation() == TopAbs_FORWARD)
318 tds.Orientation(TopAbs_REVERSED) ;
320 tds.Orientation(TopAbs_FORWARD) ;
326 if (aShape.IsNull()) return 0;
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");
334 aFunction->SetValue(aShape);
336 log.SetTouched(Label());
342 //=======================================================================
343 //function : GEOMImpl_ShapeDriver_Type_
345 //=======================================================================
346 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
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);
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),
361 (Standard_Address)_Ancestors,
362 (Standard_Address)NULL);
367 //=======================================================================
368 //function : DownCast
370 //=======================================================================
371 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
373 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
375 if (!AnObject.IsNull()) {
376 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
377 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
381 return _anOtherObject ;