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