X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FSMESH_I%2FSMESH_PythonDump.cxx;h=798b2a3fe722aee0164ab51881443b8a6177884f;hb=bba2ceaf86d0a48fb0b256801edeb2418b0f93ce;hp=0052d85e26edb48ec58b1fb78253ed7dbd730e79;hpb=10191484fe88a27e962b8e4b57e09d390d8705c7;p=modules%2Fsmesh.git diff --git a/src/SMESH_I/SMESH_PythonDump.cxx b/src/SMESH_I/SMESH_PythonDump.cxx index 0052d85e2..798b2a3fe 100644 --- a/src/SMESH_I/SMESH_PythonDump.cxx +++ b/src/SMESH_I/SMESH_PythonDump.cxx @@ -1,4 +1,4 @@ -// Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE +// Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS @@ -32,19 +32,20 @@ #include "SMESH_Gen_i.hxx" #include "SMESH_MeshEditor_i.hxx" +#include +#include #include #include -#include #include #include -#ifdef _DEBUG_ -static int MYDEBUG = 0; -#else -static int MYDEBUG = 0; +#if OCC_VERSION_LARGE < 0x07050000 +#include #endif +#include + #include "SMESH_TryCatch.hxx" namespace SMESH @@ -53,17 +54,22 @@ namespace SMESH size_t TPythonDump::myCounter = 0; const char theNotPublishedObjectName[] = "__NOT__Published__Object__"; - TVar::TVar(CORBA::Double value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } - TVar::TVar(CORBA::Long value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } - TVar::TVar(CORBA::Short value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } + TVar::TVar(CORBA::Double value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } + TVar::TVar(CORBA::Long value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } + TVar::TVar(CORBA::LongLong value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } + TVar::TVar(CORBA::Short value):myVals(1), myIsList(false) { myVals[0] = SMESH_Comment(value); } TVar::TVar(const SMESH::double_array& value):myVals(value.length()), myIsList(true) { for ( size_t i = 0; i < value.length(); i++) myVals[i] = SMESH_Comment(value[i]); } + + TPythonDump::TPythonDump():myVarsCounter(0),mySmesh(SMESH_Gen_i::GetSMESHGen()) + { + ++myCounter; + } - TPythonDump:: - TPythonDump():myVarsCounter(0) + TPythonDump::TPythonDump(SMESH_Gen_i *smesh):myVarsCounter(0),mySmesh(smesh) { ++myCounter; } @@ -76,11 +82,11 @@ namespace SMESH TCollection_AsciiString aCollection(Standard_CString(aString.c_str())); if(!aCollection.IsEmpty()) { - const std::string & objEntry = SMESH_Gen_i::GetSMESHGen()->GetLastObjEntry(); + const std::string & objEntry = aSMESHGen->GetLastObjEntry(); if ( !objEntry.empty() ) aCollection += (TVar::ObjPrefix() + objEntry ).c_str(); aSMESHGen->AddToPythonScript(aCollection); - if(MYDEBUG) MESSAGE(aString); + MESSAGE(aString); // prevent misuse of already treated variables aSMESHGen->UpdateParameters(CORBA::Object_var().in(),""); } @@ -132,6 +138,13 @@ namespace SMESH return *this; } + TPythonDump& + TPythonDump:: + operator<<(long long theArg){ + myStream< - void DumpArray(const TArray& theArray, TPythonDump & theStream) + TPythonDump& + TPythonDump::operator<<(const SMESH::long_array& theArg) { - if ( theArray.length() == 0 ) - { - theStream << "[]"; - } - else - { - theStream << "[ "; - for (CORBA::ULong i = 1; i <= theArray.length(); i++) { - theStream << theArray[i-1]; - if ( i < theArray.length() ) - theStream << ", "; - } - theStream << " ]"; - } + DumpArray( theArg, *this ); + return *this; } TPythonDump& - TPythonDump::operator<<(const SMESH::long_array& theArg) + TPythonDump::operator<<(const SMESH::smIdType_array& theArg) { DumpArray( theArg, *this ); return *this; @@ -316,7 +317,7 @@ namespace SMESH operator<<(CORBA::Object_ptr theArg) { SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen(); - SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(theArg); + SALOMEDS::SObject_wrap aSObject = mySmesh->ObjectToSObject(theArg); if(!aSObject->_is_nil()) { CORBA::String_var id = aSObject->GetID(); myStream << id; @@ -335,7 +336,7 @@ namespace SMESH TPythonDump:: operator<<(SMESH::SMESH_Hypothesis_ptr theArg) { - SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(theArg); + SALOMEDS::SObject_wrap aSObject = mySmesh->ObjectToSObject(theArg); if(aSObject->_is_nil() && !CORBA::is_nil(theArg)) myStream << "hyp_" << theArg->GetId(); else @@ -349,8 +350,7 @@ namespace SMESH { if ( CORBA::is_nil( theArg ) ) return *this << "None"; - SMESH_Gen_i* aSMESHGen = SMESH_Gen_i::GetSMESHGen(); - SALOMEDS::SObject_wrap aSObject = SMESH_Gen_i::ObjectToSObject(theArg); + SALOMEDS::SObject_wrap aSObject = mySmesh->ObjectToSObject(theArg); if(!aSObject->_is_nil()) { return *this << aSObject; @@ -361,11 +361,12 @@ namespace SMESH } if ( SMESH_MeshEditor_i::IsTemporaryIDSource( theArg )) { - SMESH::SMESH_Mesh_var mesh = theArg->GetMesh(); - SMESH::long_array_var anElementsId = theArg->GetIDs(); - SMESH::array_of_ElementType_var types = theArg->GetTypes(); - SMESH::ElementType type = types->length() ? types[0] : SMESH::ALL; - SALOMEDS::SObject_wrap meshSO = SMESH_Gen_i::ObjectToSObject(mesh); + SMESH::SMESH_Mesh_var mesh = theArg->GetMesh(); + SMESH::smIdType_array_var anElementsId = theArg->GetIDs(); + SMESH::array_of_ElementType_var types = theArg->GetTypes(); + SMESH::ElementType type = types->length() ? types[0] : SMESH::ALL; + SALOMEDS::SObject_wrap meshSO = mySmesh->ObjectToSObject(mesh); + if ( meshSO->_is_nil() ) // don't waste memory for dumping not published objects return *this << mesh << ".GetIDSource([], " << type << ")"; else @@ -384,7 +385,7 @@ namespace SMESH TPythonDump& TPythonDump:: - operator<<(SMESH::FilterManager_i* theArg) + operator<<(SMESH::FilterManager_i* /*theArg*/) { myStream<<"aFilterManager"; return *this; @@ -427,6 +428,7 @@ namespace SMESH case FT_MultiConnection2D: myStream<< "aMultiConnection2D"; break; case FT_Length: myStream<< "aLength"; break; case FT_Length2D: myStream<< "aLength2D"; break; + case FT_Length3D: myStream<< "aLength3D"; break; case FT_Deflection2D: myStream<< "aDeflection2D"; break; case FT_NodeConnectivityNumber:myStream<< "aNodeConnectivityNumber";break; case FT_BelongToMeshGroup: myStream<< "aBelongToMeshGroup"; break; @@ -464,13 +466,13 @@ namespace SMESH TPythonDump& TPythonDump:: - operator<<(SMESH::Measurements_i* theArg) + operator<<(SMESH::Measurements_i* /*theArg*/) { myStream<<"aMeasurements"; return *this; } - TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* theArg) + TPythonDump& TPythonDump:: operator<<(SMESH_Gen_i* /*theArg*/) { myStream << SMESHGenName(); return *this; } @@ -541,6 +543,16 @@ namespace SMESH DumpArray( theList, *this ); return *this; } + TPythonDump& TPythonDump::operator<<(const SMESH::submesh_array& theList) + { + DumpArray( theList, *this ); + return *this; + } + TPythonDump& TPythonDump::operator<<(const SMESH::ListOfHypothesis& theList) + { + DumpArray( theList, *this ); + return *this; + } TPythonDump& TPythonDump::operator<<(const SMESH::CoincidentFreeBorders& theCFB) { // dump CoincidentFreeBorders as a list of lists, each enclosed list @@ -675,39 +687,41 @@ namespace SMESH void printException( const char* text ) { -#ifdef _DEBUG_ - cout << "Exception in SMESH_Gen_i::DumpPython(): " << text << endl; -#endif + if (SALOME::VerbosityActivated()) + std::cout << "Exception in SMESH_Gen_i::DumpPython(): " << text << std::endl; } -} //======================================================================= //function : RemoveTabulation //purpose : //======================================================================= -void RemoveTabulation( TCollection_AsciiString& theScript ) -{ - std::string aString( theScript.ToCString() ); - std::string::size_type aPos = 0; - while( aPos < aString.length() ) + void RemoveTabulation( TCollection_AsciiString& theScript ) { - aPos = aString.find( "\n\t", aPos ); - if( aPos == std::string::npos ) - break; - aString.replace( aPos, 2, "\n" ); - aPos++; + 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(); } - theScript = aString.c_str(); } //======================================================================= //function : DumpPython //purpose : //======================================================================= -Engines::TMPFile* SMESH_Gen_i::DumpPython (CORBA::Boolean isPublished, - CORBA::Boolean isMultiFile, +Engines::TMPFile* SMESH_Gen_i::DumpPython( CORBA::Boolean isPublished, + CORBA::Boolean isMultiFile, CORBA::Boolean& isValidScript) { + // localizing + Kernel_Utils::Localizer loc; + SALOMEDS::Study_var aStudy = getStudyServant(); if (CORBA::is_nil(aStudy)) return new Engines::TMPFile(0); @@ -963,7 +977,7 @@ namespace { //================================================================================ /*! - * \brief Createa a Dump Python script + * \brief Creates a Dump Python script * \param [in,out] theObjectNames - map of an entry to a study and python name * \param [in] theNames - - map of an entry to a study name * \param [in] isPublished - \c true if dump of object publication in study is needed @@ -992,14 +1006,16 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl const TCollection_AsciiString anOldGen ( SMESH::TPythonDump::SMESHGenName() ); const TCollection_AsciiString helper; // to comfortably append C strings to TCollection_AsciiString const TCollection_AsciiString tab( isMultiFile ? "\t" : "" ), nt = helper + "\n" + tab; - + TCollection_AsciiString optionalComment; + std::list< TCollection_AsciiString > lines; // lines of a script std::list< TCollection_AsciiString >::iterator linesIt; + lines.push_back( aSMESHGen + " = smeshBuilder.New()" ); if ( isPublished ) - lines.push_back( aSMESHGen + " = smeshBuilder.New()" ); - else - lines.push_back( aSMESHGen + " = smeshBuilder.New(False)" ); + optionalComment = "#"; + lines.push_back( optionalComment + aSMESHGen + ".SetEnablePublish( False ) # Set to False to avoid publish in study if not needed or in some particular situations:" ); + lines.push_back( " # multiples meshes built in parallel, complex and numerous mesh edition (performance)\n" ); lines.push_back( helper + "aFilterManager = " + aSMESHGen + ".CreateFilterManager()" ); lines.push_back( helper + "aMeasurements = " + aSMESHGen + ".CreateMeasurements()" ); @@ -1064,12 +1080,15 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl isHistoricalDump ); bool importGeom = false; - GEOM::GEOM_Gen_ptr geom = GetGeomEngine(); + GEOM::GEOM_Gen_ptr geom[2]; + for ( int isShaper = 0; isShaper < 2; ++isShaper ) { + geom[ isShaper ] = GetGeomEngine( isShaper ); + if ( CORBA::is_nil( geom[ isShaper ])) + continue; // Add names of GEOM objects to theObjectNames to exclude same names of SMESH objects - GEOM::string_array_var aGeomNames = geom->GetAllDumpNames(); - int ign = 0, nbgn = aGeomNames->length(); - for (; ign < nbgn; ign++) { + GEOM::string_array_var aGeomNames = geom[ isShaper ]->GetAllDumpNames(); + for ( CORBA::ULong ign = 0; ign < aGeomNames->length(); ign++) { TCollection_AsciiString aName = aGeomNames[ign].in(); theObjectNames.Bind(aName, "1"); } @@ -1078,10 +1097,15 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl TCollection_AsciiString anUpdatedScript; Resource_DataMapOfAsciiStringAsciiString mapRemoved; - Resource_DataMapOfAsciiStringAsciiString mapEntries; // names and entries present in anUpdatedScript + Resource_DataMapOfAsciiStringAsciiString mapEntries; // { entry: name } present in anUpdatedScript Standard_Integer objectCounter = 0; TCollection_AsciiString anEntry, aName, aGUIName, aBaseName("smeshObj_"); + std::string compDataType = ComponentDataType(); // SMESH module's data type + SALOMEDS::SComponent_var smeshSO = getStudyServant()->FindComponent( compDataType.c_str() ); + CORBA::String_var smeshID = smeshSO->GetID(); + TCollection_AsciiString smeshEntry = smeshID.in(); + // Treat every script line and add it to anUpdatedScript for ( linesIt = lines.begin(); linesIt != lines.end(); ++linesIt ) { @@ -1103,7 +1127,11 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl anUpdatedScript += aLine.SubString( aStart, aSeq->Value(i) - 1 ); // line part before i-th entry anEntry = aLine.SubString( aSeq->Value(i), aSeq->Value(i + 1) ); // is a GEOM object? - CORBA::String_var geomName = geom->GetDumpName( anEntry.ToCString() ); + CORBA::String_var geomName; + if ( !CORBA::is_nil( geom[0] )) + geomName = geom[0]->GetDumpName( anEntry.ToCString() ); + if (( !geomName.in() || !geomName.in()[0] ) && !CORBA::is_nil( geom[1] )) + geomName = geom[1]->GetDumpName( anEntry.ToCString() ); if ( !geomName.in() || !geomName.in()[0] ) { // is a SMESH object if ( theObjectNames.IsBound( anEntry )) { @@ -1129,15 +1157,22 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl } else { - // Removed Object - do { - aName = aBaseName + (++objectCounter); - } while (theObjectNames.IsBound(aName)); + if ( !anEntry.StartsWith( smeshEntry )) // not SMESH object + { + aName = SMESH::TPythonDump::NotPublishedObjectName(); + } + else + { + // Removed Object + do { + aName = aBaseName + (++objectCounter); + } while (theObjectNames.IsBound(aName)); - if ( !aRemovedObjIDs.count( anEntry ) && aLine.Value(1) != '#') - mapRemoved.Bind(anEntry, aName); + if ( !aRemovedObjIDs.count( anEntry ) && aLine.Value(1) != '#') + mapRemoved.Bind(anEntry, aName); - theObjectNames.Bind(anEntry, aName); + theObjectNames.Bind(anEntry, aName); + } } theObjectNames.Bind(aName, anEntry); // to detect same name of diff objects } @@ -1177,26 +1212,28 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl } // import python files corresponding to plugins if they are used in anUpdatedScript { - TCollection_AsciiString importStr; + //TCollection_AsciiString importStr; std::vector pluginNames = getPluginNames(); for ( size_t i = 0; i < pluginNames.size(); ++i ) { // Convert access to plugin members: - // e.g. StdMeshers.QUAD_REDUCED -> StdMeshersBuilder.QUAD_REDUCED + // e.g. StdMeshers.QUAD_REDUCED -> smeshBuilder.QUAD_REDUCED TCollection_AsciiString pluginAccess = (pluginNames[i] + ".").c_str() ; int iFrom = 1, iPos; while (( iPos = anUpdatedScript.Location( pluginAccess, iFrom, anUpdatedScript.Length() ))) { - anUpdatedScript.Insert( iPos + pluginNames[i].size(), "Builder" ); - iFrom = iPos + pluginNames[i].size() + 8; + //anUpdatedScript.Insert( iPos + pluginNames[i].size(), "Builder" ); + anUpdatedScript.Remove( iPos, pluginNames[i].size() ); + anUpdatedScript.Insert( iPos, "smeshBuilder" ); + iFrom = iPos - pluginNames[i].size() + 12; } // if any plugin member is used, import the plugin - if ( iFrom > 1 ) - importStr += ( helper + "\n" "from salome." + pluginNames[i].c_str() + - " import " + pluginNames[i].c_str() +"Builder" ); + // if ( iFrom > 1 ) + // importStr += ( helper + "\n" "from salome." + pluginNames[i].c_str() + + // " import " + pluginNames[i].c_str() +"Builder" ); } - if ( !importStr.IsEmpty() ) - initPart += importStr + "\n"; + // if ( !importStr.IsEmpty() ) + // initPart += importStr + "\n"; } if ( isMultiFile ) @@ -1302,7 +1339,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl { // find the function name int functBeg = posAlready; - char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()" + char* script = (char*) anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def function()" while ( *script != ' ' ) { script--; functBeg--; @@ -1382,3 +1419,55 @@ void SMESH_Gen_i::CleanPythonTrace() myPythonScript->Clear(); } } + +//================================================================================ +/*! + * \brief Count inclusions of a string in a raw Python dump script + */ +//================================================================================ + +int SMESH_Gen_i::CountInPyDump(const TCollection_AsciiString& theText) +{ + int count = 0; + + SALOMEDS::Study_var aStudy = getStudyServant(); + if ( CORBA::is_nil( aStudy )) + return count; + + SMESH_Gen_i* me = GetSMESHGen(); + CORBA::String_var compDataType = me->ComponentDataType(); + SALOMEDS::SObject_wrap aSO = aStudy->FindComponent( compDataType.in() ); + if ( CORBA::is_nil( aSO )) + return count; + + // Trace saved in the study + SALOMEDS::GenericAttribute_wrap attr; + if ( aSO->FindAttribute( attr.inout(), "AttributePythonObject" )) + { + SALOMEDS::AttributePythonObject_var pyAttr = + SALOMEDS::AttributePythonObject::_narrow( attr ); + CORBA::String_var script = pyAttr->GetObject(); + for ( const char * scriptPos = script.in(); true; ++scriptPos ) + if (( scriptPos = strstr( scriptPos, theText.ToCString() ))) + ++count; + else + break; + } + + // New python commands + if ( !me->myPythonScript.IsNull() ) + { + const int nbLines = me->myPythonScript->Length(); + for ( int i = 1; i <= nbLines; ++i ) + { + const TCollection_AsciiString& line = me->myPythonScript->Value( i ); + for ( int loc = 1; loc <= line.Length(); ++loc ) + if (( loc = line.Location( theText, loc, line.Length() ))) + ++count; + else + break; + } + } + + return count; +}