Salome HOME
Update mail address
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PipeDriver.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
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_PipeDriver.hxx>
24 #include <GEOMImpl_IPipe.hxx>
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOM_Function.hxx>
27
28 #include <BRep_Tool.hxx>
29 #include <BRepCheck_Analyzer.hxx>
30 #include <BRepOffsetAPI_MakePipe.hxx>
31 #include <BRepBuilderAPI_MakeWire.hxx>
32
33 #include <TopAbs.hxx>
34 #include <TopoDS.hxx>
35 #include <TopoDS_Wire.hxx>
36 #include <TopoDS_Edge.hxx>
37 #include <TopoDS_Shape.hxx>
38 #include <BRepOffsetAPI_MakePipeShell.hxx>
39 #include <TColStd_HSequenceOfTransient.hxx>
40 #include <GEOMImpl_IPipeDiffSect.hxx>
41
42 #include <Standard_NullObject.hxx>
43 #include <Standard_TypeMismatch.hxx>
44 #include <Standard_ConstructionError.hxx>
45 #include "utilities.h"
46 #include <TopExp_Explorer.hxx>
47 #include <TopTools_SequenceOfShape.hxx>
48 #include <BRep_Builder.hxx>
49 #include <TopoDS_Compound.hxx>
50
51 //=======================================================================
52 //function : GetID
53 //purpose  :
54 //=======================================================================
55 const Standard_GUID& GEOMImpl_PipeDriver::GetID()
56 {
57   static Standard_GUID aPipeDriver("FF1BBB19-5D14-4df2-980B-3A668264EA16");
58   return aPipeDriver;
59 }
60
61
62 //=======================================================================
63 //function : GEOMImpl_PipeDriver
64 //purpose  :
65 //=======================================================================
66 GEOMImpl_PipeDriver::GEOMImpl_PipeDriver()
67 {
68 }
69
70 //=======================================================================
71 //function : Execute
72 //purpose  :
73 //=======================================================================
74 Standard_Integer GEOMImpl_PipeDriver::Execute(TFunction_Logbook& log) const
75 {
76   if (Label().IsNull()) return 0;
77   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
78   GEOMImpl_IPipe* aCI= 0;
79   Standard_Integer aType = aFunction->GetType();
80   if(aType == PIPE_BASE_PATH)
81     aCI = new GEOMImpl_IPipe(aFunction);
82   else if(aType == PIPE_DIFFERENT_SECTIONS)
83     aCI = new GEOMImpl_IPipeDiffSect(aFunction);
84   else
85     return 0;
86
87   Handle(GEOM_Function) aRefPath = aCI->GetPath();
88   TopoDS_Shape aShapePath = aRefPath->GetValue();
89
90
91   if (aShapePath.IsNull())
92   {
93     cout<<"Driver : path is null"<<endl;
94     if(aCI) delete aCI;
95     Standard_NullObject::Raise("MakePipe aborted : null path argument");
96   }
97   
98   // Get path contour
99   TopoDS_Wire aWirePath;
100   if (aShapePath.ShapeType() == TopAbs_WIRE) {
101     aWirePath = TopoDS::Wire(aShapePath);
102   } 
103   else {
104     if (aShapePath.ShapeType() == TopAbs_EDGE) {
105          
106         TopoDS_Edge anEdge = TopoDS::Edge(aShapePath);
107         aWirePath = BRepBuilderAPI_MakeWire(anEdge);
108     } 
109     else {
110       if(aCI) delete aCI;
111       Standard_TypeMismatch::Raise("MakePipe aborted : path shape is neither a wire nor an edge");
112     } 
113   }
114   
115   TopoDS_Shape aShape;
116
117   if (aType == PIPE_BASE_PATH) 
118   {
119      
120     Handle(GEOM_Function) aRefBase = aCI->GetBase();
121    
122     TopoDS_Shape aShapeBase = aRefBase->GetValue();
123     
124     if (aShapeBase.IsNull()) {
125       if(aCI) delete aCI;
126       Standard_NullObject::Raise("MakePipe aborted : null base argument");
127     }
128
129     // Make pipe
130     aShape = BRepOffsetAPI_MakePipe(aWirePath, aShapeBase);
131   }
132
133   //building pipe with different sections
134   else if (aType == PIPE_DIFFERENT_SECTIONS) 
135   {
136     GEOMImpl_IPipeDiffSect* aCIDS = (GEOMImpl_IPipeDiffSect*)aCI;
137     //GEOMImpl_IPipeDiffSect* aCIDS = static_cast<GEOMImpl_IPipeDiffSect*>(aCI);
138     BRepOffsetAPI_MakePipeShell aBuilder(aWirePath);
139     Handle(TColStd_HSequenceOfTransient) aBasesObjs = aCIDS->GetBases ();
140     Handle(TColStd_HSequenceOfTransient) aLocObjs = aCIDS->GetLocations ();
141     Standard_Boolean aWithContact = (aCIDS->GetWithContactMode());
142     Standard_Boolean aWithCorrect = (aCIDS->GetWithCorrectionMode());
143
144     Standard_Integer i =1, nbBases = aBasesObjs->Length(), 
145       nbLocs = (aLocObjs.IsNull() ? 0 :aLocObjs->Length());
146     
147     if(nbLocs && nbLocs != nbBases)
148     {
149       if(aCI) delete aCI;
150       Standard_ConstructionError::Raise("Number of sections is not equal to number of locations ");
151     }
152     TopTools_SequenceOfShape aSeqBases;
153     TopTools_SequenceOfShape aSeqLocs;
154     TopTools_SequenceOfShape aSeqFaces;
155     for( ; i <= nbBases; i++) 
156     {
157       Handle(Standard_Transient) anItem = aBasesObjs->Value(i);
158       if(anItem.IsNull())
159         continue;
160       Handle(GEOM_Function) aRefBase = Handle(GEOM_Function)::DownCast(anItem);
161       if(aRefBase.IsNull())
162         continue;
163       TopoDS_Shape aShapeBase = aRefBase->GetValue();
164       if(aShapeBase.IsNull())
165         continue;
166       TopAbs_ShapeEnum aTypeBase = aShapeBase.ShapeType();
167
168       //if for section was specified face with a few wires then a few
169       //    pipes were build and make solid 
170       if(aTypeBase == TopAbs_FACE)
171       {
172         //for case one path should be used other type function
173         aSeqFaces.Append(aShapeBase);
174         TopExp_Explorer aExpW(aShapeBase,TopAbs_WIRE);
175         for( ; aExpW.More(); aExpW.Next())
176         {
177           TopoDS_Shape aWireProf = aExpW.Current();
178           aSeqBases.Append(aWireProf);
179         }
180       }
181       else if(aTypeBase == TopAbs_WIRE || aTypeBase == TopAbs_VERTEX) 
182         aSeqBases.Append(aShapeBase);
183       else if(aTypeBase == TopAbs_EDGE)
184       {
185         TopoDS_Edge anEdge = TopoDS::Edge(aShapeBase);
186         TopoDS_Shape aWireProf = BRepBuilderAPI_MakeWire(anEdge);
187         aSeqBases.Append(aWireProf);
188       }
189       if(nbLocs) 
190       {
191         Handle(Standard_Transient) anItemLoc = aLocObjs->Value(i);
192         if(anItemLoc.IsNull())
193           continue;
194         Handle(GEOM_Function) aRefLoc = Handle(GEOM_Function)::DownCast(anItemLoc);
195         TopoDS_Shape aShapeLoc = aRefLoc->GetValue();
196         if(aShapeLoc.IsNull() || aShapeLoc.ShapeType() != TopAbs_VERTEX)
197           continue;
198         aSeqLocs.Append(aShapeLoc);
199       }
200     }
201     
202     nbLocs = aSeqLocs.Length();
203     Standard_Integer nbShapes = aSeqBases.Length();
204     Standard_Integer step = nbShapes/nbBases;
205     
206     if(nbShapes < nbBases || fmod((double)nbShapes, (double)nbBases))
207     {
208       if(aCI) delete aCI;
209       Standard_ConstructionError::Raise("Invalid sections were specified for building pipe");
210     }
211      
212     Standard_Integer ind =0;     
213     for( i=1; i  <= nbShapes && ind < nbShapes; i++) //i+nbBases <= nbShapes
214     {
215       TopTools_SequenceOfShape usedBases;
216       Standard_Integer j = 1;
217       for( ; j <= nbBases ; j++) 
218       {
219         ind = i + (j-1)*step;
220         
221          TopoDS_Shape aWireProf = aSeqBases.Value(ind);
222          usedBases.Append(aWireProf);
223          if(nbLocs) 
224          {
225            TopoDS_Shape aShapeLoc = aSeqLocs.Value(j);
226            TopoDS_Vertex aVert = TopoDS::Vertex(aShapeLoc);
227            aBuilder.Add(aWireProf,aVert,aWithContact,aWithCorrect);
228          }
229          else
230            aBuilder.Add(aWireProf,aWithContact,aWithCorrect);
231       }
232       if(!aBuilder.IsReady())
233       {
234         if(aCI) delete aCI;
235         Standard_ConstructionError::Raise("Invalid input data for building PIPE: bases are invalid");
236       }
237       aBuilder.Build();
238       aShape = aBuilder.Shape();
239       aSeqFaces.Append(aShape); 
240       for( j = 1; j <=usedBases.Length(); j++)
241         aBuilder.Delete(usedBases.Value(j));
242     }
243         
244     //for case if section is face 
245     if(aSeqFaces.Length() >1)
246     {
247       BRep_Builder aB;
248       TopoDS_Compound aComp;
249       aB.MakeCompound(aComp);
250       for( i = 1; i <= aSeqFaces.Length(); i++)
251         aB.Add(aComp,aSeqFaces.Value(i));
252       aShape = aComp;
253     }
254   }
255   if (aShape.IsNull()) return 0;
256
257   BRepCheck_Analyzer ana (aShape, Standard_False);
258   if (!ana.IsValid()) {
259     if(aCI) delete aCI;
260     Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
261   }
262
263   aFunction->SetValue(aShape);
264
265   log.SetTouched(Label());
266   if(aCI) delete aCI;
267   return 1;
268 }
269
270
271 //=======================================================================
272 //function :  GEOMImpl_PipeDriver_Type_
273 //purpose  :
274 //=======================================================================
275 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PipeDriver_Type_()
276 {
277
278   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
279   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
280   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
281   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
282   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
283   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
284
285
286   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
287   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PipeDriver",
288                                                          sizeof(GEOMImpl_PipeDriver),
289                                                          1,
290                                                          (Standard_Address)_Ancestors,
291                                                          (Standard_Address)NULL);
292
293   return _aType;
294 }
295
296 //=======================================================================
297 //function : DownCast
298 //purpose  :
299 //=======================================================================
300 const Handle(GEOMImpl_PipeDriver) Handle(GEOMImpl_PipeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
301 {
302   Handle(GEOMImpl_PipeDriver) _anOtherObject;
303
304   if (!AnObject.IsNull()) {
305      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PipeDriver))) {
306        _anOtherObject = Handle(GEOMImpl_PipeDriver)((Handle(GEOMImpl_PipeDriver)&)AnObject);
307      }
308   }
309
310   return _anOtherObject ;
311 }