Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/geom.git] / src / GEOM_I / GEOM_DumpPython.cc
1 //  Copyright (C) 2007-2008  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 #ifdef WNT
23 #pragma warning( disable:4786 )
24 #endif
25
26 #include <Standard_Stream.hxx>
27
28 #include "GEOM_Gen_i.hh"
29
30 #include <TCollection_AsciiString.hxx>
31 #include <TCollection_ExtendedString.hxx>
32 #include <TColStd_HSequenceOfAsciiString.hxx>
33 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
34
35 #include <vector>
36 #include <string>
37
38 //=======================================================================
39 //function : DumpPython
40 //purpose  : 
41 //=======================================================================
42
43 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
44                                          CORBA::Boolean isPublished, 
45                                          CORBA::Boolean& isValidScript)
46 {
47   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
48   if(CORBA::is_nil(aStudy))
49     return new Engines::TMPFile(0);   
50
51   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
52   if(CORBA::is_nil(aSO))
53     return new Engines::TMPFile(0);  
54
55   Resource_DataMapOfAsciiStringAsciiString aMap;
56
57   TVariablesList aVariableMap;
58
59   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
60   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
61     SALOMEDS::SObject_var aValue = Itr->Value();
62     CORBA::String_var IOR = aValue->GetIOR();
63     if(strlen(IOR.in()) > 0) {
64       CORBA::Object_var obj = _orb->string_to_object(IOR);
65       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
66       if(!CORBA::is_nil(GO)) {
67         CORBA::String_var aName = aValue->GetName();
68         CORBA::String_var anEntry = GO->GetEntry();
69         aMap.Bind( (char*)anEntry.in(), (char*)aName.in() );
70
71         //Find attribute with list of used notebook variables
72         SALOMEDS::GenericAttribute_var anAttr;
73         SALOMEDS::AttributeString_var anAttrStr;
74         if(aValue->FindAttribute(anAttr,"AttributeString")){
75           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
76           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
77           ObjectStates* aStates = new ObjectStates();
78           for(int i = 0; i < aSections->length(); i++) {
79             TState aState;
80             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
81             for(int j = 0; j < aListOfVars.length(); j++) {
82               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
83               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
84               aState.push_back(aVar);
85             }
86             aStates->AddState(aState);
87           }
88           aVariableMap.insert(pair<TCollection_AsciiString,ObjectStates*>(TCollection_AsciiString(anEntry),aStates));
89         }
90       }
91     }
92   }
93   
94   TCollection_AsciiString aScript =
95     "# -*- coding: iso-8859-1 -*-\n"
96     "### This file is generated by SALOME automatically by dump python functionality\n"
97       "### of GEOM component\n\n";
98   aScript += _impl->DumpPython(aStudy->StudyId(), aMap, aVariableMap, isPublished, isValidScript);
99
100   if (isPublished)
101   {
102     //Output the script that sets up the visual parameters.
103     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
104     if (script && strlen(script) > 0) {
105       aScript += "\n\t### Store presentation parameters of displayed objects\n";
106       aScript += script;
107       CORBA::string_free(script);
108     }
109   }
110
111   aScript += "\n\tpass\n";
112
113   int aLen = aScript.Length(); 
114   unsigned char* aBuffer = new unsigned char[aLen+1];
115   strcpy((char*)aBuffer, aScript.ToCString());
116
117   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
118   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
119
120   return aStreamFile._retn(); 
121 }
122
123 //=======================================================================
124 //function : GetDumpName
125 //purpose  : 
126 //=======================================================================
127
128 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
129 {
130   const char* name = _impl->GetDumpName( theStudyEntry );
131   if ( name && strlen( name ) > 0 )
132     return strdup( name );
133
134   return NULL;
135 }
136
137 //=======================================================================
138 //function : GetAllDumpNames
139 //purpose  : 
140 //=======================================================================
141
142 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
143 {
144   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
145   int i = 0, aLen = aHSeq->Length();
146
147   GEOM::string_array_var seq = new GEOM::string_array();
148   seq->length(aLen);
149
150   for (; i < aLen; i++) {
151     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
152   }
153
154   return seq._retn();
155 }