Salome HOME
Dump Python extension.
[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 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_ExtendedString.hxx>
29 #include <TColStd_HSequenceOfAsciiString.hxx>
30 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
31
32 //=======================================================================
33 //function : DumpPython
34 //purpose  : 
35 //=======================================================================
36
37 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
38                                          CORBA::Boolean isPublished, 
39                                          CORBA::Boolean& isValidScript)
40 {
41   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
42   if(CORBA::is_nil(aStudy))
43     return new Engines::TMPFile(0);   
44
45   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
46   if(CORBA::is_nil(aSO))
47     return new Engines::TMPFile(0);  
48
49   Resource_DataMapOfAsciiStringAsciiString aMap, aVariableMap;
50
51   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
52   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
53     SALOMEDS::SObject_var aValue = Itr->Value();
54     CORBA::String_var IOR = aValue->GetIOR();
55     if(strlen(IOR.in()) > 0) {
56       CORBA::Object_var obj = _orb->string_to_object(IOR);
57       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
58       if(!CORBA::is_nil(GO)) {
59         CORBA::String_var aName = aValue->GetName();
60         CORBA::String_var anEntry = GO->GetEntry();
61         aMap.Bind( (char*)anEntry.in(), (char*)aName.in() );
62
63         //Find attribute with list of used notebook variables
64         SALOMEDS::GenericAttribute_var anAttr;
65         SALOMEDS::AttributeString_var anAttrStr;
66         TCollection_AsciiString aParameters;
67         if(aValue->FindAttribute(anAttr,"AttributeString")){
68           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
69           aParameters = TCollection_AsciiString(anAttrStr->Value());
70         }
71         aVariableMap.Bind((char*)anEntry.in(),aParameters);
72       }
73     }
74   }
75
76   //Get Study variables
77   SALOMEDS::ListOfStrings_var aListOfVars = aStudy->GetVariableNames();
78   TColStd_HSequenceOfAsciiString aStudyVariables;
79   for(int i = 0;i < aListOfVars->length();i++)
80     {
81       aStudyVariables.Append(aListOfVars[i].in());
82     }
83   
84   TCollection_AsciiString aScript =
85     "### This file is generated by SALOME automatically by dump python functionality\n"
86       "### of GEOM component\n\n";
87   aScript += _impl->DumpPython(aStudy->StudyId(), aMap, aVariableMap, aStudyVariables, isPublished, isValidScript);
88
89   if (isPublished)
90   {
91     //Output the script that sets up the visual parameters.
92     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
93     if (script && strlen(script) > 0) {
94       aScript += "\n\t### Store presentation parameters of displayed objects\n";
95       aScript += script;
96       CORBA::string_free(script);
97     }
98   }
99
100   aScript += "\n\tpass\n";
101
102   int aLen = aScript.Length(); 
103   unsigned char* aBuffer = new unsigned char[aLen+1];
104   strcpy((char*)aBuffer, aScript.ToCString());
105
106   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
107   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
108
109   return aStreamFile._retn(); 
110 }
111
112 //=======================================================================
113 //function : GetDumpName
114 //purpose  : 
115 //=======================================================================
116
117 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
118 {
119   const char* name = _impl->GetDumpName( theStudyEntry );
120   if ( name && strlen( name ) > 0 )
121     return strdup( name );
122
123   return NULL;
124 }
125
126 //=======================================================================
127 //function : GetAllDumpNames
128 //purpose  : 
129 //=======================================================================
130
131 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
132 {
133   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
134   int i = 0, aLen = aHSeq->Length();
135
136   GEOM::string_array_var seq = new GEOM::string_array();
137   seq->length(aLen);
138
139   for (; i < aLen; i++) {
140     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
141   }
142
143   return seq._retn();
144 }