]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_DumpPython.cc
Salome HOME
25ffab45b60db6d5175f600ee9b53a299830af51
[modules/geom.git] / src / GEOM_I / GEOM_DumpPython.cc
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #ifdef WNT
21 #pragma warning( disable:4786 )
22 #endif
23
24 #include <Standard_Stream.hxx>
25
26 #include "GEOM_Gen_i.hh"
27
28 //#include <GEOM_Engine.hxx>
29 #include <TCollection_AsciiString.hxx>
30 #include <TCollection_ExtendedString.hxx>
31 #include <TColStd_HSequenceOfAsciiString.hxx>
32 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
33
34 #include <vector>
35 #include <string>
36
37 //=======================================================================
38 //function : DumpPython
39 //purpose  : 
40 //=======================================================================
41
42 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
43                                          CORBA::Boolean isPublished, 
44                                          CORBA::Boolean& isValidScript)
45 {
46   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
47   if(CORBA::is_nil(aStudy))
48     return new Engines::TMPFile(0);   
49
50   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
51   if(CORBA::is_nil(aSO))
52     return new Engines::TMPFile(0);  
53
54   Resource_DataMapOfAsciiStringAsciiString aMap;
55
56   TVariablesList aVariableMap;
57
58   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
59   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
60     SALOMEDS::SObject_var aValue = Itr->Value();
61     CORBA::String_var IOR = aValue->GetIOR();
62     if(strlen(IOR.in()) > 0) {
63       CORBA::Object_var obj = _orb->string_to_object(IOR);
64       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
65       if(!CORBA::is_nil(GO)) {
66         CORBA::String_var aName = aValue->GetName();
67         CORBA::String_var anEntry = GO->GetEntry();
68         aMap.Bind( (char*)anEntry.in(), (char*)aName.in() );
69
70         //Find attribute with list of used notebook variables
71         SALOMEDS::GenericAttribute_var anAttr;
72         SALOMEDS::AttributeString_var anAttrStr;
73         vector<TVariable> aVariables;
74         if(aValue->FindAttribute(anAttr,"AttributeString")){
75           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
76           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
77           for(int i = 0; i < aSections->length(); i++) {
78             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
79             for(int j = 0; j < aListOfVars.length(); j++) {
80               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
81               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
82               aVariables.push_back(aVar);
83             }
84           }
85         }
86         aVariableMap.insert(pair<TCollection_AsciiString,vector<TVariable> >((char*)anEntry,aVariables));
87       }
88     }
89   }
90   
91   TCollection_AsciiString aScript =
92     "### This file is generated by SALOME automatically by dump python functionality\n"
93       "### of GEOM component\n\n";
94   aScript += _impl->DumpPython(aStudy->StudyId(), aMap, aVariableMap, isPublished, isValidScript);
95
96   if (isPublished)
97   {
98     //Output the script that sets up the visual parameters.
99     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
100     if (script && strlen(script) > 0) {
101       aScript += "\n\t### Store presentation parameters of displayed objects\n";
102       aScript += script;
103       CORBA::string_free(script);
104     }
105   }
106
107   aScript += "\n\tpass\n";
108
109   int aLen = aScript.Length(); 
110   unsigned char* aBuffer = new unsigned char[aLen+1];
111   strcpy((char*)aBuffer, aScript.ToCString());
112
113   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
114   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
115
116   return aStreamFile._retn(); 
117 }
118
119 //=======================================================================
120 //function : GetDumpName
121 //purpose  : 
122 //=======================================================================
123
124 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
125 {
126   const char* name = _impl->GetDumpName( theStudyEntry );
127   if ( name && strlen( name ) > 0 )
128     return strdup( name );
129
130   return NULL;
131 }
132
133 //=======================================================================
134 //function : GetAllDumpNames
135 //purpose  : 
136 //=======================================================================
137
138 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
139 {
140   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
141   int i = 0, aLen = aHSeq->Length();
142
143   GEOM::string_array_var seq = new GEOM::string_array();
144   seq->length(aLen);
145
146   for (; i < aLen; i++) {
147     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
148   }
149
150   return seq._retn();
151 }