Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / Model / Model_Data.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Data.hxx
4 // Created:     21 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Data_H_
8 #define Model_Data_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Attribute.h>
12 #include <ModelAPI_AttributeBoolean.h>
13 #include <ModelAPI_AttributeDocRef.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeInteger.h>
16 #include <ModelAPI_AttributeRefAttr.h>
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeString.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Object.h>
23
24 #include <TDF_Label.hxx>
25
26 #include <memory>
27
28 #include <map>
29 #include <list>
30 #include <string>
31 #include <set>
32
33 class ModelAPI_Attribute;
34
35 /**\class Model_Data
36  * \ingroup DataModel
37  * \brief General object of the application that allows
38  * to get/set attributes from the document and compute result of an operation.
39  */
40
41 class Model_Data : public ModelAPI_Data
42 {
43   TDF_Label myLab;  ///< label of the feature in the document
44   /// All attributes of the object identified by the attribute ID
45   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
46
47   /// needed here to emit signal that object changed on change of the attribute
48   ObjectPtr myObject;
49
50   /// List of attributes referenced to owner (updated only during the transaction change)
51   std::set<AttributePtr> myRefsToMe;
52
53   Model_Data();
54
55   /// Returns label of this feature
56   TDF_Label label()
57   {
58     return myLab;
59   }
60
61   friend class Model_Document;
62   friend class Model_Update;
63   friend class Model_AttributeReference;
64   friend class Model_AttributeRefAttr;
65   friend class Model_AttributeRefList;
66   friend class Model_AttributeSelection;
67
68  public:
69   /// Returns the name of the feature visible by the user in the object browser
70   MODEL_EXPORT virtual std::string name();
71   /// Defines the name of the feature visible by the user in the object browser
72   MODEL_EXPORT virtual void setName(const std::string& theName);
73   /// Returns the attribute that references to another document
74   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
75   /// Returns the attribute that contains real value with double precision
76   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
77   /// Returns the attribute that contains integer value
78   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
79     integer(const std::string& theID);
80   /// Returns the attribute that contains reference to a feature
81   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
82     reference(const std::string& theID);
83   /// Returns the attribute that contains selection to a shape
84   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
85     selection(const std::string& theID);
86   /// Returns the attribute that contains selection to a shape
87   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
88     selectionList(const std::string& theID);
89   /// Returns the attribute that contains reference to an attribute of a feature
90   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
91     refattr(const std::string& theID);
92   /// Returns the attribute that contains list of references to features
93   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
94     reflist(const std::string& theID);
95   /// Returns the attribute that contains boolean value
96   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
97     boolean(const std::string& theID);
98   /// Returns the attribute that contains real value with double precision
99   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
100     string(const std::string& theID);
101   /// Returns the generic attribute by identifier
102   /// \param theID identifier of the attribute
103   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
104   /// Returns all attributes of the feature of the given type
105   /// or all attributes if "theType" is empty
106   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
107     attributes(const std::string& theType);
108   /// Returns all attributes ids of the feature of the given type
109   /// or all attributes if "theType" is empty
110   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
111
112   /// Identifier by the id (not fast, iteration by map)
113   /// \param theAttr attribute already created in this data
114   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
115   /// Returns true if data belongs to same features
116   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
117   /// Returns true if it is correctly connected t othe data model
118   MODEL_EXPORT virtual bool isValid();
119
120   /// Returns the label where the shape must be stored (used in ResultBody)
121   TDF_Label& shapeLab()
122   {
123     return myLab;
124   }
125
126   /// Initializes object by the attributes: must be called just after the object is created
127   /// for each attribute of the object
128   /// \param theID identifier of the attribute that can be referenced by this ID later
129   /// \param theAttrType type of the created attribute (received from the type method)
130   MODEL_EXPORT virtual void addAttribute(const std::string& theID, const std::string theAttrType);
131
132   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
133   /// makes attribute initialized
134   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
135
136   /// Puts feature to the document data sub-structure
137   MODEL_EXPORT void setLabel(TDF_Label theLab);
138
139   /// Sets the object of this data
140   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
141   {
142     myObject = theObject;
143   }
144
145   /// Erases all the data from the data model
146   MODEL_EXPORT virtual void erase();
147
148   /// Stores the state of the object to execute it later accordingly
149   MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
150
151   /// Returns the state of the latest execution of the feature
152   MODEL_EXPORT virtual ModelAPI_ExecState execState();
153
154   /// Registers error during the execution, causes the ExecutionFailed state
155   MODEL_EXPORT virtual void setError(const std::string& theError);
156
157   /// Returns the identifier of feature-owner, unique in this document
158   MODEL_EXPORT virtual int featureId() const;
159
160   // returns all objects referenced to this
161   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
162
163 private:
164   // removes all information about back references
165   void eraseBackReferences();
166   // adds a back reference (with identifier which attribute references to this object
167   void addBackReference(FeaturePtr theFeature, std::string theAttrID);
168   // returns all references by attributes of this data
169   // \param the returned list of pairs: id of referenced attribute and list of referenced objects
170   void referencesToObjects(std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
171 };
172
173 #endif