]> SALOME platform Git repositories - modules/shaper.git/blob - src/Model/Model_AttributeSelection.cpp
Salome HOME
Added a selection attribute and naming basic mechanisms
[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
11 #include <TNaming_Selector.hxx>
12 #include <TNaming_NamedShape.hxx>
13 #include <TopoDS_Shape.hxx>
14
15 using namespace std;
16
17 void Model_AttributeSelection::setValue(const ResultBodyPtr& theContext,
18   const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
19 {
20   const boost::shared_ptr<GeomAPI_Shape>& anOldShape = value();
21   bool isOldShape = 
22     (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
23   if (isOldShape) return; // shape is the same, so context is also unchanged
24   // update the referenced object if needed
25   bool isOldContext = theContext == myRef.value();
26   if (!isOldContext)
27     myRef.setValue(theContext);
28   
29   // perform the selection
30   TNaming_Selector aSel(myRef.myRef->Label());
31   TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : TopoDS_Shape();
32   TopoDS_Shape aContext = theContext->shape()->impl<TopoDS_Shape>();
33   aSel.Select(aNewShape, aContext);
34   myIsInitialized = true;
35
36   owner()->data()->sendAttributeUpdated(this);
37 }
38
39 boost::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
40 {
41   boost::shared_ptr<GeomAPI_Shape> aResult;
42   if (myIsInitialized) {
43     Handle(TNaming_NamedShape) aSelection;
44     if (myRef.myRef->Label().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
45       TopoDS_Shape aSelShape = aSelection->Get();
46       aResult->setImpl(&aSelShape);
47     }
48   }
49   return aResult;
50 }
51
52 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
53   : myRef(theLabel)
54 {
55   myIsInitialized = myRef.isInitialized();
56 }
57
58 ResultBodyPtr Model_AttributeSelection::context() {
59   return boost::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
60 }