Salome HOME
0ad5d480e35ab92ea091af6618ad4b5ce956976a
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PolylineDumper.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 //  File   : GEOMImpl_PolylineDumper.cxx
24 //  Author : Sergey KHROMOV
25 //  Module : GEOM
26
27
28 #include "GEOMImpl_PolylineDumper.hxx"
29 #include "GEOMImpl_ICurvesOperations.hxx"
30
31 #include <GEOM_PythonDump.hxx>
32
33
34 //=======================================================================
35 // function : Constructor
36 // purpose  : 
37 //=======================================================================
38 GEOMImpl_PolylineDumper::GEOMImpl_PolylineDumper
39             (const std::list <std::list <double> >         &theCoords,
40              const Handle(TColStd_HArray1OfExtendedString) &theNames,
41              const Handle(TColStd_HArray1OfByte)           &theTypes,
42              const Handle(TColStd_HArray1OfByte)           &theCloseds,
43              const Handle(TColStd_HArray1OfReal)           &thePlnCoords)
44              : myCoords    (theCoords),
45                myNames     (theNames),
46                myTypes     (theTypes),
47                myCloseds   (theCloseds),
48                myPlnCoords (thePlnCoords),
49                myIsDone    (Standard_False)
50 {
51   init();
52 }
53
54
55 //=======================================================================
56 // function : Constructor
57 // purpose  : 
58 //=======================================================================
59 GEOMImpl_PolylineDumper::GEOMImpl_PolylineDumper
60             (const std::list <std::list <double> >         &theCoords,
61              const Handle(TColStd_HArray1OfExtendedString) &theNames,
62              const Handle(TColStd_HArray1OfByte)           &theTypes,
63              const Handle(TColStd_HArray1OfByte)           &theCloseds,
64              const Handle(GEOM_Object)                     &theWorkingPlane)
65              : myCoords       (theCoords),
66                myNames        (theNames),
67                myTypes        (theTypes),
68                myCloseds      (theCloseds),
69                myWorkingPlane (theWorkingPlane),
70                myIsDone       (Standard_False)
71 {
72   init();
73 }
74
75 //=======================================================================
76 // function : Dump
77 // purpose  : 
78 //=======================================================================
79 Standard_Boolean GEOMImpl_PolylineDumper::Dump
80         (const Handle(GEOM_Object) &theObject)
81 {
82   if (theObject.IsNull()) {
83     return Standard_False;
84   }
85
86   if (myIsDone) {
87     Handle(GEOM_Function) aFunction = theObject->GetLastFunction();
88     GEOM::TPythonDump aPD(aFunction);
89
90     aPD << myDescr;
91     aPD << theObject << " = pl.result(";
92
93     if (myWorkingPlane.IsNull()) {
94       // Add coordinates of working plane.
95       Standard_Integer i;
96
97       aPD << "[";
98       for (i = 0; i < 9; ++i) {
99         aPD << myPlnCoords->Value(myPlnCoords->Lower() + i);
100
101         if (i < 8) {
102           aPD << ", ";
103         }
104       }
105       aPD << "]";
106     } else {
107       aPD << myWorkingPlane;
108     }
109
110     aPD << ")";
111   }
112
113   return myIsDone;
114 }
115
116 //=======================================================================
117 // function : init
118 // purpose  : 
119 //=======================================================================
120 void GEOMImpl_PolylineDumper::init()
121 {
122   // Check input parameters.
123   if (myCoords.empty() || myNames.IsNull() ||
124       myTypes.IsNull() || myCloseds.IsNull()) {
125     // One or more input parameters are null or empty()
126     return;
127   }
128
129   const Standard_Integer aNbSec = myCoords.size();
130
131   if (aNbSec != myNames->Length() || aNbSec != myTypes->Length() ||
132       aNbSec != myCloseds->Length()) {
133     // Inconsistent data.
134     return;
135   }
136
137   // Check the reference plane
138   if (myPlnCoords.IsNull()) {
139     if (myWorkingPlane.IsNull()) {
140       // Null working plane
141       return;
142     }
143   } else {
144     if (myWorkingPlane.IsNull() == Standard_False) {
145       // Ambiguous working plane
146       return;
147     }
148
149     if (myPlnCoords->Length() != 9) {
150       // Invalid number of plane coordinates.
151       return;
152     }
153   }
154
155   const char *aSeparator = "\n\t";
156   Standard_Integer i;
157   std::list <std::list <double> >::const_iterator anIt = myCoords.begin();
158
159   myDescr += "pl = geompy.Polyline2D()";
160
161   // Add sections.
162   for (i = 0; i < aNbSec && anIt != myCoords.end(); ++i, ++anIt) {
163     myDescr += aSeparator;
164     myDescr += "pl.addSection(";
165     // Add name
166     myDescr += "\"";
167     myDescr += myNames->Value(myNames->Lower() + i) + "\", ";
168     // Add type
169     const Standard_Integer aType = myTypes->Value(myTypes->Lower() + i);
170
171     switch (aType) {
172       case GEOMImpl_ICurvesOperations::Polyline:
173         myDescr += "GEOM.Polyline, ";
174         break;
175       case GEOMImpl_ICurvesOperations::Interpolation:
176         myDescr += "GEOM.Interpolation, ";
177         break;
178       default:
179         myDescr.Clear();
180         return;
181         break; // NEVERREACHED
182     }
183
184     // Add Closed flag.
185     if (myCloseds->Value(myCloseds->Lower() + i)) {
186       myDescr += "True";
187     } else {
188       myDescr += "False";
189     }
190
191     // Add points.
192     const Standard_Integer aNbCoords = anIt->size();
193
194     if (aNbCoords > 0) {
195       if (aNbCoords % 2) {
196         // Odd number of coordinates.
197         myDescr.Clear();
198         return;
199       }
200
201       if (aNbCoords <= 4) {
202         // Add 2 points to the same command addSection.
203         myDescr += ", [";
204
205         std::list <double>::const_iterator aCIt = anIt->begin();
206
207         while (aCIt != anIt->end()) {
208           myDescr += *aCIt;
209
210           if (++aCIt != anIt->end()) {
211             myDescr += ", ";
212           }
213         }
214       } else {
215         // Add points to a separate command addPoints.
216         // Add maximum 4 points in a command.
217         std::list <double>::const_iterator aCIt        = anIt->begin();
218         Standard_Integer                   aMaxNbCoord = 8;
219         Standard_Integer                   j           = 1;
220
221         myDescr += ")";
222         myDescr += aSeparator;
223         myDescr += "pl.addPoints([";
224
225         while (aCIt != anIt->end()) {
226           myDescr += *aCIt;
227
228           if (++aCIt != anIt->end()) {
229             if (j == aMaxNbCoord) {
230               // 4 points are added. Add a new command.
231               myDescr += "])";
232               myDescr += aSeparator;
233               myDescr += "pl.addPoints([";
234               j        = 1;
235             } else {
236               myDescr += ", ";
237               j++;
238             }
239           }
240         }
241       }
242
243       myDescr += "]";
244     }
245
246     myDescr += ")";
247   }
248
249   myDescr  += aSeparator;
250   myIsDone  = Standard_True;
251 }