]> SALOME platform Git repositories - plugins/blsurfplugin.git/commitdiff
Salome HOME
Add Attractor:
authorgdd <gdd>
Fri, 20 Feb 2009 18:09:49 +0000 (18:09 +0000)
committergdd <gdd>
Fri, 20 Feb 2009 18:09:49 +0000 (18:09 +0000)
 An attractor is defined with the following pattern:
 ATTRACTOR(xa;ya;za;a;b) where:
 xa;ya;za : coordinates of attractor point
 a        : desired size on attractor point
 b        : distance of influence of attractor

Formula computed is following: R-(R-a)*exp(-(r/b)^2)
with r : distance from attractor

------------
Change behavior when checking size map functions.
* If size map is not a valid python function:
  - it will not be taken into account at computation,
  - the text in the table is colored in white on red background.
* Empty functions are not allowed

src/GUI/BLSURFPluginGUI_HypothesisCreator.cxx
src/GUI/BLSURFPluginGUI_HypothesisCreator.h

index 5080e6d369569c69533df22ca74f3af6f215040f..1f200cd0533292b7f1fce21598045e385166998f 100644 (file)
@@ -211,8 +211,12 @@ BLSURFPluginGUI_HypothesisCreator::BLSURFPluginGUI_HypothesisCreator( const QStr
   _PTR(Study) aStudy = SMESH::GetActiveStudyDocument();
   GeomToolSelected = new GeomSelectionTools(aStudy);
 
+  aSel = GeomToolSelected->selectionMgr();
+
   /* Initialize the Python interpreter */
-  assert(Py_IsInitialized());
+  //assert(Py_IsInitialized());
+  if (not Py_IsInitialized())
+    throw ("Erreur: Python interpreter is not initialized");
   PyGILState_STATE gstate;
   gstate = PyGILState_Ensure();
   
@@ -224,6 +228,7 @@ BLSURFPluginGUI_HypothesisCreator::BLSURFPluginGUI_HypothesisCreator( const QStr
   
   PyRun_SimpleString("from math import *");
   PyGILState_Release(gstate);
+
 }
 
 BLSURFPluginGUI_HypothesisCreator::~BLSURFPluginGUI_HypothesisCreator()
@@ -494,7 +499,7 @@ QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
   mySizeMapTable->setHorizontalHeaderLabels(sizeMapHeaders);
   mySizeMapTable->horizontalHeader()->hideSection( SMP_ENTRY_COLUMN );
   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
-  mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
+  mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
   mySizeMapTable->setAlternatingRowColors(true);
   mySizeMapTable->verticalHeader()->hide();
 
@@ -544,10 +549,22 @@ QFrame* BLSURFPluginGUI_HypothesisCreator::buildFrame()
   connect(removeButton, SIGNAL(clicked()), this, SLOT(onRemoveMap()));
   connect(mySizeMapTable, SIGNAL(cellChanged ( int, int  )),this,SLOT (onSetSizeMap(int,int )));
 
+//  connect(tab, SIGNAL(currentChanged ( int  )),this,SLOT (onSetSelectionFilter(int)));
+
   return fr;
 }
 
 
+void BLSURFPluginGUI_HypothesisCreator::onSetSelectionFilter(int page) 
+{
+  cout << "BLSURFPluginGUI_HypothesisCreator::onSetSelectionFilter(" << page << ")" << endl;
+  aSel->clearFilters();
+  if (page == SMP_TAB) {
+    cout << "Page is SIZEMAP" << endl;
+    aSel = GeomToolSelected->selectionMgr();
+  }
+}
+
 void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
 {
   cout << "BLSURFPluginGUI_HypothesisCreator::retrieveParams" << endl; 
@@ -620,7 +637,7 @@ void BLSURFPluginGUI_HypothesisCreator::retrieveParams() const
     }
   
   mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
-  mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
+  mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
 
   // update widgets
   that->onPhysicalMeshChanged();
@@ -674,27 +691,56 @@ bool BLSURFPluginGUI_HypothesisCreator::readParamsFromHypo( BlsurfHypothesisData
   BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
   that->myOptions = h->GetOptionValues();
 
+  that->mySMPMap.clear();
+
+  // classic size maps
+  BLSURFPlugin::string_array_var mySizeMaps = h->GetSizeMapEntries();
+  cout << "mySizeMaps->length() = " << mySizeMaps->length() << endl;
+
+  for ( int i = 0;i<mySizeMaps->length(); ++i ) {
+    QString fullSizeMaps =  mySizeMaps[i].in();
+    QStringList fullSizeMapList = fullSizeMaps.split( "|", QString::KeepEmptyParts );
+    if ( fullSizeMapList.count() > 1 ) {
+      string fullSizeMap = fullSizeMapList[1].toStdString();
+      int pos = fullSizeMap.find("return")+7;
+      QString sizeMap = QString::fromStdString(fullSizeMap.substr(pos, fullSizeMap.size()-pos));
+      that->mySMPMap[fullSizeMapList[0]] = sizeMap;
+      that->mySMPShapeTypeMap[fullSizeMapList[0]] = GeomToolSelected->entryToShape(fullSizeMapList[0].toStdString()).ShapeType();
+      MESSAGE("mySMPMap[" << fullSizeMapList[0].toStdString() << "] = " << sizeMap.toStdString());
+      MESSAGE("mySMPShapeTypeMap[" << fullSizeMapList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[fullSizeMapList[0]]);
+    }
+  }
 
-  BLSURFPlugin::string_array_var mySMPString = h->GetSizeMapEntries();
-  
-  cout << "mySMPString->length() = " << mySMPString->length() << endl;
-  that->mySMPMap.clear();       
-
-  TopoDS_Shape S;
-
-  for ( int i = 0;i<mySMPString->length(); ++i ) {
-     QString sizemaps_line =  mySMPString[i].in();
-     QStringList entry_sm = sizemaps_line.split( "|", QString::KeepEmptyParts );
-     if ( entry_sm.count() > 1 ) {
-       string fullSizeMap = entry_sm[1].toStdString();
-       int pos = fullSizeMap.find("return")+7;
-       QString sizeMap = QString::fromStdString(fullSizeMap.substr(pos, fullSizeMap.size()-pos));
-       that->mySMPMap[entry_sm[0]] = sizeMap;
-       that->mySMPShapeTypeMap[entry_sm[0]] = GeomToolSelected->entryToShape(entry_sm[0].toStdString()).ShapeType();
-       MESSAGE("mySMPMap[" << entry_sm[0].toStdString() << "] = " << sizeMap.toStdString());
-       MESSAGE("mySMPShapeTypeMap[" << entry_sm[0].toStdString() << "] = " << that->mySMPShapeTypeMap[entry_sm[0]]);
-      }
+  // custom size maps
+/*
+  BLSURFPlugin::string_array_var myCustomSizeMaps = h->GetCustomSizeMapEntries();
+  cout << "myCustomSizeMaps->length() = " << myCustomSizeMaps->length() << endl;
+
+  for ( int i = 0;i<myCustomSizeMaps->length(); ++i ) {
+    QString fullCustomSizeMaps =  myCustomSizeMaps[i].in();
+    QStringList fullCustomSizeMapList = fullCustomSizeMaps.split( "|", QString::KeepEmptyParts );
+    if ( fullCustomSizeMapList.count() > 1 ) {
+      that->mySMPMap[fullCustomSizeMapList[0]] = fullCustomSizeMapList[1];
+      that->mySMPShapeTypeMap[fullCustomSizeMapList[0]] = GeomToolSelected->entryToShape(fullCustomSizeMapList[0].toStdString()).ShapeType();
+      MESSAGE("mySMPMap[" << fullCustomSizeMapList[0].toStdString() << "] = " << fullCustomSizeMapList[1].toStdString());
+      MESSAGE("mySMPShapeTypeMap[" << fullCustomSizeMapList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[fullCustomSizeMapList[0]]);
     }
+  }
+*/
+  // attractor
+  BLSURFPlugin::string_array_var allMyAttractors = h->GetAttractorEntries();
+  cout << "myAttractors->length() = " << allMyAttractors->length() << endl;
+
+  for ( int i = 0;i<allMyAttractors->length(); ++i ) {
+    QString myAttractors =  allMyAttractors[i].in();
+    QStringList myAttractorList = myAttractors.split( "|", QString::KeepEmptyParts );
+    if ( myAttractorList.count() > 1 ) {
+      that->mySMPMap[myAttractorList[0]] = myAttractorList[1];
+      that->mySMPShapeTypeMap[myAttractorList[0]] = GeomToolSelected->entryToShape(myAttractorList[0].toStdString()).ShapeType();
+      MESSAGE("mySMPMap[" << myAttractorList[0].toStdString() << "] = " << myAttractorList[1].toStdString());
+      MESSAGE("mySMPShapeTypeMap[" << myAttractorList[0].toStdString() << "] = " << that->mySMPShapeTypeMap[myAttractorList[0]]);
+    }
+  }
 
   return true;
 }
@@ -759,34 +805,45 @@ bool BLSURFPluginGUI_HypothesisCreator::storeParamsToHypo( const BlsurfHypothesi
     h->SetOptionValues( myOptions ); // is set in checkParams()
 
     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
-    //printf("storeParamsToHypo():that->mySMPMap.size()=%d\n",that->mySMPMap.size());
     QMapIterator<QString,QString> i(that->mySMPMap);
-    // Reset the size maps in engine
-    // h->ClearSizeMaps();
     // Iterate over each size map
     while (i.hasNext()) {
       i.next();
       const QString entry = i.key();
       const QString sizeMap = i.value();
 
+/*
+      if (not that->sizeMapValidationFromEntry(entry,false)) {
+        cout << "Size map for entry " << entry.toStdString() << " is not valid" << endl;
+        ok = false;
+        continue;
+      }
+*/
       if (sizeMap == "__TO_DELETE__") {
         cout << "Delete entry " << entry.toStdString() << " from engine" << endl;
         h->UnsetEntry(entry.toLatin1().constData());
-        ok = false;
-        break;
       }
-
-      QString fullSizeMap;
-      fullSizeMap = QString("");
-      if (that->mySMPShapeTypeMap[entry]  == TopAbs_FACE)
-        fullSizeMap = QString("def f(u,v): return ") + sizeMap;
-      else if (that->mySMPShapeTypeMap[entry]  == TopAbs_EDGE)
-        fullSizeMap = QString("def f(t): return ") + sizeMap;
-      else if (that->mySMPShapeTypeMap[entry] == TopAbs_VERTEX)
-        fullSizeMap = QString("def f(): return ") + sizeMap;
-
-      cout << "SetSizeMapEntry("<<entry.toStdString()<<") = " <<fullSizeMap.toStdString() << endl;
-      h->SetSizeMapEntry( entry.toLatin1().constData(), fullSizeMap.toLatin1().constData() );
+      else if (sizeMap.startsWith("ATTRACTOR")) {
+        cout << "SetAttractorEntry(" << entry.toStdString() << ")= " << sizeMap.toStdString()  << endl;
+        h->SetAttractorEntry( entry.toLatin1().constData(), sizeMap.toLatin1().constData() );
+      }
+      else if (sizeMap.startsWith("def")) {
+        cout << "SetCustomSizeMapEntry(" << entry.toStdString() << ")= " << sizeMap.toStdString()  << endl;
+//        h->SetCustomSizeMapEntry( entry.toLatin1().constData(), sizeMap.toLatin1().constData() );
+      }
+      else {
+        QString fullSizeMap;
+        fullSizeMap = QString("");
+        if (that->mySMPShapeTypeMap[entry]  == TopAbs_FACE)
+          fullSizeMap = QString("def f(u,v): return ") + sizeMap;
+        else if (that->mySMPShapeTypeMap[entry]  == TopAbs_EDGE)
+          fullSizeMap = QString("def f(t): return ") + sizeMap;
+        else if (that->mySMPShapeTypeMap[entry] == TopAbs_VERTEX)
+          fullSizeMap = QString("def f(): return ") + sizeMap;
+
+        cout << "SetSizeMapEntry("<<entry.toStdString()<<") = " <<fullSizeMap.toStdString() << endl;
+        h->SetSizeMapEntry( entry.toLatin1().constData(), fullSizeMap.toLatin1().constData() );
+      }
     }
   }
   catch(const SALOME::SALOME_Exception& ex)
@@ -1030,24 +1087,40 @@ void BLSURFPluginGUI_HypothesisCreator::onRemoveMap()
       mySizeMapTable->removeRow(row );
   }
   mySizeMapTable->resizeColumnToContents(SMP_NAME_COLUMN);
-  mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
+  mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
 }
 
 void BLSURFPluginGUI_HypothesisCreator::onSetSizeMap(int row,int col)
 {
-  cout<<"BLSURFPluginGUI_HypothesisCreator::onSetSizeMap()"<<endl;
+  cout<<"BLSURFPluginGUI_HypothesisCreator::onSetSizeMap("<< row << "," << col << ")"<<endl;
   if (col == SMP_SIZEMAP_COLUMN) {
     BLSURFPluginGUI_HypothesisCreator* that = (BLSURFPluginGUI_HypothesisCreator*)this;
     QString entry   = that->mySizeMapTable->item(row, SMP_ENTRY_COLUMN)->text();
     QString sizeMap = that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->text().trimmed();
+    cout << "entry: " << entry.toStdString() << ", sizeMap: " << sizeMap.toStdString() << endl;
     if (not that->mySMPShapeTypeMap.contains(entry))
       return;
-    QString prevSizeMap = that->mySMPMap[entry];
-    that->mySMPMap[entry] = sizeMap;
-    if (not sizeMapValidationFromEntry(entry)) {
-      that->mySMPMap[entry] = prevSizeMap;
-      that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setText(prevSizeMap);
+    if (that->mySMPMap.contains(entry))
+      if (that->mySMPMap[entry] == sizeMap)
+        return;
+    QColor* bgColor = new QColor("white");
+    QColor* fgColor = new QColor("black");
+    if (not sizeMap.isEmpty()) {
+      that->mySMPMap[entry] = sizeMap;
+      if (not sizeMapValidationFromRow(row)) {
+        bgColor->setRgb(255,0,0);
+        fgColor->setRgb(255,255,255);
+      }
     }
+    else {
+      cout << "Size map empty: reverse to precedent value" << endl;
+      that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setText(that->mySMPMap[entry]);
+    }
+    that->mySizeMapTable->item(row, SMP_NAME_COLUMN)->setBackground(QBrush(*bgColor));
+    that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setBackground(QBrush(*bgColor));
+    that->mySizeMapTable->item(row, SMP_NAME_COLUMN)->setForeground(QBrush(*fgColor));
+    that->mySizeMapTable->item(row, SMP_SIZEMAP_COLUMN)->setForeground(QBrush(*fgColor));
+    mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
   }
 }
 
@@ -1077,7 +1150,8 @@ void BLSURFPluginGUI_HypothesisCreator::insertElementType(TopAbs_ShapeEnum typeS
 
   TopoDS_Shape S;
   string entry, shapeName;
-  LightApp_SelectionMgr* aSel = GeomToolSelected->selectionMgr();
+//  LightApp_SelectionMgr* aSel = GeomToolSelected->selectionMgr();
+
   SALOME_ListIO ListSelectedObjects;
   aSel->selectedObjects(ListSelectedObjects, NULL, false );
   if (!ListSelectedObjects.IsEmpty())
@@ -1116,7 +1190,7 @@ void BLSURFPluginGUI_HypothesisCreator::insertElementType(TopAbs_ShapeEnum typeS
         mySizeMapTable->setItem( row, SMP_SIZEMAP_COLUMN, new QTableWidgetItem( sizeMap ) );
         mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN )->setFlags( Qt::ItemIsSelectable |Qt::ItemIsEditable   |Qt::ItemIsEnabled );
         mySizeMapTable->resizeColumnToContents( SMP_NAME_COLUMN );
-        mySizeMapTable->resizeColumnToContents(SMP_ENTRY_COLUMN);
+        mySizeMapTable->resizeColumnToContents(SMP_SIZEMAP_COLUMN);
         mySizeMapTable->clearSelection();
         mySizeMapTable->scrollToItem( mySizeMapTable->item( row, SMP_SIZEMAP_COLUMN ) );
 
@@ -1139,14 +1213,16 @@ bool BLSURFPluginGUI_HypothesisCreator::sizeMapsValidation()
   return true;
 }
 
-bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow(int myRow)
+bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow(int myRow, bool displayError)
 {
   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromRow()"<<endl;
   QString myEntry   = mySizeMapTable->item( myRow, SMP_ENTRY_COLUMN )->text();
-  return sizeMapValidationFromEntry(myEntry); 
+  bool res = sizeMapValidationFromEntry(myEntry,displayError);
+  mySizeMapTable->setFocus();
+  return res; 
 }
 
-bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEntry)
+bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEntry, bool displayError)
 {
   cout<<"BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry()"<<endl;
 
@@ -1163,14 +1239,41 @@ bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEnt
 
   string expr;
 
-  if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_FACE)
-    expr = "def f(u,v) : return " + that->mySMPMap[myEntry].toStdString();
-  else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_EDGE)
-    expr = "def f(t) : return " + that->mySMPMap[myEntry].toStdString();
-  else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_VERTEX)
-    expr = "def f() : return " + that->mySMPMap[myEntry].toStdString();
+  if (that->mySMPMap[myEntry].startsWith("def")) {
+    cout << "custom function" << endl;
+    expr = that->mySMPMap[myEntry].toStdString();
+  }
+  else if (that->mySMPMap[myEntry].startsWith("ATTRACTOR")) {
+    cout << "Attractor" << endl;
+//    if ((that->mySMPMap[myEntry].count(QRegExp("ATTRACTOR([0-9])")) != 1))
+    if ((that->mySMPMap[myEntry].count('(') != 1) or 
+        (that->mySMPMap[myEntry].count(')') != 1) or
+        (that->mySMPMap[myEntry].count(';') != 4) or
+        (that->mySMPMap[myEntry].size() == 15)){
+      if (displayError)
+        SUIT_MessageBox::warning( dlg(),"Definition of attractor : Error" ,"An attractor is defined with the following pattern: ATTRACTOR(xa;ya;za;a;b)" );
+      return false;
+    }
+    return true;
+  }
+  else {
+    // case size map is empty
+    if (that->mySMPMap[myEntry].isEmpty()) {
+      if (displayError)
+        SUIT_MessageBox::warning( dlg(),"Definition of size map : Error" , "Size map can't be empty");
+      return false;
+    }
 
-  assert(Py_IsInitialized());
+    if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_FACE)
+      expr = "def f(u,v) : return " + that->mySMPMap[myEntry].toStdString();
+    else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_EDGE)
+      expr = "def f(t) : return " + that->mySMPMap[myEntry].toStdString();
+    else if ( that->mySMPShapeTypeMap[myEntry] == TopAbs_VERTEX)
+      expr = "def f() : return " + that->mySMPMap[myEntry].toStdString();
+  }
+  //assert(Py_IsInitialized());
+  if (not Py_IsInitialized())
+    throw ("Erreur: Python interpreter is not initialized");
   PyGILState_STATE gstate;
   gstate = PyGILState_Ensure();
  
@@ -1186,7 +1289,8 @@ bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEnt
     PyErr_Print();
     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));
     Py_DECREF(new_stderr);
-    SUIT_MessageBox::warning( dlg(),"Definition of Python Function : Error" ,err_description.c_str() );
+    if (displayError)
+      SUIT_MessageBox::warning( dlg(),"Definition of Python Function : Error" ,err_description.c_str() );
     PyGILState_Release(gstate);
     return false;
   }
@@ -1202,7 +1306,8 @@ bool BLSURFPluginGUI_HypothesisCreator::sizeMapValidationFromEntry(QString myEnt
     PyErr_Print();                                                                             
     PySys_SetObject("stderr", PySys_GetObject("__stderr__"));                                  
     Py_DECREF(new_stderr);
-    SUIT_MessageBox::warning( dlg(),"Python Error" ,err_description.c_str() );
+    if (displayError)
+      SUIT_MessageBox::warning( dlg(),"Python Error" ,err_description.c_str() );
     PyGILState_Release(gstate);
     return false;
   }
index 2c85f451b248c41168ba0ea178bab049c285f224..4db9d65fd4894320ff59519478887e36029b8966 100644 (file)
@@ -51,8 +51,6 @@ class QCheckBox;
 class QLineEdit;
 class QTableWidget;
 class QTableView;
-class QStandardItemModel;
-class QStandardItem;
 class QModelIndex;
 class QSpinBox;
 class QMenu;
@@ -106,14 +104,15 @@ protected slots:
   void                onAddMapOnPoint();
   void                onRemoveMap();
   void                onSetSizeMap(int,int);
+  void                onSetSelectionFilter(int);
 
 private:
   bool                readParamsFromHypo( BlsurfHypothesisData& ) const;
   QString             readParamsFromWidgets( BlsurfHypothesisData& ) const;
   bool                storeParamsToHypo( const BlsurfHypothesisData& ) const;
   bool                sizeMapsValidation();
-  bool                sizeMapValidationFromRow(int);
-  bool                sizeMapValidationFromEntry(QString);
+  bool                sizeMapValidationFromRow(int,bool displayError = true);
+  bool                sizeMapValidationFromEntry(QString,bool displayError = true);
   GeomSelectionTools* getGeomSelectionTool();
 
 private:
@@ -149,6 +148,7 @@ private:
   QMap<QString, QString>          mySMPMap;
   QMap<QString, TopAbs_ShapeEnum> mySMPShapeTypeMap;
   GeomSelectionTools*             GeomToolSelected;
+  LightApp_SelectionMgr*          aSel;
 
   BLSURFPlugin::string_array_var myOptions;