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 <GEOM_Function.hxx>
10 #include <BRep_Tool.hxx>
11 #include <BRep_Builder.hxx>
12 #include <BRepAlgo_FaceRestrictor.hxx>
13 #include <BRepBuilderAPI_Sewing.hxx>
14 #include <BRepBuilderAPI_Copy.hxx>
15 #include <BRepTools_Quilt.hxx>
16 #include <BRepCheck.hxx>
17 #include <BRepCheck_Analyzer.hxx>
18 #include <BRepCheck_Shell.hxx>
19 #include <BRepClass3d_SolidClassifier.hxx>
20 #include <BRepBuilderAPI_MakeWire.hxx>
21 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <TopoDS_Shape.hxx>
26 #include <TopoDS_Edge.hxx>
27 #include <TopoDS_Wire.hxx>
28 #include <TopoDS_Solid.hxx>
29 #include <TopoDS_Compound.hxx>
30 #include <TopoDS_Iterator.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <TopTools_MapOfShape.hxx>
33 #include <TopTools_SequenceOfShape.hxx>
34 #include <TopTools_ListIteratorOfListOfShape.hxx>
36 #include <Precision.hxx>
37 #include <Standard_NullObject.hxx>
38 #include <Standard_TypeMismatch.hxx>
39 #include <Standard_ConstructionError.hxx>
41 //=======================================================================
44 //=======================================================================
45 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
47 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
52 //=======================================================================
53 //function : GEOMImpl_ShapeDriver
55 //=======================================================================
56 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
60 //=======================================================================
63 //=======================================================================
64 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
66 if (Label().IsNull()) return 0;
67 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
69 GEOMImpl_IShapes aCI (aFunction);
70 Standard_Integer aType = aFunction->GetType();
75 if (aType == WIRE_EDGES) {
76 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
77 unsigned int ind, nbshapes = aShapes->Length();
80 BRepBuilderAPI_MakeWire MW;
81 for (ind = 1; ind <= nbshapes; ind++) {
82 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
83 TopoDS_Shape aShape_i = aRefShape->GetValue();
84 if (aShape_i.IsNull()) {
85 Standard_NullObject::Raise("Shape for wire construction is null");
87 if (aShape_i.ShapeType() == TopAbs_EDGE)
88 MW.Add(TopoDS::Edge(aShape_i));
89 else if (aShape_i.ShapeType() == TopAbs_WIRE)
90 MW.Add(TopoDS::Wire(aShape_i));
92 Standard_TypeMismatch::Raise
93 ("Shape for wire construction is neither an edge nor a wire");
97 Standard_ConstructionError::Raise("Wire construction failed");
101 } else if (aType == FACE_WIRE) {
102 Handle(GEOM_Function) aRefBase = aCI.GetBase();
103 TopoDS_Shape aShapeBase = aRefBase->GetValue();
104 if (aShapeBase.IsNull() || aShapeBase.ShapeType() != TopAbs_WIRE) {
105 Standard_NullObject::Raise
106 ("Shape for face construction is null or not a wire");
108 TopoDS_Wire W = TopoDS::Wire(aShapeBase);
109 BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
111 Standard_ConstructionError::Raise("Face construction failed");
115 } else if (aType == FACE_WIRES) {
116 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
119 Handle(GEOM_Function) aRefWire = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
120 TopoDS_Shape aWire = aRefWire->GetValue();
121 if (aWire.IsNull() || aWire.ShapeType() != TopAbs_WIRE) {
122 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
124 TopoDS_Wire W = TopoDS::Wire(aWire);
127 BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
129 Standard_ConstructionError::Raise("Face construction failed");
131 TopoDS_Shape FFace = MF.Shape();
132 if (!FFace.IsNull()) {
133 unsigned int ind, nbshapes = aShapes->Length();
137 } else if (nbshapes >= 2) {
139 BRep_Builder aBuilder;
140 aBuilder.MakeCompound(C);
141 BRepAlgo_FaceRestrictor FR;
143 TopAbs_Orientation OriF = FFace.Orientation();
144 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
145 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
147 for (ind = 1; ind <= nbshapes; ind++) {
148 Handle(GEOM_Function) aRefWire_i =
149 Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
150 TopoDS_Shape aWire_i = aRefWire_i->GetValue();
151 if (aWire_i.IsNull() || aWire_i.ShapeType() != TopAbs_WIRE) {
152 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
155 FR.Add(TopoDS::Wire(aWire_i));
163 for (; FR.More(); FR.Next()) {
164 aFace = FR.Current().Oriented(OriF);
165 aBuilder.Add(C, aFace);
176 } else if (aType == SHELL_FACES) {
177 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
178 unsigned int ind, nbshapes = aShapes->Length();
181 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
182 for (ind = 1; ind <= nbshapes; ind++) {
183 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
184 TopoDS_Shape aShape_i = aRefShape->GetValue();
185 if (aShape_i.IsNull()) {
186 Standard_NullObject::Raise("Face for shell construction is null");
188 aSewing.Add(aShape_i);
193 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
194 Standard_Integer ish = 0;
195 for (; exp.More(); exp.Next()) {
196 aShape = exp.Current();
201 aShape = aSewing.SewedShape();
203 } else if (aType == SOLID_SHELL) {
204 Handle(GEOM_Function) aRefShell = aCI.GetBase();
205 TopoDS_Shape aShapeShell = aRefShell->GetValue();
206 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
207 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
210 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
211 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
215 B.Add(Sol, aShapeShell);
216 BRepClass3d_SolidClassifier SC (Sol);
217 SC.PerformInfinitePoint(Precision::Confusion());
218 if (SC.State() == TopAbs_IN) {
220 B.Add(Sol, aShapeShell.Reversed());
225 } else if (aType == SOLID_SHELLS) {
226 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
227 unsigned int ind, nbshapes = aShapes->Length();
228 Standard_Integer ish = 0;
233 for (ind = 1; ind <= nbshapes; ind++) {
234 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
235 TopoDS_Shape aShapeShell = aRefShape->GetValue();
236 if (aShapeShell.IsNull()) {
237 Standard_NullObject::Raise("Shell for solid construction is null");
239 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
240 B.Add(Sol, aShapeShell);
244 if ( ish == 0 ) return 0;
245 BRepClass3d_SolidClassifier SC (Sol);
246 SC.PerformInfinitePoint(Precision::Confusion());
247 switch (SC.State()) {
249 aShape = Sol.Reversed(); break;
252 default: // not closed shell?
256 } else if (aType == COMPOUND_SHAPES) {
257 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
258 unsigned int ind, nbshapes = aShapes->Length();
263 for (ind = 1; ind <= nbshapes; ind++) {
264 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
265 TopoDS_Shape aShape_i = aRefShape->GetValue();
266 if (aShape_i.IsNull()) {
267 Standard_NullObject::Raise("Shape for compound construction is null");
274 } else if (aType == REVERSE_ORIENTATION) {
275 Handle(GEOM_Function) aRefShape = aCI.GetBase();
276 TopoDS_Shape aShape_i = aRefShape->GetValue();
277 if (aShape_i.IsNull()) {
278 Standard_NullObject::Raise("Shape for reverse is null");
281 BRepBuilderAPI_Copy Copy(aShape_i);
282 if( Copy.IsDone() ) {
283 TopoDS_Shape tds = Copy.Shape();
285 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
288 if( tds.Orientation() == TopAbs_FORWARD)
289 tds.Orientation(TopAbs_REVERSED) ;
291 tds.Orientation(TopAbs_FORWARD) ;
297 if (aShape.IsNull()) return 0;
299 // Check shape validity
300 BRepCheck_Analyzer ana (aShape, false);
301 if (!ana.IsValid()) {
302 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
305 aFunction->SetValue(aShape);
307 log.SetTouched(Label());
313 //=======================================================================
314 //function : GEOMImpl_ShapeDriver_Type_
316 //=======================================================================
317 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
320 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
321 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
322 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
323 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
324 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
325 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
328 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
329 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
330 sizeof(GEOMImpl_ShapeDriver),
332 (Standard_Address)_Ancestors,
333 (Standard_Address)NULL);
338 //=======================================================================
339 //function : DownCast
341 //=======================================================================
342 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
344 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
346 if (!AnObject.IsNull()) {
347 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
348 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
352 return _anOtherObject ;