Salome HOME
IMPs 21044, 21057, 21067
[modules/geom.git] / src / GEOM / GEOM_PythonDump.cxx
1 //  Copyright (C) 2007-2010  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.
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 #include "GEOM_PythonDump.hxx"
23
24 #include <TDF_Tool.hxx>
25
26 #include <TopAbs.hxx>
27
28 #include <TColStd_ListOfInteger.hxx>
29 #include <TColStd_ListIteratorOfListOfInteger.hxx>
30
31 namespace GEOM
32 {
33   size_t TPythonDump::myCounter = 0;
34
35   TPythonDump::TPythonDump (Handle(GEOM_Function)& theFunction, bool theAppend)
36   {
37     myFunction = theFunction;
38     myCounter++;
39     myAppend = theAppend;
40   }
41
42   TPythonDump::~TPythonDump()
43   {
44     if (--myCounter == 0) {
45       TCollection_AsciiString aDescr;
46       if ( myAppend )
47         aDescr = myFunction->GetDescription() + "\n\t";
48       std::string aString = myStream.str();
49       aDescr += (char *)aString.c_str();
50       myFunction->SetDescription( aDescr );
51     }
52   }
53
54   TPythonDump& TPythonDump::operator<< (bool theArg)
55   {
56     if (theArg)
57       myStream << "True";
58     else
59       myStream << "False";
60     return *this;
61   }
62
63   TPythonDump& TPythonDump::operator<< (long int theArg)
64   {
65     myStream<<theArg;
66     return *this;
67   }
68
69   TPythonDump& TPythonDump::operator<< (int theArg)
70   {
71     myStream<<theArg;
72     return *this;
73   }
74
75   TPythonDump& TPythonDump::operator<< (double theArg)
76   {
77     myStream.precision(16);
78     myStream<<theArg;
79     return *this;
80   }
81
82   TPythonDump& TPythonDump::operator<< (float theArg)
83   {
84     myStream.precision(8);
85     myStream<<theArg;
86     return *this;
87   }
88
89   TPythonDump& TPythonDump::operator<< (const void* theArg)
90   {
91     myStream<<theArg;
92     return *this;
93   }
94
95   TPythonDump& TPythonDump::operator<< (const char* theArg)
96   {
97     myStream<<theArg;
98     return *this;
99   }
100
101   TPythonDump& TPythonDump::operator<< (const TopAbs_ShapeEnum theArg)
102   {
103     myStream<<"geompy.ShapeType[\"";
104     TopAbs::Print(theArg, myStream);
105     myStream<<"\"]";
106     return *this;
107   }
108
109   TPythonDump& TPythonDump::operator<< (const Handle(GEOM_Object)& theObject)
110   {
111     if (theObject.IsNull()) {
112       myStream << "None";
113     } else {
114       TCollection_AsciiString anEntry;
115       TDF_Tool::Entry(theObject->GetEntry(), anEntry);
116       myStream << anEntry.ToCString();
117     }
118     return *this;
119   }
120
121   Handle(GEOM_Object) GetCreatedLast(const Handle(GEOM_Object)& theObj1,
122                                      const Handle(GEOM_Object)& theObj2)
123   {
124     if (theObj1.IsNull()) return theObj2;
125     if (theObj2.IsNull()) return theObj1;
126
127     TColStd_ListOfInteger aTags1, aTags2;
128     TDF_Tool::TagList(theObj1->GetEntry(), aTags1);
129     TDF_Tool::TagList(theObj2->GetEntry(), aTags2);
130     TColStd_ListIteratorOfListOfInteger aListIter1(aTags1), aListIter2(aTags2);
131     for (; aListIter1.More(); aListIter1.Next(), aListIter2.Next()) {
132       if (!aListIter2.More())
133         return theObj1; // anObj1 is stored under anObj2
134
135       if (aListIter1.Value() > aListIter2.Value())
136         return theObj1;
137       else if (aListIter1.Value() < aListIter2.Value())
138         return theObj2;
139     }
140     return theObj1;
141   }
142
143   Handle(GEOM_Object) GetCreatedLast(const Handle(TColStd_HSequenceOfTransient)& theObjects)
144   {
145     Handle(GEOM_Object) anObject, aLatest;
146     int i, aLen = theObjects->Length();
147     if (aLen < 1)
148       return aLatest;
149
150     for (i = 1; i <= aLen; i++) {
151       anObject = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
152       aLatest = GetCreatedLast(aLatest, anObject);
153     }
154     return aLatest;
155   }
156 }