Salome HOME
Sketch line feature based on points attributes
[modules/shaper.git] / src / Model / Model_Data.cpp
1 // File:        Model_Data.hxx
2 // Created:     21 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_Data.h>
6 #include <Model_AttributeDocRef.h>
7 #include <Model_AttributeDouble.h>
8 #include <GeomData_Point.h>
9 #include <GeomData_Point2D.h>
10 #include <TDataStd_Name.hxx>
11
12 using namespace std;
13
14 Model_Data::Model_Data()
15 {
16 }
17
18 void Model_Data::setLabel(TDF_Label& theLab)
19 {
20   myLab = theLab;
21 }
22
23 string Model_Data::getName()
24 {
25   Handle(TDataStd_Name) aName;
26   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName))
27     return string(TCollection_AsciiString(aName->Get()).ToCString());
28   return ""; // not defined
29 }
30
31 void Model_Data::setName(string theName)
32 {
33   TDataStd_Name::Set(myLab, theName.c_str());
34 }
35
36 void Model_Data::addAttribute(string theID, string theAttrType)
37 {
38   TDF_Label anAttrLab = myLab.FindChild(myAttrs.size() + 1);
39   ModelAPI_Attribute* anAttr = 0;
40   if (theAttrType == ModelAPI_AttributeDocRef::type())
41     anAttr = new Model_AttributeDocRef(anAttrLab);
42   else if (theAttrType == ModelAPI_AttributeDouble::type())
43     anAttr = new Model_AttributeDouble(anAttrLab);
44   else if (theAttrType == GeomData_Point::type())
45     anAttr = new GeomData_Point(anAttrLab);
46   else if (theAttrType == GeomData_Point2D::type())
47     anAttr = new GeomData_Point2D(anAttrLab);
48
49   if (anAttr)
50     myAttrs[theID] = boost::shared_ptr<ModelAPI_Attribute>(anAttr);
51   else
52     ; // TODO: generate error on unknown attribute request and/or add mechanism for customization
53 }
54
55 boost::shared_ptr<ModelAPI_AttributeDocRef> Model_Data::docRef(const string theID)
56 {
57   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
58   if (aFound == myAttrs.end()) {
59     // TODO: generate error on unknown attribute request and/or add mechanism for customization
60     return boost::shared_ptr<ModelAPI_AttributeDocRef>();
61   }
62   boost::shared_ptr<ModelAPI_AttributeDocRef> aRes = 
63     boost::dynamic_pointer_cast<ModelAPI_AttributeDocRef>(aFound->second);
64   if (!aRes) {
65     // TODO: generate error on invalid attribute type request
66   }
67   return aRes;
68 }
69
70 boost::shared_ptr<ModelAPI_AttributeDouble> Model_Data::real(const string theID)
71 {
72   map<string, boost::shared_ptr<ModelAPI_Attribute> >::iterator aFound = myAttrs.find(theID);
73   if (aFound == myAttrs.end()) {
74     // TODO: generate error on unknown attribute request and/or add mechanism for customization
75     return boost::shared_ptr<ModelAPI_AttributeDouble>();
76   }
77   boost::shared_ptr<ModelAPI_AttributeDouble> aRes = 
78     boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aFound->second);
79   if (!aRes) {
80     // TODO: generate error on invalid attribute type request
81   }
82   return aRes;
83 }
84
85 boost::shared_ptr<ModelAPI_Attribute> Model_Data::attribute(const std::string theID)
86 {
87   boost::shared_ptr<ModelAPI_Attribute> aResult;
88   if (myAttrs.find(theID) == myAttrs.end()) // no such attribute
89     return aResult;
90   return myAttrs[theID];
91 }