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