Back to index

salome-geom  6.5.0
GEOM_PythonDump.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "GEOM_PythonDump.hxx"
00024 
00025 #include <TDF_Tool.hxx>
00026 
00027 #include <TopAbs.hxx>
00028 
00029 #include <TColStd_ListOfInteger.hxx>
00030 #include <TColStd_ListIteratorOfListOfInteger.hxx>
00031 
00032 namespace GEOM
00033 {
00034   size_t TPythonDump::myCounter = 0;
00035 
00036   TPythonDump::TPythonDump (Handle(GEOM_Function)& theFunction, bool theAppend)
00037   {
00038     myFunction = theFunction;
00039     myCounter++;
00040     myAppend = theAppend;
00041   }
00042 
00043   TPythonDump::~TPythonDump()
00044   {
00045     if (--myCounter == 0) {
00046       TCollection_AsciiString aDescr;
00047       if ( myAppend )
00048         aDescr = myFunction->GetDescription() + "\n\t";
00049       std::string aString = myStream.str();
00050       aDescr += (char *)aString.c_str();
00051       myFunction->SetDescription( aDescr );
00052     }
00053   }
00054 
00055   TPythonDump& TPythonDump::operator<< (bool theArg)
00056   {
00057     if (theArg)
00058       myStream << "True";
00059     else
00060       myStream << "False";
00061     return *this;
00062   }
00063 
00064   TPythonDump& TPythonDump::operator<< (long int theArg)
00065   {
00066     myStream<<theArg;
00067     return *this;
00068   }
00069 
00070   TPythonDump& TPythonDump::operator<< (int theArg)
00071   {
00072     myStream<<theArg;
00073     return *this;
00074   }
00075 
00076   TPythonDump& TPythonDump::operator<< (double theArg)
00077   {
00078     myStream.precision(16);
00079     myStream<<theArg;
00080     return *this;
00081   }
00082 
00083   TPythonDump& TPythonDump::operator<< (float theArg)
00084   {
00085     myStream.precision(8);
00086     myStream<<theArg;
00087     return *this;
00088   }
00089 
00090   TPythonDump& TPythonDump::operator<< (const void* theArg)
00091   {
00092     myStream<<theArg;
00093     return *this;
00094   }
00095 
00096   TPythonDump& TPythonDump::operator<< (const char* theArg)
00097   {
00098     myStream<<theArg;
00099     return *this;
00100   }
00101 
00102   TPythonDump& TPythonDump::operator<< (const TopAbs_ShapeEnum theArg)
00103   {
00104     myStream<<"geompy.ShapeType[\"";
00105     TopAbs::Print(theArg, myStream);
00106     myStream<<"\"]";
00107     return *this;
00108   }
00109 
00110   TPythonDump& TPythonDump::operator<< (const Handle(GEOM_Object)& theObject)
00111   {
00112     if (theObject.IsNull()) {
00113       myStream << "None";
00114     } else {
00115       TCollection_AsciiString anEntry;
00116       TDF_Tool::Entry(theObject->GetEntry(), anEntry);
00117       myStream << anEntry.ToCString();
00118     }
00119     return *this;
00120   }
00121 
00122   Handle(GEOM_Object) GetCreatedLast(const Handle(GEOM_Object)& theObj1,
00123                                      const Handle(GEOM_Object)& theObj2)
00124   {
00125     if (theObj1.IsNull()) return theObj2;
00126     if (theObj2.IsNull()) return theObj1;
00127 
00128     TColStd_ListOfInteger aTags1, aTags2;
00129     TDF_Tool::TagList(theObj1->GetEntry(), aTags1);
00130     TDF_Tool::TagList(theObj2->GetEntry(), aTags2);
00131     TColStd_ListIteratorOfListOfInteger aListIter1(aTags1), aListIter2(aTags2);
00132     for (; aListIter1.More(); aListIter1.Next(), aListIter2.Next()) {
00133       if (!aListIter2.More())
00134         return theObj1; // anObj1 is stored under anObj2
00135 
00136       if (aListIter1.Value() > aListIter2.Value())
00137         return theObj1;
00138       else if (aListIter1.Value() < aListIter2.Value())
00139         return theObj2;
00140     }
00141     return theObj1;
00142   }
00143 
00144   Handle(GEOM_Object) GetCreatedLast(const Handle(TColStd_HSequenceOfTransient)& theObjects)
00145   {
00146     Handle(GEOM_Object) anObject, aLatest;
00147     int i, aLen = theObjects->Length();
00148     if (aLen < 1)
00149       return aLatest;
00150 
00151     for (i = 1; i <= aLen; i++) {
00152       anObject = Handle(GEOM_Object)::DownCast(theObjects->Value(i));
00153       aLatest = GetCreatedLast(aLatest, anObject);
00154     }
00155     return aLatest;
00156   }
00157 }