Salome HOME
Fix bug 13341: geompy.SubShapeAllSortedIDs() works wrong.
[modules/geom.git] / src / GEOM / GEOM_PythonDump.cxx
1 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 #include "GEOM_PythonDump.hxx"
21
22 #include <TDF_Tool.hxx>
23
24 #include <TopAbs.hxx>
25
26 #include <TColStd_ListOfInteger.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28
29 namespace GEOM
30 {
31   size_t TPythonDump::myCounter = 0;
32
33   TPythonDump::TPythonDump (Handle(GEOM_Function)& theFunction, bool theAppend)
34   {
35     myFunction = theFunction;
36     myCounter++;
37     myAppend = theAppend;
38   }
39
40   TPythonDump::~TPythonDump()
41   {
42     if (--myCounter == 0) {
43       TCollection_AsciiString aDescr;
44       if ( myAppend )
45         aDescr = myFunction->GetDescription() + "\n\t";
46       aDescr += (char *)myStream.str().c_str();
47       myFunction->SetDescription( aDescr );
48     }
49   }
50
51 //  TPythonDump::operator TCollection_AsciiString () const
52 //  {
53 //    if (myCounter == 1) {
54 //      return TCollection_AsciiString ((char *)myStream.str().c_str());
55 //    }
56 //    return TCollection_AsciiString ();
57 //  }
58
59   TPythonDump& TPythonDump::operator<< (long int theArg)
60   {
61     myStream<<theArg;
62     return *this;
63   }
64
65   TPythonDump& TPythonDump::operator<< (int theArg)
66   {
67     myStream<<theArg;
68     return *this;
69   }
70
71   TPythonDump& TPythonDump::operator<< (double theArg)
72   {
73     myStream.precision(16);
74     myStream<<theArg;
75     return *this;
76   }
77
78   TPythonDump& TPythonDump::operator<< (float theArg)
79   {
80     myStream.precision(8);
81     myStream<<theArg;
82     return *this;
83   }
84
85   TPythonDump& TPythonDump::operator<< (const void* theArg)
86   {
87     myStream<<theArg;
88     return *this;
89   }
90
91   TPythonDump& TPythonDump::operator<< (const char* theArg)
92   {
93     myStream<<theArg;
94     return *this;
95   }
96
97   TPythonDump& TPythonDump::operator<< (const TopAbs_ShapeEnum theArg)
98   {
99     myStream<<"geompy.ShapeType[\"";
100     TopAbs::Print(theArg, myStream);
101     myStream<<"\"]";
102     return *this;
103   }
104
105   TPythonDump& TPythonDump::operator<< (const Handle(GEOM_Object)& theObject)
106   {
107     if (theObject.IsNull()) {
108       myStream << "None";
109     } else {
110       TCollection_AsciiString anEntry;
111       TDF_Tool::Entry(theObject->GetEntry(), anEntry);
112       myStream << anEntry.ToCString();
113     }
114     return *this;
115   }
116
117   Handle(GEOM_Object) GetCreatedLast(const Handle(GEOM_Object)& theObj1,
118                                      const Handle(GEOM_Object)& theObj2)
119   {
120     if (theObj1.IsNull()) return theObj2;
121     if (theObj2.IsNull()) return theObj1;
122
123     TColStd_ListOfInteger aTags1, aTags2;
124     TDF_Tool::TagList(theObj1->GetEntry(), aTags1);
125     TDF_Tool::TagList(theObj2->GetEntry(), aTags2);
126     TColStd_ListIteratorOfListOfInteger aListIter1(aTags1), aListIter2(aTags2);
127     for (; aListIter1.More(); aListIter1.Next()) {
128       if (!aListIter2.More())
129         return theObj1; // anObj1 is stored under anObj2
130
131       if (aListIter1.Value() > aListIter2.Value())
132         return theObj1;
133       else if (aListIter1.Value() < aListIter2.Value())
134         return theObj2;
135     }
136     return theObj1;
137   }
138
139   Handle(GEOM_Object) GetCreatedLast(const Handle(TColStd_HSequenceOfTransient)& theObjects)
140   {
141     Handle(GEOM_Object) anObject, aLatest;
142     int i, aLen = theObjects->Length();
143     if (aLen < 1)
144       return aLatest;
145
146     for (i = 1; i <= aLen; i++) {
147       anObject = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
148       aLatest = GetCreatedLast(aLatest, anObject);
149     }
150     return aLatest;
151   }
152 }