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_Result.h>
28 #include <ModelAPI_ResultParameter.h>
30 #include <TDocStd_Document.hxx>
31 #include <NCollection_DataMap.hxx>
32 #include <TDF_Label.hxx>
33 #include <TDF_LabelList.hxx>
38 // for TDF_Label map usage
39 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
40 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
42 /**\class Model_Objects
44 * \brief Manager of objects of the document. Normally one this class corresponds to
45 * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
46 * on the level of data storage.
51 //! Registers the fieature in the data structure
52 //! \param theFeature feature that must be added to the data structure
53 //! \param theAfterThis the feature will be added after this feature;
54 //! if it is null, the added feature will be the first
55 void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
57 //! Return a list of features, which refers to the feature
58 //! \param theFeature a feature
59 //! \param theRefs a list of reference features
60 //! \param isSendError a flag whether the error message should be send
61 void refsToFeature(FeaturePtr theFeature,
62 std::set<FeaturePtr>& theRefs,
63 const bool isSendError = true);
65 //! Removes the feature from the document (with result)
66 //! \param theFeature a removed feature
67 void removeFeature(FeaturePtr theFeature);
69 //! Moves the feature to make it after the given one in the history.
70 void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
72 //! Returns the existing feature by the label
73 //! \param theLabel base label of the feature
74 FeaturePtr feature(TDF_Label theLabel) const;
76 //! Returns the existing object: result or feature
77 //! \param theLabel base label of the object
78 ObjectPtr object(TDF_Label theLabel);
80 //! Returns the first found object in the group by the object name
81 //! \param theGroupID group that contains an object
82 //! \param theName name of the object to search
83 //! \returns null if such object is not found
84 std::shared_ptr<ModelAPI_Object> objectByName(
85 const std::string& theGroupID, const std::string& theName);
87 //! Returns the result by the result name
88 ResultPtr findByName(const std::string theName);
91 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
92 //! \param theObject object of this document
93 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
94 const int index(std::shared_ptr<ModelAPI_Object> theObject);
96 //! Returns the feature in the group by the index (started from zero)
97 //! \param theGroupID group that contains a feature
98 //! \param theIndex zero-based index of feature in the group
99 //! \param theAllowFolder take into account grouping feature by folders
100 ObjectPtr object(const std::string& theGroupID,
102 const bool theAllowFolder = false);
104 //! Returns the number of features in the group
105 //! \param theGroupID group of objects
106 //! \param theAllowFolder take into account grouping feature by folders
107 int size(const std::string& theGroupID, const bool theAllowFolder = false);
109 //! Returns all (and disabled) results of the given type.
110 //! Not fast method (iterates all features).
111 void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
113 //! Returns the number of all features: in the history or not
114 int numInternalFeatures();
115 //! Returns the feature by zero-based index: features in the history or not
116 std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
118 //! Returns feature by the id of the feature (produced by the Data "featureId" method)
119 std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
122 /// Creates a construction result
123 std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
124 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
125 /// Creates a body result
126 std::shared_ptr<ModelAPI_ResultBody> createBody(
127 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
128 /// Creates a part result
129 std::shared_ptr<ModelAPI_ResultPart> createPart(
130 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
131 /// Copies a part result, keeping the reference to origin
132 std::shared_ptr<ModelAPI_ResultPart> copyPart(
133 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
134 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
135 /// Creates a group result
136 std::shared_ptr<ModelAPI_ResultGroup> createGroup(
137 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
138 /// Creates a field result
139 std::shared_ptr<ModelAPI_ResultField> createField(
140 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
141 /// Creates a parameter result
142 std::shared_ptr<ModelAPI_ResultParameter> createParameter(
143 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
145 //! Returns a feature by result (owner of result)
146 std::shared_ptr<ModelAPI_Feature>
147 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
149 /// Creates a folder (group of the features in the object browser)
150 std::shared_ptr<ModelAPI_Folder> createFolder(
151 const std::shared_ptr<ModelAPI_Feature>& theBeforeThis);
153 //! Sets the owner of this manager
154 void setOwner(DocumentPtr theDoc);
156 //! Returns the owner of this manager
157 DocumentPtr owner() {return myDoc;}
159 //! Deletes all managed features wit hemmitting of corresponded signal
164 //! Returns (creates if needed) the features label
165 TDF_Label featuresLabel() const;
167 //! Initializes feature with a unique name in this group (unique name is generated as
168 //! feature type + "_" + index
169 void setUniqueName(FeaturePtr theFeature);
171 //! Synchronizes myFeatures list with the updated document
172 //! \param theUpdated list of labels that are marked as modified, so featrues must be also
173 //! \param theUpdateReferences causes the update of back-references
174 //! \param theExecuteFeatures requires re-execute modified persistent features
175 //! (not needed on undo/redo/abort/open)
176 //! \param theOpen - on open nothing must be reexecuted, except not persistent results
177 //! \param theFlush makes flush all events in the end of all modifications of this method
178 void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
179 const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
180 //! Synchronizes the BackReferences list in Data of Features and Results
181 void synchronizeBackRefs();
183 //! Creates manager on the OCAF document main label
184 Model_Objects(TDF_Label theMainLab);
186 //! Initializes the data fields of the feature
187 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
189 //! Allows to store the result in the data tree of the document
190 //! (attaches 'data' of result to tree)
191 void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
192 std::shared_ptr<ModelAPI_Result> theResult,
193 const int theResultIndex = 0);
195 //! returns the label of result by index; creates this label if it was not created before
196 TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
197 const int theResultIndex);
199 //! Updates the results list of the feature basing on the current data tree
200 //! theProcessed is used to avoid update twice (since the function is recursive)
201 void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
203 /// Internally makes document know that feature was removed or added in history after creation
204 void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
206 /// Internally makes document know that feature was removed or added in history after creation
207 void updateHistory(const std::string theGroup);
209 /// Clears the history arrays related to this object
210 void clearHistory(ObjectPtr theObj);
212 /// Creates the history: up to date with the current state
213 void createHistory(const std::string& theGroupID);
215 /// Returns the next (from the history point of view) feature, any: invisible or disabled
216 /// \param theCurrent previous to the resulting feature
217 /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
218 FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
219 /// Returns to the first (from the history point of view) feature, any: invisible or disabled
220 FeaturePtr firstFeature();
221 /// Returns to the last (from the history point of view) feature, any: invisible or disabled
222 FeaturePtr lastFeature();
223 /// Returns true if theLater is in history of features creation later than theCurrent
224 bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
226 /// Returns the next or previous label
227 /// \param theCurrent given label
228 /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
229 TDF_Label nextLabel(TDF_Label theCurrent, const bool theReverse = false);
231 /// Returns the result group identifier of the given feature (for this at least one result must
232 /// be created before)
233 std::string featureResultGroup(FeaturePtr theFeature);
235 //! Returns all features of the document including the hidden features which are not in
236 //! history. Not very fast method, for calling once, not in big cycles.
237 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
239 //! Returns all objects of the document including the hidden features which are not in
240 //! history. Not very fast method, for calling once, not in big cycles.
241 std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
243 //! synchronises back references for the given object basing on the collected data
244 void synchronizeBackRefsForObject(
245 const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
247 /// Just removes all features without touching the document data (to be able undo)
248 virtual void eraseAllFeatures();
250 // Check whether the pre-image of the result had user-defined name.
251 // If yes, return this name.
252 bool hasCustomName(DataPtr theFeatureData,
255 std::string& theParentName) const;
257 /// Return object representing a folder or empty pointer
258 ObjectPtr folder(TDF_Label theLabel) const;
261 TDF_Label myMain; ///< main label of the data storage
263 DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
265 /// All managed features (not only in history of OB)
266 /// For optimization mapped by labels
267 NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
270 NCollection_DataMap<TDF_Label, ObjectPtr> myFolders;
272 /// Map from group id to the array that contains all objects located in history.
273 /// Each array is updated by demand from scratch, by browing all the features in the history.
274 std::map<std::string, std::vector<ObjectPtr> > myHistory;
276 friend class Model_Document;
277 friend class Model_Session;
278 friend class Model_Update;
279 friend class Model_AttributeReference;
280 friend class Model_AttributeRefAttr;
281 friend class Model_AttributeRefList;
282 friend class Model_AttributeSelection;
283 friend class Model_SelectionNaming;