X-Git-Url: http://git.salome-platform.org/gitweb/?p=modules%2Fsmesh.git;a=blobdiff_plain;f=src%2FSMESH_I%2FSMESH_DumpPython.cxx;h=24e9751eaba0538e87581849d1fe169d42df53f3;hp=bd0e62e1dc6c9afd93795bc3f8e365ec0b93597c;hb=0f463dbca54b373f5fc5b096da0ac36db49ec508;hpb=be55a1922a02e1c944d04526e4099e746f1bf8bb diff --git a/src/SMESH_I/SMESH_DumpPython.cxx b/src/SMESH_I/SMESH_DumpPython.cxx index bd0e62e1d..24e9751ea 100644 --- a/src/SMESH_I/SMESH_DumpPython.cxx +++ b/src/SMESH_I/SMESH_DumpPython.cxx @@ -294,6 +294,100 @@ namespace SMESH << P.z << " ))"; return *this; } + + TCollection_AsciiString myLongStringStart( "TPythonDump::LongStringStart" ); + TCollection_AsciiString myLongStringEnd ( "TPythonDump::LongStringEnd" ); + + //================================================================================ + /*! + * \brief Return marker of long string literal beginning + * \param type - a name of functionality producing the string literal + * \retval TCollection_AsciiString - the marker string to be written into + * a raw python script + */ + //================================================================================ + + TCollection_AsciiString TPythonDump::LongStringStart(const char* type) + { + return + myLongStringStart + + (Standard_Integer) strlen(type) + + " " + + (char*) type; + } + + //================================================================================ + /*! + * \brief Return marker of long string literal end + * \retval TCollection_AsciiString - the marker string to be written into + * a raw python script + */ + //================================================================================ + + TCollection_AsciiString TPythonDump::LongStringEnd() + { + return myLongStringEnd; + } + + //================================================================================ + /*! + * \brief Cut out a long string literal from a string + * \param theText - text possibly containing string literals + * \param theFrom - position in the text to search from + * \param theLongString - the retrieved literal + * \param theStringType - a name of functionality produced the literal + * \retval bool - true if a string literal found + * + * The literal is removed from theText; theFrom points position right after + * the removed literal + */ + //================================================================================ + + bool TPythonDump::CutoutLongString( TCollection_AsciiString & theText, + int & theFrom, + TCollection_AsciiString & theLongString, + TCollection_AsciiString & theStringType) + { + if ( theFrom < 1 || theFrom > theText.Length() ) + return false; + + // ...script \ beg marker \ \ type \ literal \ end marker \ script... + // "theText myLongStringStart7 Pattern!!! SALOME Mesh Pattern file myLongStringEndtextEnd" + // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 + // 0 1 2 3 4 5 6 7 8 + + theFrom = theText.Location( myLongStringStart, theFrom, theText.Length() ); // = 09 + if ( !theFrom ) + return false; + + // find where literal begins + int literalBeg = theFrom + myLongStringStart.Length(); // = 26 + char* typeLenStr = theText.ToCString() + literalBeg - 1; // = "7 Pattern!!! SALO...." + int typeLen = atoi ( typeLenStr ); // = 7 + while ( *typeLenStr != ' ' ) { // look for ' ' after typeLen + literalBeg++; // 26 -> 27 + typeLenStr++; + } + literalBeg += typeLen + 1; // = 35 + if ( literalBeg > theText.Length() ) + return false; + + // where literal ends (i.e. end marker begins) + int literalEnd = theText.Location( myLongStringEnd, literalBeg, theText.Length() ); // = 64 + if ( !literalEnd ) + literalEnd = theText.Length(); + + // literal + theLongString = theText.SubString( literalBeg, literalEnd - 1); // "!!! SALOME Mesh Pattern file " + // type + theStringType = theText.SubString( literalBeg - typeLen, literalBeg - 1 ); // "Pattern" + // cut off literal + literalEnd += myLongStringEnd.Length(); // = 79 + TCollection_AsciiString textEnd = theText.SubString( literalEnd, theText.Length() ); // "textE..." + theText = theText.SubString( 1, theFrom - 1 ) + textEnd; + + return true; + } } //======================================================================= @@ -464,6 +558,39 @@ Handle(TColStd_HSequenceOfInteger) FindEntries (TCollection_AsciiString& theStri return aSeq; } +namespace { + + //================================================================================ + /*! + * \brief Make a string be a valid python name + * \param aName - a string to fix + * \retval bool - true if aName was not modified + */ + //================================================================================ + + bool fixPythonName(TCollection_AsciiString & aName ) + { + const TCollection_AsciiString allowedChars = + "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_"; + bool isValidName = true; + int p=1; // replace not allowed chars with underscore + while (p <= aName.Length() && + (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length()))) + { + if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_') + aName.Remove( p, 1 ); // remove double _ and from the start and the end + else + aName.SetValue(p, '_'); + isValidName = false; + } + if ( aName.IsIntegerValue() ) { // aName must not start with a digit + aName.Insert( 1, 'a' ); + isValidName = false; + } + return isValidName; + } +} + //============================================================================= /*! * DumpPython @@ -513,8 +640,8 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl int beg, end = aSavedTrace.Length(), from = 1; while ( from < end && ( beg = aSavedTrace.Location( aSmeshCall, from, end ))) { char charBefore = ( beg == 1 ) ? ' ' : aSavedTrace.Value( beg - 1 ); - if ( isspace( charBefore ) || charBefore == '=' ) { - aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen ); + if ( isspace( charBefore ) || charBefore == '=' ) { // "smesh." is not a part of a long word + aSavedTrace.Insert( beg + aSmeshCall.Length() - 1, gen );// "smesh" -> "smeshgen" end += gen.Length(); } from = beg + aSmeshCall.Length(); @@ -550,8 +677,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl TColStd_SequenceOfAsciiString seqRemoved; Resource_DataMapOfAsciiStringAsciiString mapRemoved; Standard_Integer objectCounter = 0, aStart = 1, aScriptLength = aScript.Length(); - TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_"), - allowedChars ("qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0987654321_"); + TCollection_AsciiString anUpdatedScript, anEntry, aName, aBaseName("smeshObj_"); // Collect names of GEOM objects to exclude same names for SMESH objects GEOM::string_array_var aGeomNames = geom->GetAllDumpNames(); @@ -573,21 +699,7 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl // The Object is in Study aName = theObjectNames.Find(anEntry); // check validity of aName - bool isValidName = true; - int p=1; // replace not allowed chars with underscore - while (p <= aName.Length() && - (p = aName.FirstLocationNotInSet(allowedChars, p, aName.Length()))) - { - if ( p == 1 || p == aName.Length() || aName.Value(p-1) == '_') - aName.Remove( p, 1 ); // remove double _ and from the start and the end - else - aName.SetValue(p, '_'); - isValidName = false; - } - if ( aName.IsIntegerValue() ) { // aName must not start with a digit - aName.Insert( 1, 'a' ); - isValidName = false; - } + bool isValidName = fixPythonName( aName ); if (theObjectNames.IsBound(aName) && anEntry != theObjectNames(aName)) { // diff objects have same name - make a new name by appending a digit TCollection_AsciiString aName2; @@ -679,6 +791,49 @@ TCollection_AsciiString SMESH_Gen_i::DumpPython_impl anUpdatedScript += "\n\n\tpass\n"; + // ----------------------------------------------------------------- + // put string literals describing patterns into separate functions + // ----------------------------------------------------------------- + + TCollection_AsciiString aLongString, aFunctionType; + int where = 1; + set< string > functionNameSet; + while ( SMESH::TPythonDump::CutoutLongString( anUpdatedScript, where, aLongString, aFunctionType )) + { + // make a python string literal + aLongString.Prepend(":\n\treturn '''\n"); + aLongString += "\n\t'''\n\tpass\n"; + + TCollection_AsciiString functionName; + + // check if the function returning this literal is already defined + int posAlready = anUpdatedScript.Location( aLongString, where, anUpdatedScript.Length() ); + if ( posAlready ) // already defined + { + // find the function name + int functBeg = posAlready; + char* script = anUpdatedScript.ToCString() + posAlready - 1; // look at ":" after "def fuction()" + while ( *script != ' ' ) { + script--; + functBeg--; + } + functBeg++; // do not take ' ' + posAlready--; // do not take ':' + functionName = anUpdatedScript.SubString( functBeg, posAlready ); + } + else // not defined yet + { + // find a unique function name + fixPythonName( aFunctionType ); + Standard_Integer nb = 0; + do functionName = aFunctionType + "_" + ( nb++ ) + "()"; + while ( !functionNameSet.insert( functionName.ToCString() ).second ); + + anUpdatedScript += helper + "\n\ndef " + functionName + aLongString; // define function + } + anUpdatedScript.InsertBefore( where, functionName ); // call function + } + aValidScript = true; return anUpdatedScript;