Back to index

salome-geom  6.5.0
GEOM_DumpPython.cc
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 #ifdef WNT
00024 #pragma warning( disable:4786 )
00025 #endif
00026 
00027 #include <Standard_Stream.hxx>
00028 
00029 #include "GEOM_Gen_i.hh"
00030 
00031 #include <TCollection_AsciiString.hxx>
00032 #include <TCollection_ExtendedString.hxx>
00033 #include <TColStd_HSequenceOfAsciiString.hxx>
00034 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
00035 
00036 #include <vector>
00037 #include <string>
00038 
00039 //=======================================================================
00040 //function : RemoveTabulation
00041 //purpose  : 
00042 //=======================================================================
00043 void RemoveTabulation( TCollection_AsciiString& theScript )
00044 {
00045   std::string aString( theScript.ToCString() );
00046   std::string::size_type aPos = 0;
00047   while( aPos < aString.length() )
00048   {
00049     aPos = aString.find( "\n\t", aPos );
00050     if( aPos == std::string::npos )
00051       break;
00052     aString.replace( aPos, 2, "\n" );
00053     aPos++;
00054   }
00055   theScript = aString.c_str();
00056 }
00057 
00058 //=======================================================================
00059 //function : DumpPython
00060 //purpose  : 
00061 //=======================================================================
00062 
00063 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
00064                                     CORBA::Boolean isPublished, 
00065                                     CORBA::Boolean isMultiFile,
00066                                          CORBA::Boolean& isValidScript)
00067 {
00068   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
00069   if(CORBA::is_nil(aStudy))
00070     return new Engines::TMPFile(0);   
00071 
00072   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
00073   if(CORBA::is_nil(aSO))
00074     return new Engines::TMPFile(0);  
00075 
00076   TObjectData objData;
00077   std::vector<TObjectData> objectDataVec;
00078 
00079   TVariablesList aVariableMap;
00080 
00081   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
00082   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
00083     SALOMEDS::SObject_var aValue = Itr->Value();
00084     CORBA::String_var IOR = aValue->GetIOR();
00085     if(strlen(IOR.in()) > 0) {
00086       CORBA::Object_var obj = _orb->string_to_object(IOR);
00087       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
00088       if(!CORBA::is_nil(GO)) {
00089         CORBA::String_var aName       = aValue->GetName();
00090         CORBA::String_var anEntry     = GO->GetEntry();
00091         CORBA::String_var aStudyEntry = aValue->GetID();
00092         objData._name       = aName.in();
00093         objData._entry      = anEntry.in();
00094         objData._studyEntry = aStudyEntry.in();
00095 
00096        //Find Drawable Attribute
00097        SALOMEDS::GenericAttribute_var aGenAttr;
00098        if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
00099          SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
00100          objData._unpublished = !aDrw->IsDrawable();
00101        } else {
00102          objData._unpublished = false;
00103        }
00104 
00105        objectDataVec.push_back( objData );
00106 
00107        //Find attribute with list of used notebook variables
00108        SALOMEDS::GenericAttribute_var anAttr;
00109        SALOMEDS::AttributeString_var anAttrStr;
00110        if(aValue->FindAttribute(anAttr,"AttributeString")){
00111          anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
00112          SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
00113          ObjectStates* aStates = new ObjectStates();
00114          for(int i = 0; i < aSections->length(); i++) {
00115            TState aState;
00116            SALOMEDS::ListOfStrings aListOfVars = aSections[i];
00117            for(int j = 0; j < aListOfVars.length(); j++) {
00118              bool isVar = aStudy->IsVariable(aListOfVars[j].in());
00119              TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
00120              aState.push_back(aVar);
00121            }
00122            aStates->AddState(aState);
00123          }
00124          aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
00125        }
00126       }
00127     }
00128   }
00129   
00130   TCollection_AsciiString aScript;
00131   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
00132 
00133   if (isPublished)
00134   {
00135 
00136     SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative", 
00137                                                               ComponentDataType(),
00138                                                               -1);
00139     if(!CORBA::is_nil(ap)) {
00140       //Add the id parameter of the object
00141       std::vector<TObjectData>::iterator it = objectDataVec.begin();
00142       for( ;it != objectDataVec.end(); it++ ) {
00143       
00144        //1. Encode entry
00145         if ( (*it)._studyEntry.Length() < 7 ) continue;
00146        std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
00147        std::string newEntry(ComponentDataType());
00148        newEntry+=("_"+tail);
00149        
00150        CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
00151        
00152        if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
00153          std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
00154          SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
00155          int oldLenght = aSeq->length();  
00156          aSeq->length(oldLenght+2);
00157          aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
00158          aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
00159          ap->SetStrArray( anEntry, aSeq );
00160        }       
00161       }
00162     }
00163   
00164     //Output the script that sets up the visual parameters.
00165     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
00166     if (script && strlen(script) > 0) {
00167       aScript += "\n\t### Store presentation parameters of displayed objects\n";
00168       aScript += script;
00169       CORBA::string_free(script);
00170     }
00171   }
00172 
00173   if( isMultiFile )
00174     aScript += "\n\tpass";
00175   aScript += "\n";
00176 
00177   if( !isMultiFile ) // remove unnecessary tabulation
00178     RemoveTabulation( aScript );
00179 
00180   int aLen = aScript.Length(); 
00181   unsigned char* aBuffer = new unsigned char[aLen+1];
00182   strcpy((char*)aBuffer, aScript.ToCString());
00183 
00184   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
00185   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
00186 
00187   return aStreamFile._retn(); 
00188 }
00189 
00190 //=======================================================================
00191 //function : GetDumpName
00192 //purpose  : 
00193 //=======================================================================
00194 
00195 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
00196 {
00197   const char* name = _impl->GetDumpName( theStudyEntry );
00198   if ( name && strlen( name ) > 0 )
00199     return strdup( name );
00200 
00201   return NULL;
00202 }
00203 
00204 //=======================================================================
00205 //function : GetAllDumpNames
00206 //purpose  : 
00207 //=======================================================================
00208 
00209 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
00210 {
00211   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
00212   int i = 0, aLen = aHSeq->Length();
00213 
00214   GEOM::string_array_var seq = new GEOM::string_array();
00215   seq->length(aLen);
00216 
00217   for (; i < aLen; i++) {
00218     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
00219   }
00220 
00221   return seq._retn();
00222 }