]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOM_I/GEOM_DumpPython.cc
Salome HOME
Additional fix for the issue "16219: EDF PAL 469: "RemoveFromStudy" Function": 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 : RemoveTabulation
41 //purpose  : 
42 //=======================================================================
43 void RemoveTabulation( TCollection_AsciiString& theScript )
44 {
45   std::string aString( theScript.ToCString() );
46   std::string::size_type aPos = 0;
47   while( aPos < aString.length() )
48   {
49     aPos = aString.find( "\n\t", aPos );
50     if( aPos == std::string::npos )
51       break;
52     aString.replace( aPos, 2, "\n" );
53     aPos++;
54   }
55   theScript = aString.c_str();
56 }
57
58 //=======================================================================
59 //function : DumpPython
60 //purpose  : 
61 //=======================================================================
62
63 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy, 
64                                          CORBA::Boolean isPublished, 
65                                          CORBA::Boolean isMultiFile,
66                                          CORBA::Boolean& isValidScript)
67 {
68   SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
69   if(CORBA::is_nil(aStudy))
70     return new Engines::TMPFile(0);   
71
72   SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
73   if(CORBA::is_nil(aSO))
74     return new Engines::TMPFile(0);  
75
76   TObjectData objData;
77   std::vector<TObjectData> objectDataVec;
78
79   TVariablesList aVariableMap;
80
81   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
82   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
83     SALOMEDS::SObject_var aValue = Itr->Value();
84     CORBA::String_var IOR = aValue->GetIOR();
85     if(strlen(IOR.in()) > 0) {
86       CORBA::Object_var obj = _orb->string_to_object(IOR);
87       GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
88       if(!CORBA::is_nil(GO)) {
89         CORBA::String_var aName       = aValue->GetName();
90         CORBA::String_var anEntry     = GO->GetEntry();
91         CORBA::String_var aStudyEntry = aValue->GetID();
92         objData._name       = aName.in();
93         objData._entry      = anEntry.in();
94         objData._studyEntry = aStudyEntry.in();
95
96         //Find Drawable Attribute
97         SALOMEDS::GenericAttribute_var aGenAttr;
98         if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
99           SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
100           objData._unpublished = !aDrw->IsDrawable();
101         } else {
102           objData._unpublished = false;
103         }
104
105         objectDataVec.push_back( objData );
106
107         //Find attribute with list of used notebook variables
108         SALOMEDS::GenericAttribute_var anAttr;
109         SALOMEDS::AttributeString_var anAttrStr;
110         if(aValue->FindAttribute(anAttr,"AttributeString")){
111           anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
112           SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
113           ObjectStates* aStates = new ObjectStates();
114           for(int i = 0; i < aSections->length(); i++) {
115             TState aState;
116             SALOMEDS::ListOfStrings aListOfVars = aSections[i];
117             for(int j = 0; j < aListOfVars.length(); j++) {
118               bool isVar = aStudy->IsVariable(aListOfVars[j].in());
119               TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
120               aState.push_back(aVar);
121             }
122             aStates->AddState(aState);
123           }
124           aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
125         }
126       }
127     }
128   }
129   
130   TCollection_AsciiString aScript;
131   aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
132
133   if (isPublished)
134   {
135     //Output the script that sets up the visual parameters.
136     char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
137     if (script && strlen(script) > 0) {
138       aScript += "\n\t### Store presentation parameters of displayed objects\n";
139       aScript += script;
140       CORBA::string_free(script);
141     }
142   }
143
144   if( isMultiFile )
145     aScript += "\n\tpass";
146   aScript += "\n";
147
148   if( !isMultiFile ) // remove unnecessary tabulation
149     RemoveTabulation( aScript );
150
151   int aLen = aScript.Length(); 
152   unsigned char* aBuffer = new unsigned char[aLen+1];
153   strcpy((char*)aBuffer, aScript.ToCString());
154
155   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
156   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1); 
157
158   return aStreamFile._retn(); 
159 }
160
161 //=======================================================================
162 //function : GetDumpName
163 //purpose  : 
164 //=======================================================================
165
166 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
167 {
168   const char* name = _impl->GetDumpName( theStudyEntry );
169   if ( name && strlen( name ) > 0 )
170     return strdup( name );
171
172   return NULL;
173 }
174
175 //=======================================================================
176 //function : GetAllDumpNames
177 //purpose  : 
178 //=======================================================================
179
180 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
181 {
182   Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
183   int i = 0, aLen = aHSeq->Length();
184
185   GEOM::string_array_var seq = new GEOM::string_array();
186   seq->length(aLen);
187
188   for (; i < aLen; i++) {
189     seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
190   }
191
192   return seq._retn();
193 }