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