Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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_AttributeIntArray.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24
25 #include <TDF_Label.hxx>
26
27 #include <memory>
28
29 #include <map>
30 #include <list>
31 #include <string>
32 #include <set>
33
34 class ModelAPI_Attribute;
35
36 /**\class Model_Data
37  * \ingroup DataModel
38  * \brief General object of the application that allows
39  * to get/set attributes from the document and compute result of an operation.
40  */
41
42 class Model_Data : public ModelAPI_Data
43 {
44   TDF_Label myLab;  ///< label of the feature in the document
45   /// All attributes of the object identified by the attribute ID
46   std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
47
48   /// needed here to emit signal that object changed on change of the attribute
49   ObjectPtr myObject;
50
51   /// List of attributes referenced to owner (updated only during the transaction change)
52   std::set<AttributePtr> myRefsToMe;
53   /// flag that may block the "attribute updated" sending
54   bool mySendAttributeUpdated;
55
56   Model_Data();
57
58   /// Returns label of this feature
59   TDF_Label label()
60   {
61     return myLab;
62   }
63
64   friend class Model_Document;
65   friend class Model_Update;
66   friend class Model_AttributeReference;
67   friend class Model_AttributeRefAttr;
68   friend class Model_AttributeRefList;
69   friend class Model_AttributeSelection;
70
71  public:
72   /// Returns the name of the feature visible by the user in the object browser
73   MODEL_EXPORT virtual std::string name();
74   /// Defines the name of the feature visible by the user in the object browser
75   MODEL_EXPORT virtual void setName(const std::string& theName);
76   /// Returns the attribute that references to another document
77   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
78   /// Returns the attribute that contains real value with double precision
79   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
80   /// Returns the attribute that contains integer value
81   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
82     integer(const std::string& theID);
83   /// Returns the attribute that contains reference to a feature
84   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
85     reference(const std::string& theID);
86   /// Returns the attribute that contains selection to a shape
87   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
88     selection(const std::string& theID);
89   /// Returns the attribute that contains selection to a shape
90   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList> 
91     selectionList(const std::string& theID);
92   /// Returns the attribute that contains reference to an attribute of a feature
93   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
94     refattr(const std::string& theID);
95   /// Returns the attribute that contains list of references to features
96   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
97     reflist(const std::string& theID);
98   /// Returns the attribute that contains boolean value
99   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
100     boolean(const std::string& theID);
101   /// Returns the attribute that contains real value with double precision
102   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
103     string(const std::string& theID);
104   /// Returns the attribute that contains integer values array
105   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
106     intArray(const std::string& theID);
107
108   /// Returns the generic attribute by identifier
109   /// \param theID identifier of the attribute
110   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
111   /// Returns all attributes of the feature of the given type
112   /// or all attributes if "theType" is empty
113   MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
114     attributes(const std::string& theType);
115   /// Returns all attributes ids of the feature of the given type
116   /// or all attributes if "theType" is empty
117   MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
118
119   /// Identifier by the id (not fast, iteration by map)
120   /// \param theAttr attribute already created in this data
121   MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
122   /// Returns true if data belongs to same features
123   MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
124   /// Returns true if it is correctly connected t othe data model
125   MODEL_EXPORT virtual bool isValid();
126
127   /// Returns the label where the shape must be stored (used in ResultBody)
128   TDF_Label& shapeLab()
129   {
130     return myLab;
131   }
132
133   /// Initializes object by the attributes: must be called just after the object is created
134   /// for each attribute of the object
135   /// \param theID identifier of the attribute that can be referenced by this ID later
136   /// \param theAttrType type of the created attribute (received from the type method)
137   /// \returns the just created attribute
138   MODEL_EXPORT virtual AttributePtr 
139     addAttribute(const std::string& theID, const std::string theAttrType);
140
141   /// Useful method for "set" methods of the attributes: sends an UPDATE event and
142   /// makes attribute initialized
143   MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
144   /// Blocks sending "attribute updated" if theBlock is true
145   MODEL_EXPORT virtual void blockSendAttributeUpdated(const bool theBlock);
146
147   /// Puts feature to the document data sub-structure
148   MODEL_EXPORT void setLabel(TDF_Label theLab);
149
150   /// Sets the object of this data
151   MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
152   {
153     myObject = theObject;
154   }
155
156   /// Erases all the data from the data model
157   MODEL_EXPORT virtual void erase();
158
159   /// Stores the state of the object to execute it later accordingly
160   MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
161
162   /// Returns the state of the latest execution of the feature
163   MODEL_EXPORT virtual ModelAPI_ExecState execState();
164
165   /// Registers error during the execution, causes the ExecutionFailed state
166   MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
167
168   /// Registers error during the execution, causes the ExecutionFailed state
169   MODEL_EXPORT virtual std::string error() const;
170
171   /// Returns the identifier of feature-owner, unique in this document
172   MODEL_EXPORT virtual int featureId() const;
173
174   /// returns all objects referenced to this
175   MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
176
177   /// returns all references by attributes of this data
178   /// \param theRefs returned list of pairs: id of referenced attribute and list of referenced objects
179   MODEL_EXPORT virtual void referencesToObjects(
180     std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
181
182   /// Copies all atributes content into theTarget data
183   MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
184
185 private:
186   /// Removes all information about back references
187   void eraseBackReferences();
188   /// Adds a back reference (with identifier which attribute references to this object
189   /// It does not change the consealment flag of the data object result
190   /// \param theFeature feature referenced to this
191   /// \param theAttrID identifier of the attribute that is references from theFeature to this
192   void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
193   /// Adds a back reference (with identifier which attribute references to this object
194   /// \param theFeature feature referenced to this
195   /// \param theAttrID identifier of the attribute that is references from theFeature to this
196   /// \param theApplyConcealment applies consealment flag changes
197   void addBackReference(FeaturePtr theFeature, std::string theAttrID, 
198     const bool theApplyConcealment = true);
199 };
200
201 /// Generic method to register back reference, used in referencing attributes.
202 /// Without concealment change, it will be done later, on synchronization.
203 #define ADD_BACK_REF(TARGET) \
204   if (TARGET.get() != NULL) { \
205     FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
206     if (anAttributeOwnerFeature.get()) { \
207       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
208         (TARGET)->data()); \
209       aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
210     } \
211   }
212
213 /// Generic method to unregister back reference, used in referencing attributes.
214 /// Without concealment change, it will be done later, on synchronization.
215 #define REMOVE_BACK_REF(TARGET) \
216   if (TARGET.get() != NULL) { \
217     FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
218     if (anAttOwnerFeature.get()) { \
219       std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
220         (TARGET)->data()); \
221       aTargetData->removeBackReference(anAttOwnerFeature, id()); \
222     } \
223   }
224
225 #endif