Salome HOME
Issue #2052 : really remove results if during the feature execution number of results...
[modules/shaper.git] / src / Model / Model_Objects.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Objects.h
4 // Created:     15 May 2015
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Objects_H_
8 #define Model_Objects_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
15
16 #include <TDocStd_Document.hxx>
17 #include <NCollection_DataMap.hxx>
18 #include <TDF_Label.hxx>
19 #include <TDF_LabelList.hxx>
20 #include <map>
21 #include <set>
22 #include <vector>
23
24 // for TDF_Label map usage
25 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
26 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
27
28 /**\class Model_Objects
29  * \ingroup DataModel
30  * \brief Manager of objects of the document. Normally one this class corresponds to
31  * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
32  * on the level of data storage.
33  */
34 class Model_Objects
35 {
36  public:
37   //! Registers the fieature in the data structure
38   //! \param theFeature feature that must be added to the data structure
39   //! \param theAfterThis the feature will be added after this feature;
40   //!        if it is null, the added feature will be the first
41   void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
42
43   //! Return a list of features, which refers to the feature
44   //! \param theFeature a feature
45   //! \param theRefs a list of reference features
46   //! \param isSendError a flag whether the error message should be send
47   void refsToFeature(FeaturePtr theFeature,
48                      std::set<FeaturePtr>& theRefs,
49                      const bool isSendError = true);
50
51   //! Removes the feature from the document (with result)
52   //! \param theFeature a removed feature
53   void removeFeature(FeaturePtr theFeature);
54
55   //! Moves the feature to make it after the given one in the history.
56   void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
57
58   //! Returns the existing feature by the label
59   //! \param theLabel base label of the feature
60   FeaturePtr feature(TDF_Label theLabel) const;
61
62   //! Returns the existing object: result or feature
63   //! \param theLabel base label of the object
64   ObjectPtr object(TDF_Label theLabel);
65
66   //! Returns the first found object in the group by the object name
67   //! \param theGroupID group that contains an object
68   //! \param theName name of the object to search
69   //! \returns null if such object is not found
70   std::shared_ptr<ModelAPI_Object> objectByName(
71     const std::string& theGroupID, const std::string& theName);
72
73   //! Returns the result by the result name
74   ResultPtr findByName(const std::string theName);
75
76
77   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
78   //! \param theObject object of this document
79   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
80   const int index(std::shared_ptr<ModelAPI_Object> theObject);
81
82   //! Returns the feature in the group by the index (started from zero)
83   //! \param theGroupID group that contains a feature
84   //! \param theIndex zero-based index of feature in the group
85   ObjectPtr object(const std::string& theGroupID, const int theIndex);
86
87   //! Returns the number of features in the group
88   int size(const std::string& theGroupID);
89
90   //! Returns all (and disabled) results of the given type.
91   //! Not fast method (iterates all features).
92   void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
93
94   //! Returns the number of all features: in the history or not
95   int numInternalFeatures();
96   //! Returns the feature by zero-based index: features in the history or not
97   std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
98
99   //! Returns feature by the id of the feature (produced by the Data "featureId" method)
100   std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
101
102
103   /// Creates a construction result
104   std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
105       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
106   /// Creates a body result
107   std::shared_ptr<ModelAPI_ResultBody> createBody(
108       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
109   /// Creates a part result
110   std::shared_ptr<ModelAPI_ResultPart> createPart(
111       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
112   /// Copies a part result, keeping the reference to origin
113   std::shared_ptr<ModelAPI_ResultPart> copyPart(
114       const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
115       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
116   /// Creates a group result
117   std::shared_ptr<ModelAPI_ResultGroup> createGroup(
118       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
119   /// Creates a field result
120   std::shared_ptr<ModelAPI_ResultField> createField(
121       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
122   /// Creates a parameter result
123   std::shared_ptr<ModelAPI_ResultParameter> createParameter(
124       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
125
126   //! Returns a feature by result (owner of result)
127   std::shared_ptr<ModelAPI_Feature>
128     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
129
130   //! Sets the owner of this manager
131   void setOwner(DocumentPtr theDoc);
132
133   //! Returns the owner of this manager
134   DocumentPtr owner() {return myDoc;}
135
136   //! Deletes all managed features wit hemmitting of corresponded signal
137   ~Model_Objects();
138
139  protected:
140
141   //! Returns (creates if needed) the features label
142   TDF_Label featuresLabel() const;
143
144   //! Initializes feature with a unique name in this group (unique name is generated as
145   //! feature type + "_" + index
146   void setUniqueName(FeaturePtr theFeature);
147
148   //! Synchronizes myFeatures list with the updated document
149   //! \param theUpdated list of labels that are marked as modified, so featrues must be also
150   //! \param theUpdateReferences causes the update of back-references
151   //! \param theExecuteFeatures requires re-execute modified persistent features
152   //!            (not needed on undo/redo/abort/open)
153   //! \param theOpen - on open nothing must be reexecuted, except not persistent results
154   //! \param theFlush makes flush all events in the end of all modifications of this method
155   void synchronizeFeatures(const TDF_LabelList& theUpdated, const bool theUpdateReferences,
156     const bool theOpen, const bool theExecuteFeatures, const bool theFlush);
157   //! Synchronizes the BackReferences list in Data of Features and Results
158   void synchronizeBackRefs();
159
160   //! Creates manager on the OCAF document main label
161   Model_Objects(TDF_Label theMainLab);
162
163   //! Initializes the data fields of the feature
164   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
165
166   //! Allows to store the result in the data tree of the document
167   //! (attaches 'data' of result to tree)
168   void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
169                    std::shared_ptr<ModelAPI_Result> theResult,
170                    const int theResultIndex = 0);
171
172   //! returns the label of result by index; creates this label if it was not created before
173   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData,
174                         const int theResultIndex);
175
176   //! Updates the results list of the feature basing on the current data tree
177   //! theProcessed is used to avoid update twice (since the function is recursive)
178   void updateResults(FeaturePtr theFeature, std::set<FeaturePtr>& theProcessed);
179
180   /// Internally makes document know that feature was removed or added in history after creation
181   void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
182
183   /// Internally makes document know that feature was removed or added in history after creation
184   void updateHistory(const std::string theGroup);
185
186   /// Clears the history arrays related to this object
187   void clearHistory(ObjectPtr theObj);
188
189   /// Creates the history: up to date with the current state
190   void createHistory(const std::string& theGroupID);
191
192   /// Returns the next (from the history point of view) feature, any: invisible or disabled
193   /// \param theCurrent previous to the resulting feature
194   /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
195   FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
196   /// Returns to the first (from the history point of view) feature, any: invisible or disabled
197   FeaturePtr firstFeature();
198   /// Returns to the last (from the history point of view) feature, any: invisible or disabled
199   FeaturePtr lastFeature();
200   /// Returns true if theLater is in history of features creation later than theCurrent
201   bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
202
203   /// Returns the result group identifier of the given feature (for this at least one result must
204   /// be created before)
205   std::string featureResultGroup(FeaturePtr theFeature);
206
207   //! Returns all features of the document including the hidden features which are not in
208   //! history. Not very fast method, for calling once, not in big cycles.
209   std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
210
211   //! synchronises back references for the given object basing on the collected data
212   void synchronizeBackRefsForObject(
213     const std::set<std::shared_ptr<ModelAPI_Attribute>>& theNewRefs, ObjectPtr theObject);
214
215  private:
216   TDF_Label myMain; ///< main label of the data storage
217
218   DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
219
220   /// All managed features (not only in history of OB)
221   /// For optimization mapped by labels
222   NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
223
224   /// Map from group id to the array that contains all objects located in history.
225   /// Each array is updated by demand from scratch, by browing all the features in the history.
226   std::map<std::string, std::vector<ObjectPtr> > myHistory;
227
228   friend class Model_Document;
229   friend class Model_Session;
230   friend class Model_Update;
231   friend class Model_AttributeReference;
232   friend class Model_AttributeRefAttr;
233   friend class Model_AttributeRefList;
234   friend class Model_AttributeSelection;
235   friend class Model_SelectionNaming;
236 };
237
238 #endif