Salome HOME
Merge from V6_main 01/04/2013
[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 : DumpPython
60 //purpose  : 
61 //=======================================================================
62
63 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
64                                          CORBA::Boolean isPublished, 
65                                          CORBA::Boolean isMultiFile,
66                                          CORBA::Boolean& isValidScript)
67 {
68   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
69   if(CORBA::is_nil(aStudy))
70     return new Engines::TMPFile(0);   
71
72   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
73   if(CORBA::is_nil(aSO))
74     return new Engines::TMPFile(0);  
75
76   TObjectData objData;
77   std::vector<TObjectData> objectDataVec;
78
79   TVariablesList aVariableMap;
80
81   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
82   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
83     SALOMEDS::SObject_var aValue = Itr->Value();
84     CORBA::String_var IOR = aValue->GetIOR();
85     if(strlen(IOR.in()) > 0) {
86       CORBA::Object_var obj = _orb->string_to_object(IOR);
87       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
88       if(!CORBA::is_nil(GO)) {
89         CORBA::String_var aName       = aValue->GetName();
90         CORBA::String_var anEntry     = GO->GetEntry();
91         CORBA::String_var aStudyEntry = aValue->GetID();
92         objData._name       = aName.in();
93         objData._entry      = anEntry.in();
94         objData._studyEntry = aStudyEntry.in();
95
96         //Find Drawable Attribute
97         SALOMEDS::GenericAttribute_var aGenAttr;
98         if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
99           SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
100           objData._unpublished = !aDrw->IsDrawable();
101         } else {
102           objData._unpublished = false;
103         }
104
105         objectDataVec.push_back( objData );
106
107         //Find attribute with list of used notebook variables
108         SALOMEDS::GenericAttribute_var anAttr;
109         SALOMEDS::AttributeString_var anAttrStr;
110         if(aValue->FindAttribute(anAttr,"AttributeString")){
111           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
112           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
113           ObjectStates* aStates = new ObjectStates();
114           for(int i = 0; i < aSections->length(); i++) {
115             TState aState;
116             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
117             for(int j = 0; j < aListOfVars.length(); j++) {
118               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
119               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
120               aState.push_back(aVar);
121             }
122             aStates->AddState(aState);
123           }
124           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
125         }
126       }
127     }
128   }
129   
130   TCollection_AsciiString aScript;
131   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
132
133   if (isPublished)
134   {
135
136     SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative", 
137                                                                       ComponentDataType(),
138                                                                       -1);
139     if(!CORBA::is_nil(ap)) {
140       //Add the id parameter of the object
141       std::vector<TObjectData>::iterator it = objectDataVec.begin();
142       for( ;it != objectDataVec.end(); it++ ) {
143       
144         //1. Encode entry
145         if ( (*it)._studyEntry.Length() < 7 ) continue;
146         std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
147         std::string newEntry(ComponentDataType());
148         newEntry+=("_"+tail);
149         
150         CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
151         
152         if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
153           std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
154           SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
155           int oldLenght = aSeq->length();       
156           aSeq->length(oldLenght+2);
157           aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
158           aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
159           ap->SetStrArray( anEntry, aSeq );
160         }        
161       }
162     }
163   
164     //Output the script that sets up the visual parameters.
165     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
166     if (script && strlen(script) > 0) {
167       aScript += "\n\t### Store presentation parameters of displayed objects\n";
168       aScript += script;
169       CORBA::string_free(script);
170     }
171   }
172
173   if( isMultiFile )
174     aScript += "\n\tpass";
175   aScript += "\n";
176
177   if( !isMultiFile ) // remove unnecessary tabulation
178     RemoveTabulation( aScript );
179
180   int aLen = aScript.Length(); 
181   unsigned char* aBuffer = new unsigned char[aLen+1];
182   strcpy((char*)aBuffer, aScript.ToCString());
183
184   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
185   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
186
187   return aStreamFile._retn(); 
188 }
189
190 //=======================================================================
191 //function : GetDumpName
192 //purpose  : 
193 //=======================================================================
194
195 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
196 {
197   const char* name = _impl->GetDumpName( theStudyEntry );
198   if ( name && strlen( name ) > 0 )
199     return strdup( name );
200
201   return NULL;
202 }
203
204 //=======================================================================
205 //function : GetAllDumpNames
206 //purpose  : 
207 //=======================================================================
208
209 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
210 {
211   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
212   int i = 0, aLen = aHSeq->Length();
213
214   GEOM::string_array_var seq = new GEOM::string_array();
215   seq->length(aLen);
216
217   for (; i < aLen; i++) {
218     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
219   }
220
221   return seq._retn();
222 }