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_AttributeString.h>
20 #include <ModelAPI_AttributeIntArray.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
25 #include <TDF_Label.hxx>
26 #include <TDataStd_BooleanArray.hxx>
35 class ModelAPI_Attribute;
39 * \brief General object of the application that allows
40 * to get/set attributes from the document and compute result of an operation.
43 class Model_Data : public ModelAPI_Data
45 TDF_Label myLab; ///< label of the feature in the document
46 /// All attributes of the object identified by the attribute ID
47 std::map<std::string, std::shared_ptr<ModelAPI_Attribute> > myAttrs;
48 /// Array of flags of this data
49 Handle(TDataStd_BooleanArray) myFlags;
51 /// needed here to emit signal that object changed on change of the attribute
54 /// List of attributes referenced to owner (updated only during the transaction change)
55 std::set<AttributePtr> myRefsToMe;
56 /// flag that may block the "attribute updated" sending
57 bool mySendAttributeUpdated;
59 /// Returns label of this feature
65 friend class Model_Document;
66 friend class Model_Objects;
67 friend class Model_Update;
68 friend class Model_AttributeReference;
69 friend class Model_AttributeRefAttr;
70 friend class Model_AttributeRefList;
71 friend class Model_AttributeSelection;
72 friend class Model_AttributeSelectionList;
75 /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
77 /// Returns the name of the feature visible by the user in the object browser
78 MODEL_EXPORT virtual std::string name();
79 /// Defines the name of the feature visible by the user in the object browser
80 MODEL_EXPORT virtual void setName(const std::string& theName);
81 /// Returns the attribute that references to another document
82 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
83 /// Returns the attribute that contains real value with double precision
84 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
85 /// Returns the attribute that contains integer value
86 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
87 integer(const std::string& theID);
88 /// Returns the attribute that contains reference to a feature
89 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
90 reference(const std::string& theID);
91 /// Returns the attribute that contains selection to a shape
92 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
93 selection(const std::string& theID);
94 /// Returns the attribute that contains selection to a shape
95 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
96 selectionList(const std::string& theID);
97 /// Returns the attribute that contains reference to an attribute of a feature
98 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
99 refattr(const std::string& theID);
100 /// Returns the attribute that contains list of references to features
101 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
102 reflist(const std::string& theID);
103 /// Returns the attribute that contains boolean value
104 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
105 boolean(const std::string& theID);
106 /// Returns the attribute that contains real value with double precision
107 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
108 string(const std::string& theID);
109 /// Returns the attribute that contains integer values array
110 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
111 intArray(const std::string& theID);
113 /// Returns the generic attribute by identifier
114 /// \param theID identifier of the attribute
115 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
116 /// Returns all attributes of the feature of the given type
117 /// or all attributes if "theType" is empty
118 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
119 attributes(const std::string& theType);
120 /// Returns all attributes ids of the feature of the given type
121 /// or all attributes if "theType" is empty
122 MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
124 /// Identifier by the id (not fast, iteration by map)
125 /// \param theAttr attribute already created in this data
126 MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
127 /// Returns true if data belongs to same features
128 MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
129 /// Returns true if it is correctly connected t othe data model
130 MODEL_EXPORT virtual bool isValid();
132 /// Returns the label where the shape must be stored (used in ResultBody)
133 TDF_Label& shapeLab()
138 /// Initializes object by the attributes: must be called just after the object is created
139 /// for each attribute of the object
140 /// \param theID identifier of the attribute that can be referenced by this ID later
141 /// \param theAttrType type of the created attribute (received from the type method)
142 /// \returns the just created attribute
143 MODEL_EXPORT virtual AttributePtr
144 addAttribute(const std::string& theID, const std::string theAttrType);
146 /// Useful method for "set" methods of the attributes: sends an UPDATE event and
147 /// makes attribute initialized
148 MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
149 /// Blocks sending "attribute updated" if theBlock is true
150 MODEL_EXPORT virtual void blockSendAttributeUpdated(const bool theBlock);
152 /// Puts feature to the document data sub-structure
153 MODEL_EXPORT void setLabel(TDF_Label theLab);
155 /// Sets the object of this data
156 MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
158 myObject = theObject;
161 /// Erases all the data from the data model
162 MODEL_EXPORT virtual void erase();
164 /// Stores the state of the object to execute it later accordingly
165 MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
167 /// Returns the state of the latest execution of the feature
168 MODEL_EXPORT virtual ModelAPI_ExecState execState();
170 /// Registers error during the execution, causes the ExecutionFailed state
171 MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
173 /// Registers error during the execution, causes the ExecutionFailed state
174 MODEL_EXPORT virtual std::string error() const;
176 /// Returns the identifier of feature-owner, unique in this document
177 MODEL_EXPORT virtual int featureId() const;
179 /// returns all objects referenced to this
180 MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
182 /// returns all references by attributes of this data
183 /// \param theRefs returned list of pairs: id of referenced attribute and list of referenced objects
184 MODEL_EXPORT virtual void referencesToObjects(
185 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
187 /// Copies all atributes content into theTarget data
188 MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
190 /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
191 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
193 /// Returns the invalid data pointer: static method
194 static std::shared_ptr<ModelAPI_Data> invalidData();
196 /// Identifier of the transaction when object (feature or result) was updated last time.
197 MODEL_EXPORT virtual int updateID();
199 /// Identifier of the transaction when object (feature or result) was updated last time.
200 /// This method is called by the updater.
201 MODEL_EXPORT virtual void setUpdateID(const int theID);
204 /// Returns true if "is in history" custom behaviors is defined for the feature
205 MODEL_EXPORT virtual bool isInHistory();
207 /// Defines the custom "is in history" behavior
208 MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
211 /// Removes all information about back references
212 void eraseBackReferences();
213 /// Adds a back reference (with identifier which attribute references to this object
214 /// It does not change the consealment flag of the data object result
215 /// \param theFeature feature referenced to this
216 /// \param theAttrID identifier of the attribute that is references from theFeature to this
217 void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
218 /// Adds a back reference (with identifier which attribute references to this object
219 /// \param theFeature feature referenced to this
220 /// \param theAttrID identifier of the attribute that is references from theFeature to this
221 /// \param theApplyConcealment applies consealment flag changes
222 void addBackReference(FeaturePtr theFeature, std::string theAttrID,
223 const bool theApplyConcealment = true);
225 /// Returns true if object must be displayed in the viewer: flag is stored in the
226 /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
227 /// the original state i nthe current transaction.
228 MODEL_EXPORT virtual bool isDisplayed();
230 /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
232 MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
235 /// Generic method to register back reference, used in referencing attributes.
236 /// Without concealment change, it will be done later, on synchronization.
237 #define ADD_BACK_REF(TARGET) \
238 if (TARGET.get() != NULL) { \
239 FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
240 if (anAttributeOwnerFeature.get()) { \
241 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
243 aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
247 /// Generic method to unregister back reference, used in referencing attributes.
248 /// Without concealment change, it will be done later, on synchronization.
249 #define REMOVE_BACK_REF(TARGET) \
250 if (TARGET.get() != NULL) { \
251 FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
252 if (anAttOwnerFeature.get()) { \
253 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
255 aTargetData->removeBackReference(anAttOwnerFeature, id()); \