Back to index

salome-smesh  6.5.0
SMESH_NoteBook.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 // File      : SMESH_NoteBook.cxx
00021 // Author    : Roman NIKOLAEV
00022 //
00023 #include "SMESH_2smeshpy.hxx"
00024 #include "SMESH_NoteBook.hxx"
00025 #include "SMESH_Gen_i.hxx"
00026 #include "SMESH_PythonDump.hxx"
00027 #include "SMESH_Hypothesis_i.hxx"
00028 
00029 #include <Resource_DataMapOfAsciiStringAsciiString.hxx>
00030 #include <TColStd_SequenceOfAsciiString.hxx>
00031 #include <TColStd_HSequenceOfInteger.hxx>
00032 
00033 #include <vector>
00034 #include <string>
00035 
00036 #ifdef _DEBUG_
00037 static int MYDEBUG = 0;
00038 #else
00039 static int MYDEBUG = 0;
00040 #endif
00041 
00042 using namespace std;
00043 
00044 
00045 namespace
00046 {
00051   void SetVariable(Handle(_pyCommand) theCommand,
00052                    const SMESH_ObjectStates* theStates,
00053                    int position, int theArgNb)
00054   {
00055     if(theStates->GetCurrectState().size() > position)
00056       if(!theStates->GetCurrectState().at(position).IsEmpty())
00057         theCommand->SetArg(theArgNb,theStates->GetCurrectState().at(position));
00058   }
00059 }
00060 
00061 //================================================================================
00065 //================================================================================
00066 SMESH_ObjectStates::SMESH_ObjectStates(TCollection_AsciiString theType)
00067 {
00068   _type = theType;
00069   _dumpstate = 0;
00070 }
00071 
00072 //================================================================================
00076 //================================================================================
00077 SMESH_ObjectStates::~SMESH_ObjectStates()
00078 {
00079 }
00080 
00081 //================================================================================
00086 //================================================================================
00087 void SMESH_ObjectStates::AddState(const TState &theState)
00088 {
00089   _states.push_back(theState);
00090 }
00091 
00092 //================================================================================
00097 //================================================================================
00098 TState SMESH_ObjectStates::GetCurrectState() const
00099 {
00100   if(_states.size() > _dumpstate)
00101     return _states[_dumpstate];
00102   TState empty;
00103   return empty;
00104 }
00105 
00106 
00107 //================================================================================
00111 //================================================================================
00112 TAllStates SMESH_ObjectStates::GetAllStates() const
00113 {
00114   return _states;
00115 }
00116 
00117 //================================================================================
00121 //================================================================================
00122 void SMESH_ObjectStates::IncrementState()
00123 {
00124   _dumpstate++;
00125 }
00126 
00127 //================================================================================
00131 //================================================================================
00132 TCollection_AsciiString SMESH_ObjectStates::GetObjectType() const{
00133   return _type;
00134 }
00135 
00136 
00137 //================================================================================
00141 //================================================================================
00142 LayerDistributionStates::LayerDistributionStates():
00143   SMESH_ObjectStates("LayerDistribution")
00144 {
00145 }
00146 //================================================================================
00150 //================================================================================
00151 LayerDistributionStates::~LayerDistributionStates()
00152 {
00153 }
00154 
00155 
00156 //================================================================================
00160 //================================================================================
00161 void LayerDistributionStates::AddDistribution(const TCollection_AsciiString& theDistribution)
00162 {
00163   _distributions.insert(pair<TCollection_AsciiString,TCollection_AsciiString>(theDistribution,""));
00164 }
00165 
00166 //================================================================================
00170 //================================================================================
00171 bool LayerDistributionStates::HasDistribution(const TCollection_AsciiString& theDistribution) const
00172 {
00173   return _distributions.find(theDistribution) != _distributions.end();
00174 }
00175 
00176 //================================================================================
00180 //================================================================================
00181 bool LayerDistributionStates::SetDistributionType(const TCollection_AsciiString& theDistribution,
00182                                                   const TCollection_AsciiString& theType)
00183 {
00184   TDistributionMap::iterator it = _distributions.find(theDistribution);
00185   if(it == _distributions.end())
00186     return false;
00187   (*it).second = theType;
00188   return true;
00189 }
00190 
00191 //================================================================================
00195 //================================================================================
00196 TCollection_AsciiString LayerDistributionStates::
00197 GetDistributionType(const TCollection_AsciiString& theDistribution) const
00198 {
00199   TDistributionMap::const_iterator it = _distributions.find(theDistribution);
00200   return (it == _distributions.end()) ? TCollection_AsciiString() : (*it).second;
00201 }
00202 
00203 //================================================================================
00207 //================================================================================
00208 SMESH_NoteBook::SMESH_NoteBook()
00209 {
00210   InitObjectMap();
00211 }
00212 
00213 //================================================================================
00217 //================================================================================
00218 SMESH_NoteBook::~SMESH_NoteBook()
00219 {
00220   TVariablesMap::const_iterator it = _objectMap.begin();
00221   for(;it!=_objectMap.end();it++) {
00222     if((*it).second)
00223       delete (*it).second;
00224   }
00225 }
00226 
00227 //================================================================================
00233 //================================================================================
00234 void SMESH_NoteBook::ReplaceVariables()
00235 {
00236   for(int i=0;i<_commands.size();i++) {
00237     Handle(_pyCommand) aCmd = _commands[i];
00238     TCollection_AsciiString aMethod = aCmd->GetMethod();
00239     TCollection_AsciiString aObject = aCmd->GetObject();
00240     TCollection_AsciiString aResultValue = aCmd->GetResultValue();
00241     if(MYDEBUG) {
00242       cout<<"Command before : "<< aCmd->GetString()<<endl;
00243       cout<<"Method : "<< aMethod<<endl;
00244       cout<<"Object : "<< aObject<<endl;
00245       cout<<"Result : "<< aResultValue<<endl;
00246     }
00247 
00248     // check if method modifies the object itself
00249     TVariablesMap::const_iterator it = _objectMap.find(aObject);
00250     if(it == _objectMap.end()) // check if method returns a new object
00251       it = _objectMap.find(aResultValue);
00252     
00253     if(it == _objectMap.end()) { // check if method modifies a mesh using mesh editor
00254       TMeshEditorMap::const_iterator meIt = myMeshEditors.find(aObject);
00255       if(meIt != myMeshEditors.end()) {
00256         TCollection_AsciiString aMesh = (*meIt).second;
00257         it = _objectMap.find(aMesh);
00258       }
00259     }
00260     
00261     if(it == _objectMap.end()) { // additional check for pattern mapping
00262       if(aMethod.IsEqual("ApplyToMeshFaces") ||
00263          aMethod.IsEqual("ApplyToHexahedrons"))
00264         it = _objectMap.find(aCmd->GetArg(1));
00265     }
00266 
00267     if(it != _objectMap.end()) {
00268       if(MYDEBUG)
00269         cout << "Found object : " << (*it).first << endl;
00270       SMESH_ObjectStates *aStates = (*it).second;
00271       // Case for LocalLength hypothesis
00272       if(aStates->GetObjectType().IsEqual("LocalLength") && aStates->GetCurrectState().size() >= 2)
00273       {
00274         if(aMethod.IsEqual("SetLength")) {
00275           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00276             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00277           aStates->IncrementState();
00278         }
00279         else if(aMethod.IsEqual("SetPrecision")) {
00280           if(!aStates->GetCurrectState().at(1).IsEmpty() )
00281             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
00282           aStates->IncrementState();
00283         }
00284       }
00285       
00286       // Case for SegmentLengthAroundVertex hypothesis
00287       else if(aStates->GetObjectType().IsEqual("SegmentLengthAroundVertex")
00288               && aStates->GetCurrectState().size() >= 1) {
00289         if(aMethod == "SetLength") {
00290           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00291             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00292           aStates->IncrementState();
00293         }
00294       }
00295 
00296       // Case for Arithmetic1D and StartEndLength hypothesis
00297       else if(aStates->GetObjectType().IsEqual("Arithmetic1D") || 
00298               aStates->GetObjectType().IsEqual("StartEndLength")) {
00299         if(aMethod == "SetLength" &&
00300            aStates->GetCurrectState().size() >= 2) {
00301           if(aCmd->GetArg(2) == "1" && !aStates->GetCurrectState().at(0).IsEmpty())
00302             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00303           else if(!aStates->GetCurrectState().at(1).IsEmpty())
00304             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
00305           aStates->IncrementState();
00306         }
00307       }
00308       
00309       //Case for Deflection1D hypothesis
00310       else if(aStates->GetObjectType().IsEqual("Deflection1D")){
00311         if(aMethod == "SetDeflection" && aStates->GetCurrectState().size() >= 1) {
00312           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00313             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00314           aStates->IncrementState();
00315         }
00316       }
00317       
00318       // Case for LayerDistribution hypothesis (not finished yet)
00319       else if(aStates->GetObjectType() == "LayerDistribution") {
00320         if(aMethod == "SetLayerDistribution"){
00321           LayerDistributionStates* aLDStates = (LayerDistributionStates*)(aStates);
00322           aLDStates->AddDistribution(aCmd->GetArg(1));
00323           if(MYDEBUG)
00324             cout<<"Add Distribution :"<<aCmd->GetArg(1)<<endl;
00325         }
00326       }
00327       
00328       // Case for MaxElementArea hypothesis
00329       else if(aStates->GetObjectType().IsEqual("MaxElementArea")){
00330         if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 1) {
00331           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00332             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00333           aStates->IncrementState();
00334         }
00335       }
00336 
00337       // Case for MaxElementVolume hypothesis
00338       else if(aStates->GetObjectType().IsEqual("MaxElementVolume")){
00339         if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 1) {
00340           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00341             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00342           aStates->IncrementState();
00343         }
00344       }
00345       // Case for NumberOfLayers hypothesis
00346       else if(aStates->GetObjectType().IsEqual("NumberOfLayers")){
00347         if(aMethod == "SetNumberOfLayers" && aStates->GetCurrectState().size() >= 1) {
00348           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00349             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00350           aStates->IncrementState();
00351         }
00352       }
00353 
00354       // Case for NumberOfSegments hypothesis
00355       else if(aStates->GetObjectType().IsEqual("NumberOfSegments")){
00356         if(aMethod == "SetNumberOfSegments" && aStates->GetCurrectState().size() >= 1) {
00357           if(!aStates->GetCurrectState().at(0).IsEmpty() )
00358             aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00359           if(aStates->GetCurrectState().size()==1)
00360             aStates->IncrementState();
00361         }
00362         else if (aMethod == "SetScaleFactor" && aStates->GetCurrectState().size() >= 2) {
00363           if(!aStates->GetCurrectState().at(1).IsEmpty() )
00364             aCmd->SetArg(1,aStates->GetCurrectState().at(1));
00365           aStates->IncrementState();
00366         }
00367       }
00368       
00369       else if(aStates->GetObjectType().IsEqual("Mesh")) {
00370         TState aCurrentState = aStates->GetCurrectState();
00371         int aCurrentStateSize = aCurrentState.size();
00372         if(aMethod.IsEqual("Translate")                  ||
00373            aMethod.IsEqual("TranslateMakeGroups")        ||
00374            aMethod.IsEqual("TranslateMakeMesh")          ||
00375            aMethod.IsEqual("TranslateObject")            ||
00376            aMethod.IsEqual("TranslateObjectMakeGroups")  ||
00377            aMethod.IsEqual("TranslateObjectMakeMesh")) {
00378           bool isVariableFound = false;
00379           int anArgIndex = 0;
00380           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
00381             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
00382               anArgIndex = i+1;
00383               break;
00384             }
00385           }
00386           if(anArgIndex > 0) {
00387             if(aCurrentStateSize == 3) { // translation by dx, dy, dz
00388               for(int j = 0; j < aCurrentStateSize; j++) {
00389                 if(!aCurrentState.at(j).IsEmpty()) {
00390                   isVariableFound = true;
00391                   aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
00392                 }
00393               }
00394             }
00395             else if(aCurrentStateSize == 6) { // translation by x1, x2, y1, y2, z1, z2
00396               // TODO: limitation until operations on the variables will be introduced
00397               /*
00398               isVariableFound = true;
00399               for(int j = 0; j < 3; j++) {
00400                 TCollection_AsciiString anArg = aCmd->GetArg(anArgIndex+j);
00401                 TCollection_AsciiString aValue1 = aCurrentState.at(2*j), aValue2 = aCurrentState.at(2*j+1);
00402                 bool aV1 = !aValue1.IsEmpty();
00403                 bool aV2 = !aValue2.IsEmpty();
00404                 double aValue, aCurrentValue = anArg.IsRealValue() ? anArg.RealValue() : 0;
00405                 if(aV1 && !aV2) {
00406                   if(!GetReal(aValue1, aValue))
00407                     aValue = 0;
00408                   aValue2 = TCollection_AsciiString( aValue + aCurrentValue );
00409                 }
00410                 else if(!aV1 && aV2) {
00411                   if(!GetReal(aValue2, aValue))
00412                     aValue = 0;
00413                   aValue1 = TCollection_AsciiString( aValue - aCurrentValue );
00414                 }
00415                 else if(!aV1 && !aV2) {
00416                   aValue1 = TCollection_AsciiString( 0 );
00417                   aValue2 = TCollection_AsciiString( aCurrentValue );
00418                 }
00419                 aCmd->SetArg(anArgIndex+j, aValue1 + ", " + aValue2 );
00420               }
00421               */
00422             }
00423           }
00424           if(isVariableFound) {
00425             TCollection_AsciiString aDim;
00426             if(aCurrentStateSize == 6)
00427               aDim = "6";
00428             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr"+aDim);
00429             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
00430           }
00431           aStates->IncrementState();
00432         }
00433         else if(aMethod.IsEqual("Rotate")                  ||
00434                 aMethod.IsEqual("RotateMakeGroups")        ||
00435                 aMethod.IsEqual("RotateMakeMesh")          ||
00436                 aMethod.IsEqual("RotateObject")            ||
00437                 aMethod.IsEqual("RotateObjectMakeGroups")  ||
00438                 aMethod.IsEqual("RotateObjectMakeMesh")    ||
00439                 aMethod.IsEqual("RotationSweep")           ||
00440                 aMethod.IsEqual("RotationSweepObject")     ||
00441                 aMethod.IsEqual("RotationSweepObject1D")   ||
00442                 aMethod.IsEqual("RotationSweepObject2D")   ||
00443                 aMethod.IsEqual("RotationSweepMakeGroups") ||
00444                 aMethod.IsEqual("RotationSweepObjectMakeGroups") ||
00445                 aMethod.IsEqual("RotationSweepObject1DMakeGroups") ||
00446                 aMethod.IsEqual("RotationSweepObject2DMakeGroups") ||
00447                 aMethod.IsEqual("Mirror")                  ||
00448                 aMethod.IsEqual("MirrorMakeMesh")          ||
00449                 aMethod.IsEqual("MirrorMakeGroups")        ||
00450                 aMethod.IsEqual("MirrorObject")            || 
00451                 aMethod.IsEqual("MirrorObjectMakeMesh")    ||
00452                 aMethod.IsEqual("MirrorObjectMakeGroups")) {
00453           bool isSubstitute = false;
00454           int anArgIndex = 0;
00455           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
00456             if(aCmd->GetArg(i).IsEqual("SMESH.AxisStruct")) {
00457               anArgIndex = i+1;
00458               break;
00459             }
00460           }
00461           if(anArgIndex > 0) {
00462             for(int j = 0; j < aCurrentStateSize; j++) {
00463               if(!aCurrentState.at(j).IsEmpty()) {
00464                 if(j < 6) // 0-5 - axis struct, 6 - angle (rotation & sweep), 7-8 - nbSteps and tolerance (sweep)
00465                   isSubstitute = true;
00466                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
00467               }
00468             }
00469           }
00470           if(isSubstitute)
00471             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".AxisStructStr");
00472           aStates->IncrementState();
00473         }
00474         else if(aMethod.IsEqual("AddNode") ||
00475                 aMethod.IsEqual("MoveClosestNodeToPoint")) {
00476           for(int j = 0; j < aCurrentStateSize; j++) {
00477             if(!aCurrentState.at(j).IsEmpty())
00478               aCmd->SetArg(j+1, aCurrentState.at(j));
00479           }
00480           aStates->IncrementState();
00481         }
00482         else if(aMethod.IsEqual("MoveNode")) {
00483           for(int j = 0; j < aCurrentStateSize; j++) {
00484             if(!aCurrentState.at(j).IsEmpty())
00485               aCmd->SetArg(j+2, aCurrentState.at(j));
00486           }
00487           aStates->IncrementState();
00488         }
00489         else if(aMethod.IsEqual("ExtrusionSweep") ||
00490                 aMethod.IsEqual("ExtrusionSweepObject") ||
00491                 aMethod.IsEqual("ExtrusionSweepObject1D") ||
00492                 aMethod.IsEqual("ExtrusionSweepObject2D") ||
00493                 aMethod.IsEqual("ExtrusionSweepMakeGroups") ||
00494                 aMethod.IsEqual("ExtrusionSweepObjectMakeGroups") ||
00495                 aMethod.IsEqual("ExtrusionSweepObject1DMakeGroups") ||
00496                 aMethod.IsEqual("ExtrusionSweepObject2DMakeGroups")) {
00497           bool isSubstitute = false;
00498           int anArgIndex = 0;
00499           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
00500             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
00501               anArgIndex = i+1;
00502               break;
00503             }
00504           }
00505           if(anArgIndex > 0) {
00506             for(int j = 0; j < aCurrentStateSize; j++) {
00507               if(!aCurrentState.at(j).IsEmpty()) {
00508                 if(j < 3) // 0-2 - dir struct, 3 - number of steps
00509                   isSubstitute = true;
00510                 aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
00511               }
00512             }
00513           }
00514           if(isSubstitute) {
00515             aCmd->SetArg(anArgIndex - 1, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
00516             aCmd->SetArg(anArgIndex - 2, TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".DirStructStr");
00517           }
00518           aStates->IncrementState();
00519         }
00520         else if(aMethod.IsEqual("ExtrusionAlongPath") ||
00521                 aMethod.IsEqual("ExtrusionAlongPathObject") ||
00522                 aMethod.IsEqual("ExtrusionAlongPathObject1D") ||
00523                 aMethod.IsEqual("ExtrusionAlongPathObject2D") ||
00524                 aMethod.IsEqual("ExtrusionAlongPathMakeGroups") ||
00525                 aMethod.IsEqual("ExtrusionAlongPathObjectMakeGroups") ||
00526                 aMethod.IsEqual("ExtrusionAlongPathObject1DMakeGroups") ||
00527                 aMethod.IsEqual("ExtrusionAlongPathObject2DMakeGroups") ||
00528                 /* workaround for a bug in the command parsing algorithm */
00529                 aCmd->GetString().Search("ExtrusionAlongPathMakeGroups") != -1 ||
00530                 aCmd->GetString().Search("ExtrusionAlongPathObjectMakeGroups") != -1 ||
00531                 aCmd->GetString().Search("ExtrusionAlongPathObject1DMakeGroups") != -1 ||
00532                 aCmd->GetString().Search("ExtrusionAlongPathObject2DMakeGroups") != -1 ) {
00533           int aNbAngles = aCurrentStateSize-3; // State looks like "Angle1:...:AngleN:X:Y:Z"
00534           bool isSubstitute = false;
00535           int anArgIndex = 0;
00536           for(int i = 1, n = aCmd->GetNbArgs(); i <= n; i++) {
00537             if(aCmd->GetArg(i).IsEqual("SMESH.PointStruct")) {
00538               anArgIndex = i-1-aNbAngles;
00539               break;
00540             }
00541           }
00542           if(anArgIndex > 0) {
00543             int j = 0;
00544             for(; j < aNbAngles; j++) {
00545               if(!aCurrentState.at(j).IsEmpty()) {
00546                 aCmd->SetArg(anArgIndex+j-1, aCurrentState.at(j));
00547               }
00548             }
00549             for(; j < aNbAngles+3; j++) {
00550               if(!aCurrentState.at(j).IsEmpty()) {
00551                 isSubstitute = true;
00552                 aCmd->SetArg(anArgIndex+j+2, aCurrentState.at(j));
00553               }
00554             }
00555           }
00556           if(isSubstitute)
00557             aCmd->SetArg(anArgIndex + aNbAngles + 1,
00558                          TCollection_AsciiString(SMESH_2smeshpy::SmeshpyName())+".PointStructStr");
00559           aStates->IncrementState();
00560         }
00561         else if(aMethod.IsEqual("TriToQuad") ||
00562                 aMethod.IsEqual("Concatenate") ||
00563                 aMethod.IsEqual("ConcatenateWithGroups")) {
00564           if(aCurrentStateSize && !aCurrentState.at(0).IsEmpty())
00565             aCmd->SetArg(aCmd->GetNbArgs(), aCurrentState.at(0));
00566           aStates->IncrementState();
00567         }
00568         else if(aMethod.IsEqual("Smooth") ||
00569                 aMethod.IsEqual("SmoothObject") ||
00570                 aMethod.IsEqual("SmoothParametric") ||
00571                 aMethod.IsEqual("SmoothParametricObject")) {
00572           int anArgIndex = aCmd->GetNbArgs() - 2;
00573           for(int j = 0; j < aCurrentStateSize; j++) {
00574             if(!aCurrentState.at(j).IsEmpty())
00575               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
00576           }
00577           aStates->IncrementState();
00578         }
00579         else if(aMethod.IsEqual("ApplyToMeshFaces") ||
00580                 aMethod.IsEqual("ApplyToHexahedrons")) {
00581           int anArgIndex = aCmd->GetNbArgs()-1;
00582           for(int j = 0; j < aCurrentStateSize; j++)
00583             if(!aCurrentState.at(j).IsEmpty())
00584               aCmd->SetArg(anArgIndex+j, aCurrentState.at(j));
00585           aStates->IncrementState();
00586         }
00587       } // if ( aStates->GetObjectType().IsEqual("Mesh"))
00588 
00589       // Case for NETGEN_Parameters_2D or NETGEN_Parameters_2D hypothesis
00590       // else if(aStates->GetObjectType().IsEqual("NETGEN_Parameters_2D") ||
00591       //         aStates->GetObjectType().IsEqual("NETGEN_Parameters")){
00592       //   if(aMethod == "SetMaxSize" && aStates->GetCurrectState().size() >= 1) {
00593       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
00594       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00595       //     aStates->IncrementState();
00596       //   }
00597       //   else if(aMethod == "SetGrowthRate" && aStates->GetCurrectState().size() >= 2) {
00598       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
00599       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
00600       //     aStates->IncrementState();
00601       //   }
00602       //   else if(aMethod == "SetNbSegPerEdge" && aStates->GetCurrectState().size() >= 3) {
00603       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
00604       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
00605       //     aStates->IncrementState();
00606       //   }
00607       //   else if(aMethod == "SetNbSegPerRadius" && aStates->GetCurrectState().size() >= 4) {
00608       //     if(!aStates->GetCurrectState().at(3).IsEmpty() )
00609       //       aCmd->SetArg(1,aStates->GetCurrectState().at(3));
00610       //     aStates->IncrementState();
00611       //   }
00612       // }
00613 
00614       // // Case for NETGEN_SimpleParameters_3D or NETGEN_SimpleParameters_2D hypothesis
00615       // else if(aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_3D") ||
00616       //         aStates->GetObjectType().IsEqual("NETGEN_SimpleParameters_2D")) {
00617 
00618       //   if((aMethod == "SetNumberOfSegments" || aMethod == "SetLocalLength") && 
00619       //      aStates->GetCurrectState().size() >= 1) {
00620       //     if(!aStates->GetCurrectState().at(0).IsEmpty() )
00621       //       aCmd->SetArg(1,aStates->GetCurrectState().at(0));
00622       //     aStates->IncrementState();
00623       //   }
00624       //   else if(aMethod == "SetMaxElementArea" && aStates->GetCurrectState().size() >= 2) {
00625       //     if(!aStates->GetCurrectState().at(1).IsEmpty() )
00626       //       aCmd->SetArg(1,aStates->GetCurrectState().at(1));
00627       //     aStates->IncrementState();
00628       //   }
00629       //   else if(aMethod == "SetMaxElementVolume" && aStates->GetCurrectState().size() >= 3) {
00630       //     if(!aStates->GetCurrectState().at(2).IsEmpty() )
00631       //       aCmd->SetArg(1,aStates->GetCurrectState().at(2));
00632       //     aStates->IncrementState();
00633       //   }
00634       //   else if(aMethod == "LengthFromEdges" || aMethod == "LengthFromFaces"){
00635       //     aStates->IncrementState();
00636       //   }
00637       // }
00638 
00639       else
00640       {
00641         // treat Netgen hypotheses;
00642         // this (and above) code can work wrong since nb of states can differ from nb of
00643         // dumped calls due to the fix of
00644         // issue 0021364:: Dump of netgen parameters has duplicate lines
00645         SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
00646         SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
00647         SALOMEDS::SObject_var sobj = aStudy->FindObjectID( (*it).first.ToCString() );
00648         CORBA::Object_var      obj = aGen->SObjectToObject( sobj );
00649         if ( SMESH_Hypothesis_i* h = SMESH::DownCast< SMESH_Hypothesis_i*>( obj ))
00650         {
00651           TState aCurrentState = aStates->GetCurrectState();
00652           int argIndex = h->getParamIndex( aMethod, aCurrentState.size() );
00653           if ( 0 <= argIndex && argIndex < aCurrentState.size() &&
00654                !aCurrentState[argIndex].IsEmpty() )
00655             aCmd->SetArg( 1, aCurrentState[argIndex] );
00656 
00657           if ( argIndex >= 0 )
00658             aStates->IncrementState();
00659         }
00660       }
00661     }
00662     else {
00663       if(MYDEBUG)
00664         cout << "Object not found" << endl;
00665     }
00666     if(MYDEBUG) {
00667       cout<<"Command after: "<< aCmd->GetString()<<endl;
00668     }
00669   }
00670   
00671   ProcessLayerDistribution();
00672 }
00673 //================================================================================
00677 //================================================================================
00678 void SMESH_NoteBook::InitObjectMap()
00679 {
00680   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
00681   if(!aGen)
00682     return;
00683   
00684   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
00685   if(aStudy->_is_nil())
00686     return;
00687   
00688   SALOMEDS::SObject_var aSO = aStudy->FindComponent(aGen->ComponentDataType());
00689   if(CORBA::is_nil(aSO))
00690     return;
00691   
00692   SALOMEDS::ChildIterator_var Itr = aStudy->NewChildIterator(aSO);
00693   char* aParameters;
00694   for(Itr->InitEx(true); Itr->More(); Itr->Next()) {
00695     SALOMEDS::SObject_var aSObject = Itr->Value();
00696     SALOMEDS::GenericAttribute_var anAttr;
00697     if ( aSObject->FindAttribute(anAttr, "AttributeString")) {
00698       aParameters = SALOMEDS::AttributeString::_narrow(anAttr)->Value();
00699       SALOMEDS::ListOfListOfStrings_var aSections = aStudy->ParseVariables(aParameters);
00700       if(MYDEBUG) {
00701         cout<<"Entry : "<< aSObject->GetID()<<endl;
00702         cout<<"aParameters : "<<aParameters<<endl;
00703       }      
00704       TCollection_AsciiString anObjType;
00705       CORBA::Object_var       anObject = SMESH_Gen_i::SObjectToObject(aSObject);
00706       SMESH::SMESH_Hypothesis_var aHyp = SMESH::SMESH_Hypothesis::_narrow(anObject);
00707       SMESH::SMESH_Mesh_var      aMesh = SMESH::SMESH_Mesh::_narrow(anObject);
00708       if(!aHyp->_is_nil()) {
00709         CORBA::String_var hypName = aHyp->GetName();
00710         anObjType = hypName.in();
00711       }
00712       else if (!aMesh->_is_nil() ) {
00713         anObjType = "Mesh";
00714       }
00715       if(MYDEBUG)
00716         cout<<"The object Type : "<<anObjType<<endl;
00717       SMESH_ObjectStates *aState = NULL;
00718       if(anObjType == "LayerDistribution")
00719         aState = new LayerDistributionStates();
00720       else
00721         aState = new  SMESH_ObjectStates(anObjType);
00722 
00723       for(int i = 0; i < aSections->length(); i++) {
00724         TState aVars;
00725         SALOMEDS::ListOfStrings aListOfVars = aSections[i];
00726         for(int j = 0;j<aListOfVars.length();j++) {
00727           TCollection_AsciiString aVar(aListOfVars[j].in());
00728           if(!aVar.IsEmpty() && aStudy->IsVariable(aVar.ToCString())) {
00729             aVar.InsertBefore(1,            SMESH::TVar::Quote() );
00730             aVar.InsertAfter(aVar.Length(), SMESH::TVar::Quote() );
00731           }
00732           aVars.push_back(aVar);
00733           if(MYDEBUG) {
00734             cout<<"Variable: '"<<aVar<<"'"<<endl;
00735           }
00736         }
00737         aState->AddState(aVars);
00738       }
00739       if ( aState->GetAllStates().empty() )
00740       {
00741         delete aState;
00742       }
00743       else
00744       {
00745         CORBA::String_var objID = aSObject->GetID();
00746         _objectMap.insert( make_pair(TCollection_AsciiString( objID.in() ), aState ));
00747       }
00748     }
00749   }
00750 }
00751 
00752 //================================================================================
00756 //================================================================================
00757 void SMESH_NoteBook::AddCommand(const TCollection_AsciiString& theString)
00758 {
00759   if(MYDEBUG)
00760     cout<<theString<<endl;
00761   Handle(_pyCommand) aCommand = new _pyCommand( theString, -1);
00762   _commands.push_back(aCommand);
00763 
00764   if ( aCommand->GetMethod() == "GetMeshEditor" ) { // MeshEditor creation
00765     myMeshEditors.insert( make_pair( aCommand->GetResultValue(),
00766                                      aCommand->GetObject() ) );
00767   }
00768 }
00769 
00770 //================================================================================
00774 //================================================================================
00775 void SMESH_NoteBook::ProcessLayerDistribution()
00776 {
00777   // 1) Find all LayerDistribution states
00778   vector<LayerDistributionStates*> aLDS;
00779   TVariablesMap::const_iterator it = _objectMap.begin();
00780   for(;it != _objectMap.end();it++) {
00781     LayerDistributionStates* aLDStates = dynamic_cast<LayerDistributionStates*>(((*it).second));
00782     if(aLDStates!=NULL) {
00783       aLDS.push_back(aLDStates);
00784     }
00785   }
00786   
00787   if(!aLDS.size())
00788     return;
00789   
00790   // 2) Initialize all type of 1D Distribution hypothesis
00791   for(int i=0;i<_commands.size();i++){
00792     for(int j =0;j < aLDS.size();j++){
00793       TCollection_AsciiString aResultValue = _commands[i]->GetResultValue();
00794       if(_commands[i]->GetMethod() == "CreateHypothesis" &&
00795          aLDS[j]->HasDistribution(aResultValue)){
00796         TCollection_AsciiString aType = _commands[i]->GetArg(1);
00797         aType.RemoveAll('\'');
00798         aLDS[j]->SetDistributionType(aResultValue,aType);
00799       }
00800     }
00801   }
00802   // 3) ... and replase variables ...
00803 
00804   for(int i=0;i<_commands.size();i++){
00805     for(int j =0;j < aLDS.size();j++){
00806       TCollection_AsciiString anObject = _commands[i]->GetObject();
00807 
00808       if(aLDS[j]->HasDistribution(anObject)) {
00809         TCollection_AsciiString aType = aLDS[j]->GetDistributionType(anObject);
00810         TCollection_AsciiString aMethod = _commands[i]->GetMethod();
00811         if(aType == "LocalLength") {
00812           if(aMethod == "SetLength") {
00813             SetVariable(_commands[i], aLDS[j],0,1);
00814             aLDS[j]->IncrementState();
00815           }
00816           else if(aMethod == "SetPrecision") {
00817             SetVariable(_commands[i], aLDS[j],1,1);
00818             aLDS[j]->IncrementState();
00819           }
00820         }
00821 
00822         // Case for NumberOfSegments hypothesis
00823         else if(aType == "NumberOfSegments"){
00824           if(aMethod == "SetNumberOfSegments") {
00825             SetVariable(_commands[i], aLDS[j],0,1);
00826             if(aLDS[j]->GetCurrectState().size()==1)
00827               aLDS[j]->IncrementState();
00828           }
00829           else if (aMethod == "SetScaleFactor") {
00830             SetVariable(_commands[i], aLDS[j],1,1);
00831             aLDS[j]->IncrementState();
00832           }
00833         }
00834         
00835         else if( aType == "Deflection1D" ){
00836           if(aMethod == "SetDeflection"){
00837             SetVariable(_commands[i], aLDS[j],0,1);
00838             aLDS[j]->IncrementState();
00839           }
00840         }
00841         // Case for Arithmetic1D and StartEndLength hypothesis
00842         else if(aType == "Arithmetic1D" || aType == "StartEndLength") {
00843           if(aMethod == "SetLength") {
00844             int anArgNb = (_commands[i]->GetArg(2) == "1") ? 0 : 1;
00845             SetVariable(_commands[i], aLDS[j],anArgNb,1);
00846             aLDS[j]->IncrementState();
00847           }
00848         }
00849       }
00850     }
00851   }
00852 }
00853 //================================================================================
00857 //================================================================================
00858 TCollection_AsciiString SMESH_NoteBook::GetResultScript() const
00859 {
00860   TCollection_AsciiString aResult;
00861   for(int i=0;i<_commands.size();i++)
00862     aResult+=_commands[i]->GetString()+"\n";
00863   return aResult;
00864 }
00865 
00866 //================================================================================
00870 //================================================================================
00871 bool SMESH_NoteBook::GetReal(const TCollection_AsciiString& theVarName, double& theValue)
00872 {
00873   bool ok = false;
00874 
00875   SMESH_Gen_i *aGen = SMESH_Gen_i::GetSMESHGen();
00876   if(!aGen)
00877     return ok;
00878 
00879   SALOMEDS::Study_ptr aStudy = aGen->GetCurrentStudy();
00880   if(aStudy->_is_nil())
00881     return ok;
00882 
00883   TCollection_AsciiString aVarName = theVarName;
00884   aVarName.RemoveAll('\"');
00885 
00886   if(aVarName.IsEmpty())
00887     return ok;
00888 
00889   const char* aName = aVarName.ToCString();
00890   if(aStudy->IsVariable(aName) && (aStudy->IsReal(aName) || aStudy->IsInteger(aName))) {
00891     theValue = aStudy->GetReal(aVarName.ToCString());
00892     ok = true;
00893   }
00894 
00895   return ok;
00896 }
00897