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_Solid.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Iterator.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopTools_MapOfShape.hxx>
56 #include <TopTools_SequenceOfShape.hxx>
57 #include <TopTools_ListIteratorOfListOfShape.hxx>
59 #include <Precision.hxx>
60 #include <Standard_NullObject.hxx>
61 #include <Standard_TypeMismatch.hxx>
62 #include <Standard_ConstructionError.hxx>
64 //=======================================================================
67 //=======================================================================
68 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
70 static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
75 //=======================================================================
76 //function : GEOMImpl_ShapeDriver
78 //=======================================================================
79 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
83 //=======================================================================
86 //=======================================================================
87 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
89 if (Label().IsNull()) return 0;
90 Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
92 GEOMImpl_IShapes aCI (aFunction);
93 Standard_Integer aType = aFunction->GetType();
98 if (aType == WIRE_EDGES) {
99 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
100 unsigned int ind, nbshapes = aShapes->Length();
104 BRepBuilderAPI_MakeWire MW;
105 bool isMWDone = true;
108 for (ind = 1; ind <= nbshapes; ind++) {
109 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
110 TopoDS_Shape aShape_i = aRefShape->GetValue();
111 if (aShape_i.IsNull()) {
112 Standard_NullObject::Raise("Shape for wire construction is null");
114 if (aShape_i.ShapeType() == TopAbs_EDGE) {
115 B.Add(aWire, TopoDS::Edge(aShape_i));
116 MW.Add(TopoDS::Edge(aShape_i));
118 // check status after each edge/wire addition, because the final status
119 // can be OK even in case, when some edges/wires was not accepted.
122 } else if (aShape_i.ShapeType() == TopAbs_WIRE) {
123 TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
124 for (; exp.More(); exp.Next()) {
125 B.Add(aWire, TopoDS::Edge(exp.Current()));
126 MW.Add(TopoDS::Edge(exp.Current()));
128 // check status after each edge/wire addition, because the final status
129 // can be OK even in case, when some edges/wires was not accepted.
134 Standard_TypeMismatch::Raise
135 ("Shape for wire construction is neither an edge nor a wire");
143 Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
147 if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
148 Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
149 } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
150 Standard_ConstructionError::Raise("Wire construction failed");
151 } else if (aFW->StatusReorder(ShapeExtend_DONE2)) {
152 Standard_ConstructionError::Raise("Wire construction failed: some gaps detected");
155 aShape = aFW->WireAPIMake();
158 } else if (aType == FACE_WIRE) {
159 Handle(GEOM_Function) aRefBase = aCI.GetBase();
160 TopoDS_Shape aShapeBase = aRefBase->GetValue();
161 if (aShapeBase.IsNull() || aShapeBase.ShapeType() != TopAbs_WIRE) {
162 Standard_NullObject::Raise
163 ("Shape for face construction is null or not a wire");
165 TopoDS_Wire W = TopoDS::Wire(aShapeBase);
166 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
167 //if (!MF.IsDone()) {
168 // Standard_ConstructionError::Raise("Face construction failed");
170 //aShape = MF.Shape();
171 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
172 if (aShape.IsNull()) {
173 Standard_ConstructionError::Raise("Face construction failed");
176 } else if (aType == FACE_WIRES) {
177 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
178 int nbshapes = aShapes->Length();
180 Standard_ConstructionError::Raise("No wires given");
184 Handle(GEOM_Function) aRefWire = Handle(GEOM_Function)::DownCast(aShapes->Value(1));
185 TopoDS_Shape aWire = aRefWire->GetValue();
186 if (aWire.IsNull() || aWire.ShapeType() != TopAbs_WIRE) {
187 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
189 TopoDS_Wire W = TopoDS::Wire(aWire);
192 //BRepBuilderAPI_MakeFace MF (W, aCI.GetIsPlanar());
193 //if (!MF.IsDone()) {
194 // Standard_ConstructionError::Raise("Face construction failed");
196 //TopoDS_Shape FFace = MF.Shape();
198 GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), FFace);
199 if (FFace.IsNull()) {
200 Standard_ConstructionError::Raise("Face construction failed");
208 BRep_Builder aBuilder;
209 aBuilder.MakeCompound(C);
210 BRepAlgo_FaceRestrictor FR;
212 TopAbs_Orientation OriF = FFace.Orientation();
213 TopoDS_Shape aLocalS = FFace.Oriented(TopAbs_FORWARD);
214 FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
216 for (int ind = 1; ind <= nbshapes; ind++) {
217 Handle(GEOM_Function) aRefWire_i =
218 Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
219 TopoDS_Shape aWire_i = aRefWire_i->GetValue();
220 if (aWire_i.IsNull() || aWire_i.ShapeType() != TopAbs_WIRE) {
221 Standard_NullObject::Raise("Shape for face construction is null or not a wire");
224 FR.Add(TopoDS::Wire(aWire_i));
232 for (; FR.More(); FR.Next()) {
233 aFace = FR.Current().Oriented(OriF);
234 aBuilder.Add(C, aFace);
244 } else if (aType == SHELL_FACES) {
245 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
246 unsigned int ind, nbshapes = aShapes->Length();
249 BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
250 for (ind = 1; ind <= nbshapes; ind++) {
251 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
252 TopoDS_Shape aShape_i = aRefShape->GetValue();
253 if (aShape_i.IsNull()) {
254 Standard_NullObject::Raise("Face for shell construction is null");
256 aSewing.Add(aShape_i);
261 TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
262 Standard_Integer ish = 0;
263 for (; exp.More(); exp.Next()) {
264 aShape = exp.Current();
269 aShape = aSewing.SewedShape();
271 } else if (aType == SOLID_SHELL) {
272 Handle(GEOM_Function) aRefShell = aCI.GetBase();
273 TopoDS_Shape aShapeShell = aRefShell->GetValue();
274 if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
275 Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
278 BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
279 if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
283 B.Add(Sol, aShapeShell);
284 BRepClass3d_SolidClassifier SC (Sol);
285 SC.PerformInfinitePoint(Precision::Confusion());
286 if (SC.State() == TopAbs_IN) {
288 B.Add(Sol, aShapeShell.Reversed());
293 } else if (aType == SOLID_SHELLS) {
294 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
295 unsigned int ind, nbshapes = aShapes->Length();
296 Standard_Integer ish = 0;
301 for (ind = 1; ind <= nbshapes; ind++) {
302 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
303 TopoDS_Shape aShapeShell = aRefShape->GetValue();
304 if (aShapeShell.IsNull()) {
305 Standard_NullObject::Raise("Shell for solid construction is null");
307 if (aShapeShell.ShapeType() == TopAbs_SHELL) {
308 B.Add(Sol, aShapeShell);
312 if ( ish == 0 ) return 0;
313 BRepClass3d_SolidClassifier SC (Sol);
314 SC.PerformInfinitePoint(Precision::Confusion());
315 switch (SC.State()) {
317 aShape = Sol.Reversed(); break;
320 default: // not closed shell?
324 } else if (aType == COMPOUND_SHAPES) {
325 Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
326 unsigned int ind, nbshapes = aShapes->Length();
331 for (ind = 1; ind <= nbshapes; ind++) {
332 Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
333 TopoDS_Shape aShape_i = aRefShape->GetValue();
334 if (aShape_i.IsNull()) {
335 Standard_NullObject::Raise("Shape for compound construction is null");
342 } else if (aType == REVERSE_ORIENTATION) {
343 Handle(GEOM_Function) aRefShape = aCI.GetBase();
344 TopoDS_Shape aShape_i = aRefShape->GetValue();
345 if (aShape_i.IsNull()) {
346 Standard_NullObject::Raise("Shape for reverse is null");
349 BRepBuilderAPI_Copy Copy(aShape_i);
350 if( Copy.IsDone() ) {
351 TopoDS_Shape tds = Copy.Shape();
353 Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
356 if( tds.Orientation() == TopAbs_FORWARD)
357 tds.Orientation(TopAbs_REVERSED) ;
359 tds.Orientation(TopAbs_FORWARD) ;
365 if (aShape.IsNull()) return 0;
367 // Check shape validity
368 BRepCheck_Analyzer ana (aShape, false);
369 if (!ana.IsValid()) {
370 Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
373 aFunction->SetValue(aShape);
375 log.SetTouched(Label());
381 //=======================================================================
382 //function : GEOMImpl_ShapeDriver_Type_
384 //=======================================================================
385 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
388 static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
389 if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
390 static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
391 if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
392 static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
393 if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
396 static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
397 static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
398 sizeof(GEOMImpl_ShapeDriver),
400 (Standard_Address)_Ancestors,
401 (Standard_Address)NULL);
406 //=======================================================================
407 //function : DownCast
409 //=======================================================================
410 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
412 Handle(GEOMImpl_ShapeDriver) _anOtherObject;
414 if (!AnObject.IsNull()) {
415 if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
416 _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
420 return _anOtherObject ;