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