3 #include "GEOMImpl_ShapeDriver.hxx"
4 #include "GEOMImpl_IShapes.hxx"
5 #include "GEOMImpl_IShapesOperations.hxx"
6 #include "GEOMImpl_Types.hxx"
7 #include "GEOM_Function.hxx"
9 #include <BRep_Tool.hxx>
10 #include <BRep_Builder.hxx>
11 #include <BRepAlgo_FaceRestrictor.hxx>
13 #include <Standard_Version.hxx>
14 #ifdef OCC_VERSION_MAINTENANCE
15 #if (OCC_VERSION_MAJOR >= 5 && OCC_VERSION_MINOR >= 2 && OCC_VERSION_MAINTENANCE >= 4)
16 #include <BRepBuilderAPI_Sewing.hxx>
17 #define BRepAlgo_Sewing BRepBuilderAPI_Sewing
19 #include <BRepAlgo_Sewing.hxx>
22 #include <BRepAlgo_Sewing.hxx>
25 #include <BRepBuilderAPI_Copy.hxx>
26 #include <BRepTools_Quilt.hxx>
27 #include <BRepCheck.hxx>
28 #include <BRepCheck_Analyzer.hxx>
29 #include <BRepCheck_Shell.hxx>
30 #include <BRepClass3d_SolidClassifier.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32 #include <BRepBuilderAPI_MakeFace.hxx>
36 #include <TopoDS_Shape.hxx>
37 #include <TopoDS_Edge.hxx>
38 #include <TopoDS_Wire.hxx>
39 #include <TopoDS_Solid.hxx>
40 #include <TopoDS_Compound.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <TopTools_MapOfShape.hxx>
44 #include <TopTools_SequenceOfShape.hxx>
45 #include <TopTools_ListIteratorOfListOfShape.hxx>
47 #include <Precision.hxx>
48 #include <Standard_NullObject.hxx>
49 #include <Standard_TypeMismatch.hxx>
50 #include <Standard_ConstructionError.hxx>
52 //=======================================================================
55 //=======================================================================
56 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
58 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
63 //=======================================================================
64 //function : GEOMImpl_ShapeDriver
66 //=======================================================================
67 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
71 //=======================================================================
74 //=======================================================================
75 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
77 if (Label().IsNull()) return 0;
78 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
80 GEOMImpl_IShapes aCI (aFunction);
81 Standard_Integer aType = aFunction->GetType();
86 if (aType == WIRE_EDGES) {
87 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
88 unsigned int ind, nbshapes = aShapes->Length();
91 BRepBuilderAPI_MakeWire MW;
92 for (ind = 1; ind <= nbshapes; ind++) {
93 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
94 TopoDS_Shape aShape_i = aRefShape->GetValue();
95 if (aShape_i.IsNull()) {
96 Standard_NullObject::Raise("Shape for wire construction is null");
98 if (aShape_i.ShapeType() == TopAbs_EDGE)
99 MW.Add(TopoDS::Edge(aShape_i));
100 else if (aShape_i.ShapeType() == TopAbs_WIRE)
101 MW.Add(TopoDS::Wire(aShape_i));
103 Standard_TypeMismatch::Raise
104 ("Shape for wire construction is neither an edge nor a wire");
108 Standard_ConstructionError::Raise("Wire construction failed");
112 } else if (aType == FACE_WIRE) {
113 Handle(GEOM_Function) aRefBase = aCI.GetBase();
114 TopoDS_Shape aShapeBase = aRefBase->GetValue();
115 if (aShapeBase.IsNull() || aShapeBase.ShapeType() != TopAbs_WIRE) {
116 Standard_NullObject::Raise
117 ("Shape for face construction is null or not a wire");
119 TopoDS_Wire W = TopoDS::Wire(aShapeBase);
120 BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
122 Standard_ConstructionError::Raise("Face construction failed");
126 } else if (aType == FACE_WIRES) {
127 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
130 Handle(GEOM_Function) aRefWire = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
131 TopoDS_Shape aWire = aRefWire->GetValue();
132 if (aWire.IsNull() || aWire.ShapeType() != TopAbs_WIRE) {
133 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
135 TopoDS_Wire W = TopoDS::Wire(aWire);
138 BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
140 Standard_ConstructionError::Raise("Face construction failed");
142 TopoDS_Shape FFace = MF.Shape();
143 if (!FFace.IsNull()) {
144 unsigned int ind, nbshapes = aShapes->Length();
148 } else if (nbshapes >= 2) {
150 BRep_Builder aBuilder;
151 aBuilder.MakeCompound(C);
152 BRepAlgo_FaceRestrictor FR;
154 TopAbs_Orientation OriF = FFace.Orientation();
155 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
156 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
158 for (ind = 1; ind <= nbshapes; ind++) {
159 Handle(GEOM_Function) aRefWire_i =
160 Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
161 TopoDS_Shape aWire_i = aRefWire_i->GetValue();
162 if (aWire_i.IsNull() || aWire_i.ShapeType() != TopAbs_WIRE) {
163 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
166 FR.Add(TopoDS::Wire(aWire_i));
174 for (; FR.More(); FR.Next()) {
175 aFace = FR.Current().Oriented(OriF);
176 aBuilder.Add(C, aFace);
187 } else if (aType == SHELL_FACES) {
188 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
189 unsigned int ind, nbshapes = aShapes->Length();
192 BRepAlgo_Sewing aSewing(Precision::Confusion()*10.0);
193 for (ind = 1; ind <= nbshapes; ind++) {
194 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
195 TopoDS_Shape aShape_i = aRefShape->GetValue();
196 if (aShape_i.IsNull()) {
197 Standard_NullObject::Raise("Face for shell construction is null");
199 aSewing.Add(aShape_i);
204 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
205 Standard_Integer ish = 0;
206 for (; exp.More(); exp.Next()) {
207 aShape = exp.Current();
212 aShape = aSewing.SewedShape();
214 } else if (aType == SOLID_SHELL) {
215 Handle(GEOM_Function) aRefShell = aCI.GetBase();
216 TopoDS_Shape aShapeShell = aRefShell->GetValue();
217 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
218 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
221 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
222 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
226 B.Add(Sol, aShapeShell);
227 BRepClass3d_SolidClassifier SC (Sol);
228 SC.PerformInfinitePoint(Precision::Confusion());
229 if (SC.State() == TopAbs_IN) {
231 B.Add(Sol, aShapeShell.Reversed());
236 } else if (aType == SOLID_SHELLS) {
237 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
238 unsigned int ind, nbshapes = aShapes->Length();
239 Standard_Integer ish = 0;
244 for (ind = 1; ind <= nbshapes; ind++) {
245 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
246 TopoDS_Shape aShapeShell = aRefShape->GetValue();
247 if (aShapeShell.IsNull()) {
248 Standard_NullObject::Raise("Shell for solid construction is null");
250 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
251 B.Add(Sol, aShapeShell);
255 if ( ish == 0 ) return 0;
256 BRepClass3d_SolidClassifier SC (Sol);
257 SC.PerformInfinitePoint(Precision::Confusion());
258 switch (SC.State()) {
260 aShape = Sol.Reversed(); break;
263 default: // not closed shell?
267 } else if (aType == COMPOUND_SHAPES) {
268 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
269 unsigned int ind, nbshapes = aShapes->Length();
274 for (ind = 1; ind <= nbshapes; ind++) {
275 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
276 TopoDS_Shape aShape_i = aRefShape->GetValue();
277 if (aShape_i.IsNull()) {
278 Standard_NullObject::Raise("Shape for compound construction is null");
285 } else if (aType == REVERSE_ORIENTATION) {
286 Handle(GEOM_Function) aRefShape = aCI.GetBase();
287 TopoDS_Shape aShape_i = aRefShape->GetValue();
288 if (aShape_i.IsNull()) {
289 Standard_NullObject::Raise("Shape for reverse is null");
292 BRepBuilderAPI_Copy Copy(aShape_i);
293 if( Copy.IsDone() ) {
294 TopoDS_Shape tds = Copy.Shape();
296 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
299 if( tds.Orientation() == TopAbs_FORWARD)
300 tds.Orientation(TopAbs_REVERSED) ;
302 tds.Orientation(TopAbs_FORWARD) ;
308 if (aShape.IsNull()) return 0;
310 // Check shape validity
311 BRepCheck_Analyzer ana (aShape, false);
312 if (!ana.IsValid()) {
313 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
316 aFunction->SetValue(aShape);
318 log.SetTouched(Label());
324 //=======================================================================
325 //function : GEOMImpl_ShapeDriver_Type_
327 //=======================================================================
328 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
331 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
332 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
333 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
334 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
335 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
336 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
339 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
340 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
341 sizeof(GEOMImpl_ShapeDriver),
343 (Standard_Address)_Ancestors,
344 (Standard_Address)NULL);
349 //=======================================================================
350 //function : DownCast
352 //=======================================================================
353 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
355 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
357 if (!AnObject.IsNull()) {
358 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
359 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
363 return _anOtherObject ;