Salome HOME
Naming now stores primitives on tags 11+.
[modules/shaper.git] / src / Model / Model_Objects.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #ifndef Model_Objects_H_
22 #define Model_Objects_H_
23
24 #include <Model.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>
30
31 #include <TDocStd_Document.hxx>
32 #include <NCollection_DataMap.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelList.hxx>
35 #include <map>
36 #include <set>
37 #include <vector>
38
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);
42
43 /**\class Model_Objects
44  * \ingroup DataModel
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.
48  */
49 class Model_Objects
50 {
51  public:
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);
57
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);
65
66   //! Removes the feature from the document (with result)
67   //! \param theFeature a removed feature
68   void removeFeature(FeaturePtr theFeature);
69
70   //! Moves the feature to make it after the given one in the history.
71   void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
72
73   //! Returns the existing feature by the label
74   //! \param theLabel base label of the feature
75   FeaturePtr feature(TDF_Label theLabel) const;
76
77   //! Returns the existing object: result or feature
78   //! \param theLabel base label of the object
79   ObjectPtr object(TDF_Label theLabel);
80
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);
87
88   //! Returns the result by the result name
89   ResultPtr findByName(const std::string theName);
90
91
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);
98
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,
104                    const int theIndex,
105                    const bool theAllowFolder = false);
106
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);
111
112   //! Returns the parent object of this child. This may be result or feature, parent of a
113   //! top result. Fast method, that uses internal data structure specifics.
114   std::shared_ptr<ModelAPI_Object> parent(const std::shared_ptr<ModelAPI_Object> theChild);
115
116   //! Returns all (and disabled) results of the given type.
117   //! Not fast method (iterates all features).
118   void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
119
120   //! Returns the number of all features: in the history or not
121   int numInternalFeatures();
122   //! Returns the feature by zero-based index: features in the history or not
123   std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
124
125   //! Returns feature by the id of the feature (produced by the Data "featureId" method)
126   std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
127
128
129   /// Creates a construction result
130   std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
131       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
132   /// Creates a body result
133   std::shared_ptr<ModelAPI_ResultBody> createBody(
134       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
135   /// Creates a part result
136   std::shared_ptr<ModelAPI_ResultPart> createPart(
137       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
138   /// Copies a part result, keeping the reference to origin
139   std::shared_ptr<ModelAPI_ResultPart> copyPart(
140       const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
141       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
142   /// Creates a group result
143   std::shared_ptr<ModelAPI_ResultGroup> createGroup(
144       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
145   /// Creates a field result
146   std::shared_ptr<ModelAPI_ResultField> createField(
147       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
148   /// Creates a parameter result
149   std::shared_ptr<ModelAPI_ResultParameter> createParameter(
150       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
151
152   //! Returns a feature by result (owner of result)
153   std::shared_ptr<ModelAPI_Feature>
154     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
155
156   /// Creates a folder (group of the features in the object browser)
157   std::shared_ptr<ModelAPI_Folder> createFolder(
158       const std::shared_ptr<ModelAPI_Feature>& theBeforeThis);
159   //! Removes the folder from the document (all features in the folder will be kept).
160   void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
161   //! Search a folder applicable for the list of features
162   //! (it means the list of features stored in the folder should be consequential)
163   //! \param theFeatures list of features to be added to folder
164   //! \param theBelow    search the folder below the features (if \c false, search above)
165   //! \return Empty pointer if there is no applicable folder
166   std::shared_ptr<ModelAPI_Folder> findFolder(
167       const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
168       const bool theBelow);
169   //! Search a folder containing the given feature.
170   //! Addtionally calculates a zero-based index of the feature in this folder.
171   //! \param theFeature feature to search
172   //! \param theIndexInFolder zero-based index in the folder or -1 if the feature is top-level.
173   //! \return the folder containing the feature or empty pointer if the feature is top-level.
174   std::shared_ptr<ModelAPI_Folder> findContainingFolder(
175       const std::shared_ptr<ModelAPI_Feature>& theFeature,
176       int& theIndexInFolder);
177   //! Add a list of features to the folder. The correctness of the adding is not performed
178   //! (such checks have been done in corresponding find.. method).
179   //! \return \c true if the movement is successfull
180   bool moveToFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
181                     const std::shared_ptr<ModelAPI_Folder>& theFolder);
182   //! Remove features from the folder
183   //! \param theFeatures list of features to be removed
184   //! \param theBefore   extract features before the folder (this parameter is applicable only
185   //!                    when all features in the folder are taking out,
186   //!                    in other cases the direction is taken automatically)
187   //! \return \c true if the features have been moved out
188   bool removeFromFolder(const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
189                         const bool theBefore = true);
190
191   //! Sets the owner of this manager
192   void setOwner(DocumentPtr theDoc);
193
194   //! Returns the owner of this manager
195   DocumentPtr owner() {return myDoc;}
196
197   //! Deletes all managed features wit hemmitting of corresponded signal
198   ~Model_Objects();
199
200  protected:
201
202   //! Returns (creates if needed) the features label
203   TDF_Label featuresLabel() const;
204
205   //! Initializes feature with a unique name in this group (unique name is generated as
206   //! feature type + "_" + index
207   void setUniqueName(FeaturePtr theFeature);
208
209   //! Initializes the foldet with an unique name ("Folder_" + index)
210   void setUniqueName(FolderPtr theFolder);
211
212   //! Synchronizes myFeatures list with the updated document
213   //! \param theUpdated list of labels that are marked as modified, so featrues must be also
214   //! \param theUpdateReferences causes the update of back-references
215   //! \param theExecuteFeatures requires re-execute modified persistent features
216   //!            (not needed on undo/redo/abort/open)
217   //! \param theOpen - on open nothing must be reexecuted, except not persistent results
218   //! \param theFlush makes flush all events in the end of all modifications of this method
219   void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
220     const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
221   //! Synchronizes the BackReferences list in Data of Features and Results
222   void synchronizeBackRefs();
223
224   //! Creates manager on the OCAF document main label
225   Model_Objects(TDF_Label theMainLab);
226
227   //! Initializes the data fields of the feature
228   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
229
230   //! Allows to store the result in the data tree of the document
231   //! (attaches 'data' of result to tree)
232   void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
233                    std::shared_ptr<ModelAPI_Result> theResult,
234                    const int theResultIndex = 0);
235
236   //! returns the label of result by index; creates this label if it was not created before
237   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
238                         const int theResultIndex);
239
240   //! Updates the results list of the feature basing on the current data tree
241   //! theProcessed is used to avoid update twice (since the function is recursive)
242   void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
243
244   /// Internally makes document know that feature was removed or added in history after creation
245   void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
246
247   /// Internally makes document know that feature was removed or added in history after creation
248   void updateHistory(const std::string theGroup);
249
250   /// Clears the history arrays related to this object
251   void clearHistory(ObjectPtr theObj);
252
253   /// Creates the history: up to date with the current state
254   void createHistory(const std::string& theGroupID);
255
256   /// Returns the next (from the history point of view) feature, any: invisible or disabled
257   /// \param theCurrent previous to the resulting feature
258   /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
259   FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
260   /// Returns to the first (from the history point of view) feature, any: invisible or disabled
261   FeaturePtr firstFeature();
262   /// Returns to the last (from the history point of view) feature, any: invisible or disabled
263   FeaturePtr lastFeature();
264   /// Returns true if theLater is in history of features creation later than theCurrent
265   bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
266
267   /// Returns the next or previous label
268   /// \param theCurrent given label
269   /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
270   TDF_Label nextLabel(TDF_Label theCurrent, const bool theReverse = false);
271
272   /// Returns the result group identifier of the given feature (for this at least one result must
273   /// be created before)
274   std::string featureResultGroup(FeaturePtr theFeature);
275
276   //! Returns all features of the document including the hidden features which are not in
277   //! history. Not very fast method, for calling once, not in big cycles.
278   std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
279
280   //! Returns all objects of the document including the hidden features which are not in
281   //! history. Not very fast method, for calling once, not in big cycles.
282   std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
283
284   //! synchronises back references for the given object basing on the collected data
285   void synchronizeBackRefsForObject(
286     const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
287
288   /// Just removes all features without touching the document data (to be able undo)
289   virtual void eraseAllFeatures();
290
291   // Check whether the pre-image of the result had user-defined name.
292   // If yes, return this name.
293   bool hasCustomName(DataPtr theFeatureData,
294                      ResultPtr theResult,
295                      int theResultIndex,
296                      std::string& theParentName) const;
297
298   /// Return object representing a folder or empty pointer
299   const ObjectPtr& folder(TDF_Label theLabel) const;
300
301  private:
302   TDF_Label myMain; ///< main label of the data storage
303
304   DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
305
306   /// All managed features (not only in history of OB)
307   /// For optimization mapped by labels
308   NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
309
310   /// Managed folders
311   NCollection_DataMap<TDF_Label, ObjectPtr> myFolders;
312
313   /// Map from group id to the array that contains all objects located in history.
314   /// Each array is updated by demand from scratch, by browing all the features in the history.
315   std::map<std::string, std::vector<ObjectPtr> > myHistory;
316
317   friend class Model_Document;
318   friend class Model_Session;
319   friend class Model_Update;
320   friend class Model_AttributeReference;
321   friend class Model_AttributeRefAttr;
322   friend class Model_AttributeRefList;
323   friend class Model_AttributeSelection;
324   friend class Model_SelectionNaming;
325 };
326
327 #endif