1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Data.hxx
4 // Created: 21 Mar 2014
5 // Author: Mikhail PONIKAROV
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_AttributeRefAttrList.h>
20 #include <ModelAPI_AttributeString.h>
21 #include <ModelAPI_AttributeIntArray.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Feature.h>
24 #include <ModelAPI_Object.h>
26 #include <TDF_Label.hxx>
27 #include <TDataStd_BooleanArray.hxx>
36 class ModelAPI_Attribute;
40 * \brief General object of the application that allows
41 * to get/set attributes from the document and compute result of an operation.
44 class Model_Data : public ModelAPI_Data
46 TDF_Label myLab; ///< label of the feature in the document
47 /// All attributes of the object identified by the attribute ID
48 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
49 /// Array of flags of this data
50 Handle(TDataStd_BooleanArray) myFlags;
52 /// needed here to emit signal that object changed on change of the attribute
55 /// List of attributes referenced to owner (updated only during the transaction change)
56 std::set<AttributePtr> myRefsToMe;
57 /// flag that may block the "attribute updated" sending
58 bool mySendAttributeUpdated;
59 /// if some attribute was changed, but mySendAttributeUpdated was false, this stores this
60 std::list<ModelAPI_Attribute*> myWasChangedButBlocked;
62 /// Returns label of this feature
68 friend class Model_Document;
69 friend class Model_Objects;
70 friend class Model_Update;
71 friend class Model_AttributeReference;
72 friend class Model_AttributeRefAttr;
73 friend class Model_AttributeRefList;
74 friend class Model_AttributeRefAttrList;
75 friend class Model_AttributeSelection;
76 friend class Model_AttributeSelectionList;
77 friend class Model_ValidatorsFactory;
78 friend class Model_SelectionNaming;
81 /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
83 /// Returns the name of the feature visible by the user in the object browser
84 MODEL_EXPORT virtual std::string name();
85 /// Defines the name of the feature visible by the user in the object browser
86 MODEL_EXPORT virtual void setName(const std::string& theName);
87 /// Returns the attribute that references to another document
88 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
89 /// Returns the attribute that contains real value with double precision
90 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
91 /// Returns the attribute that contains double values array
92 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDoubleArray> realArray(const std::string& theID);
93 /// Returns the attribute that contains integer value
94 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
95 integer(const std::string& theID);
96 /// Returns the attribute that contains reference to a feature
97 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
98 reference(const std::string& theID);
99 /// Returns the attribute that contains selection to a shape
100 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
101 selection(const std::string& theID);
102 /// Returns the attribute that contains selection to a shape
103 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
104 selectionList(const std::string& theID);
105 /// Returns the attribute that contains reference to an attribute of a feature
106 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
107 refattr(const std::string& theID);
108 /// Returns the attribute that contains list of references to features
109 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
110 reflist(const std::string& theID);
111 /// Returns the attribute that contains list of references to features
112 /// or reference to an attribute of a feature
113 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttrList>
114 refattrlist(const std::string& theID);
115 /// Returns the attribute that contains boolean value
116 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
117 boolean(const std::string& theID);
118 /// Returns the attribute that contains real value with double precision
119 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
120 string(const std::string& theID);
121 /// Returns the attribute that contains integer values array
122 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
123 intArray(const std::string& theID);
125 /// Returns the generic attribute by identifier
126 /// \param theID identifier of the attribute
127 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
128 /// Returns all attributes of the feature of the given type
129 /// or all attributes if "theType" is empty
130 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
131 attributes(const std::string& theType);
132 /// Returns all attributes ids of the feature of the given type
133 /// or all attributes if "theType" is empty
134 MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
136 /// Identifier by the id (not fast, iteration by map)
137 /// \param theAttr attribute already created in this data
138 MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
139 /// Returns true if data belongs to same features
140 MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
141 /// Returns true if it is correctly connected t othe data model
142 MODEL_EXPORT virtual bool isValid();
144 /// Returns the label where the shape must be stored (used in ResultBody)
145 TDF_Label& shapeLab()
150 /// Initializes object by the attributes: must be called just after the object is created
151 /// for each attribute of the object
152 /// \param theID identifier of the attribute that can be referenced by this ID later
153 /// \param theAttrType type of the created attribute (received from the type method)
154 /// \returns the just created attribute
155 MODEL_EXPORT virtual AttributePtr
156 addAttribute(const std::string& theID, const std::string theAttrType);
158 /// Useful method for "set" methods of the attributes: sends an UPDATE event and
159 /// makes attribute initialized
160 MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
161 /// Blocks sending "attribute updated" if theBlock is true
162 /// \param theBlock allows switching on/off the blocking state
163 /// \param theSendMessage if false, it does not send the update message even if something is changed
164 /// (normally is it used in attributeChanged because this message will be sent anyway)
165 MODEL_EXPORT virtual void blockSendAttributeUpdated(
166 const bool theBlock, const bool theSendMessage = true);
168 /// Puts feature to the document data sub-structure
169 MODEL_EXPORT void setLabel(TDF_Label theLab);
171 /// Sets the object of this data
172 MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
174 myObject = theObject;
177 /// Erases all the data from the data model
178 MODEL_EXPORT virtual void erase();
180 /// Stores the state of the object to execute it later accordingly
181 MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
183 /// Returns the state of the latest execution of the feature
184 MODEL_EXPORT virtual ModelAPI_ExecState execState();
186 /// Registers error during the execution, causes the ExecutionFailed state
187 MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
189 /// Erases the error string if it is not empty
190 void eraseErrorString();
192 /// Registers error during the execution, causes the ExecutionFailed state
193 MODEL_EXPORT virtual std::string error() const;
195 /// Returns the identifier of feature-owner, unique in this document
196 MODEL_EXPORT virtual int featureId() const;
198 /// returns all objects referenced to this
199 MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
201 /// returns all references by attributes of this data
202 /// \param theRefs returned list of pairs: id of referenced attribute and list of referenced objects
203 MODEL_EXPORT virtual void referencesToObjects(
204 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
206 /// Copies all atributes content into theTarget data
207 MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
209 /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
210 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
212 /// Returns the invalid data pointer: static method
213 static std::shared_ptr<ModelAPI_Data> invalidData();
215 /// Identifier of the transaction when object (feature or result) was updated last time.
216 MODEL_EXPORT virtual int updateID();
218 /// Identifier of the transaction when object (feature or result) was updated last time.
219 /// This method is called by the updater.
220 MODEL_EXPORT virtual void setUpdateID(const int theID);
222 /// Returns true if the given object is owner of this data (needed for correct erase of object
223 /// with duplicated data)
224 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> owner();
227 /// Returns true if "is in history" custom behaviors is defined for the feature
228 MODEL_EXPORT virtual bool isInHistory();
230 /// Defines the custom "is in history" behavior
231 MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
233 /// Returns true if the object is deleted, but some data is still keept in memory
234 MODEL_EXPORT virtual bool isDeleted();
236 /// Sets true if the object is deleted, but some data is still keept in memory
237 MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
240 /// Removes all information about back references
241 void eraseBackReferences();
242 /// Removes a back reference (with identifier which attribute references to this object)
243 /// \param theFeature feature referenced to this
244 /// \param theAttrID identifier of the attribute that is references from theFeature to this
245 void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
246 /// Removes a back reference (by the attribute)
247 /// \param theAttr the referenced attribute
248 void removeBackReference(AttributePtr theAttr);
249 /// Adds a back reference (with identifier which attribute references to this object
250 /// \param theFeature feature referenced to this
251 /// \param theAttrID identifier of the attribute that is references from theFeature to this
252 /// \param theApplyConcealment applies consealment flag changes
253 void addBackReference(FeaturePtr theFeature, std::string theAttrID,
254 const bool theApplyConcealment = true);
256 /// Makes the concealment flag up to date for this object-owner.
257 MODEL_EXPORT virtual void updateConcealmentFlag();
259 /// Returns true if object must be displayed in the viewer: flag is stored in the
260 /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
261 /// the original state i nthe current transaction.
262 MODEL_EXPORT virtual bool isDisplayed();
264 /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
266 MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
269 /// Generic method to register back reference, used in referencing attributes.
270 /// Without concealment change, it will be done later, on synchronization.
271 #define ADD_BACK_REF(TARGET) \
272 if (TARGET.get() != NULL) { \
273 FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
274 if (anAttributeOwnerFeature.get()) { \
275 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
277 aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
281 /// Generic method to unregister back reference, used in referencing attributes.
282 /// Without concealment change, it will be done later, on synchronization.
283 #define REMOVE_BACK_REF(TARGET) \
284 if (TARGET.get() != NULL) { \
285 FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
286 if (anAttOwnerFeature.get()) { \
287 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
289 aTargetData->removeBackReference(anAttOwnerFeature, id()); \