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