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>
21 #ifndef Model_Objects_H_
22 #define Model_Objects_H_
25 #include <ModelAPI_Document.h>
26 #include <ModelAPI_Feature.h>
27 #include <ModelAPI_Folder.h>
28 #include <ModelAPI_Result.h>
29 #include <ModelAPI_ResultParameter.h>
31 #include <TDocStd_Document.hxx>
32 #include <NCollection_DataMap.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelList.hxx>
39 // for TDF_Label map usage
40 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
41 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
43 extern int kUNDEFINED_FEATURE_INDEX;
45 /**\class Model_Objects
47 * \brief Manager of objects of the document. Normally one this class corresponds to
48 * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
49 * on the level of data storage.
54 //! Registers the feature in the data structure
55 //! \param theFeature feature that must be added to the data structure
56 //! \param theAfterThis the feature will be added after this feature;
57 //! if it is null, the added feature will be the first
58 void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
60 //! Return a list of features, which refers to the feature
61 //! \param theFeature a feature
62 //! \param theRefs a list of reference features
63 //! \param isSendError a flag whether the error message should be send
64 void refsToFeature(FeaturePtr theFeature,
65 std::set<FeaturePtr>& theRefs,
66 const bool isSendError = true);
68 //! Removes the feature from the document (with result)
69 //! \param theFeature a removed feature
70 void removeFeature(FeaturePtr theFeature);
72 //! Moves the feature to make it after the given one in the history.
73 void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
75 //! Returns the existing feature by the label
76 //! \param theLabel base label of the feature
77 FeaturePtr feature(TDF_Label theLabel) const;
79 //! Returns the existing object: result or feature
80 //! \param theLabel base label of the object
81 ObjectPtr object(TDF_Label theLabel);
83 //! Returns the first found object in the group by the object name
84 //! \param theGroupID group that contains an object
85 //! \param theName name of the object to search
86 //! \returns null if such object is not found
87 std::shared_ptr<ModelAPI_Object> objectByName(
88 const std::string& theGroupID, const std::string& theName);
90 //! Returns the result by the result name
91 ResultPtr findByName(const std::string theName);
94 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
95 //! \param theObject object of this document
96 //! \param theAllowFolder take into account grouping feature by folders
97 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
98 const int index(std::shared_ptr<ModelAPI_Object> theObject,
99 const bool theAllowFolder = false);
101 //! Returns the feature in the group by the index (started from zero)
102 //! \param theGroupID group that contains a feature
103 //! \param theIndex zero-based index of feature in the group
104 //! \param theAllowFolder take into account grouping feature by folders
105 ObjectPtr object(const std::string& theGroupID,
107 const bool theAllowFolder = false);
109 //! Returns the number of features in the group
110 //! \param theGroupID group of objects
111 //! \param theAllowFolder take into account grouping feature by folders
112 int size(const std::string& theGroupID, const bool theAllowFolder = false);
114 //! Returns the parent object of this child. This may be result or feature, parent of a
115 //! top result. Fast method, that uses internal data structure specifics.
116 std::shared_ptr<ModelAPI_Object> parent(const std::shared_ptr<ModelAPI_Object> theChild);
118 //! Returns all (and disabled) results of the given type.
119 //! Not fast method (iterates all features).
120 void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
122 //! Returns the number of all features: in the history or not
123 int numInternalFeatures();
124 //! Returns the feature by zero-based index: features in the history or not
125 std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
127 /// Creates a construction result
128 std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
129 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
130 /// Creates a body result
131 std::shared_ptr<ModelAPI_ResultBody> createBody(
132 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
133 /// Creates a part result
134 std::shared_ptr<ModelAPI_ResultPart> createPart(
135 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
136 /// Copies a part result, keeping the reference to origin
137 std::shared_ptr<ModelAPI_ResultPart> copyPart(
138 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
139 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
140 /// Creates a group result
141 std::shared_ptr<ModelAPI_ResultGroup> createGroup(
142 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
143 /// Creates a field result
144 std::shared_ptr<ModelAPI_ResultField> createField(
145 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
146 /// Creates a parameter result
147 std::shared_ptr<ModelAPI_ResultParameter> createParameter(
148 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
150 //! Returns a feature by result (owner of result)
151 std::shared_ptr<ModelAPI_Feature>
152 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
154 /// Creates a folder (group of the features in the object browser)
155 std::shared_ptr<ModelAPI_Folder> createFolder(
156 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis);
157 //! Removes the folder from the document (all features in the folder will be kept).
158 void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
159 //! Search a folder applicable for the list of features
160 //! (it means the list of features stored in the folder should be consequential)
161 //! \param theFeatures list of features to be added to folder
162 //! \param theBelow search the folder below the features (if \c false, search above)
163 //! \return Empty pointer if there is no applicable folder
164 std::shared_ptr<ModelAPI_Folder> findFolder(
165 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
166 const bool theBelow);
167 //! Search a folder containing the given feature.
168 //! Additionally calculates a zero-based index of the feature in this folder.
169 //! \param theFeature feature to search
170 //! \param theIndexInFolder zero-based index in the folder or -1 if the feature is top-level.
171 //! \return the folder containing the feature or empty pointer if the feature is top-level.
172 std::shared_ptr<ModelAPI_Folder> findContainingFolder(
173 const std::shared_ptr<ModelAPI_Feature>& theFeature,
174 int& theIndexInFolder);
175 //! Add a list of features to the folder. The correctness of the adding is not performed
176 //! (such checks have been done in corresponding find.. method).
177 //! \return \c true if the movement is successful
178 bool moveToFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
179 const std::shared_ptr<ModelAPI_Folder>& theFolder);
180 //! Remove features from the folder
181 //! \param theFeatures list of features to be removed
182 //! \param theBefore extract features before the folder (this parameter is applicable only
183 //! when all features in the folder are taking out,
184 //! in other cases the direction is taken automatically)
185 //! \return \c true if the features have been moved out
186 bool removeFromFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
187 const bool theBefore = true);
189 //! Sets the owner of this manager
190 void setOwner(DocumentPtr theDoc);
192 //! Returns the owner of this manager
193 DocumentPtr owner() {return myDoc;}
195 //! Deletes all managed features with emitting of corresponded signal
200 //! Returns (creates if needed) the features label
201 TDF_Label featuresLabel() const;
203 //! Initializes feature with a unique name in this group (unique name is generated as
204 //! feature type + "_" + index
205 void setUniqueName(FeaturePtr theFeature);
207 //! Initializes the folder with an unique name ("Folder_" + index)
208 void setUniqueName(FolderPtr theFolder);
210 //! Synchronizes myFeatures list with the updated document
211 //! \param theUpdated list of labels that are marked as modified, so features must be also
212 //! \param theUpdateReferences causes the update of back-references
213 //! \param theExecuteFeatures requires re-execute modified persistent features
214 //! (not needed on undo/redo/abort/open)
215 //! \param theOpen - on open nothing must be re-executed, except not persistent results
216 //! \param theFlush makes flush all events in the end of all modifications of this method
217 void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
218 const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
219 //! Synchronizes the BackReferences list in Data of Features and Results
220 void synchronizeBackRefs();
222 //! Creates manager on the OCAF document main label
223 Model_Objects(TDF_Label theMainLab);
225 //! Initializes the data fields of the feature
226 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
228 //! Allows to store the result in the data tree of the document
229 //! (attaches 'data' of result to tree)
230 void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
231 std::shared_ptr<ModelAPI_Result> theResult,
232 const int theResultIndex = 0);
234 //! returns the label of result by index; creates this label if it was not created before
235 TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
236 const int theResultIndex);
238 //! Updates the results list of the feature basing on the current data tree
239 //! theProcessed is used to avoid update twice (since the function is recursive)
240 void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
242 /// Internally makes document know that feature was removed or added in history after creation
243 void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
245 /// Internally makes document know that feature was removed or added in history after creation
246 void updateHistory(const std::string theGroup);
248 /// Clears the history arrays related to this object
249 void clearHistory(ObjectPtr theObj);
251 /// Creates the history: up to date with the current state
252 void createHistory(const std::string& theGroupID);
254 /// Returns the next (from the history point of view) feature, any: invisible or disabled
255 /// \param theCurrent previous to the resulting feature
256 /// \param theReverse if it is true, iterates in reversed order (next becomes previous)
257 /// \param theIndex may be used for optimization: index of theCurrent in references array
258 FeaturePtr nextFeature(FeaturePtr theCurrent, int& theIndex, const bool theReverse = false);
259 /// Returns to the first (from the history point of view) feature, any: invisible or disabled
260 FeaturePtr firstFeature();
261 /// Returns to the last (from the history point of view) feature, any: invisible or disabled
262 FeaturePtr lastFeature();
263 /// Returns true if theLater is in history of features creation later than theCurrent
264 bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
266 /// Returns the next or previous label
267 /// \param theCurrent given label
268 /// \param theReverse if it is true, iterates in reversed order (next becomes previous)
269 TDF_Label nextLabel(TDF_Label theCurrent, int& theIndex, const bool theReverse = false);
271 /// Returns the result group identifier of the given feature (for this at least one result must
272 /// be created before)
273 std::string featureResultGroup(FeaturePtr theFeature);
275 //! Returns all features of the document including the hidden features which are not in
276 //! history. Not very fast method, for calling once, not in big cycles.
277 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
279 //! Returns all objects of the document including the hidden features which are not in
280 //! history. Not very fast method, for calling once, not in big cycles.
281 std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
283 //! synchronizes back references for the given object basing on the collected data
284 void synchronizeBackRefsForObject(
285 const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
287 /// Just removes all features without touching the document data (to be able undo)
288 virtual void eraseAllFeatures();
290 // Check whether the predefined-image of the result had user-defined name.
291 // If yes, return this name.
292 bool hasCustomName(DataPtr theFeatureData,
295 std::string& theParentName) const;
297 /// Return object representing a folder or empty pointer
298 const ObjectPtr& folder(TDF_Label theLabel) const;
301 TDF_Label myMain; ///< main label of the data storage
303 DocumentPtr myDoc; ///< document, owner of this objects manager: needed for events creation
305 /// All managed features (not only in history of OB)
306 /// For optimization mapped by labels
307 NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
310 NCollection_DataMap<TDF_Label, ObjectPtr> myFolders;
312 /// Map from group id to the array that contains all objects located in history.
313 /// Each array is updated by demand from scratch, by browsing all the features in the history.
314 std::map<std::string, std::vector<ObjectPtr> > myHistory;
316 friend class Model_Document;
317 friend class Model_Session;
318 friend class Model_Update;
319 friend class Model_AttributeReference;
320 friend class Model_AttributeRefAttr;
321 friend class Model_AttributeRefList;
322 friend class Model_AttributeSelection;
323 friend class Model_SelectionNaming;