Salome HOME
PAL14122: EDF307: GetInPlace --> COMM_FAILURE. Use new method GEOMAlgo_BuilderShape...
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ThruSectionsDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,F
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_ThruSectionsDriver.hxx>
24 #include <GEOMImpl_IThruSections.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <TColStd_HSequenceOfTransient.hxx>
29 #include <Precision.hxx>
30 #include <BRepCheck_Analyzer.hxx>
31 #include <BRepOffsetAPI_ThruSections.hxx>
32 #include <BRepBuilderAPI_MakeWire.hxx>
33 #include <TopExp_Explorer.hxx>
34 #include <TopoDS.hxx>
35
36 #include <TopAbs.hxx>
37 #include <TopoDS.hxx>
38 #include <TopoDS_Wire.hxx>
39 #include <TopoDS_Edge.hxx>
40 #include <TopoDS_Shape.hxx>
41
42 #include <Standard_NullObject.hxx>
43 #include <Standard_TypeMismatch.hxx>
44 #include <Standard_ConstructionError.hxx>
45 #include <ShapeFix_Shape.hxx>
46 #include <ShapeFix_ShapeTolerance.hxx>
47 #include <Precision.hxx>
48 //=======================================================================
49 //function : GetID
50 //purpose  :
51 //=======================================================================
52 const Standard_GUID& GEOMImpl_ThruSectionsDriver::GetID()
53 {
54   static Standard_GUID aThruSectionsDriver("FF1BB971-E99C-4f89-B989-5B48E061049B");
55   return aThruSectionsDriver;
56 }
57
58
59 //=======================================================================
60 //function : GEOMImpl_ThruSectionsDriver
61 //purpose  :
62 //=======================================================================
63 GEOMImpl_ThruSectionsDriver::GEOMImpl_ThruSectionsDriver()
64 {
65 }
66
67 //=======================================================================
68 //function : Execute
69 //purpose  :
70 //=======================================================================
71 Standard_Integer GEOMImpl_ThruSectionsDriver::Execute(TFunction_Logbook& log) const
72 {
73   if (Label().IsNull()) return 0;
74   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
75
76   GEOMImpl_IThruSections aCI (aFunction);
77   Standard_Integer aType = aFunction->GetType();
78
79   Standard_Boolean isSolid = (aCI.GetSolidMode() == 1);
80   Handle(TColStd_HSequenceOfTransient) aSeqSections = aCI.GetSections();
81
82   if( aSeqSections.IsNull())
83     return 0;
84
85   Standard_Integer aNbSections = aSeqSections->Length();
86   Standard_Real aPreci = Max(aCI.GetPrecision(),Precision::Confusion());
87   if(!aNbSections )
88     return 0;
89
90   BRepOffsetAPI_ThruSections aBuilder(isSolid,aType ==THRUSECTIONS_RULED,aPreci);
91   
92  
93   aBuilder.CheckCompatibility(Standard_False);
94   //added sections for building surface
95   Standard_Integer i =1;
96   Standard_Integer nbAdded =0;
97   for( ; i <= aNbSections; i++,nbAdded++)
98   {
99     Handle(Standard_Transient) anItem = aSeqSections->Value(i);
100     if(anItem.IsNull())
101       continue;
102
103     Handle(GEOM_Function) aSection = Handle(GEOM_Function)::DownCast(anItem);
104     if(aSection.IsNull())
105       continue;
106
107     TopoDS_Shape aShapeSection = aSection->GetValue();
108     TopAbs_ShapeEnum aTypeSect = aShapeSection.ShapeType();
109     if(aTypeSect == TopAbs_WIRE)
110       aBuilder.AddWire(TopoDS::Wire(aShapeSection));
111
112     else if(aTypeSect == TopAbs_EDGE) {
113       TopoDS_Edge anEdge = TopoDS::Edge(aShapeSection);
114       TopoDS_Wire aWire = BRepBuilderAPI_MakeWire(anEdge);
115       aBuilder.AddWire(aWire);
116     }
117     else if(aTypeSect == TopAbs_VERTEX) {
118       TopoDS_Vertex aVert = TopoDS::Vertex(aShapeSection);
119       aBuilder.AddVertex(aVert);
120     }
121     else
122        nbAdded--; 
123   }  
124   if(!nbAdded)
125      Standard_TypeMismatch::Raise("ThruSections aborted : invalid types of sections");
126   //make surface by sections
127   aBuilder.Build();
128   TopoDS_Shape aShape = aBuilder.Shape();
129   if (aShape.IsNull()) {
130     return 0;
131   }
132
133   BRepCheck_Analyzer ana (aShape, Standard_False);
134   if (!ana.IsValid()) {
135     //algoritm thru section creats on the arcs invalid shapes gka
136     ShapeFix_ShapeTolerance aSFT;
137     aSFT.LimitTolerance(aShape,Precision::Confusion(),Precision::Confusion());
138     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape(aShape);
139     aSfs->SetPrecision(Precision::Confusion());
140     aSfs->Perform();
141     aShape = aSfs->Shape();
142     //ana.Init(aShape, Standard_False);
143     //if (!ana.IsValid()) 
144     //  Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
145   }
146
147
148   aFunction->SetValue(aShape);
149
150   log.SetTouched(Label());
151
152   return 1;
153 }
154
155
156 //=======================================================================
157 //function :  GEOMImpl_ThruSectionsDriver_Type_
158 //purpose  :
159 //=======================================================================
160 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ThruSectionsDriver_Type_()
161 {
162
163   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
164   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
165   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
166   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
167   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
168   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
169
170
171   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
172   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ThruSectionsDriver",
173                                                          sizeof(GEOMImpl_ThruSectionsDriver),
174                                                          1,
175                                                          (Standard_Address)_Ancestors,
176                                                          (Standard_Address)NULL);
177
178   return _aType;
179 }
180
181 //=======================================================================
182 //function : DownCast
183 //purpose  :
184 //=======================================================================
185 const Handle(GEOMImpl_ThruSectionsDriver) Handle(GEOMImpl_ThruSectionsDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
186 {
187   Handle(GEOMImpl_ThruSectionsDriver) _anOtherObject;
188
189   if (!AnObject.IsNull()) {
190      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ThruSectionsDriver))) {
191        _anOtherObject = Handle(GEOMImpl_ThruSectionsDriver)((Handle(GEOMImpl_ThruSectionsDriver)&)AnObject);
192      }
193   }
194
195   return _anOtherObject ;
196 }