Salome HOME
0021122: EDF 1735 SMESH: Bug when saving in .hdf
[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 : DumpPython
41 //purpose  : 
42 //=======================================================================
43
44 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
45                                          CORBA::Boolean isPublished, 
46                                          CORBA::Boolean& isValidScript)
47 {
48   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
49   if(CORBA::is_nil(aStudy))
50     return new Engines::TMPFile(0);   
51
52   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
53   if(CORBA::is_nil(aSO))
54     return new Engines::TMPFile(0);  
55
56   TObjectData objData;
57   std::vector<TObjectData> objectDataVec;
58
59   TVariablesList aVariableMap;
60
61   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
62   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
63     SALOMEDS::SObject_var aValue = Itr->Value();
64     CORBA::String_var IOR = aValue->GetIOR();
65     if(strlen(IOR.in()) > 0) {
66       CORBA::Object_var obj = _orb->string_to_object(IOR);
67       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
68       if(!CORBA::is_nil(GO)) {
69         CORBA::String_var aName       = aValue->GetName();
70         CORBA::String_var anEntry     = GO->GetEntry();
71         CORBA::String_var aStudyEntry = aValue->GetID();
72         objData._name       = aName.in();
73         objData._entry      = anEntry.in();
74         objData._studyEntry = aStudyEntry.in();
75         objectDataVec.push_back( objData );
76
77         //Find attribute with list of used notebook variables
78         SALOMEDS::GenericAttribute_var anAttr;
79         SALOMEDS::AttributeString_var anAttrStr;
80         if(aValue->FindAttribute(anAttr,"AttributeString")){
81           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
82           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
83           ObjectStates* aStates = new ObjectStates();
84           for(int i = 0; i < aSections->length(); i++) {
85             TState aState;
86             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
87             for(int j = 0; j < aListOfVars.length(); j++) {
88               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
89               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
90               aState.push_back(aVar);
91             }
92             aStates->AddState(aState);
93           }
94           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
95         }
96       }
97     }
98   }
99   
100   TCollection_AsciiString aScript;
101   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isValidScript);
102
103   if (isPublished)
104   {
105     //Output the script that sets up the visual parameters.
106     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
107     if (script && strlen(script) > 0) {
108       aScript += "\n\t### Store presentation parameters of displayed objects\n";
109       aScript += script;
110       CORBA::string_free(script);
111     }
112   }
113
114   aScript += "\n\tpass\n";
115
116   int aLen = aScript.Length(); 
117   unsigned char* aBuffer = new unsigned char[aLen+1];
118   strcpy((char*)aBuffer, aScript.ToCString());
119
120   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
121   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
122
123   return aStreamFile._retn(); 
124 }
125
126 //=======================================================================
127 //function : GetDumpName
128 //purpose  : 
129 //=======================================================================
130
131 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
132 {
133   const char* name = _impl->GetDumpName( theStudyEntry );
134   if ( name && strlen( name ) > 0 )
135     return strdup( name );
136
137   return NULL;
138 }
139
140 //=======================================================================
141 //function : GetAllDumpNames
142 //purpose  : 
143 //=======================================================================
144
145 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
146 {
147   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
148   int i = 0, aLen = aHSeq->Length();
149
150   GEOM::string_array_var seq = new GEOM::string_array();
151   seq->length(aLen);
152
153   for (; i < aLen; i++) {
154     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
155   }
156
157   return seq._retn();
158 }