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