Salome HOME
Bug 0020228: [CEA 331] the geompy.RemoveExtraEdges crash with the attached brep shape...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ThruSectionsDriver.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_ThruSectionsDriver.hxx>
25 #include <GEOMImpl_IThruSections.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <TColStd_HSequenceOfTransient.hxx>
30 #include <Precision.hxx>
31 #include <BRepCheck_Analyzer.hxx>
32 #include <BRepOffsetAPI_ThruSections.hxx>
33 #include <BRepBuilderAPI_MakeWire.hxx>
34 #include <TopExp_Explorer.hxx>
35 #include <TopoDS.hxx>
36
37 #include <TopAbs.hxx>
38 #include <TopoDS.hxx>
39 #include <TopoDS_Wire.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Shape.hxx>
42
43 #include <Standard_NullObject.hxx>
44 #include <Standard_TypeMismatch.hxx>
45 #include <Standard_ConstructionError.hxx>
46 #include <ShapeFix_Shape.hxx>
47 #include <ShapeFix_ShapeTolerance.hxx>
48 #include <Precision.hxx>
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //=======================================================================
53 const Standard_GUID& GEOMImpl_ThruSectionsDriver::GetID()
54 {
55   static Standard_GUID aThruSectionsDriver("FF1BB971-E99C-4f89-B989-5B48E061049B");
56   return aThruSectionsDriver;
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_ThruSectionsDriver
62 //purpose  :
63 //=======================================================================
64 GEOMImpl_ThruSectionsDriver::GEOMImpl_ThruSectionsDriver()
65 {
66 }
67
68 //=======================================================================
69 //function : Execute
70 //purpose  :
71 //=======================================================================
72 Standard_Integer GEOMImpl_ThruSectionsDriver::Execute(TFunction_Logbook& log) const
73 {
74   if (Label().IsNull()) return 0;
75   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
76
77   GEOMImpl_IThruSections aCI (aFunction);
78   Standard_Integer aType = aFunction->GetType();
79
80   Standard_Boolean isSolid = (aCI.GetSolidMode() == 1);
81   Handle(TColStd_HSequenceOfTransient) aSeqSections = aCI.GetSections();
82
83   if( aSeqSections.IsNull())
84     return 0;
85
86   Standard_Integer aNbSections = aSeqSections->Length();
87   Standard_Real aPreci = Max(aCI.GetPrecision(),Precision::Confusion());
88   if(!aNbSections )
89     return 0;
90
91   BRepOffsetAPI_ThruSections aBuilder(isSolid,aType ==THRUSECTIONS_RULED,aPreci);
92   
93  
94   aBuilder.CheckCompatibility(Standard_False);
95   //added sections for building surface
96   Standard_Integer i =1;
97   Standard_Integer nbAdded =0;
98   for( ; i <= aNbSections; i++,nbAdded++)
99   {
100     Handle(Standard_Transient) anItem = aSeqSections->Value(i);
101     if(anItem.IsNull())
102       continue;
103
104     Handle(GEOM_Function) aSection = Handle(GEOM_Function)::DownCast(anItem);
105     if(aSection.IsNull())
106       continue;
107
108     TopoDS_Shape aShapeSection = aSection->GetValue();
109     TopAbs_ShapeEnum aTypeSect = aShapeSection.ShapeType();
110     if(aTypeSect == TopAbs_WIRE)
111       aBuilder.AddWire(TopoDS::Wire(aShapeSection));
112
113     else if(aTypeSect == TopAbs_EDGE) {
114       TopoDS_Edge anEdge = TopoDS::Edge(aShapeSection);
115       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge);
116       aBuilder.AddWire(aWire);
117     }
118     else if(aTypeSect == TopAbs_VERTEX) {
119       TopoDS_Vertex aVert = TopoDS::Vertex(aShapeSection);
120       aBuilder.AddVertex(aVert);
121     }
122     else
123        nbAdded--; 
124   }  
125   if(!nbAdded)
126      Standard_TypeMismatch::Raise("ThruSections aborted : invalid types of sections");
127   //make surface by sections
128   aBuilder.Build();
129   TopoDS_Shape aShape = aBuilder.Shape();
130   if (aShape.IsNull()) {
131     return 0;
132   }
133
134   BRepCheck_Analyzer ana (aShape, Standard_False);
135   if (!ana.IsValid()) {
136     //algoritm thru section creats on the arcs invalid shapes gka
137     ShapeFix_ShapeTolerance aSFT;
138     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
139     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
140     aSfs->SetPrecision(Precision::Confusion());
141     aSfs->Perform();
142     aShape = aSfs->Shape();
143     //ana.Init(aShape, Standard_False);
144     //if (!ana.IsValid()) 
145     //  Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
146   }
147
148
149   aFunction->SetValue(aShape);
150
151   log.SetTouched(Label());
152
153   return 1;
154 }
155
156
157 //=======================================================================
158 //function :  GEOMImpl_ThruSectionsDriver_Type_
159 //purpose  :
160 //=======================================================================
161 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ThruSectionsDriver_Type_()
162 {
163
164   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
165   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
166   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
167   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
168   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
169   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
170
171
172   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
173   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ThruSectionsDriver",
174                                                          sizeof(GEOMImpl_ThruSectionsDriver),
175                                                          1,
176                                                          (Standard_Address)_Ancestors,
177                                                          (Standard_Address)NULL);
178
179   return _aType;
180 }
181
182 //=======================================================================
183 //function : DownCast
184 //purpose  :
185 //=======================================================================
186 const Handle(GEOMImpl_ThruSectionsDriver) Handle(GEOMImpl_ThruSectionsDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
187 {
188   Handle(GEOMImpl_ThruSectionsDriver) _anOtherObject;
189
190   if (!AnObject.IsNull()) {
191      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ThruSectionsDriver))) {
192        _anOtherObject = Handle(GEOMImpl_ThruSectionsDriver)((Handle(GEOMImpl_ThruSectionsDriver)&)AnObject);
193      }
194   }
195
196   return _anOtherObject ;
197 }