1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
25 #include <ModelAPI_Attribute.h>
26 #include <ModelAPI_AttributeBoolean.h>
27 #include <ModelAPI_AttributeDocRef.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_AttributeInteger.h>
30 #include <ModelAPI_AttributeRefAttr.h>
31 #include <ModelAPI_AttributeReference.h>
32 #include <ModelAPI_AttributeRefList.h>
33 #include <ModelAPI_AttributeRefAttrList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_AttributeStringArray.h>
36 #include <ModelAPI_AttributeIntArray.h>
37 #include <ModelAPI_Data.h>
38 #include <ModelAPI_Feature.h>
39 #include <ModelAPI_Folder.h>
40 #include <ModelAPI_Object.h>
42 #include <TDF_Label.hxx>
43 #include <TDataStd_BooleanArray.hxx>
52 class ModelAPI_Attribute;
56 * \brief General object of the application that allows
57 * to get/set attributes from the document and compute result of an operation.
60 class Model_Data : public ModelAPI_Data
62 typedef std::map<std::string, std::pair<std::shared_ptr<ModelAPI_Attribute>, int> > AttributeMap;
64 TDF_Label myLab; ///< label of the feature in the document
65 /// All attributes of the object identified by the attribute ID
66 /// (the attribute is stored together with its index in the feature)
68 /// Array of flags of this data
69 Handle(TDataStd_BooleanArray) myFlags;
71 /// needed here to emit signal that object changed on change of the attribute
74 /// List of attributes referenced to owner (updated only during the transaction change)
75 std::set<AttributePtr> myRefsToMe;
76 /// flag that may block the "attribute updated" sending
77 bool mySendAttributeUpdated;
78 /// if some attribute was changed, but mySendAttributeUpdated was false, this stores this
79 std::list<ModelAPI_Attribute*> myWasChangedButBlocked;
81 /// Returns label of this feature
87 friend class Model_Document;
88 friend class Model_Objects;
89 friend class Model_Update;
90 friend class Model_AttributeReference;
91 friend class Model_AttributeRefAttr;
92 friend class Model_AttributeRefList;
93 friend class Model_AttributeRefAttrList;
94 friend class Model_AttributeSelection;
95 friend class Model_AttributeSelectionList;
96 friend class Model_ValidatorsFactory;
97 friend class Model_SelectionNaming;
98 friend class Model_ResultConstruction;
99 friend class Model_ResultBody;
102 /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
104 /// Returns the name of the feature visible by the user in the object browser
105 MODEL_EXPORT virtual std::string name();
106 /// Defines the name of the feature visible by the user in the object browser
107 MODEL_EXPORT virtual void setName(const std::string& theName);
108 /// Return \c true if the object has been renamed by the user
109 MODEL_EXPORT virtual bool hasUserDefinedName() const;
110 /// Returns the attribute that references to another document
111 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
112 /// Returns the attribute that contains real value with double precision
113 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
114 /// Returns the attribute that contains double values array
116 std::shared_ptr<ModelAPI_AttributeDoubleArray> realArray(const std::string& theID);
117 /// Returns the attribute that contains integer value
118 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
119 integer(const std::string& theID);
120 /// Returns the attribute that contains reference to a feature
121 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
122 reference(const std::string& theID);
123 /// Returns the attribute that contains selection to a shape
124 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
125 selection(const std::string& theID);
126 /// Returns the attribute that contains selection to a shape
127 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
128 selectionList(const std::string& theID);
129 /// Returns the attribute that contains reference to an attribute of a feature
130 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
131 refattr(const std::string& theID);
132 /// Returns the attribute that contains list of references to features
133 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
134 reflist(const std::string& theID);
135 /// Returns the attribute that contains list of references to features
136 /// or reference to an attribute of a feature
137 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttrList>
138 refattrlist(const std::string& theID);
139 /// Returns the attribute that contains boolean value
140 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
141 boolean(const std::string& theID);
142 /// Returns the attribute that contains real value with double precision
143 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
144 string(const std::string& theID);
145 /// Returns the attribute that contains integer values array
146 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
147 intArray(const std::string& theID);
148 /// Returns the attribute that contains string values array
149 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeStringArray>
150 stringArray(const std::string& theID);
151 /// Returns the attribute that contains string values array
152 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeTables>
153 tables(const std::string& theID);
155 /// Returns the generic attribute by identifier
156 /// \param theID identifier of the attribute
157 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
158 /// Returns all attributes of the feature of the given type
159 /// or all attributes if "theType" is empty
160 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
161 attributes(const std::string& theType);
162 /// Returns all attributes ids of the feature of the given type
163 /// or all attributes if "theType" is empty
164 MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
166 /// Identifier by the id (not fast, iteration by map)
167 /// \param theAttr attribute already created in this data
168 MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
169 /// Returns true if data belongs to same features
170 MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
171 /// Returns true if it is correctly connected t othe data model
172 MODEL_EXPORT virtual bool isValid();
174 /// Returns the label where the shape must be stored (used in ResultBody)
175 TDF_Label shapeLab() const
177 return myLab.IsNull() ? myLab : myLab.Father().FindChild(2);
180 /// Initializes object by the attributes: must be called just after the object is created
181 /// for each attribute of the object
182 /// \param theID identifier of the attribute that can be referenced by this ID later
183 /// \param theAttrType type of the created attribute (received from the type method)
184 /// \returns the just created attribute
185 MODEL_EXPORT virtual AttributePtr
186 addAttribute(const std::string& theID, const std::string theAttrType);
188 /// Useful method for "set" methods of the attributes: sends an UPDATE event and
189 /// makes attribute initialized
190 MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
191 /// Blocks sending "attribute updated" if theBlock is true
192 /// \param theBlock allows switching on/off the blocking state
193 /// \param theSendMessage if false, it does not send the update message
194 /// even if something is changed
195 /// (normally is it used in attributeChanged because this message will be sent anyway)
196 /// \returns the previous state of block
197 MODEL_EXPORT virtual bool blockSendAttributeUpdated(
198 const bool theBlock, const bool theSendMessage = true);
200 /// Puts feature to the document data sub-structure
201 MODEL_EXPORT void setLabel(TDF_Label theLab);
203 /// Sets the object of this data
204 MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
206 myObject = theObject;
209 /// Erases all the data from the data model
210 MODEL_EXPORT virtual void erase();
212 /// Stores the state of the object to execute it later accordingly
213 MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
215 /// Returns the state of the latest execution of the feature
216 MODEL_EXPORT virtual ModelAPI_ExecState execState();
218 /// Registers error during the execution, causes the ExecutionFailed state
219 MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
221 /// Erases the error string if it is not empty
222 void eraseErrorString();
224 /// Registers error during the execution, causes the ExecutionFailed state
225 MODEL_EXPORT virtual std::string error() const;
227 /// Returns the identifier of feature-owner, unique in this document
228 MODEL_EXPORT virtual int featureId() const;
230 /// returns all objects referenced to this
231 MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
233 /// returns all references by attributes of this data
234 /// \param theRefs returned list of pairs:
235 /// id of referenced attribute and list of referenced objects
236 MODEL_EXPORT virtual void referencesToObjects(
237 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
239 /// Copies all atributes content into theTarget data
240 MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
242 /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
243 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
245 /// Returns the invalid data pointer: static method
246 static std::shared_ptr<ModelAPI_Data> invalidData();
248 /// Identifier of the transaction when object (feature or result) was updated last time.
249 MODEL_EXPORT virtual int updateID();
251 /// Identifier of the transaction when object (feature or result) was updated last time.
252 /// This method is called by the updater.
253 MODEL_EXPORT virtual void setUpdateID(const int theID);
255 /// Returns true if the given object is owner of this data (needed for correct erase of object
256 /// with duplicated data)
257 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> owner();
260 /// Returns true if "is in history" custom behaviors is defined for the feature
261 MODEL_EXPORT virtual bool isInHistory();
263 /// Defines the custom "is in history" behavior
264 MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
266 /// Returns true if the object is deleted, but some data is still keept in memory
267 MODEL_EXPORT virtual bool isDeleted();
269 /// Sets true if the object is deleted, but some data is still keept in memory
270 MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
273 /// Removes all information about back references
274 void eraseBackReferences();
275 /// Removes a back reference (with identifier which attribute references to this object)
276 /// \param theFeature feature referenced to this
277 /// \param theAttrID identifier of the attribute that is references from theFeature to this
278 void removeBackReference(ObjectPtr theObject, std::string theAttrID);
279 /// Removes a back reference (by the attribute)
280 /// \param theAttr the referenced attribute
281 void removeBackReference(AttributePtr theAttr);
282 /// Adds a back reference (with identifier which attribute references to this object
283 /// \param theFeature feature referenced to this
284 /// \param theAttrID identifier of the attribute that is references from theFeature to this
285 /// \param theApplyConcealment applies consealment flag changes
286 void addBackReference(FeaturePtr theFeature, std::string theAttrID,
287 const bool theApplyConcealment = true);
288 /// Adds a back reference to an object
289 /// \param theObject object referenced to this
290 /// \param theAttrID identifier of the attribute that is references from theFolder to this
291 void addBackReference(ObjectPtr theObject, std::string theAttrID);
293 /// Makes the concealment flag up to date for this object-owner.
294 MODEL_EXPORT virtual void updateConcealmentFlag();
296 /// Returns true if object must be displayed in the viewer: flag is stored in the
297 /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
298 /// the original state i nthe current transaction.
299 MODEL_EXPORT virtual bool isDisplayed();
301 /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
303 MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
305 /// Returns \c true if theAttribute1 is going earlier than theAttribute2 in the data
306 MODEL_EXPORT virtual bool isPrecedingAttribute(const std::string& theAttribute1,
307 const std::string& theAttribute2) const;
310 /// Generic method to register back reference, used in referencing attributes.
311 /// Without concealment change, it will be done later, on synchronization.
312 #define ADD_BACK_REF(TARGET) \
313 if (TARGET.get() != NULL) { \
314 std::shared_ptr<Model_Data> aTargetData = \
315 std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
316 FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
317 if (anAttributeOwnerFeature.get()) \
318 aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
320 FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
321 if (anAttributeOwnerFolder.get()) \
322 aTargetData->addBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \
326 /// Generic method to unregister back reference, used in referencing attributes.
327 /// Without concealment change, it will be done later, on synchronization.
328 #define REMOVE_BACK_REF(TARGET) \
329 if (TARGET.get() != NULL) { \
330 std::shared_ptr<Model_Data> aTargetData = \
331 std::dynamic_pointer_cast<Model_Data>((TARGET)->data()); \
332 FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
333 if (anAttOwnerFeature.get()) \
334 aTargetData->removeBackReference(anAttOwnerFeature, id()); \
336 FolderPtr anAttributeOwnerFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(owner()); \
337 if (anAttributeOwnerFolder.get()) \
338 aTargetData->removeBackReference(ObjectPtr(anAttributeOwnerFolder), id()); \