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