]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_DumpPython.cc
Salome HOME
CoTech 32: Action 32.3: In GEOM, the function Import() renamed to ImportFile()
[modules/geom.git] / src / GEOM_I / GEOM_DumpPython.cc
1 //  Copyright (C) 2007-2010  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         objectDataVec.push_back( objData );
96
97         //Find attribute with list of used notebook variables
98         SALOMEDS::GenericAttribute_var anAttr;
99         SALOMEDS::AttributeString_var anAttrStr;
100         if(aValue->FindAttribute(anAttr,"AttributeString")){
101           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
102           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
103           ObjectStates* aStates = new ObjectStates();
104           for(int i = 0; i < aSections->length(); i++) {
105             TState aState;
106             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
107             for(int j = 0; j < aListOfVars.length(); j++) {
108               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
109               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
110               aState.push_back(aVar);
111             }
112             aStates->AddState(aState);
113           }
114           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
115         }
116       }
117     }
118   }
119   
120   TCollection_AsciiString aScript;
121   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
122
123   if (isPublished)
124   {
125     //Output the script that sets up the visual parameters.
126     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
127     if (script && strlen(script) > 0) {
128       aScript += "\n\t### Store presentation parameters of displayed objects\n";
129       aScript += script;
130       CORBA::string_free(script);
131     }
132   }
133
134   if( isMultiFile )
135     aScript += "\n\tpass";
136   aScript += "\n";
137
138   if( !isMultiFile ) // remove unnecessary tabulation
139     RemoveTabulation( aScript );
140
141   int aLen = aScript.Length(); 
142   unsigned char* aBuffer = new unsigned char[aLen+1];
143   strcpy((char*)aBuffer, aScript.ToCString());
144
145   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
146   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
147
148   return aStreamFile._retn(); 
149 }
150
151 //=======================================================================
152 //function : GetDumpName
153 //purpose  : 
154 //=======================================================================
155
156 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
157 {
158   const char* name = _impl->GetDumpName( theStudyEntry );
159   if ( name && strlen( name ) > 0 )
160     return strdup( name );
161
162   return NULL;
163 }
164
165 //=======================================================================
166 //function : GetAllDumpNames
167 //purpose  : 
168 //=======================================================================
169
170 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
171 {
172   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
173   int i = 0, aLen = aHSeq->Length();
174
175   GEOM::string_array_var seq = new GEOM::string_array();
176   seq->length(aLen);
177
178   for (; i < aLen; i++) {
179     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
180   }
181
182   return seq._retn();
183 }