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 <GEOMImpl_ShapeDriver.hxx>
23 #include <GEOMImpl_IShapes.hxx>
24 #include <GEOMImpl_Types.hxx>
25 #include <GEOMImpl_Block6Explorer.hxx>
27 #include <GEOM_Function.hxx>
30 #include <ShapeFix_Wire.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAlgo_FaceRestrictor.hxx>
35 #include <BRepBuilderAPI_Sewing.hxx>
36 #include <BRepBuilderAPI_Copy.hxx>
37 #include <BRepCheck.hxx>
38 #include <BRepCheck_Analyzer.hxx>
39 #include <BRepCheck_Shell.hxx>
40 #include <BRepClass3d_SolidClassifier.hxx>
41 #include <BRepBuilderAPI_MakeWire.hxx>
42 #include <BRepBuilderAPI_Sewing.hxx>
44 #include <ShapeAnalysis_FreeBounds.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>
57 #include <TopTools_MapOfShape.hxx>
58 #include <TopTools_HSequenceOfShape.hxx>
60 #include <TColStd_HSequenceOfTransient.hxx>
62 #include <Precision.hxx>
63 #include <Standard_NullObject.hxx>
64 #include <Standard_TypeMismatch.hxx>
65 #include <Standard_ConstructionError.hxx>
67 //=======================================================================
70 //=======================================================================
71 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
73 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
78 //=======================================================================
79 //function : GEOMImpl_ShapeDriver
81 //=======================================================================
82 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
86 //=======================================================================
89 //=======================================================================
90 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
92 if (Label().IsNull()) return 0;
93 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
95 GEOMImpl_IShapes aCI (aFunction);
96 Standard_Integer aType = aFunction->GetType();
101 if (aType == WIRE_EDGES) {
102 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
103 unsigned int ind, nbshapes = aShapes->Length();
107 BRepBuilderAPI_MakeWire MW;
108 bool isMWDone = true;
111 for (ind = 1; ind <= nbshapes; ind++) {
112 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
113 TopoDS_Shape aShape_i = aRefShape->GetValue();
114 if (aShape_i.IsNull()) {
115 Standard_NullObject::Raise("Shape for wire construction is null");
117 if (aShape_i.ShapeType() == TopAbs_EDGE) {
118 B.Add(aWire, TopoDS::Edge(aShape_i));
119 MW.Add(TopoDS::Edge(aShape_i));
121 // check status after each edge/wire addition, because the final status
122 // can be OK even in case, when some edges/wires was not accepted.
125 } else if (aShape_i.ShapeType() == TopAbs_WIRE) {
126 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
127 for (; exp.More(); exp.Next()) {
128 B.Add(aWire, TopoDS::Edge(exp.Current()));
129 MW.Add(TopoDS::Edge(exp.Current()));
131 // check status after each edge/wire addition, because the final status
132 // can be OK even in case, when some edges/wires was not accepted.
137 Standard_TypeMismatch::Raise
138 ("Shape for wire construction is neither an edge nor a wire");
146 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
150 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
151 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
152 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
153 Standard_ConstructionError::Raise("Wire construction failed");
154 } else if (aFW->StatusReorder(ShapeExtend_DONE2)) {
155 Standard_ConstructionError::Raise("Wire construction failed: some gaps detected");
158 aShape = aFW->WireAPIMake();
161 else if (aType == FACE_WIRE) {
162 Handle(GEOM_Function) aRefBase = aCI.GetBase();
163 TopoDS_Shape aShapeBase = aRefBase->GetValue();
164 if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
166 if (aShapeBase.ShapeType() == TopAbs_WIRE) {
167 W = TopoDS::Wire(aShapeBase);
169 else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
170 BRepBuilderAPI_MakeWire MW;
171 MW.Add(TopoDS::Edge(aShapeBase));
173 Standard_ConstructionError::Raise("Wire construction failed");
178 Standard_NullObject::Raise
179 ("Shape for face construction is neither a wire nor a closed edge");
181 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
182 if (aShape.IsNull()) {
183 Standard_ConstructionError::Raise("Face construction failed");
186 else if (aType == FACE_WIRES) {
187 // Try to build a face from a set of wires and edges
190 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
191 int nbshapes = aShapes->Length();
193 Standard_ConstructionError::Raise("No wires or edges given");
196 // 1. Extract all edges from the given arguments
197 TopTools_MapOfShape aMapEdges;
198 Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
201 for (ind = 1; ind <= nbshapes; ind++) {
202 Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
203 TopoDS_Shape aSh_i = aRefSh_i->GetValue();
205 TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
206 for (; anExpE_i.More(); anExpE_i.Next()) {
207 if (aMapEdges.Add(anExpE_i.Current())) {
208 aSeqEdgesIn->Append(anExpE_i.Current());
213 // 2. Connect edges to wires of maximum length
214 Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
215 ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
216 /*shared*/Standard_False, aSeqWiresOut);
218 // 3. Separate closed wires
219 Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
220 Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
221 for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
222 if (aSeqWiresOut->Value(ind).Closed())
223 aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
225 aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
228 if (aSeqClosedWires->Length() < 1) {
229 Standard_ConstructionError::Raise
230 ("There is no closed contour can be built from the given arguments");
233 // 4. Build a face / list of faces from all the obtained closed wires
237 TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
238 GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
239 if (aFFace.IsNull()) {
240 Standard_ConstructionError::Raise("Face construction failed");
243 // 4.b. Add other wires
244 if (aSeqClosedWires->Length() == 1) {
249 BRep_Builder aBuilder;
250 aBuilder.MakeCompound(C);
251 BRepAlgo_FaceRestrictor FR;
253 TopAbs_Orientation OriF = aFFace.Orientation();
254 TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
255 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
257 for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
258 TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
267 for (; FR.More(); FR.Next()) {
268 aFace = FR.Current().Oriented(OriF);
269 aBuilder.Add(C, aFace);
280 // 5. Add all open wires to the result
281 if (aSeqOpenWires->Length() > 0) {
282 //Standard_ConstructionError::Raise("There are some open wires");
284 BRep_Builder aBuilder;
285 if (aSeqClosedWires->Length() == 1) {
286 aBuilder.MakeCompound(C);
287 aBuilder.Add(C, aShape);
290 C = TopoDS::Compound(aShape);
293 for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
294 aBuilder.Add(C, aSeqOpenWires->Value(ind));
300 else if (aType == SHELL_FACES) {
301 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
302 unsigned int ind, nbshapes = aShapes->Length();
305 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
306 for (ind = 1; ind <= nbshapes; ind++) {
307 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
308 TopoDS_Shape aShape_i = aRefShape->GetValue();
309 if (aShape_i.IsNull()) {
310 Standard_NullObject::Raise("Face for shell construction is null");
312 aSewing.Add(aShape_i);
317 TopoDS_Shape sh = aSewing.SewedShape();
318 if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
319 // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
326 //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
327 TopExp_Explorer exp (sh, TopAbs_SHELL);
328 Standard_Integer ish = 0;
329 for (; exp.More(); exp.Next()) {
330 aShape = exp.Current();
335 aShape = aSewing.SewedShape();
339 else if (aType == SOLID_SHELL) {
340 Handle(GEOM_Function) aRefShell = aCI.GetBase();
341 TopoDS_Shape aShapeShell = aRefShell->GetValue();
342 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
343 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
346 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
347 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
351 B.Add(Sol, aShapeShell);
352 BRepClass3d_SolidClassifier SC (Sol);
353 SC.PerformInfinitePoint(Precision::Confusion());
354 if (SC.State() == TopAbs_IN) {
356 B.Add(Sol, aShapeShell.Reversed());
362 else if (aType == SOLID_SHELLS) {
363 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
364 unsigned int ind, nbshapes = aShapes->Length();
365 Standard_Integer ish = 0;
370 for (ind = 1; ind <= nbshapes; ind++) {
371 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
372 TopoDS_Shape aShapeShell = aRefShape->GetValue();
373 if (aShapeShell.IsNull()) {
374 Standard_NullObject::Raise("Shell for solid construction is null");
376 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
377 B.Add(Sol, aShapeShell);
381 if ( ish == 0 ) return 0;
382 BRepClass3d_SolidClassifier SC (Sol);
383 SC.PerformInfinitePoint(Precision::Confusion());
384 switch (SC.State()) {
386 aShape = Sol.Reversed(); break;
389 default: // not closed shell?
394 else if (aType == COMPOUND_SHAPES) {
395 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
396 unsigned int ind, nbshapes = aShapes->Length();
401 for (ind = 1; ind <= nbshapes; ind++) {
402 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
403 TopoDS_Shape aShape_i = aRefShape->GetValue();
404 if (aShape_i.IsNull()) {
405 Standard_NullObject::Raise("Shape for compound construction is null");
413 else if (aType == REVERSE_ORIENTATION) {
414 Handle(GEOM_Function) aRefShape = aCI.GetBase();
415 TopoDS_Shape aShape_i = aRefShape->GetValue();
416 if (aShape_i.IsNull()) {
417 Standard_NullObject::Raise("Shape for reverse is null");
420 BRepBuilderAPI_Copy Copy(aShape_i);
421 if( Copy.IsDone() ) {
422 TopoDS_Shape tds = Copy.Shape();
424 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
427 if( tds.Orientation() == TopAbs_FORWARD)
428 tds.Orientation(TopAbs_REVERSED);
430 tds.Orientation(TopAbs_FORWARD);
436 if (aShape.IsNull()) return 0;
438 // Check shape validity
439 BRepCheck_Analyzer ana (aShape, false);
440 if (!ana.IsValid()) {
441 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
444 aFunction->SetValue(aShape);
446 log.SetTouched(Label());
452 //=======================================================================
453 //function : GEOMImpl_ShapeDriver_Type_
455 //=======================================================================
456 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
459 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
460 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
461 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
462 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
463 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
464 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
467 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
468 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
469 sizeof(GEOMImpl_ShapeDriver),
471 (Standard_Address)_Ancestors,
472 (Standard_Address)NULL);
477 //=======================================================================
478 //function : DownCast
480 //=======================================================================
481 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
483 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
485 if (!AnObject.IsNull()) {
486 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
487 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
491 return _anOtherObject;