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