1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include <Standard_Stream.hxx>
23 #include <GEOMImpl_ShapeDriver.hxx>
24 #include <GEOMImpl_IShapes.hxx>
25 #include <GEOMImpl_IShapesOperations.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_Block6Explorer.hxx>
29 #include <GEOM_Function.hxx>
32 #include <ShapeFix_Wire.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepAlgo_FaceRestrictor.hxx>
37 #include <BRepBuilderAPI_Sewing.hxx>
38 #include <BRepBuilderAPI_Copy.hxx>
39 #include <BRepTools_Quilt.hxx>
40 #include <BRepCheck.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepCheck_Shell.hxx>
43 #include <BRepClass3d_SolidClassifier.hxx>
44 #include <BRepBuilderAPI_MakeWire.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Edge.hxx>
50 #include <TopoDS_Wire.hxx>
51 #include <TopoDS_Shell.hxx>
52 #include <TopoDS_Solid.hxx>
53 #include <TopoDS_Compound.hxx>
54 #include <TopoDS_Iterator.hxx>
55 #include <TopExp_Explorer.hxx>
56 #include <TopTools_MapOfShape.hxx>
57 #include <TopTools_SequenceOfShape.hxx>
58 #include <TopTools_ListIteratorOfListOfShape.hxx>
60 #include <Precision.hxx>
61 #include <Standard_NullObject.hxx>
62 #include <Standard_TypeMismatch.hxx>
63 #include <Standard_ConstructionError.hxx>
65 //=======================================================================
68 //=======================================================================
69 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
71 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
76 //=======================================================================
77 //function : GEOMImpl_ShapeDriver
79 //=======================================================================
80 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
84 //=======================================================================
87 //=======================================================================
88 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
90 if (Label().IsNull()) return 0;
91 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
93 GEOMImpl_IShapes aCI (aFunction);
94 Standard_Integer aType = aFunction->GetType();
99 if (aType == WIRE_EDGES) {
100 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
101 unsigned int ind, nbshapes = aShapes->Length();
105 BRepBuilderAPI_MakeWire MW;
106 bool isMWDone = true;
109 for (ind = 1; ind <= nbshapes; ind++) {
110 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
111 TopoDS_Shape aShape_i = aRefShape->GetValue();
112 if (aShape_i.IsNull()) {
113 Standard_NullObject::Raise("Shape for wire construction is null");
115 if (aShape_i.ShapeType() == TopAbs_EDGE) {
116 B.Add(aWire, TopoDS::Edge(aShape_i));
117 MW.Add(TopoDS::Edge(aShape_i));
119 // check status after each edge/wire addition, because the final status
120 // can be OK even in case, when some edges/wires was not accepted.
123 } else if (aShape_i.ShapeType() == TopAbs_WIRE) {
124 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
125 for (; exp.More(); exp.Next()) {
126 B.Add(aWire, TopoDS::Edge(exp.Current()));
127 MW.Add(TopoDS::Edge(exp.Current()));
129 // check status after each edge/wire addition, because the final status
130 // can be OK even in case, when some edges/wires was not accepted.
135 Standard_TypeMismatch::Raise
136 ("Shape for wire construction is neither an edge nor a wire");
144 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
148 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
149 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
150 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
151 Standard_ConstructionError::Raise("Wire construction failed");
152 } else if (aFW->StatusReorder(ShapeExtend_DONE2)) {
153 Standard_ConstructionError::Raise("Wire construction failed: some gaps detected");
156 aShape = aFW->WireAPIMake();
160 else if (aType == FACE_WIRE) {
161 Handle(GEOM_Function) aRefBase = aCI.GetBase();
162 TopoDS_Shape aShapeBase = aRefBase->GetValue();
163 if (aShapeBase.IsNull() || aShapeBase.ShapeType() != TopAbs_WIRE) {
164 Standard_NullObject::Raise
165 ("Shape for face construction is null or not a wire");
167 TopoDS_Wire W = TopoDS::Wire(aShapeBase);
168 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
169 //if (!MF.IsDone()) {
170 // Standard_ConstructionError::Raise("Face construction failed");
172 //aShape = MF.Shape();
173 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
174 if (aShape.IsNull()) {
175 Standard_ConstructionError::Raise("Face construction failed");
179 else if (aType == FACE_WIRES) {
180 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
181 int nbshapes = aShapes->Length();
183 Standard_ConstructionError::Raise("No wires given");
187 Handle(GEOM_Function) aRefWire = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
188 TopoDS_Shape aWire = aRefWire->GetValue();
189 if (aWire.IsNull() || aWire.ShapeType() != TopAbs_WIRE) {
190 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
192 TopoDS_Wire W = TopoDS::Wire(aWire);
195 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
196 //if (!MF.IsDone()) {
197 // Standard_ConstructionError::Raise("Face construction failed");
199 //TopoDS_Shape FFace = MF.Shape();
201 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
202 if (FFace.IsNull()) {
203 Standard_ConstructionError::Raise("Face construction failed");
211 BRep_Builder aBuilder;
212 aBuilder.MakeCompound(C);
213 BRepAlgo_FaceRestrictor FR;
215 TopAbs_Orientation OriF = FFace.Orientation();
216 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
217 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
219 for (int ind = 1; ind <= nbshapes; ind++) {
220 Handle(GEOM_Function) aRefWire_i =
221 Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
222 TopoDS_Shape aWire_i = aRefWire_i->GetValue();
223 if (aWire_i.IsNull() || aWire_i.ShapeType() != TopAbs_WIRE) {
224 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
227 FR.Add(TopoDS::Wire(aWire_i));
235 for (; FR.More(); FR.Next()) {
236 aFace = FR.Current().Oriented(OriF);
237 aBuilder.Add(C, aFace);
248 else if (aType == SHELL_FACES) {
249 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
250 unsigned int ind, nbshapes = aShapes->Length();
253 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
254 for (ind = 1; ind <= nbshapes; ind++) {
255 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
256 TopoDS_Shape aShape_i = aRefShape->GetValue();
257 if (aShape_i.IsNull()) {
258 Standard_NullObject::Raise("Face for shell construction is null");
260 aSewing.Add(aShape_i);
265 TopoDS_Shape sh = aSewing.SewedShape();
266 if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
267 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
274 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
275 TopExp_Explorer exp (sh, TopAbs_SHELL);
276 Standard_Integer ish = 0;
277 for (; exp.More(); exp.Next()) {
278 aShape = exp.Current();
283 aShape = aSewing.SewedShape();
287 else if (aType == SOLID_SHELL) {
288 Handle(GEOM_Function) aRefShell = aCI.GetBase();
289 TopoDS_Shape aShapeShell = aRefShell->GetValue();
290 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
291 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
294 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
295 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
299 B.Add(Sol, aShapeShell);
300 BRepClass3d_SolidClassifier SC (Sol);
301 SC.PerformInfinitePoint(Precision::Confusion());
302 if (SC.State() == TopAbs_IN) {
304 B.Add(Sol, aShapeShell.Reversed());
310 else if (aType == SOLID_SHELLS) {
311 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
312 unsigned int ind, nbshapes = aShapes->Length();
313 Standard_Integer ish = 0;
318 for (ind = 1; ind <= nbshapes; ind++) {
319 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
320 TopoDS_Shape aShapeShell = aRefShape->GetValue();
321 if (aShapeShell.IsNull()) {
322 Standard_NullObject::Raise("Shell for solid construction is null");
324 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
325 B.Add(Sol, aShapeShell);
329 if ( ish == 0 ) return 0;
330 BRepClass3d_SolidClassifier SC (Sol);
331 SC.PerformInfinitePoint(Precision::Confusion());
332 switch (SC.State()) {
334 aShape = Sol.Reversed(); break;
337 default: // not closed shell?
342 else if (aType == COMPOUND_SHAPES) {
343 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
344 unsigned int ind, nbshapes = aShapes->Length();
349 for (ind = 1; ind <= nbshapes; ind++) {
350 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
351 TopoDS_Shape aShape_i = aRefShape->GetValue();
352 if (aShape_i.IsNull()) {
353 Standard_NullObject::Raise("Shape for compound construction is null");
361 else if (aType == REVERSE_ORIENTATION) {
362 Handle(GEOM_Function) aRefShape = aCI.GetBase();
363 TopoDS_Shape aShape_i = aRefShape->GetValue();
364 if (aShape_i.IsNull()) {
365 Standard_NullObject::Raise("Shape for reverse is null");
368 BRepBuilderAPI_Copy Copy(aShape_i);
369 if( Copy.IsDone() ) {
370 TopoDS_Shape tds = Copy.Shape();
372 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
375 if( tds.Orientation() == TopAbs_FORWARD)
376 tds.Orientation(TopAbs_REVERSED) ;
378 tds.Orientation(TopAbs_FORWARD) ;
384 if (aShape.IsNull()) return 0;
386 // Check shape validity
387 BRepCheck_Analyzer ana (aShape, false);
388 if (!ana.IsValid()) {
389 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
392 aFunction->SetValue(aShape);
394 log.SetTouched(Label());
400 //=======================================================================
401 //function : GEOMImpl_ShapeDriver_Type_
403 //=======================================================================
404 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
407 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
408 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
409 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
410 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
411 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
412 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
415 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
416 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
417 sizeof(GEOMImpl_ShapeDriver),
419 (Standard_Address)_Ancestors,
420 (Standard_Address)NULL);
425 //=======================================================================
426 //function : DownCast
428 //=======================================================================
429 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
431 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
433 if (!AnObject.IsNull()) {
434 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
435 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
439 return _anOtherObject ;