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>
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>
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>
37 #include <Precision.hxx>
38 #include <Standard_NullObject.hxx>
39 #include <Standard_TypeMismatch.hxx>
40 #include <Standard_ConstructionError.hxx>
42 //=======================================================================
45 //=======================================================================
46 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
48 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
53 //=======================================================================
54 //function : GEOMImpl_ShapeDriver
56 //=======================================================================
57 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
61 //=======================================================================
64 //=======================================================================
65 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
67 if (Label().IsNull()) return 0;
68 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
70 GEOMImpl_IShapes aCI (aFunction);
71 Standard_Integer aType = aFunction->GetType();
76 if (aType == WIRE_EDGES) {
77 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
78 unsigned int ind, nbshapes = aShapes->Length();
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");
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));
93 Standard_TypeMismatch::Raise
94 ("Shape for wire construction is neither an edge nor a wire");
98 Standard_ConstructionError::Raise("Wire construction failed");
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");
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");
114 //aShape = MF.Shape();
115 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
116 if (aShape.IsNull()) {
117 Standard_ConstructionError::Raise("Face construction failed");
120 } else if (aType == FACE_WIRES) {
121 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
122 int nbshapes = aShapes->Length();
124 Standard_ConstructionError::Raise("No wires given");
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");
133 TopoDS_Wire W = TopoDS::Wire(aWire);
136 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
137 //if (!MF.IsDone()) {
138 // Standard_ConstructionError::Raise("Face construction failed");
140 //TopoDS_Shape FFace = MF.Shape();
142 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
143 if (FFace.IsNull()) {
144 Standard_ConstructionError::Raise("Face construction failed");
152 BRep_Builder aBuilder;
153 aBuilder.MakeCompound(C);
154 BRepAlgo_FaceRestrictor FR;
156 TopAbs_Orientation OriF = FFace.Orientation();
157 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
158 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
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");
168 FR.Add(TopoDS::Wire(aWire_i));
176 for (; FR.More(); FR.Next()) {
177 aFace = FR.Current().Oriented(OriF);
178 aBuilder.Add(C, aFace);
188 } else if (aType == SHELL_FACES) {
189 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
190 unsigned int ind, nbshapes = aShapes->Length();
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");
200 aSewing.Add(aShape_i);
205 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
206 Standard_Integer ish = 0;
207 for (; exp.More(); exp.Next()) {
208 aShape = exp.Current();
213 aShape = aSewing.SewedShape();
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");
222 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
223 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
227 B.Add(Sol, aShapeShell);
228 BRepClass3d_SolidClassifier SC (Sol);
229 SC.PerformInfinitePoint(Precision::Confusion());
230 if (SC.State() == TopAbs_IN) {
232 B.Add(Sol, aShapeShell.Reversed());
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;
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");
251 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
252 B.Add(Sol, aShapeShell);
256 if ( ish == 0 ) return 0;
257 BRepClass3d_SolidClassifier SC (Sol);
258 SC.PerformInfinitePoint(Precision::Confusion());
259 switch (SC.State()) {
261 aShape = Sol.Reversed(); break;
264 default: // not closed shell?
268 } else if (aType == COMPOUND_SHAPES) {
269 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
270 unsigned int ind, nbshapes = aShapes->Length();
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");
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");
293 BRepBuilderAPI_Copy Copy(aShape_i);
294 if( Copy.IsDone() ) {
295 TopoDS_Shape tds = Copy.Shape();
297 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
300 if( tds.Orientation() == TopAbs_FORWARD)
301 tds.Orientation(TopAbs_REVERSED) ;
303 tds.Orientation(TopAbs_FORWARD) ;
309 if (aShape.IsNull()) return 0;
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");
317 aFunction->SetValue(aShape);
319 log.SetTouched(Label());
325 //=======================================================================
326 //function : GEOMImpl_ShapeDriver_Type_
328 //=======================================================================
329 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
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);
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),
344 (Standard_Address)_Ancestors,
345 (Standard_Address)NULL);
350 //=======================================================================
351 //function : DownCast
353 //=======================================================================
354 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
356 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
358 if (!AnObject.IsNull()) {
359 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
360 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
364 return _anOtherObject ;