Salome HOME
787602aeef839fd240e6f36d017f07a7fbbde96c
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDriver.cxx
1 // Copyright (C) 2007-2023  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(Handle(TFunction_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 != (int)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   log->SetTouched(Label());
182
183   return 1;
184 }
185
186 //=======================================================================
187 //function : Execute
188 //purpose  :
189 //======================================================================= 
190 Standard_Integer GEOMImpl_PolylineDriver::Execute(Handle(TFunction_Logbook)& log) const
191 {
192   if (Label().IsNull()) return 0;    
193   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
194   Standard_Integer aType = aFunction->GetType();
195
196   if (aType == POLYLINE2D_PLN_COORDS || aType == POLYLINE2D_PLN_OBJECT) {
197     return MakePolyline2D(log);
198   }
199
200   GEOMImpl_IPolyline aCI (aFunction);
201   
202   TopoDS_Shape aShape;
203
204   if (aType == POLYLINE_POINTS) {
205
206     bool useCoords = aCI.GetConstructorType() == COORD_CONSTRUCTOR;
207     TColgp_Array1OfPnt points(1, (useCoords ? aCI.GetLength() : 1) );
208     if(useCoords) {
209       Handle(TColStd_HArray1OfReal) aCoordsArray = aCI.GetCoordinates();
210       int anArrayLength = aCoordsArray->Length();
211       for (int i = 0, j = 1; i <= (anArrayLength-3); i += 3) {
212         gp_Pnt aPnt = gp_Pnt(aCoordsArray->Value(i+1), aCoordsArray->Value(i+2), aCoordsArray->Value(i+3));
213         points.SetValue(j,aPnt);
214         j++;
215       } 
216     }
217
218     int aLen = aCI.GetLength();
219     int ind = 1;
220     BRepBuilderAPI_MakePolygon aMakePoly;
221     for (; ind <= aLen; ind++)
222     {
223       if(useCoords) {
224         aMakePoly.Add(BRepBuilderAPI_MakeVertex(points.Value(ind)));
225       } else {
226         Handle(GEOM_Function) aRefPoint = aCI.GetPoint(ind);
227         TopoDS_Shape aShapePnt = aRefPoint->GetValue();
228         if (aShapePnt.ShapeType() != TopAbs_VERTEX) {
229           Standard_TypeMismatch::Raise
230             ("Polyline creation aborted : arguments are not a vertexes");
231           return 0;
232         }
233         if (aShapePnt.ShapeType() == TopAbs_VERTEX) {
234           aMakePoly.Add(TopoDS::Vertex(aShapePnt));
235           //if (!aMakePoly.Added()) return 0;
236         }
237       }
238     }
239     // Compare first and last point coordinates and close polyline if it's the same.
240     if ( aLen > 2 ) {
241       TopoDS_Vertex aV1;
242       if( useCoords ) {
243         aV1 = BRepBuilderAPI_MakeVertex(points.Value(1));
244       } else {
245         Handle(GEOM_Function) aFPoint = aCI.GetPoint(1);
246         TopoDS_Shape aFirstPnt = aFPoint->GetValue();
247         aV1 = TopoDS::Vertex(aFirstPnt);
248       }
249       
250       TopoDS_Vertex aV2;
251       if( useCoords ) {
252         aV2 = BRepBuilderAPI_MakeVertex(points.Value(aLen));
253       } else {
254         Handle(GEOM_Function) aLPoint = aCI.GetPoint(aLen);
255         TopoDS_Shape aLastPnt = aLPoint->GetValue();
256         aV2 = TopoDS::Vertex(aLastPnt);
257       }
258       
259       if ( (!aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2)) ||
260            aCI.GetIsClosed())
261         aMakePoly.Close();
262     }
263     
264     if (aMakePoly.IsDone()) {
265       aShape = aMakePoly.Wire();
266     }
267   }
268   else {
269   }
270
271   if (aShape.IsNull()) return 0;
272   
273   aFunction->SetValue(aShape);
274   
275   log->SetTouched(Label());
276   
277   return 1;    
278 }
279
280 //================================================================================
281 /*!
282  * \brief Returns a name of creation operation and names and values of creation parameters
283  */
284 //================================================================================
285
286 bool GEOMImpl_PolylineDriver::
287 GetCreationInformation(std::string&             theOperationName,
288                        std::vector<GEOM_Param>& theParams)
289 {
290   if (Label().IsNull()) return 0;
291   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
292
293   GEOMImpl_IPolyline        aCI( function );
294   GEOMImpl_ICurveParametric aIP( function );
295   Standard_Integer aType = function->GetType();
296
297   switch ( aType ) {
298   case POLYLINE_POINTS:
299     theOperationName = "CURVE";
300     AddParam( theParams, "Type", "Polyline");
301     if ( aIP.HasData() )
302     {
303       AddParam( theParams, "X(t) equation", aIP.GetExprX() );
304       AddParam( theParams, "Y(t) equation", aIP.GetExprY() );
305       AddParam( theParams, "Z(t) equation", aIP.GetExprZ() );
306       AddParam( theParams, "Min t", aIP.GetParamMin() );
307       AddParam( theParams, "Max t", aIP.GetParamMax() );
308       if ( aIP.GetParamNbStep() )
309         AddParam( theParams, "Number of steps", aIP.GetParamNbStep() );
310       else
311         AddParam( theParams, "t step", aIP.GetParamStep() );
312     }
313     else
314     {
315       GEOM_Param& pntParam = AddParam( theParams, "Points");
316       if ( aCI.GetConstructorType() == COORD_CONSTRUCTOR )
317       {
318         Handle(TColStd_HArray1OfReal) coords = aCI.GetCoordinates();
319         if ( coords->Length() > 3 )
320           pntParam << ( coords->Length() ) / 3 << " points: ";
321         for ( int i = coords->Lower(), nb = coords->Lower()+(coords->Length()/3)*3; i < nb; i+=3)
322           pntParam << "( " << coords->Value( i ) << ", " << coords->Value( i+1 ) << ", " << coords->Value( i+2 ) << " ) ";
323       }
324       else
325       {
326         if ( aCI.GetLength() > 1 )
327           pntParam << aCI.GetLength() << " points: ";
328         for ( int i = 1, nb = aCI.GetLength(); i <= nb; ++i )
329           pntParam << aCI.GetPoint( i ) << " ";
330       }
331       AddParam( theParams, "Is closed", aCI.GetIsClosed() );
332     }
333     break;
334   case POLYLINE2D_PLN_COORDS:
335   case POLYLINE2D_PLN_OBJECT:
336     {
337       theOperationName = "SKETCH";
338
339       GEOMImpl_IPolyline2D                    aP2d(function);
340       Handle(TColStd_HArray1OfExtendedString) aNames = aP2d.GetNames();
341
342       if (aNames.IsNull() == Standard_False) {
343         if (aNames->Length() == 1) {
344           // This is the single curve. Make its full dump.
345           AddParam(theParams, "Name", aNames->Value(aNames->Lower()));
346
347           Handle(TColStd_HArray1OfByte) aTypes = aP2d.GetTypes();
348
349           if (aTypes.IsNull() == Standard_False && aTypes->Length() == 1) {
350             Standard_Integer aType = aTypes->Value(aTypes->Lower());
351
352             if (aType == GEOMImpl_ICurvesOperations::Polyline) {
353               AddParam(theParams, "Type") << "Polyline";
354             } else if (aType == GEOMImpl_ICurvesOperations::Interpolation) {
355               AddParam(theParams, "Type") << "Interpolation";
356             }
357           }
358
359           Handle(TColStd_HArray1OfByte) aCloseds = aP2d.GetClosedFlags();
360
361           if (aCloseds.IsNull() == Standard_False && aCloseds->Length() == 1) {
362             const char *aYesNo =
363               aCloseds->Value(aCloseds->Lower()) ? "Yes" : "No";
364
365             AddParam(theParams, "Is closed", aYesNo);
366           }
367
368           std::list <std::list <double> > aCoords;
369
370           aP2d.GetCoords(aCoords);
371
372           if (aCoords.size() == 1) {
373             AddParam(theParams, "Number of points", aCoords.front().size()/2);
374           }
375         } else {
376           // There are more than 1 curve.
377           Standard_Integer                aNbCurves = aNames->Length();
378           Standard_Integer                i;
379           std::list <std::list <double> > aCoords;
380
381           AddParam(theParams, "Number of curves", aNbCurves);
382           aP2d.GetCoords(aCoords);
383
384           //Standard_Integer aNbCoords = aCoords.size();
385           std::list <std::list <double> >::const_iterator
386                            anIt      = aCoords.begin();
387
388           for (i = 0; i < aNbCurves; i++) {
389             TCollection_AsciiString aName("Curve ");
390             TCollection_ExtendedString
391               aValue(aNames->Value(aNames->Lower() + i));
392
393             aName.AssignCat(i + 1);
394
395             if (anIt != aCoords.end()) {
396               aValue.AssignCat(" (");
397               aValue.AssignCat(Standard_Integer(anIt->size()));
398               aValue.AssignCat(" points)");
399               anIt++;
400             }
401
402             AddParam(theParams, aName.ToCString(), aValue);
403           }
404         }
405       }
406
407       if (aType == POLYLINE2D_PLN_COORDS) {
408         Handle(TColStd_HArray1OfReal) aPln = aP2d.GetWorkingPlaneDbls();
409
410         if (aPln.IsNull() == Standard_False && aPln->Length() == 9) {
411           Standard_Integer i = aPln->Lower();
412
413           AddParam( theParams, "Origin")
414             << aPln->Value(i)     << " "
415             << aPln->Value(i + 1) << " "
416             << aPln->Value(i + 2);
417           AddParam( theParams, "OZ")
418             << aPln->Value(i + 3) << " "
419             << aPln->Value(i + 4) << " "
420             << aPln->Value(i + 5);
421           AddParam( theParams, "OX")
422             << aPln->Value(i + 6) << " "
423             << aPln->Value(i + 7) << " "
424             << aPln->Value(i + 8);
425         }
426       } else {
427         AddParam(theParams, "Working plane", aP2d.GetWorkingPlane(), "XOY");
428       }
429     }
430     break;
431   default:
432     return false;
433   }
434
435   return true;
436 }
437
438 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PolylineDriver,GEOM_BaseDriver)