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