Salome HOME
0023505: Sigsegv with fuse on cylinder and cone
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDriver.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 "GEOMImpl_PolylineDriver.hxx"
24
25 #include "GEOMImpl_ICurveParametric.hxx"
26 #include "GEOMImpl_ICurvesOperations.hxx"
27 #include "GEOMImpl_IPolyline.hxx"
28 #include "GEOMImpl_IPolyline2D.hxx"
29 #include "GEOMImpl_Types.hxx"
30 #include "GEOM_Function.hxx"
31 #include <GEOMUtils.hxx>
32 #include <Sketcher_Utils.hxx>
33
34 #include <BRepBuilderAPI_MakePolygon.hxx>
35 #include <BRepBuilderAPI_MakeVertex.hxx>
36 #include <BRep_Builder.hxx>
37 #include <BRep_Tool.hxx>
38 #include <Precision.hxx>
39 #include <TColgp_Array1OfPnt.hxx>
40 #include <TopAbs.hxx>
41 #include <TopExp.hxx>
42 #include <TopoDS.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopoDS_Vertex.hxx>
45 #include <TopoDS_Wire.hxx>
46 #include <gp_Ax3.hxx>
47 #include <gp_Pnt.hxx>
48
49 //=======================================================================
50 //function : GetID
51 //purpose  :
52 //======================================================================= 
53 const Standard_GUID& GEOMImpl_PolylineDriver::GetID()
54 {
55   static Standard_GUID aPolylineDriver("FF1BBB31-5D14-4df2-980B-3A668264EA16");
56   return aPolylineDriver; 
57 }
58
59
60 //=======================================================================
61 //function : GEOMImpl_PolylineDriver
62 //purpose  : 
63 //=======================================================================
64 GEOMImpl_PolylineDriver::GEOMImpl_PolylineDriver() 
65 {
66 }
67
68 //=======================================================================
69 //function : MakePolyline2D
70 //purpose  :
71 //======================================================================= 
72 Standard_Integer GEOMImpl_PolylineDriver::MakePolyline2D(LOGBOOK& log) const
73 {
74   if (Label().IsNull()) {
75     return 0;
76   }
77
78   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
79   GEOMImpl_IPolyline2D  aCI(aFunction);
80   Standard_Integer      aType = aFunction->GetType();
81   TopoDS_Shape          aShape;
82
83   // Get data.
84   Handle(TColStd_HArray1OfExtendedString) aNames       = aCI.GetNames();
85   Handle(TColStd_HArray1OfByte)           aTypes       = aCI.GetTypes();
86   Handle(TColStd_HArray1OfByte)           aClosedFlags = aCI.GetClosedFlags();
87   std::list <std::list <double> >         aCoords;
88   gp_Ax3                                  aWPlane;
89
90   aCI.GetCoords(aCoords);
91
92   // Check the data validity
93   if (aNames.IsNull()) {
94     return 0;
95   }
96
97   Standard_Integer aNbSections = aNames->Length();
98
99   if (aTypes.IsNull() || aNbSections != aTypes->Length()) {
100     return 0;
101   }
102
103   if (aClosedFlags.IsNull() || aNbSections != aClosedFlags->Length()) {
104     return 0;
105   }
106
107   if (aNbSections != aCoords.size()) {
108     return 0;
109   }
110
111   if (aType == POLYLINE2D_PLN_COORDS) {
112     Handle(TColStd_HArray1OfReal) aPlaneCoords = aCI.GetWorkingPlaneDbls();
113
114     if (aPlaneCoords.IsNull()) {
115       return 0;
116     }
117
118     if (aPlaneCoords->Length() != 9) {
119       return 0;
120     }
121
122     Standard_Integer i = aPlaneCoords->Lower();
123     gp_Pnt aOrigin(aPlaneCoords->Value(i), aPlaneCoords->Value(i + 1),
124                  aPlaneCoords->Value(i + 2));
125     gp_Dir aDirZ(aPlaneCoords->Value(i + 3), aPlaneCoords->Value(i + 4),
126                  aPlaneCoords->Value(i + 5));
127     gp_Dir aDirX(aPlaneCoords->Value(i + 6), aPlaneCoords->Value(i + 7),
128                  aPlaneCoords->Value(i + 8));
129     aWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
130   } else if (aType == POLYLINE2D_PLN_OBJECT) {
131     Handle(GEOM_Function) aRefFace = aCI.GetWorkingPlane();
132     TopoDS_Shape aShape = aRefFace->GetValue();
133
134     aWPlane = GEOMUtils::GetPosition(aShape);
135   } else {
136     return 0;
137   }
138
139   // Construct a shape.
140   Standard_Integer iN = aNames->Lower();
141   Standard_Integer iT = aTypes->Lower();
142   Standard_Integer iC = aClosedFlags->Lower();
143   std::list <std::list <double> >::const_iterator anIter = aCoords.begin();
144   BRep_Builder aBuilder;
145   Standard_Boolean isEmpty = Standard_True;
146
147   if (aNbSections > 1) {
148     aBuilder.MakeCompound(TopoDS::Compound(aShape));
149   }
150
151   for (; anIter != aCoords.end(); ++anIter, ++iN, ++iT, ++iC) {
152     Standard_Integer aType = aTypes->Value(iT);
153     TopoDS_Shape     aSection;
154
155     if (aType == GEOMImpl_ICurvesOperations::Polyline) {
156       aSection = Sketcher_Utils::MakePolyline
157           (*anIter, aClosedFlags->Value(iC), aWPlane);
158     } else if (aType == GEOMImpl_ICurvesOperations::Interpolation) {
159       aSection = Sketcher_Utils::MakeInterpolation
160         (*anIter, aClosedFlags->Value(iC), aWPlane);
161     }
162
163     if (aNbSections > 1) {
164       // There are multiple sections.
165       if (aSection.IsNull() == Standard_False) {
166         aBuilder.Add(aShape, aSection);
167         isEmpty = Standard_False;
168       }
169     } else {
170       // There is only one section.
171       isEmpty = aSection.IsNull();
172       aShape  = aSection;
173     }
174   }
175
176   if (isEmpty) {
177     return 0;
178   }
179
180   aFunction->SetValue(aShape);
181 #if OCC_VERSION_MAJOR < 7
182   log.SetTouched(Label());
183 #else
184   log->SetTouched(Label());
185 #endif
186
187   return 1;
188 }
189
190 //=======================================================================
191 //function : Execute
192 //purpose  :
193 //======================================================================= 
194 Standard_Integer GEOMImpl_PolylineDriver::Execute(LOGBOOK& log) const
195 {
196   if (Label().IsNull()) return 0;    
197   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
198   Standard_Integer aType = aFunction->GetType();
199
200   if (aType == POLYLINE2D_PLN_COORDS || aType == POLYLINE2D_PLN_OBJECT) {
201     return MakePolyline2D(log);
202   }
203
204   GEOMImpl_IPolyline aCI (aFunction);
205   
206   TopoDS_Shape aShape;
207
208   if (aType == POLYLINE_POINTS) {
209
210     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
211     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
212     if(useCoords) {
213       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
214       int anArrayLength = aCoordsArray->Length();
215       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
216         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
217         points.SetValue(j,aPnt);
218         j++;
219       } 
220     }
221
222     int aLen = aCI.GetLength();
223     int ind = 1;
224     BRepBuilderAPI_MakePolygon aMakePoly;
225     for (; ind <= aLen; ind++)
226     {
227       if(useCoords) {
228         aMakePoly.Add(BRepBuilderAPI_MakeVertex(points.Value(ind)));
229       } else {
230         Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
231         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
232         if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
233           Standard_TypeMismatch::Raise
234             ("Polyline creation aborted : arguments are not a vertexes");
235           return 0;
236         }
237         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
238           aMakePoly.Add(TopoDS::Vertex(aShapePnt));
239           //if (!aMakePoly.Added()) return 0;
240         }
241       }
242     }
243     // Compare first and last point coordinates and close polyline if it's the same.
244     if ( aLen > 2 ) {
245       TopoDS_Vertex aV1;
246       if( useCoords ) {
247         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
248       } else {
249         Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
250         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
251         aV1 = TopoDS::Vertex(aFirstPnt);
252       }
253       
254       TopoDS_Vertex aV2;
255       if( useCoords ) {
256         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
257       } else {
258         Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
259         TopoDS_Shape aLastPnt = aLPoint->GetValue();
260         aV2 = TopoDS::Vertex(aLastPnt);
261       }
262       
263       if ( (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2)) ||
264            aCI.GetIsClosed())
265         aMakePoly.Close();
266     }
267     
268     if (aMakePoly.IsDone()) {
269       aShape = aMakePoly.Wire();
270     }
271   }
272   else {
273   }
274
275   if (aShape.IsNull()) return 0;
276   
277   aFunction->SetValue(aShape);
278   
279 #if OCC_VERSION_MAJOR < 7
280   log.SetTouched(Label());
281 #else
282   log->SetTouched(Label());
283 #endif
284   
285   return 1;    
286 }
287
288 //================================================================================
289 /*!
290  * \brief Returns a name of creation operation and names and values of creation parameters
291  */
292 //================================================================================
293
294 bool GEOMImpl_PolylineDriver::
295 GetCreationInformation(std::string&             theOperationName,
296                        std::vector<GEOM_Param>& theParams)
297 {
298   if (Label().IsNull()) return 0;
299   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
300
301   GEOMImpl_IPolyline        aCI( function );
302   GEOMImpl_ICurveParametric aIP( function );
303   Standard_Integer aType = function->GetType();
304
305   switch ( aType ) {
306   case POLYLINE_POINTS:
307     theOperationName = "CURVE";
308     AddParam( theParams, "Type", "Polyline");
309     if ( aIP.HasData() )
310     {
311       AddParam( theParams, "X(t) equation", aIP.GetExprX() );
312       AddParam( theParams, "Y(t) equation", aIP.GetExprY() );
313       AddParam( theParams, "Z(t) equation", aIP.GetExprZ() );
314       AddParam( theParams, "Min t", aIP.GetParamMin() );
315       AddParam( theParams, "Max t", aIP.GetParamMax() );
316       if ( aIP.GetParamNbStep() )
317         AddParam( theParams, "Number of steps", aIP.GetParamNbStep() );
318       else
319         AddParam( theParams, "t step", aIP.GetParamStep() );
320     }
321     else
322     {
323       GEOM_Param& pntParam = AddParam( theParams, "Points");
324       if ( aCI.GetConstructorType() == COORD_CONSTRUCTOR )
325       {
326         Handle(TColStd_HArray1OfReal) coords = aCI.GetCoordinates();
327         if ( coords->Length() > 3 )
328           pntParam << ( coords->Length() ) / 3 << " points: ";
329         for ( int i = coords->Lower(), nb = coords->Upper(); i <= nb; )
330           pntParam << "( " << coords->Value( i++ )
331                    << ", " << coords->Value( i++ )
332                    << ", " << coords->Value( i++ ) << " ) ";
333       }
334       else
335       {
336         if ( aCI.GetLength() > 1 )
337           pntParam << aCI.GetLength() << " points: ";
338         for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
339           pntParam << aCI.GetPoint( i ) << " ";
340       }
341       AddParam( theParams, "Is closed", aCI.GetIsClosed() );
342     }
343     break;
344   case POLYLINE2D_PLN_COORDS:
345   case POLYLINE2D_PLN_OBJECT:
346     {
347       theOperationName = "SKETCH";
348
349       GEOMImpl_IPolyline2D                    aP2d(function);
350       Handle(TColStd_HArray1OfExtendedString) aNames = aP2d.GetNames();
351
352       if (aNames.IsNull() == Standard_False) {
353         if (aNames->Length() == 1) {
354           // This is the single curve. Make its full dump.
355           AddParam(theParams, "Name", aNames->Value(aNames->Lower()));
356
357           Handle(TColStd_HArray1OfByte) aTypes = aP2d.GetTypes();
358
359           if (aTypes.IsNull() == Standard_False && aTypes->Length() == 1) {
360             Standard_Integer aType = aTypes->Value(aTypes->Lower());
361
362             if (aType == GEOMImpl_ICurvesOperations::Polyline) {
363               AddParam(theParams, "Type") << "Polyline";
364             } else if (aType == GEOMImpl_ICurvesOperations::Interpolation) {
365               AddParam(theParams, "Type") << "Interpolation";
366             }
367           }
368
369           Handle(TColStd_HArray1OfByte) aCloseds = aP2d.GetClosedFlags();
370
371           if (aCloseds.IsNull() == Standard_False && aCloseds->Length() == 1) {
372             const char *aYesNo =
373               aCloseds->Value(aCloseds->Lower()) ? "Yes" : "No";
374
375             AddParam(theParams, "Is closed", aYesNo);
376           }
377
378           std::list <std::list <double> > aCoords;
379
380           aP2d.GetCoords(aCoords);
381
382           if (aCoords.size() == 1) {
383             AddParam(theParams, "Number of points", aCoords.front().size()/2);
384           }
385         } else {
386           // There are more than 1 curve.
387           Standard_Integer                aNbCurves = aNames->Length();
388           Standard_Integer                i;
389           std::list <std::list <double> > aCoords;
390
391           AddParam(theParams, "Number of curves", aNbCurves);
392           aP2d.GetCoords(aCoords);
393
394           Standard_Integer aNbCoords = aCoords.size();
395           std::list <std::list <double> >::const_iterator
396                            anIt      = aCoords.begin();
397
398           for (i = 0; i < aNbCurves; i++) {
399             TCollection_AsciiString aName("Curve ");
400             TCollection_ExtendedString
401               aValue(aNames->Value(aNames->Lower() + i));
402
403             aName.AssignCat(i + 1);
404
405             if (anIt != aCoords.end()) {
406               aValue.AssignCat(" (");
407               aValue.AssignCat(Standard_Integer(anIt->size()));
408               aValue.AssignCat(" points)");
409               anIt++;
410             }
411
412             AddParam(theParams, aName.ToCString(), aValue);
413           }
414         }
415       }
416
417       if (aType == POLYLINE2D_PLN_COORDS) {
418         Handle(TColStd_HArray1OfReal) aPln = aP2d.GetWorkingPlaneDbls();
419
420         if (aPln.IsNull() == Standard_False && aPln->Length() == 9) {
421           Standard_Integer i = aPln->Lower();
422
423           AddParam( theParams, "Origin")
424             << aPln->Value(i)     << " "
425             << aPln->Value(i + 1) << " "
426             << aPln->Value(i + 2);
427           AddParam( theParams, "OZ")
428             << aPln->Value(i + 3) << " "
429             << aPln->Value(i + 4) << " "
430             << aPln->Value(i + 5);
431           AddParam( theParams, "OX")
432             << aPln->Value(i + 6) << " "
433             << aPln->Value(i + 7) << " "
434             << aPln->Value(i + 8);
435         }
436       } else {
437         AddParam(theParams, "Working plane", aP2d.GetWorkingPlane(), "XOY");
438       }
439     }
440     break;
441   default:
442     return false;
443   }
444
445   return true;
446 }
447
448 OCCT_IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PolylineDriver,GEOM_BaseDriver);