geom/src/GEOM_I/GEOM_DumpPython.cc

223 lines
7.5 KiB
C++
Raw Normal View History

2013-04-01 18:25:01 +06:00
// Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2009-02-13 17:16:39 +05:00
//
2012-08-09 13:58:02 +06:00
// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
2005-12-05 21:23:52 +05:00
//
2012-08-09 13:58:02 +06:00
2005-08-11 10:14:22 +06:00
#ifdef WNT
#pragma warning( disable:4786 )
#endif
#include <Standard_Stream.hxx>
2005-06-02 15:34:45 +06:00
#include "GEOM_Gen_i.hh"
2009-02-13 17:16:39 +05:00
2005-06-02 15:34:45 +06:00
#include <TCollection_AsciiString.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TColStd_HSequenceOfAsciiString.hxx>
#include <Resource_DataMapOfAsciiStringAsciiString.hxx>
2009-02-13 17:16:39 +05:00
#include <vector>
#include <string>
2012-08-09 13:58:02 +06:00
//=======================================================================
//function : RemoveTabulation
//purpose :
//=======================================================================
void RemoveTabulation( TCollection_AsciiString& theScript )
{
std::string aString( theScript.ToCString() );
std::string::size_type aPos = 0;
while( aPos < aString.length() )
{
aPos = aString.find( "\n\t", aPos );
if( aPos == std::string::npos )
break;
aString.replace( aPos, 2, "\n" );
aPos++;
}
theScript = aString.c_str();
}
2005-06-02 15:34:45 +06:00
//=======================================================================
//function : DumpPython
//purpose :
//=======================================================================
Engines::TMPFile* GEOM_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
CORBA::Boolean isPublished,
2012-08-09 13:58:02 +06:00
CORBA::Boolean isMultiFile,
CORBA::Boolean& isValidScript)
2005-06-02 15:34:45 +06:00
{
SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
if(CORBA::is_nil(aStudy))
return new Engines::TMPFile(0);
SALOMEDS::SObject_var aSO = aStudy->FindComponent(ComponentDataType());
if(CORBA::is_nil(aSO))
return new Engines::TMPFile(0);
2012-08-09 13:58:02 +06:00
TObjectData objData;
std::vector<TObjectData> objectDataVec;
2005-06-02 15:34:45 +06:00
2009-02-13 17:16:39 +05:00
TVariablesList aVariableMap;
2005-06-02 15:34:45 +06:00
SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
SALOMEDS::SObject_var aValue = Itr->Value();
CORBA::String_var IOR = aValue->GetIOR();
if(strlen(IOR.in()) > 0) {
CORBA::Object_var obj = _orb->string_to_object(IOR);
GEOM::GEOM_Object_var GO = GEOM::GEOM_Object::_narrow(obj);
if(!CORBA::is_nil(GO)) {
2012-08-09 13:58:02 +06:00
CORBA::String_var aName = aValue->GetName();
CORBA::String_var anEntry = GO->GetEntry();
CORBA::String_var aStudyEntry = aValue->GetID();
objData._name = aName.in();
objData._entry = anEntry.in();
objData._studyEntry = aStudyEntry.in();
//Find Drawable Attribute
SALOMEDS::GenericAttribute_var aGenAttr;
if(aValue->FindAttribute(aGenAttr, "AttributeDrawable") ) {
SALOMEDS::AttributeDrawable_var aDrw = SALOMEDS::AttributeDrawable::_narrow(aGenAttr);
objData._unpublished = !aDrw->IsDrawable();
} else {
objData._unpublished = false;
}
objectDataVec.push_back( objData );
2009-02-13 17:16:39 +05:00
//Find attribute with list of used notebook variables
SALOMEDS::GenericAttribute_var anAttr;
SALOMEDS::AttributeString_var anAttrStr;
if(aValue->FindAttribute(anAttr,"AttributeString")){
anAttrStr = SALOMEDS::AttributeString::_narrow(anAttr);
SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(anAttrStr->Value());
ObjectStates* aStates = new ObjectStates();
for(int i = 0; i < aSections->length(); i++) {
TState aState;
SALOMEDS::ListOfStrings aListOfVars = aSections[i];
for(int j = 0; j < aListOfVars.length(); j++) {
bool isVar = aStudy->IsVariable(aListOfVars[j].in());
TVariable aVar = TVariable( (char*)aListOfVars[j].in(), isVar );
aState.push_back(aVar);
}
aStates->AddState(aState);
}
2012-08-09 13:58:02 +06:00
aVariableMap.insert(std::make_pair(TCollection_AsciiString(anEntry),aStates));
2009-02-13 17:16:39 +05:00
}
2005-06-02 15:34:45 +06:00
}
}
}
2009-02-13 17:16:39 +05:00
2012-08-09 13:58:02 +06:00
TCollection_AsciiString aScript;
aScript += _impl->DumpPython(aStudy->StudyId(), objectDataVec, aVariableMap, isPublished, isMultiFile, isValidScript);
2009-02-13 17:16:39 +05:00
if (isPublished)
{
2012-08-09 13:58:02 +06:00
SALOMEDS::AttributeParameter_var ap = aStudy->GetModuleParameters("Interface Applicative",
ComponentDataType(),
-1);
if(!CORBA::is_nil(ap)) {
//Add the id parameter of the object
std::vector<TObjectData>::iterator it = objectDataVec.begin();
for( ;it != objectDataVec.end(); it++ ) {
//1. Encode entry
if ( (*it)._studyEntry.Length() < 7 ) continue;
std::string tail( (*it)._studyEntry.ToCString(), 6, (*it)._studyEntry.Length()-1 );
std::string newEntry(ComponentDataType());
newEntry+=("_"+tail);
CORBA::String_var anEntry = CORBA::string_dup(newEntry.c_str());
if( ap->IsSet(anEntry, 6) ) { //6 Means string array, see SALOMEDS_Attributes.idl AttributeParameter interface
std::string idCommand = std::string("geompy.getObjectID(") + GetDumpName((*it)._studyEntry.ToCString()) + std::string(")");
SALOMEDS::StringSeq_var aSeq= ap->GetStrArray(anEntry);
int oldLenght = aSeq->length();
aSeq->length(oldLenght+2);
aSeq[oldLenght] = CORBA::string_dup("_PT_OBJECT_ID_");
aSeq[oldLenght + 1] = CORBA::string_dup(idCommand.c_str());
ap->SetStrArray( anEntry, aSeq );
}
}
}
2009-02-13 17:16:39 +05:00
//Output the script that sets up the visual parameters.
char* script = aStudy->GetDefaultScript(ComponentDataType(), "\t");
if (script && strlen(script) > 0) {
aScript += "\n\t### Store presentation parameters of displayed objects\n";
aScript += script;
CORBA::string_free(script);
}
}
2012-08-09 13:58:02 +06:00
if( isMultiFile )
aScript += "\n\tpass";
aScript += "\n";
if( !isMultiFile ) // remove unnecessary tabulation
RemoveTabulation( aScript );
2005-06-02 15:34:45 +06:00
int aLen = aScript.Length();
unsigned char* aBuffer = new unsigned char[aLen+1];
strcpy((char*)aBuffer, aScript.ToCString());
CORBA::Octet* anOctetBuf = (CORBA::Octet*)aBuffer;
Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
return aStreamFile._retn();
}
//=======================================================================
//function : GetDumpName
//purpose :
//=======================================================================
char* GEOM_Gen_i::GetDumpName (const char* theStudyEntry)
{
const char* name = _impl->GetDumpName( theStudyEntry );
if ( name && strlen( name ) > 0 )
return strdup( name );
return NULL;
}
//=======================================================================
//function : GetAllDumpNames
//purpose :
//=======================================================================
GEOM::string_array* GEOM_Gen_i::GetAllDumpNames()
{
Handle(TColStd_HSequenceOfAsciiString) aHSeq = _impl->GetAllDumpNames();
int i = 0, aLen = aHSeq->Length();
GEOM::string_array_var seq = new GEOM::string_array();
seq->length(aLen);
for (; i < aLen; i++) {
seq[i] = CORBA::string_dup(aHSeq->Value(i + 1).ToCString());
}
return seq._retn();
}