Salome HOME
ILMAB project. DS for the Field on Geometry implemented
[modules/geom.git] / src / GEOM_I / GEOM_DumpPython.cc
1 // Copyright (C) 2007-2013  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 #ifdef WNT
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::Object_ptr theStudy, 
87                                          CORBA::Boolean isPublished, 
88                                          CORBA::Boolean isMultiFile,
89                                          CORBA::Boolean& isValidScript)
90 {
91   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
92   if(CORBA::is_nil(aStudy))
93     return new Engines::TMPFile(0);   
94
95   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
96   if(CORBA::is_nil(aSO))
97     return new Engines::TMPFile(0);  
98
99   TObjectData objData;
100   std::vector<TObjectData> objectDataVec;
101
102   TVariablesList aVariableMap;
103
104   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
105   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
106     SALOMEDS::SObject_var aValue = Itr->Value();
107     CORBA::String_var IOR = aValue->GetIOR();
108     if(strlen(IOR.in()) > 0) {
109       CORBA::Object_var obj = _orb->string_to_object(IOR);
110       GEOM::GEOM_BaseObject_var GO = GEOM::GEOM_BaseObject::_narrow(obj);
111       if(!CORBA::is_nil(GO)) {
112         CORBA::String_var aName       = aValue->GetName();
113         CORBA::String_var anEntry     = GO->GetEntry();
114         CORBA::String_var aStudyEntry = aValue->GetID();
115         objData._name       = aName.in();
116         objData._entry      = anEntry.in();
117         objData._studyEntry = aStudyEntry.in();
118
119         //Find Drawable Attribute
120         SALOMEDS::GenericAttribute_var aGenAttr;
121         if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
122           SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
123           objData._unpublished = !aDrw->IsDrawable();
124         } else {
125           objData._unpublished = false;
126         }
127
128         objectDataVec.push_back( objData );
129
130         //Find attribute with list of used notebook variables
131         SALOMEDS::GenericAttribute_var anAttr;
132         SALOMEDS::AttributeString_var anAttrStr;
133         if(aValue->FindAttribute(anAttr,"AttributeString")){
134           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
135           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
136           ObjectStates* aStates = new ObjectStates();
137           for(int i = 0; i < aSections->length(); i++) {
138             TState aState;
139             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
140             for(int j = 0; j < aListOfVars.length(); j++) {
141               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
142               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
143               aState.push_back(aVar);
144             }
145             aStates->AddState(aState);
146           }
147           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
148         }
149       }
150     }
151   }
152   
153   TCollection_AsciiString aScript;
154   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
155
156   if (isPublished)
157   {
158
159     SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative", 
160                                                                       ComponentDataType(),
161                                                                       -1);
162     if(!CORBA::is_nil(ap)) {
163       //Add the id parameter of the object
164       std::vector<TObjectData>::iterator it = objectDataVec.begin();
165       for( ;it != objectDataVec.end(); it++ ) {
166       
167         //1. Encode entry
168         if ( (*it)._studyEntry.Length() < 7 ) continue;
169         std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
170         std::string newEntry(ComponentDataType());
171         newEntry+=("_"+tail);
172         
173         CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
174         
175         if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
176           std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
177           SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
178           int oldLenght = aSeq->length();       
179           aSeq->length(oldLenght+2);
180           aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
181           aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
182           ap->SetStrArray( anEntry, aSeq );
183         }        
184       }
185     }
186
187     TCollection_AsciiString aDirScript, aNodeName, aNodeEntry, aFatherName, aFatherEntry;
188     Resource_DataMapOfAsciiStringAsciiString aNameToEntry;
189     SALOMEDS::UseCaseBuilder_var useCaseBuilder = aStudy->GetUseCaseBuilder();
190     SALOMEDS::UseCaseIterator_var Itr = useCaseBuilder->GetUseCaseIterator(aSO);
191     SALOMEDS::SObject_var aNodeSO;
192     SALOMEDS::SObject_var aFatherSO;
193     SALOMEDS::GenericAttribute_var anIDAttr;
194     for(Itr->Init(true); Itr->More(); Itr->Next()) {
195       aFatherName.Clear();
196       aFatherEntry.Clear();
197       aNodeSO = Itr->Value();
198       // get father info
199       aFatherSO = useCaseBuilder->GetFather( aNodeSO );
200       if ( aFatherSO->FindAttribute(anIDAttr, "AttributeLocalID") ) {
201         SALOMEDS::AttributeLocalID_var aLocalID = SALOMEDS::AttributeLocalID::_narrow(anIDAttr);
202         if ( aLocalID->Value() == 999 ) {
203           aFatherName = aFatherSO->GetName();
204           aFatherEntry = aFatherSO->GetID();
205           _impl->healPyName( aFatherName, aFatherEntry, aNameToEntry);
206         }
207         aLocalID->UnRegister();
208       }
209       // get node info
210       if ( aNodeSO->FindAttribute(anIDAttr, "AttributeLocalID") ) {
211         SALOMEDS::AttributeLocalID_var aLocalID = SALOMEDS::AttributeLocalID::_narrow(anIDAttr);
212         if ( aLocalID->Value() == 999 ) {
213           // the node is folder
214           aNodeName = aNodeSO->GetName();
215           aNodeEntry = aNodeSO->GetID();
216           _impl->healPyName( aNodeName, aNodeEntry, aNameToEntry);
217           aDirScript += aNodeName;
218           aDirScript += " = geompy.NewFolder('";
219           aDirScript += aNodeSO->GetName();
220           aDirScript += "'";
221           if ( !aFatherName.IsEmpty() && !aFatherEntry.IsEmpty() ) {
222             // the folder takes place under another folder
223             aDirScript += ", ";
224             aDirScript += aFatherName;
225           }
226           aDirScript += ")\n";
227           aNameToEntry.Bind( aNodeName, aNodeEntry );
228         }
229         aLocalID->UnRegister();
230       } else if ( !aFatherName.IsEmpty() && !aFatherEntry.IsEmpty() ) {
231         // the node is Geom object under folder
232         aDirScript += "geompy.PutToFolder(";
233         aDirScript += GetDumpName( aNodeSO->GetID() );
234         aDirScript += ", ";
235         aDirScript += aFatherName;
236         aDirScript += ")\n";
237       }
238     }
239     if ( !aDirScript.IsEmpty() )
240     {
241       aScript += "\n\t### Folders and it's content\n";
242       aScript += aDirScript;
243     }
244     //Output the script that sets up the visual parameters.
245     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
246     if (script && strlen(script) > 0) {
247       aScript += "\n\t### Store presentation parameters of displayed objects\n";
248       aScript += script;
249       CORBA::string_free(script);
250     }
251   }
252
253   if( isMultiFile )
254     aScript += "\n\tpass";
255   aScript += "\n";
256
257   if( !isMultiFile ) // remove unnecessary tabulation
258     RemoveTabulation( aScript );
259
260   ConvertV6toV7( aScript ); //convert scripts related to studies saved in SALOME V6 and older
261   
262   int aLen = aScript.Length(); 
263   unsigned char* aBuffer = new unsigned char[aLen+1];
264   strcpy((char*)aBuffer, aScript.ToCString());
265
266   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
267   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
268
269   return aStreamFile._retn(); 
270 }
271
272 //=======================================================================
273 //function : GetDumpName
274 //purpose  : 
275 //=======================================================================
276
277 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
278 {
279   const char* name = _impl->GetDumpName( theStudyEntry );
280   if ( name && strlen( name ) > 0 )
281     return strdup( name );
282
283   return NULL;
284 }
285
286 //=======================================================================
287 //function : GetAllDumpNames
288 //purpose  : 
289 //=======================================================================
290
291 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
292 {
293   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
294   int i = 0, aLen = aHSeq->Length();
295
296   GEOM::string_array_var seq = new GEOM::string_array();
297   seq->length(aLen);
298
299   for (; i < aLen; i++) {
300     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
301   }
302
303   return seq._retn();
304 }