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 /**\class Model_Objects
45 * \brief Manager of objects of the document. Normally one this class corresponds to
46 * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
47 * on the level of data storage.
52 //! Registers the fieature in the data structure
53 //! \param theFeature feature that must be added to the data structure
54 //! \param theAfterThis the feature will be added after this feature;
55 //! if it is null, the added feature will be the first
56 void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
58 //! Return a list of features, which refers to the feature
59 //! \param theFeature a feature
60 //! \param theRefs a list of reference features
61 //! \param isSendError a flag whether the error message should be send
62 void refsToFeature(FeaturePtr theFeature,
63 std::set<FeaturePtr>& theRefs,
64 const bool isSendError = true);
66 //! Removes the feature from the document (with result)
67 //! \param theFeature a removed feature
68 void removeFeature(FeaturePtr theFeature);
70 //! Moves the feature to make it after the given one in the history.
71 void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
73 //! Returns the existing feature by the label
74 //! \param theLabel base label of the feature
75 FeaturePtr feature(TDF_Label theLabel) const;
77 //! Returns the existing object: result or feature
78 //! \param theLabel base label of the object
79 ObjectPtr object(TDF_Label theLabel);
81 //! Returns the first found object in the group by the object name
82 //! \param theGroupID group that contains an object
83 //! \param theName name of the object to search
84 //! \returns null if such object is not found
85 std::shared_ptr<ModelAPI_Object> objectByName(
86 const std::string& theGroupID, const std::string& theName);
88 //! Returns the result by the result name
89 ResultPtr findByName(const std::string theName);
92 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
93 //! \param theObject object of this document
94 //! \param theAllowFolder take into account grouping feature by folders
95 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
96 const int index(std::shared_ptr<ModelAPI_Object> theObject,
97 const bool theAllowFolder = false);
99 //! Returns the feature in the group by the index (started from zero)
100 //! \param theGroupID group that contains a feature
101 //! \param theIndex zero-based index of feature in the group
102 //! \param theAllowFolder take into account grouping feature by folders
103 ObjectPtr object(const std::string& theGroupID,
105 const bool theAllowFolder = false);
107 //! Returns the number of features in the group
108 //! \param theGroupID group of objects
109 //! \param theAllowFolder take into account grouping feature by folders
110 int size(const std::string& theGroupID, const bool theAllowFolder = false);
112 //! Returns all (and disabled) results of the given type.
113 //! Not fast method (iterates all features).
114 void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
116 //! Returns the number of all features: in the history or not
117 int numInternalFeatures();
118 //! Returns the feature by zero-based index: features in the history or not
119 std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
121 //! Returns feature by the id of the feature (produced by the Data "featureId" method)
122 std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
125 /// Creates a construction result
126 std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
127 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
128 /// Creates a body result
129 std::shared_ptr<ModelAPI_ResultBody> createBody(
130 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
131 /// Creates a part result
132 std::shared_ptr<ModelAPI_ResultPart> createPart(
133 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
134 /// Copies a part result, keeping the reference to origin
135 std::shared_ptr<ModelAPI_ResultPart> copyPart(
136 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
137 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
138 /// Creates a group result
139 std::shared_ptr<ModelAPI_ResultGroup> createGroup(
140 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
141 /// Creates a field result
142 std::shared_ptr<ModelAPI_ResultField> createField(
143 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
144 /// Creates a parameter result
145 std::shared_ptr<ModelAPI_ResultParameter> createParameter(
146 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
148 //! Returns a feature by result (owner of result)
149 std::shared_ptr<ModelAPI_Feature>
150 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
152 /// Creates a folder (group of the features in the object browser)
153 std::shared_ptr<ModelAPI_Folder> createFolder(
154 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis);
155 //! Removes the folder from the document (all features in the folder will be kept).
156 void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
157 //! Search a folder applicable for the list of features
158 //! (it means the list of features stored in the folder should be consequential)
159 //! \param theFeatures list of features to be added to folder
160 //! \param theBelow search the folder below the features (if \c false, search above)
161 //! \return Empty pointer if there is no applicable folder
162 std::shared_ptr<ModelAPI_Folder> findFolder(
163 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
164 const bool theBelow);
165 //! Add a list of features to the folder. The correctness of the adding is not performed
166 //! (such checks have been done in corresponding find.. method).
167 //! \return \c true if the movement is successfull
168 bool moveToFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
169 const std::shared_ptr<ModelAPI_Folder>& theFolder);
170 //! Remove features from the folder
171 //! \return \c true if the features have been moved out
172 bool removeFromFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
174 //! Sets the owner of this manager
175 void setOwner(DocumentPtr theDoc);
177 //! Returns the owner of this manager
178 DocumentPtr owner() {return myDoc;}
180 //! Deletes all managed features wit hemmitting of corresponded signal
185 //! Returns (creates if needed) the features label
186 TDF_Label featuresLabel() const;
188 //! Initializes feature with a unique name in this group (unique name is generated as
189 //! feature type + "_" + index
190 void setUniqueName(FeaturePtr theFeature);
192 //! Initializes the foldet with an unique name ("Folder_" + index)
193 void setUniqueName(FolderPtr theFolder);
195 //! Synchronizes myFeatures list with the updated document
196 //! \param theUpdated list of labels that are marked as modified, so featrues must be also
197 //! \param theUpdateReferences causes the update of back-references
198 //! \param theExecuteFeatures requires re-execute modified persistent features
199 //! (not needed on undo/redo/abort/open)
200 //! \param theOpen - on open nothing must be reexecuted, except not persistent results
201 //! \param theFlush makes flush all events in the end of all modifications of this method
202 void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
203 const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
204 //! Synchronizes the BackReferences list in Data of Features and Results
205 void synchronizeBackRefs();
207 //! Creates manager on the OCAF document main label
208 Model_Objects(TDF_Label theMainLab);
210 //! Initializes the data fields of the feature
211 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
213 //! Allows to store the result in the data tree of the document
214 //! (attaches 'data' of result to tree)
215 void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
216 std::shared_ptr<ModelAPI_Result> theResult,
217 const int theResultIndex = 0);
219 //! returns the label of result by index; creates this label if it was not created before
220 TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
221 const int theResultIndex);
223 //! Updates the results list of the feature basing on the current data tree
224 //! theProcessed is used to avoid update twice (since the function is recursive)
225 void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
227 /// Internally makes document know that feature was removed or added in history after creation
228 void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
230 /// Internally makes document know that feature was removed or added in history after creation
231 void updateHistory(const std::string theGroup);
233 /// Clears the history arrays related to this object
234 void clearHistory(ObjectPtr theObj);
236 /// Creates the history: up to date with the current state
237 void createHistory(const std::string& theGroupID);
239 /// Returns the next (from the history point of view) feature, any: invisible or disabled
240 /// \param theCurrent previous to the resulting feature
241 /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
242 FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
243 /// Returns to the first (from the history point of view) feature, any: invisible or disabled
244 FeaturePtr firstFeature();
245 /// Returns to the last (from the history point of view) feature, any: invisible or disabled
246 FeaturePtr lastFeature();
247 /// Returns true if theLater is in history of features creation later than theCurrent
248 bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
250 /// Returns the next or previous label
251 /// \param theCurrent given label
252 /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
253 TDF_Label nextLabel(TDF_Label theCurrent, const bool theReverse = false);
255 /// Returns the result group identifier of the given feature (for this at least one result must
256 /// be created before)
257 std::string featureResultGroup(FeaturePtr theFeature);
259 //! Returns all features of the document including the hidden features which are not in
260 //! history. Not very fast method, for calling once, not in big cycles.
261 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
263 //! Returns all objects of the document including the hidden features which are not in
264 //! history. Not very fast method, for calling once, not in big cycles.
265 std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
267 //! synchronises back references for the given object basing on the collected data
268 void synchronizeBackRefsForObject(
269 const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
271 /// Just removes all features without touching the document data (to be able undo)
272 virtual void eraseAllFeatures();
274 // Check whether the pre-image of the result had user-defined name.
275 // If yes, return this name.
276 bool hasCustomName(DataPtr theFeatureData,
279 std::string& theParentName) const;
281 /// Return object representing a folder or empty pointer
282 ObjectPtr folder(TDF_Label theLabel) const;
285 TDF_Label myMain; ///< main label of the data storage
287 DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
289 /// All managed features (not only in history of OB)
290 /// For optimization mapped by labels
291 NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
294 NCollection_DataMap<TDF_Label, ObjectPtr> myFolders;
296 /// Map from group id to the array that contains all objects located in history.
297 /// Each array is updated by demand from scratch, by browing all the features in the history.
298 std::map<std::string, std::vector<ObjectPtr> > myHistory;
300 friend class Model_Document;
301 friend class Model_Session;
302 friend class Model_Update;
303 friend class Model_AttributeReference;
304 friend class Model_AttributeRefAttr;
305 friend class Model_AttributeRefList;
306 friend class Model_AttributeSelection;
307 friend class Model_SelectionNaming;