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_Object.h>
41 #include <TDF_Label.hxx>
42 #include <TDataStd_BooleanArray.hxx>
51 class ModelAPI_Attribute;
55 * \brief General object of the application that allows
56 * to get/set attributes from the document and compute result of an operation.
59 class Model_Data : public ModelAPI_Data
61 typedef std::map<std::string, std::pair<std::shared_ptr<ModelAPI_Attribute>, int> > AttributeMap;
63 TDF_Label myLab; ///< label of the feature in the document
64 /// All attributes of the object identified by the attribute ID
65 /// (the attribute is stored together with its index in the feature)
67 /// Array of flags of this data
68 Handle(TDataStd_BooleanArray) myFlags;
70 /// needed here to emit signal that object changed on change of the attribute
73 /// List of attributes referenced to owner (updated only during the transaction change)
74 std::set<AttributePtr> myRefsToMe;
75 /// flag that may block the "attribute updated" sending
76 bool mySendAttributeUpdated;
77 /// if some attribute was changed, but mySendAttributeUpdated was false, this stores this
78 std::list<ModelAPI_Attribute*> myWasChangedButBlocked;
80 /// Returns label of this feature
86 friend class Model_Document;
87 friend class Model_Objects;
88 friend class Model_Update;
89 friend class Model_AttributeReference;
90 friend class Model_AttributeRefAttr;
91 friend class Model_AttributeRefList;
92 friend class Model_AttributeRefAttrList;
93 friend class Model_AttributeSelection;
94 friend class Model_AttributeSelectionList;
95 friend class Model_ValidatorsFactory;
96 friend class Model_SelectionNaming;
97 friend class Model_ResultConstruction;
100 /// The simplest constructor. "setLabel" must be called just after to initialize correctly.
102 /// Returns the name of the feature visible by the user in the object browser
103 MODEL_EXPORT virtual std::string name();
104 /// Defines the name of the feature visible by the user in the object browser
105 MODEL_EXPORT virtual void setName(const std::string& theName);
106 /// Returns the attribute that references to another document
107 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDocRef> document(const std::string& theID);
108 /// Returns the attribute that contains real value with double precision
109 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeDouble> real(const std::string& theID);
110 /// Returns the attribute that contains double values array
112 std::shared_ptr<ModelAPI_AttributeDoubleArray> realArray(const std::string& theID);
113 /// Returns the attribute that contains integer value
114 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeInteger>
115 integer(const std::string& theID);
116 /// Returns the attribute that contains reference to a feature
117 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeReference>
118 reference(const std::string& theID);
119 /// Returns the attribute that contains selection to a shape
120 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelection>
121 selection(const std::string& theID);
122 /// Returns the attribute that contains selection to a shape
123 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeSelectionList>
124 selectionList(const std::string& theID);
125 /// Returns the attribute that contains reference to an attribute of a feature
126 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttr>
127 refattr(const std::string& theID);
128 /// Returns the attribute that contains list of references to features
129 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefList>
130 reflist(const std::string& theID);
131 /// Returns the attribute that contains list of references to features
132 /// or reference to an attribute of a feature
133 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeRefAttrList>
134 refattrlist(const std::string& theID);
135 /// Returns the attribute that contains boolean value
136 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeBoolean>
137 boolean(const std::string& theID);
138 /// Returns the attribute that contains real value with double precision
139 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeString>
140 string(const std::string& theID);
141 /// Returns the attribute that contains integer values array
142 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeIntArray>
143 intArray(const std::string& theID);
144 /// Returns the attribute that contains string values array
145 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeStringArray>
146 stringArray(const std::string& theID);
147 /// Returns the attribute that contains string values array
148 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_AttributeTables>
149 tables(const std::string& theID);
151 /// Returns the generic attribute by identifier
152 /// \param theID identifier of the attribute
153 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Attribute> attribute(const std::string& theID);
154 /// Returns all attributes of the feature of the given type
155 /// or all attributes if "theType" is empty
156 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Attribute> >
157 attributes(const std::string& theType);
158 /// Returns all attributes ids of the feature of the given type
159 /// or all attributes if "theType" is empty
160 MODEL_EXPORT virtual std::list<std::string> attributesIDs(const std::string& theType);
162 /// Identifier by the id (not fast, iteration by map)
163 /// \param theAttr attribute already created in this data
164 MODEL_EXPORT virtual const std::string& id(const std::shared_ptr<ModelAPI_Attribute>& theAttr);
165 /// Returns true if data belongs to same features
166 MODEL_EXPORT virtual bool isEqual(const std::shared_ptr<ModelAPI_Data>& theData);
167 /// Returns true if it is correctly connected t othe data model
168 MODEL_EXPORT virtual bool isValid();
170 /// Returns the label where the shape must be stored (used in ResultBody)
171 TDF_Label& shapeLab()
176 /// Initializes object by the attributes: must be called just after the object is created
177 /// for each attribute of the object
178 /// \param theID identifier of the attribute that can be referenced by this ID later
179 /// \param theAttrType type of the created attribute (received from the type method)
180 /// \returns the just created attribute
181 MODEL_EXPORT virtual AttributePtr
182 addAttribute(const std::string& theID, const std::string theAttrType);
184 /// Useful method for "set" methods of the attributes: sends an UPDATE event and
185 /// makes attribute initialized
186 MODEL_EXPORT virtual void sendAttributeUpdated(ModelAPI_Attribute* theAttr);
187 /// Blocks sending "attribute updated" if theBlock is true
188 /// \param theBlock allows switching on/off the blocking state
189 /// \param theSendMessage if false, it does not send the update message
190 /// even if something is changed
191 /// (normally is it used in attributeChanged because this message will be sent anyway)
192 /// \returns the previous state of block
193 MODEL_EXPORT virtual bool blockSendAttributeUpdated(
194 const bool theBlock, const bool theSendMessage = true);
196 /// Puts feature to the document data sub-structure
197 MODEL_EXPORT void setLabel(TDF_Label theLab);
199 /// Sets the object of this data
200 MODEL_EXPORT virtual void setObject(ObjectPtr theObject)
202 myObject = theObject;
205 /// Erases all the data from the data model
206 MODEL_EXPORT virtual void erase();
208 /// Stores the state of the object to execute it later accordingly
209 MODEL_EXPORT virtual void execState(const ModelAPI_ExecState theState);
211 /// Returns the state of the latest execution of the feature
212 MODEL_EXPORT virtual ModelAPI_ExecState execState();
214 /// Registers error during the execution, causes the ExecutionFailed state
215 MODEL_EXPORT virtual void setError(const std::string& theError, bool theSend = true);
217 /// Erases the error string if it is not empty
218 void eraseErrorString();
220 /// Registers error during the execution, causes the ExecutionFailed state
221 MODEL_EXPORT virtual std::string error() const;
223 /// Returns the identifier of feature-owner, unique in this document
224 MODEL_EXPORT virtual int featureId() const;
226 /// returns all objects referenced to this
227 MODEL_EXPORT virtual const std::set<AttributePtr>& refsToMe() {return myRefsToMe;}
229 /// returns all references by attributes of this data
230 /// \param theRefs returned list of pairs:
231 /// id of referenced attribute and list of referenced objects
232 MODEL_EXPORT virtual void referencesToObjects(
233 std::list<std::pair<std::string, std::list<ObjectPtr> > >& theRefs);
235 /// Copies all atributes content into theTarget data
236 MODEL_EXPORT virtual void copyTo(std::shared_ptr<ModelAPI_Data> theTarget);
238 /// Returns the invalid data pointer (to avoid working with NULL shared ptrs in swig)
239 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Data> invalidPtr();
241 /// Returns the invalid data pointer: static method
242 static std::shared_ptr<ModelAPI_Data> invalidData();
244 /// Identifier of the transaction when object (feature or result) was updated last time.
245 MODEL_EXPORT virtual int updateID();
247 /// Identifier of the transaction when object (feature or result) was updated last time.
248 /// This method is called by the updater.
249 MODEL_EXPORT virtual void setUpdateID(const int theID);
251 /// Returns true if the given object is owner of this data (needed for correct erase of object
252 /// with duplicated data)
253 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> owner();
256 /// Returns true if "is in history" custom behaviors is defined for the feature
257 MODEL_EXPORT virtual bool isInHistory();
259 /// Defines the custom "is in history" behavior
260 MODEL_EXPORT virtual void setIsInHistory(const bool theFlag);
262 /// Returns true if the object is deleted, but some data is still keept in memory
263 MODEL_EXPORT virtual bool isDeleted();
265 /// Sets true if the object is deleted, but some data is still keept in memory
266 MODEL_EXPORT virtual void setIsDeleted(const bool theFlag);
269 /// Removes all information about back references
270 void eraseBackReferences();
271 /// Removes a back reference (with identifier which attribute references to this object)
272 /// \param theFeature feature referenced to this
273 /// \param theAttrID identifier of the attribute that is references from theFeature to this
274 void removeBackReference(FeaturePtr theFeature, std::string theAttrID);
275 /// Removes a back reference (by the attribute)
276 /// \param theAttr the referenced attribute
277 void removeBackReference(AttributePtr theAttr);
278 /// Adds a back reference (with identifier which attribute references to this object
279 /// \param theFeature feature referenced to this
280 /// \param theAttrID identifier of the attribute that is references from theFeature to this
281 /// \param theApplyConcealment applies consealment flag changes
282 void addBackReference(FeaturePtr theFeature, std::string theAttrID,
283 const bool theApplyConcealment = true);
285 /// Makes the concealment flag up to date for this object-owner.
286 MODEL_EXPORT virtual void updateConcealmentFlag();
288 /// Returns true if object must be displayed in the viewer: flag is stored in the
289 /// data model, so on undo/redo, open/save or recreation of object by history-playing it keeps
290 /// the original state i nthe current transaction.
291 MODEL_EXPORT virtual bool isDisplayed();
293 /// Sets the displayed/hidden state of the object. If it is changed, sends the "redisplay"
295 MODEL_EXPORT virtual void setDisplayed(const bool theDisplay);
297 /// Returns \c true if theAttribute1 is going earlier than theAttribute2 in the data
298 MODEL_EXPORT virtual bool isPrecedingAttribute(const std::string& theAttribute1,
299 const std::string& theAttribute2) const;
302 /// Generic method to register back reference, used in referencing attributes.
303 /// Without concealment change, it will be done later, on synchronization.
304 #define ADD_BACK_REF(TARGET) \
305 if (TARGET.get() != NULL) { \
306 FeaturePtr anAttributeOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
307 if (anAttributeOwnerFeature.get()) { \
308 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
310 aTargetData->addBackReference(anAttributeOwnerFeature, id(), false); \
314 /// Generic method to unregister back reference, used in referencing attributes.
315 /// Without concealment change, it will be done later, on synchronization.
316 #define REMOVE_BACK_REF(TARGET) \
317 if (TARGET.get() != NULL) { \
318 FeaturePtr anAttOwnerFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(owner()); \
319 if (anAttOwnerFeature.get()) { \
320 std::shared_ptr<Model_Data> aTargetData = std::dynamic_pointer_cast<Model_Data>( \
322 aTargetData->removeBackReference(anAttOwnerFeature, id()); \