Salome HOME
Bug #119: create obstacles from several objects in GEOM is wrong.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportGeomObjectOp.cxx
index fd32dfad5d30c928c5489c4c7321448d8e2163ab..4ba06589d180a3cce243b3e641ff58c3db810d2e 100644 (file)
@@ -27,6 +27,7 @@
 #include "HYDROGUI_DataModel.h"
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_Tool.h"
+#include "HYDROGUI_UpdateFlags.h"
 
 #include <HYDROData_Obstacle.h>
 #include <HYDROData_Iterator.h>
@@ -74,6 +75,17 @@ void HYDROGUI_ImportGeomObjectOp::startOperation()
       // Reset the panel state
       aPanel->reset();
 
+      // Set default name
+      SalomeApp_Study* aStudy = 
+        dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
+      if ( aStudy ) {
+        QString anEntry = myGeomObjects.first();
+        _PTR(SObject) aSObject( aStudy->studyDS()->FindObjectID( qPrintable(anEntry) ) );
+        if ( aSObject ) {
+          aPanel->setDefaultName( QString::fromStdString(aSObject->GetName()) );
+        }
+      }
+
       // Pass the existing obstacle names to the panel
       QStringList anObstacles = 
         HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_OBSTACLE );
@@ -110,17 +122,34 @@ bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
   QString anObstacleName;
   Handle(HYDROData_Obstacle) anObstacleToEdit;  
 
-  // Get panel
-  HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
-  if ( aPanel ) {
-    QString anEditedName = aPanel->getEditedObstacleName().simplified();
+  if ( myGeomObjects.count() == 1 ) {
+    // Get panel
+    HYDROGUI_ObstacleDlg* aPanel = ::qobject_cast<HYDROGUI_ObstacleDlg*>( inputPanel() );
+    if ( aPanel ) {
+      // Check obstacle name
+      anObstacleName = aPanel->getObstacleName().simplified();
+      if ( anObstacleName.isEmpty() ) {
+        theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
+        return false;
+      }
+
+      // Get obstacle to edit
+      QString anEditedName = aPanel->getEditedObstacleName().simplified();
 
-    if ( !anEditedName.isEmpty() ) {
-      anObstacleToEdit = Handle(HYDROData_Obstacle)::DownCast(
-        HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
+      if ( !anEditedName.isEmpty() ) {
+        anObstacleToEdit = Handle(HYDROData_Obstacle)::DownCast(
+          HYDROGUI_Tool::FindObjectByName( module(), anEditedName, KIND_OBSTACLE ) );
+      }
     }
 
-    anObstacleName = anEditedName; //TODO: aPanel->getObstacleName();
+    if( anObstacleToEdit.IsNull() || anObstacleToEdit->GetName() != anObstacleName ) {
+      // check that there are no other objects with the same name in the document
+      Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObstacleName );
+      if( !anObject.IsNull() ) {
+        theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObstacleName );
+        return false;
+      }
+    }
   }
 
   bool anIsOk = false;
@@ -133,7 +162,6 @@ bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
       TopoDS_Shape aShape = GEOMBase::GetShapeFromIOR( aSObject->GetIOR().c_str() );
       if ( !aShape.IsNull() ) {
         // Create/edit an obstacle object
-        // TODO refactoring: get rid of obstacle from TopoDS_Shape creation code copy/paste
         Handle(HYDROData_Obstacle) anObstacle; 
 
         if ( anObstacleToEdit.IsNull() ) {
@@ -146,18 +174,21 @@ bool HYDROGUI_ImportGeomObjectOp::processApply( int& theUpdateFlags,
         // Set name
         if ( anObstacleName.isEmpty() ) {
           QString aName = QString::fromStdString( aSObject->GetName() );
-          anObstacleName = HYDROGUI_Tool::GenerateObjectName( module(), aName );
+          anObstacleName = HYDROGUI_Tool::GenerateObjectName( 
+            module(), aName, QStringList(), true );
         }
         if ( anObstacle->GetName() != anObstacleName ) {
           anObstacle->SetName( anObstacleName );
         }
 
+        anObstacleName.clear();
+
         // Set shape
         anObstacle->SetShape3D( aShape );
 
         // Set operation status
         anIsOk = true;
-        theUpdateFlags = UF_Model;
+        theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
       }
     }
   }