Salome HOME
bd1890b251a00b12cf027223b78f7df056b89d45
[modules/geom.git] / src / GEOM_I / GEOM_DumpPython.cc
1 // Copyright (C) 2007-2020  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, or (at your option) any later version.
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 #ifdef WIN32
24 #pragma warning( disable:4786 )
25 #endif
26
27 #include <Standard_Stream.hxx>
28
29 #include "GEOM_Gen_i.hh"
30
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
33 #include <TColStd_HSequenceOfAsciiString.hxx>
34 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
35
36 #include <vector>
37 #include <string>
38
39 //=======================================================================
40 //function : RemoveTabulation
41 //purpose  : 
42 //=======================================================================
43 void RemoveTabulation( TCollection_AsciiString& theScript )
44 {
45   std::string aString( theScript.ToCString() );
46   std::string::size_type aPos = 0;
47   while( aPos < aString.length() )
48   {
49     aPos = aString.find( "\n\t", aPos );
50     if( aPos == std::string::npos )
51       break;
52     aString.replace( aPos, 2, "\n" );
53     aPos++;
54   }
55   theScript = aString.c_str();
56 }
57
58 //=======================================================================
59 //function : ConvertV6toV7
60 //purpose  : dump elements are stored when study is saved,
61 //           and kept when study is reloaded.
62 //           When a study from an older version is loaded,
63 //           the dump must be post processed in case of syntax modification.
64 //           In V7.2, geompy.GEOM --> GEOM
65 //=======================================================================
66 void ConvertV6toV7( TCollection_AsciiString& theScript )
67 {
68   std::string aString( theScript.ToCString() );
69   std::string::size_type aPos = 0;
70   while( aPos < aString.length() )
71   {
72     aPos = aString.find( "geompy.GEOM", aPos );
73     if( aPos == std::string::npos )
74       break;
75     aString.replace( aPos, 11, "GEOM" );
76     aPos++;
77   }
78   theScript = aString.c_str();
79 }
80
81 //=======================================================================
82 //function : DumpPython
83 //purpose  : 
84 //=======================================================================
85
86 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Boolean isPublished,
87                                          CORBA::Boolean isMultiFile,
88                                          CORBA::Boolean& isValidScript)
89 {
90   SALOMEDS::Study_var aStudy = getStudyServant();
91   if(CORBA::is_nil(aStudy))
92     return new Engines::TMPFile(0);   
93
94   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
95   if(CORBA::is_nil(aSO))
96     return new Engines::TMPFile(0);  
97
98   TObjectData objData;
99   std::vector<TObjectData> objectDataVec;
100
101   TVariablesList aVariableMap;
102
103   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
104   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
105     SALOMEDS::SObject_var aValue = Itr->Value();
106     CORBA::String_var IOR = aValue->GetIOR();
107     if(strlen(IOR.in()) > 0) {
108       CORBA::Object_var obj = _orb->string_to_object(IOR);
109       GEOM::GEOM_BaseObject_var GO = GEOM::GEOM_BaseObject::_narrow(obj);
110       if(!CORBA::is_nil(GO)) {
111         CORBA::String_var aName       = aValue->GetName();
112         CORBA::String_var anEntry     = GO->GetEntry();
113         CORBA::String_var aStudyEntry = aValue->GetID();
114         objData._name       = aName.in();
115         objData._entry      = anEntry.in();
116         objData._studyEntry = aStudyEntry.in();
117
118         //Find Drawable Attribute
119         SALOMEDS::GenericAttribute_var aGenAttr;
120         if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
121           SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
122           objData._unpublished = !aDrw->IsDrawable();
123         } else {
124           objData._unpublished = false;
125         }
126
127         objectDataVec.push_back( objData );
128
129         //Find attribute with list of used notebook variables
130         SALOMEDS::GenericAttribute_var anAttr;
131         SALOMEDS::AttributeString_var anAttrStr;
132         if(aValue->FindAttribute(anAttr,"AttributeString")){
133           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
134           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
135           ObjectStates* aStates = new ObjectStates();
136           for( CORBA::ULong i = 0; i < aSections->length(); i++) {
137             TState aState;
138             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
139             for( CORBA::ULong j = 0; j < aListOfVars.length(); j++) {
140               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
141               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
142               aState.push_back(aVar);
143             }
144             aStates->AddState(aState);
145           }
146           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
147         }
148       }
149     }
150   }
151   
152   TCollection_AsciiString aScript;
153   aScript += _impl->DumpPython(objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
154
155   if (isPublished)
156   {
157
158     SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative", 
159                                                                       ComponentDataType(),
160                                                                       -1);
161     if(!CORBA::is_nil(ap)) {
162       //Add the id parameter of the object
163       std::vector<TObjectData>::iterator it = objectDataVec.begin();
164       for( ;it != objectDataVec.end(); it++ ) {
165       
166         //1. Encode entry
167         if ( (*it)._studyEntry.Length() < 7 ) continue;
168         std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
169         std::string newEntry(ComponentDataType());
170         newEntry+=("_"+tail);
171         
172         CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
173         
174         if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
175           if ( GetDumpName((*it)._studyEntry.ToCString()) == NULL ) 
176             continue;
177           std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
178           SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
179           int oldLenght = aSeq->length();       
180           aSeq->length(oldLenght+2);
181           aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
182           aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
183           ap->SetStrArray( anEntry, aSeq );
184         }        
185       }
186     }
187
188     TCollection_AsciiString aDirScript, aNodeName, aNodeEntry, aFatherName, aFatherEntry;
189     Resource_DataMapOfAsciiStringAsciiString aNameToEntry;
190     SALOMEDS::UseCaseBuilder_var useCaseBuilder = aStudy->GetUseCaseBuilder();
191     SALOMEDS::UseCaseIterator_var Itr = useCaseBuilder->GetUseCaseIterator(aSO);
192     SALOMEDS::SObject_var aNodeSO;
193     SALOMEDS::SObject_var aFatherSO;
194     SALOMEDS::GenericAttribute_var anIDAttr;
195     for(Itr->Init(true); Itr->More(); Itr->Next()) {
196       aFatherName.Clear();
197       aFatherEntry.Clear();
198       aNodeSO = Itr->Value();
199       // get father info
200       aFatherSO = useCaseBuilder->GetFather( aNodeSO );
201       if ( aFatherSO->FindAttribute(anIDAttr, "AttributeLocalID") ) {
202         SALOMEDS::AttributeLocalID_var aLocalID = SALOMEDS::AttributeLocalID::_narrow(anIDAttr);
203         if ( aLocalID->Value() == 999 ) {
204           aFatherName = aFatherSO->GetName();
205           aFatherEntry = aFatherSO->GetID();
206           _impl->healPyName( aFatherName, aFatherEntry, aNameToEntry);
207         }
208         aLocalID->UnRegister();
209       }
210       // get node info
211       if ( aNodeSO->FindAttribute(anIDAttr, "AttributeLocalID") ) {
212         SALOMEDS::AttributeLocalID_var aLocalID = SALOMEDS::AttributeLocalID::_narrow(anIDAttr);
213         if ( aLocalID->Value() == 999 ) {
214           // the node is folder
215           aNodeName = aNodeSO->GetName();
216           aNodeEntry = aNodeSO->GetID();
217           _impl->healPyName( aNodeName, aNodeEntry, aNameToEntry);
218           aDirScript += aNodeName;
219           aDirScript += " = geompy.NewFolder('";
220           aDirScript += aNodeSO->GetName();
221           aDirScript += "'";
222           if ( !aFatherName.IsEmpty() && !aFatherEntry.IsEmpty() ) {
223             // the folder takes place under another folder
224             aDirScript += ", ";
225             aDirScript += aFatherName;
226           }
227           aDirScript += ")\n";
228           aNameToEntry.Bind( aNodeName, aNodeEntry );
229         }
230         aLocalID->UnRegister();
231       } else if ( !aFatherName.IsEmpty() && !aFatherEntry.IsEmpty() ) {
232         // the node is Geom object under folder
233         aDirScript += "geompy.PutToFolder(";
234         if ( GetDumpName( aNodeSO->GetID() ) == NULL )
235           continue;
236         aDirScript += GetDumpName( aNodeSO->GetID() );
237         aDirScript += ", ";
238         aDirScript += aFatherName;
239         aDirScript += ")\n";
240       }
241     }
242     if ( !aDirScript.IsEmpty() )
243     {
244       aScript += "\n\t### Folders and it's content\n";
245       aScript += aDirScript;
246     }
247     //Output the script that sets up the visual parameters.
248     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
249     if (script && strlen(script) > 0) {
250       aScript += "\n\t### Store presentation parameters of displayed objects\n";
251       aScript += script;
252       CORBA::string_free(script);
253     }
254   }
255
256   if( isMultiFile )
257     aScript += "\n\tpass";
258   aScript += "\n";
259
260   if( !isMultiFile ) // remove unnecessary tabulation
261     RemoveTabulation( aScript );
262
263   ConvertV6toV7( aScript ); //convert scripts related to studies saved in SALOME V6 and older
264   
265   int aLen = aScript.Length(); 
266   unsigned char* aBuffer = new unsigned char[aLen+1];
267   strcpy((char*)aBuffer, aScript.ToCString());
268
269   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
270   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
271
272   return aStreamFile._retn(); 
273 }
274
275 //=======================================================================
276 //function : GetDumpName
277 //purpose  : 
278 //=======================================================================
279
280 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
281 {
282   const char* name = _impl->GetDumpName( theStudyEntry );
283   if ( name && strlen( name ) > 0 )
284     return CORBA::string_dup( name );
285
286   return NULL;
287 }
288
289 //=======================================================================
290 //function : GetAllDumpNames
291 //purpose  : 
292 //=======================================================================
293
294 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
295 {
296   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
297   int i = 0, aLen = aHSeq->Length();
298
299   GEOM::string_array_var seq = new GEOM::string_array();
300   seq->length(aLen);
301
302   for (; i < aLen; i++) {
303     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
304   }
305
306   return seq._retn();
307 }