]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_AttributeSelection.cpp
Salome HOME
62659de6c59560f4468b759b0102c7c5b0d09fd6
[modules/shaper.git] / src / Model / Model_AttributeSelection.cpp
1 // File:        Model_AttributeSelection.h
2 // Created:     2 Oct 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include "Model_AttributeSelection.h"
6 #include "Model_Application.h"
7 #include "Model_Events.h"
8 #include "Model_Data.h"
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <GeomAPI_Shape.h>
13
14 #include <TNaming_Selector.hxx>
15 #include <TNaming_NamedShape.hxx>
16 #include <TopoDS_Shape.hxx>
17
18 using namespace std;
19
20 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
21   const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
22 {
23   const boost::shared_ptr<GeomAPI_Shape>& anOldShape = value();
24   bool isOldShape = 
25     (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
26   if (isOldShape) return; // shape is the same, so context is also unchanged
27   // update the referenced object if needed
28   bool isOldContext = theContext == myRef.value();
29   if (!isOldContext)
30     myRef.setValue(theContext);
31
32   // perform the selection
33   TNaming_Selector aSel(myRef.myRef->Label());
34   TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : TopoDS_Shape();
35   TopoDS_Shape aContext;
36
37   ResultBodyPtr aBody = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
38   if (aBody)
39     aContext = aBody->shape()->impl<TopoDS_Shape>();
40   else {
41     ResultConstructionPtr aConstr = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myRef.value());
42     if (aConstr)
43       aContext = aConstr->shape()->impl<TopoDS_Shape>();
44     else
45       throw std::invalid_argument("a result with shape is expected");
46   }
47   aSel.Select(aNewShape, aContext);
48   myIsInitialized = true;
49
50   owner()->data()->sendAttributeUpdated(this);
51 }
52
53 boost::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
54 {
55   boost::shared_ptr<GeomAPI_Shape> aResult;
56   if (myIsInitialized) {
57     Handle(TNaming_NamedShape) aSelection;
58     if (myRef.myRef->Label().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
59       TopoDS_Shape aSelShape = aSelection->Get();
60       aResult->setImpl(&aSelShape);
61     }
62   }
63   return aResult;
64 }
65
66 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
67   : myRef(theLabel)
68 {
69   myIsInitialized = myRef.isInitialized();
70 }
71
72 ResultPtr Model_AttributeSelection::context() {
73   return boost::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
74 }
75
76
77 void Model_AttributeSelection::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
78 {
79   myRef.setObject(theObject);
80 }