Salome HOME
b39cc9c3b233f21c71a423c1a1065687f0f44f4d
[modules/geom.git] / src / GEOM / GEOM_PythonDump.cxx
1 // Copyright (C) 2007-2022  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 "GEOM_PythonDump.hxx"
24
25 #include <TDF_Tool.hxx>
26
27 #include <TopAbs.hxx>
28
29 #include <TColStd_ListOfInteger.hxx>
30 #include <TColStd_ListIteratorOfListOfInteger.hxx>
31
32 namespace GEOM
33 {
34   size_t TPythonDump::myCounter = 0;
35
36   TPythonDump::TPythonDump (const Handle(GEOM_Function)& theFunction, bool theAppend)
37   {
38     myFunction = theFunction;
39     myCounter++;
40     myAppend = theAppend;
41   }
42
43   TPythonDump::~TPythonDump()
44   {
45     if (--myCounter == 0) {
46       TCollection_AsciiString aDescr;
47       if ( myAppend )
48         aDescr = myFunction->GetDescription();
49       std::string aString = myStream.str();
50       if ( !aDescr.IsEmpty() && !aString.empty())
51         aDescr += "\n\t";
52       aDescr += (char *)aString.c_str();
53       myFunction->SetDescription( aDescr );
54     }
55   }
56
57   TPythonDump& TPythonDump::operator<< (bool theArg)
58   {
59     if (theArg)
60       myStream << "True";
61     else
62       myStream << "False";
63     return *this;
64   }
65
66   TPythonDump& TPythonDump::operator<< (long int theArg)
67   {
68     myStream<<theArg;
69     return *this;
70   }
71
72   TPythonDump& TPythonDump::operator<< (int theArg)
73   {
74     myStream<<theArg;
75     return *this;
76   }
77
78   TPythonDump& TPythonDump::operator<< (double theArg)
79   {
80     myStream.precision(16);
81     myStream<<theArg;
82     return *this;
83   }
84
85   TPythonDump& TPythonDump::operator<< (float theArg)
86   {
87     myStream.precision(8);
88     myStream<<theArg;
89     return *this;
90   }
91
92   TPythonDump& TPythonDump::operator<< (const void* theArg)
93   {
94     myStream<<theArg;
95     return *this;
96   }
97
98   TPythonDump& TPythonDump::operator<< (const char* theArg)
99   {
100     myStream<<theArg;
101     return *this;
102   }
103
104   TPythonDump& TPythonDump::operator<< (const TCollection_AsciiString theArg)
105   {
106     myStream<<theArg;
107     return *this;
108   }
109
110   TPythonDump& TPythonDump::operator<< (const TopAbs_ShapeEnum theArg)
111   {
112     myStream<<"geompy.ShapeType[\"";
113     if (theArg == -1)
114       myStream<<"AUTO";
115     else if (theArg == 9)
116       myStream<<"FLAT";
117     else if (theArg >= TopAbs_COMPOUND && theArg <= TopAbs_SHAPE)
118       TopAbs::Print(theArg, myStream);
119     else
120       myStream<<int(theArg);
121     myStream<<"\"]";
122     return *this;
123   }
124
125   TPythonDump& TPythonDump::operator<< (const Handle(GEOM_BaseObject)& theObject)
126   {
127     if (theObject.IsNull()) {
128       myStream << "None";
129     } else {
130       *this << theObject.operator->();
131     }
132     return *this;
133   }
134
135   TPythonDump& TPythonDump::operator<< (const Handle(TColStd_HSequenceOfTransient)& theObjects)
136   {
137     Standard_Integer aLength = theObjects->Length();
138     if ( aLength > 1 ) {
139       myStream << "[";
140     }
141     for ( Standard_Integer i = 1; i <= aLength; i++ ) {
142       *this << Handle(GEOM_Object)::DownCast(theObjects->Value(i));
143       if ( i < aLength ) myStream << ", ";
144     }
145     if ( aLength > 1 ) {
146       myStream << "]";
147     }
148     return *this;
149   }
150   Standard_EXPORT TPythonDump&
151   TPythonDump::operator<< (const std::list<Handle(GEOM_Object)>& theObjects)
152   {
153     Standard_Integer aLength = theObjects.size();
154     if ( aLength != 1 ) {
155       myStream << "[";
156     }
157     std::list<Handle(GEOM_Object)>::const_iterator obj = theObjects.begin();
158     for ( Standard_Integer i = 1; i <= aLength; i++, ++obj ) {
159       *this << *obj;
160       if ( i < aLength ) myStream << ", ";
161     }
162     if ( aLength != 1 ) {
163       myStream << "]";
164     }
165     return *this;
166   }
167
168   TPythonDump& TPythonDump::operator<< (const GEOM_BaseObject* theObject)
169   {
170     if ( !theObject ) {
171       myStream << "None";
172     } else {
173       TCollection_AsciiString anEntry;
174       TDF_Tool::Entry(theObject->GetEntry(), anEntry);
175       myStream << anEntry.ToCString();
176     }
177     return *this;
178   }
179
180   Handle(GEOM_BaseObject) GetCreatedLast(const Handle(Standard_Transient)& theObj1,
181                                          const Handle(Standard_Transient)& theObj2)
182   {
183     Handle(GEOM_BaseObject) bo1 = Handle(GEOM_Object)::DownCast(theObj1);
184     Handle(GEOM_BaseObject) bo2 = Handle(GEOM_Object)::DownCast(theObj2);
185     if (bo1.IsNull()) return bo2;
186     if (bo2.IsNull()) return bo1;
187
188     TColStd_ListOfInteger aTags1, aTags2;
189     TDF_Tool::TagList(bo1->GetEntry(), aTags1);
190     TDF_Tool::TagList(bo2->GetEntry(), aTags2);
191     TColStd_ListIteratorOfListOfInteger aListIter1(aTags1), aListIter2(aTags2);
192     for (; aListIter1.More(); aListIter1.Next(), aListIter2.Next()) {
193       if (!aListIter2.More())
194         return bo1; // anObj1 is stored under anObj2
195
196       if (aListIter1.Value() > aListIter2.Value())
197         return bo1;
198       else if (aListIter1.Value() < aListIter2.Value())
199         return bo2;
200     }
201     return bo1;
202   }
203
204   Handle(GEOM_BaseObject) GetCreatedLast(const Handle(TColStd_HSequenceOfTransient)& theObjects)
205   {
206     Handle(GEOM_BaseObject) anObject, aLatest;
207     int i, aLen = theObjects->Length();
208     if (aLen < 1)
209       return aLatest;
210
211     for (i = 1; i <= aLen; i++) {
212       anObject = Handle(GEOM_BaseObject)::DownCast(theObjects->Value(i));
213       if ( anObject.IsNull() ) {
214         Handle(GEOM_Function) fun = Handle(GEOM_Function)::DownCast(theObjects->Value(i));
215         if ( !fun.IsNull() )
216           anObject = GEOM_BaseObject::GetObject( fun->GetOwnerEntry() );
217       }
218       aLatest = GetCreatedLast(aLatest, anObject);
219     }
220     return aLatest;
221   }
222 }