Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / ModuleBase / ModuleBase_WidgetShapeSelector.cpp
index 7c1d0f69a48d3b6f966b51bf0c49d4682d50f9bb..bd5e54e707b51b445aeeaad5f50eb4cac305047b 100644 (file)
@@ -2,24 +2,30 @@
 // Created:     2 June 2014
 // Author:      Vitaly Smetannikov
 
-#include "ModuleBase_WidgetShapeSelector.h"
-#include <ModuleBase_IWorkshop.h>
+#include <ModuleBase_WidgetShapeSelector.h>
+#include <ModuleBase_Definitions.h>
 #include <ModuleBase_ISelection.h>
-#include "ModuleBase_WidgetValue.h"
+#include <ModuleBase_IWorkshop.h>
 #include <ModuleBase_Tools.h>
-#include "ModuleBase_WidgetValueFeature.h"
-
+#include <ModuleBase_WidgetValueFeature.h>
+#include <Config_WidgetAPI.h>
 #include <Events_Loop.h>
-#include <ModelAPI_Events.h>
-#include <ModelAPI_Tools.h>
-
+#include <Events_Message.h>
+#include <GeomAPI_Interface.h>
+#include <GeomAPI_Shape.h>
+#include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_Data.h>
 #include <ModelAPI_Document.h>
-#include <ModelAPI_Object.h>
+#include <ModelAPI_Events.h>
+#include <ModelAPI_Feature.h>
 #include <ModelAPI_Result.h>
 #include <ModelAPI_AttributeReference.h>
 #include <ModelAPI_AttributeSelection.h>
+#include <ModelAPI_Session.h>
+#include <ModelAPI_Tools.h>
+#include <ModelAPI_ResultBody.h>
 #include <Config_WidgetAPI.h>
+#include <Events_Error.h>
 
 #include <GeomAPI_Shape.h>
 
 #include <QEvent>
 #include <QDockWidget>
 
-#include <stdexcept>
+#include <TopExp_Explorer.hxx>
+#include <TopoDS_Shape.hxx>
+
+#include <boost/smart_ptr/shared_ptr.hpp>
+
+#include <list>
+#include <string>
 
 typedef QMap<QString, TopAbs_ShapeEnum> ShapeTypes;
 static ShapeTypes MyShapeTypes;
@@ -44,16 +56,21 @@ TopAbs_ShapeEnum ModuleBase_WidgetShapeSelector::shapeType(const QString& theTyp
 {
   if (MyShapeTypes.count() == 0) {
     MyShapeTypes["face"] = TopAbs_FACE;
+    MyShapeTypes["faces"] = TopAbs_FACE;
     MyShapeTypes["vertex"] = TopAbs_VERTEX;
+    MyShapeTypes["vertices"] = TopAbs_VERTEX;
     MyShapeTypes["wire"] = TopAbs_WIRE;
     MyShapeTypes["edge"] = TopAbs_EDGE;
+    MyShapeTypes["edges"] = TopAbs_EDGE;
     MyShapeTypes["shell"] = TopAbs_SHELL;
     MyShapeTypes["solid"] = TopAbs_SOLID;
+    MyShapeTypes["solids"] = TopAbs_SOLID;
   }
   QString aType = theType.toLower();
   if (MyShapeTypes.contains(aType))
     return MyShapeTypes[aType];
-  throw std::invalid_argument("Shape type defined in XML is not implemented!");
+  Events_Error::send("Shape type defined in XML is not implemented!");
+  return TopAbs_SHAPE;
 }
 
 ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParent,
@@ -91,11 +108,7 @@ ModuleBase_WidgetShapeSelector::ModuleBase_WidgetShapeSelector(QWidget* theParen
   std::string aTypes = theData->getProperty("shape_types");
   myShapeTypes = QString(aTypes.c_str()).split(' ');
 
-  std::string aUseSubShapes = theData->getProperty("use_subshapes");
-  if (aUseSubShapes.length() > 0) {
-    QString aVal(aUseSubShapes.c_str());
-    myUseSubShapes = (aVal.toUpper() == "TRUE");
-  }
+  myUseSubShapes = theData->getBooleanAttribute("use_subshapes", false); 
 }
 
 //********************************************************************
@@ -116,9 +129,12 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
     boost::shared_ptr<ModelAPI_AttributeSelection> aSelect = 
       boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>(aData->attribute(attributeID()));
 
-    ResultBodyPtr aBody = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(mySelectedObject);
-    if (aBody)
+    ResultPtr aBody = boost::dynamic_pointer_cast<ModelAPI_Result>(mySelectedObject);
+    if (aBody) {
       aSelect->setValue(aBody, myShape);
+      updateObject(myFeature);
+      return true;
+    }
   } else {
     boost::shared_ptr<ModelAPI_AttributeReference> aRef = 
       boost::dynamic_pointer_cast<ModelAPI_AttributeReference>(aData->attribute(attributeID()));
@@ -127,9 +143,10 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const
     if (!(aObject && aObject->isSame(mySelectedObject))) {
       aRef->setValue(mySelectedObject);
       updateObject(myFeature);
+      return true;
     }
   }
-  return true;
+  return false;
 }
 
 //********************************************************************
@@ -172,12 +189,20 @@ void ModuleBase_WidgetShapeSelector::onSelectionChanged()
       return;
     if (mySelectedObject && aObject && mySelectedObject->isSame(aObject))
       return;
+    // Check that the selected object is result (others can not be accepted)
+    ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aObject);
+    if (!aRes)
+      return;
+    // Check that the result has a shape
+    GeomShapePtr aShape = ModelAPI_Tools::shape(aRes);
+    if (!aShape)
+      return;
 
     // Get sub-shapes from local selection
-    boost::shared_ptr<GeomAPI_Shape> aShape;
     if (myUseSubShapes) {
       NCollection_List<TopoDS_Shape> aShapeList;
-      myWorkshop->selection()->selectedShapes(aShapeList);
+      std::list<ObjectPtr> aOwners;
+      myWorkshop->selection()->selectedShapes(aShapeList, aOwners);
       if (aShapeList.Extent() > 0) {
         aShape = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
         aShape->setImpl(new TopoDS_Shape(aShapeList.First()));
@@ -209,7 +234,6 @@ void ModuleBase_WidgetShapeSelector::setObject(ObjectPtr theObj, boost::shared_p
     if (!myUseSubShapes) {
       static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_TOHIDE);
       ModelAPI_EventCreator::get()->sendUpdated(mySelectedObject, anEvent);
-      Events_Loop::loop()->flush(anEvent);
     }
   } 
   updateSelectionName();