1 // Copyright (C) 2007-2011 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);
136 SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative",
139 if(!CORBA::is_nil(ap)) {
140 //Add the id parameter of the object
141 std::vector<TObjectData>::iterator it = objectDataVec.begin();
142 for( ;it != objectDataVec.end(); it++ ) {
145 if ( (*it)._studyEntry.Length() < 7 ) continue;
146 std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
147 std::string newEntry(ComponentDataType());
148 newEntry+=("_"+tail);
150 CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
152 if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
153 std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
154 SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
155 int oldLenght = aSeq->length();
156 aSeq->length(oldLenght+2);
157 aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
158 aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
159 ap->SetStrArray( anEntry, aSeq );
164 //Output the script that sets up the visual parameters.
165 char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
166 if (script && strlen(script) > 0) {
167 aScript += "\n\t### Store presentation parameters of displayed objects\n";
169 CORBA::string_free(script);
174 aScript += "\n\tpass";
177 if( !isMultiFile ) // remove unnecessary tabulation
178 RemoveTabulation( aScript );
180 int aLen = aScript.Length();
181 unsigned char* aBuffer = new unsigned char[aLen+1];
182 strcpy((char*)aBuffer, aScript.ToCString());
184 CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
185 Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
187 return aStreamFile._retn();
190 //=======================================================================
191 //function : GetDumpName
193 //=======================================================================
195 char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
197 const char* name = _impl->GetDumpName( theStudyEntry );
198 if ( name && strlen( name ) > 0 )
199 return strdup( name );
204 //=======================================================================
205 //function : GetAllDumpNames
207 //=======================================================================
209 GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
211 Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
212 int i = 0, aLen = aHSeq->Length();
214 GEOM::string_array_var seq = new GEOM::string_array();
217 for (; i < aLen; i++) {
218 seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());