Salome HOME
Fix problem with dumping names of entities copied by Multi-Rotation/Translation ...
[modules/shaper.git] / src / ModelHighAPI / ModelHighAPI_Selection.cpp
index e997aa4e01db5cd53d2304e9e7f9c4de986de32a..e63c40ec8b3b1c5d1577dc4045d1d2c67286983a 100644 (file)
@@ -1,5 +1,6 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
 // Name   : ModelHighAPI_Selection.cpp
-// Purpose: 
+// Purpose:
 //
 // History:
 // 06/06/16 - Sergey POKHODENKO - Creation of the file
@@ -7,20 +8,30 @@
 //--------------------------------------------------------------------------------------
 #include "ModelHighAPI_Selection.h"
 
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_AttributeIntArray.h>
 #include <ModelAPI_AttributeSelection.h>
 #include <ModelAPI_AttributeSelectionList.h>
+#include <ModelAPI_ResultCompSolid.h>
 //--------------------------------------------------------------------------------------
 
 //--------------------------------------------------------------------------------------
+ModelHighAPI_Selection::ModelHighAPI_Selection()
+: myVariantType(VT_Empty)
+{
+}
+
 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::shared_ptr<ModelAPI_Result>& theContext,
                                                const std::shared_ptr<GeomAPI_Shape>& theSubShape)
-: myValue(ResultSubShapePair(theContext, theSubShape))
+: myVariantType(VT_ResultSubShapePair)
+, myResultSubShapePair(theContext, theSubShape)
 {
 }
 
 ModelHighAPI_Selection::ModelHighAPI_Selection(const std::string& theType,
                                                const std::string& theSubShapeName)
-: myValue(TypeSubShapeNamePair(theType, theSubShapeName))
+: myVariantType(VT_TypeSubShapeNamePair)
+, myTypeSubShapeNamePair(theType, theSubShapeName)
 {
 }
 
@@ -29,40 +40,108 @@ ModelHighAPI_Selection::~ModelHighAPI_Selection()
 }
 
 //--------------------------------------------------------------------------------------
-struct fill_visitor : boost::static_visitor<void>
-{
-  mutable std::shared_ptr<ModelAPI_AttributeSelection> myAttribute;
-
-  fill_visitor(const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute)
-  : myAttribute(theAttribute) {}
-
-  void operator()(const ResultSubShapePair & thePair) const { myAttribute->setValue(thePair.first, thePair.second); }
-  void operator()(const TypeSubShapeNamePair & thePair) const { myAttribute->selectSubShape(thePair.first, thePair.second); }
-};
-
 void ModelHighAPI_Selection::fillAttribute(
     const std::shared_ptr<ModelAPI_AttributeSelection> & theAttribute) const
 {
-  boost::apply_visitor(fill_visitor(theAttribute), myValue);
+  switch(myVariantType) {
+    case VT_Empty: return;
+    case VT_ResultSubShapePair:
+      theAttribute->setValue(myResultSubShapePair.first, myResultSubShapePair.second);
+      return;
+    case VT_TypeSubShapeNamePair:
+      theAttribute->selectSubShape(myTypeSubShapeNamePair.first, myTypeSubShapeNamePair.second);
+      return;
+  }
 }
 
 //--------------------------------------------------------------------------------------
-struct append_visitor : boost::static_visitor<void>
+void ModelHighAPI_Selection::appendToList(
+    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
 {
-  mutable std::shared_ptr<ModelAPI_AttributeSelectionList> myAttribute;
+  switch(myVariantType) {
+    case VT_Empty: return;
+    case VT_ResultSubShapePair:
+      theAttribute->append(myResultSubShapePair.first, myResultSubShapePair.second);
+      return;
+    case VT_TypeSubShapeNamePair:
+      // Note: the reverse order (first - type, second - sub-shape name)
+      theAttribute->append(myTypeSubShapeNamePair.second, myTypeSubShapeNamePair.first);
+      return;
+  }
+}
+
+//==================================================================================================
+ModelHighAPI_Selection::VariantType ModelHighAPI_Selection::variantType() const
+{
+  return myVariantType;
+}
 
-  append_visitor(const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute)
-  : myAttribute(theAttribute) {}
+//==================================================================================================
+ResultSubShapePair ModelHighAPI_Selection::resultSubShapePair() const
+{
+  return myResultSubShapePair;
+}
+
+//==================================================================================================
+TypeSubShapeNamePair ModelHighAPI_Selection::typeSubShapeNamePair() const
+{
+  return myTypeSubShapeNamePair;
+}
 
-  void operator()(const ResultSubShapePair & thePair) const { myAttribute->append(thePair.first, thePair.second); }
-  void operator()(const TypeSubShapeNamePair & thePair) const {
-    // Note: the reverse order (first - type, second - sub-shape name)
-    myAttribute->append(thePair.second, thePair.first);
+//==================================================================================================
+std::string ModelHighAPI_Selection::shapeType() const
+{
+  switch(myVariantType) {
+  case VT_ResultSubShapePair:
+    return myResultSubShapePair.second.get() ? myResultSubShapePair.second->shapeTypeStr() :
+                                               myResultSubShapePair.first->shape()->shapeTypeStr();
+  case VT_TypeSubShapeNamePair: return myTypeSubShapeNamePair.first;
   }
-};
 
-void ModelHighAPI_Selection::appendToList(
-    const std::shared_ptr<ModelAPI_AttributeSelectionList> & theAttribute) const
+  return "SHAPE";
+}
+
+//==================================================================================================
+void ModelHighAPI_Selection::setName(const std::string& theName)
 {
-  boost::apply_visitor(append_visitor(theAttribute), myValue);
+  if (myVariantType == VT_ResultSubShapePair)
+    myResultSubShapePair.first->data()->setName(theName);
+}
+
+void ModelHighAPI_Selection::setColor(int theRed, int theGreen, int theBlue)
+{
+  if (myVariantType != VT_ResultSubShapePair)
+    return;
+
+  AttributeIntArrayPtr aColor =
+      myResultSubShapePair.first->data()->intArray(ModelAPI_Result::COLOR_ID());
+  aColor->setSize(3);
+  aColor->setValue(0, theRed);
+  aColor->setValue(1, theGreen);
+  aColor->setValue(2, theBlue);
+}
+
+void ModelHighAPI_Selection::setDeflection(double theValue)
+{
+  if (myVariantType != VT_ResultSubShapePair)
+    return;
+
+  AttributeDoublePtr aDeflectionAttr =
+    myResultSubShapePair.first->data()->real(ModelAPI_Result::DEFLECTION_ID());
+
+  aDeflectionAttr->setValue(theValue);
+}
+
+ModelHighAPI_Selection ModelHighAPI_Selection::subResult(int theIndex)
+{
+  if (myVariantType != VT_ResultSubShapePair)
+    return ModelHighAPI_Selection();
+
+  ResultCompSolidPtr aCompSolid =
+      std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(myResultSubShapePair.first);
+  if (!aCompSolid)
+    return ModelHighAPI_Selection();
+
+  ResultBodyPtr aResult = aCompSolid->subResult(theIndex);
+  return ModelHighAPI_Selection(aResult, aResult->shape());
 }