1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 #pragma warning( disable:4786 )
27 #include <Standard_Stream.hxx>
29 #include "GEOM_Gen_i.hh"
31 #include <TCollection_AsciiString.hxx>
32 #include <TCollection_ExtendedString.hxx>
33 #include <TColStd_HSequenceOfAsciiString.hxx>
34 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
39 //=======================================================================
40 //function : RemoveTabulation
42 //=======================================================================
43 void RemoveTabulation( TCollection_AsciiString& theScript )
45 std::string aString( theScript.ToCString() );
46 std::string::size_type aPos = 0;
47 while( aPos < aString.length() )
49 aPos = aString.find( "\n\t", aPos );
50 if( aPos == std::string::npos )
52 aString.replace( aPos, 2, "\n" );
55 theScript = aString.c_str();
58 //=======================================================================
59 //function : DumpPython
61 //=======================================================================
63 Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
64 CORBA::Boolean isPublished,
65 CORBA::Boolean isMultiFile,
66 CORBA::Boolean& isValidScript)
68 SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
69 if(CORBA::is_nil(aStudy))
70 return new Engines::TMPFile(0);
72 SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
73 if(CORBA::is_nil(aSO))
74 return new Engines::TMPFile(0);
77 std::vector<TObjectData> objectDataVec;
79 TVariablesList aVariableMap;
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();
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();
102 objData._unpublished = false;
105 objectDataVec.push_back( objData );
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++) {
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);
122 aStates->AddState(aState);
124 aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
130 TCollection_AsciiString aScript;
131 aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
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";
140 CORBA::string_free(script);
145 aScript += "\n\tpass";
148 if( !isMultiFile ) // remove unnecessary tabulation
149 RemoveTabulation( aScript );
151 int aLen = aScript.Length();
152 unsigned char* aBuffer = new unsigned char[aLen+1];
153 strcpy((char*)aBuffer, aScript.ToCString());
155 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
156 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
158 return aStreamFile._retn();
161 //=======================================================================
162 //function : GetDumpName
164 //=======================================================================
166 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
168 const char* name = _impl->GetDumpName( theStudyEntry );
169 if ( name && strlen( name ) > 0 )
170 return strdup( name );
175 //=======================================================================
176 //function : GetAllDumpNames
178 //=======================================================================
180 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
182 Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
183 int i = 0, aLen = aHSeq->Length();
185 GEOM::string_array_var seq = new GEOM::string_array();
188 for (; i < aLen; i++) {
189 seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());