Salome HOME
Implementation of "allFeatures" method needed for the parameters inside of attributes...
[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 <map>
20 #include <set>
21 #include <vector>
22
23 // for TDF_Label map usage
24 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
25 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
26
27 /**\class Model_Objects
28  * \ingroup DataModel
29  * \brief Manager of objects of the document. Normally one this class corresponds to
30  * one document and just helper to manage objects (ModelAPI_Objects) inside of the document
31  * on the level of data storage.
32  */
33 class Model_Objects
34 {
35  public:
36   //! Registers the fieature in the data structure
37   //! \param theFeature feature that must be added to the data structure
38   //! \param theAfterThis the feature will be added after this feature; 
39   //!        if it is null, the added feature will be the first
40   void addFeature(FeaturePtr theFeature, const FeaturePtr theAfterThis);
41
42   //! Return a list of features, which refers to the feature
43   //! \param theFeature a feature
44   //! \param theRefs a list of reference features
45   //! \param isSendError a flag whether the error message should be send
46   void refsToFeature(FeaturePtr theFeature,
47                      std::set<FeaturePtr>& theRefs,
48                      const bool isSendError = true);
49
50   //! Removes the feature from the document (with result)
51   //! \param theFeature a removed feature
52   void removeFeature(FeaturePtr theFeature);
53
54   //! Returns the existing feature by the label
55   //! \param theLabel base label of the feature
56   FeaturePtr feature(TDF_Label theLabel) const;
57
58   //! Returns the existing object: result or feature
59   //! \param theLabel base label of the object
60   ObjectPtr object(TDF_Label theLabel);
61
62   //! Returns the first found object in the group by the object name
63   //! \param theGroupID group that contains an object
64   //! \param theName name of the object to search
65   //! \returns null if such object is not found
66   std::shared_ptr<ModelAPI_Object> objectByName(
67     const std::string& theGroupID, const std::string& theName);
68
69   //! Returns the result by the result name
70   ResultPtr findByName(const std::string theName);
71
72
73   //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
74   //! \param theObject object of this document
75   //! \returns index started from zero, or -1 if object is invisible or belongs to another document
76   const int index(std::shared_ptr<ModelAPI_Object> theObject);
77
78   //! Returns the feature in the group by the index (started from zero)
79   //! \param theGroupID group that contains a feature
80   //! \param theIndex zero-based index of feature in the group
81   ObjectPtr object(const std::string& theGroupID, const int theIndex);
82
83   //! Returns the number of features in the group
84   int size(const std::string& theGroupID);
85
86   ///! Returns all (and disabled) results of the given type. Not fast method (iterates all features).
87   void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
88
89   /// Creates a construction cresults
90   std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
91       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
92   /// Creates a body results
93   std::shared_ptr<ModelAPI_ResultBody> createBody(
94       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
95   /// Creates a part results
96   std::shared_ptr<ModelAPI_ResultPart> createPart(
97       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
98   /// Creates a group results
99   std::shared_ptr<ModelAPI_ResultGroup> createGroup(
100       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
101
102   std::shared_ptr<ModelAPI_ResultParameter> createParameter(
103       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
104
105   //! Returns a feature by result (owner of result)
106   std::shared_ptr<ModelAPI_Feature>
107     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
108
109   //! Sets the owner of this manager
110   void setOwner(DocumentPtr theDoc);
111
112   //! Returns the owner of this manager
113   DocumentPtr owner() {return myDoc;}
114
115   //! Deletes all managed features wit hemmitting of corresponded signal
116   ~Model_Objects();
117
118  protected:
119
120   //! Returns (creates if needed) the features label
121   TDF_Label featuresLabel() const;
122
123   //! Initializes feature with a unique name in this group (unique name is generated as 
124   //! feature type + "_" + index
125   void setUniqueName(FeaturePtr theFeature);
126
127   //! Synchronizes myFeatures list with the updated document
128   //! \param theMarkUpdated causes the "update" event for all features
129   //! \param theUpdateReferences causes the update of back-references
130   //! \param theFlush makes flush all events in the end of all modifications of this method
131   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences,
132     const bool theFlush);
133   //! Synchronizes the BackReferences list in Data of Features and Results
134   void synchronizeBackRefs();
135
136   //! Creates manager on the OCAF document main label
137   Model_Objects(TDF_Label theMainLab);
138
139   //! Initializes the data fields of the feature
140   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
141
142   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
143   void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
144                    std::shared_ptr<ModelAPI_Result> theResult,
145                    const int theResultIndex = 0);
146
147   //! returns the label of result by index; creates this label if it was not created before
148   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
149
150   //! Updates the results list of the feature basing on the current data tree
151   void updateResults(FeaturePtr theFeature);
152
153   /// Internally makes document know that feature was removed or added in history after creation
154   void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
155
156   /// Internally makes document know that feature was removed or added in history after creation
157   void updateHistory(const std::string theGroup);
158
159   /// Clears the history arrays related to this object
160   void clearHistory(ObjectPtr theObj);
161
162   /// Creates the history: up to date with the current state
163   void createHistory(const std::string& theGroupID);
164
165   /// Returns to the next (from the history point of view) feature, any: invisible or disabled
166   /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
167   FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
168   /// Returns to the first (from the history point of view) feature, any: invisible or disabled
169   FeaturePtr firstFeature();
170   /// Returns to the last (from the history point of view) feature, any: invisible or disabled
171   FeaturePtr lastFeature();
172
173   /// Returns the result group identifier of the given feature (for this at least one result must 
174   /// be created before)
175   std::string featureResultGroup(FeaturePtr theFeature);
176
177   ///! Returns all features of the document including the hidden features which are not in
178   ///! history. Not very fast method, for calling once, not in big cycles.
179   std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
180
181
182  private:
183   TDF_Label myMain; ///< main label of the data storage
184
185   DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
186
187   /// All managed features (not only in history of OB)
188   /// For optimization mapped by labels
189   NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
190
191   /// Map from group id to the array that contains all objects located in history.
192   /// Each array is updated by demand from scratch, by browing all the features in the history.
193   std::map<std::string, std::vector<ObjectPtr> > myHistory;
194
195   friend class Model_Document;
196   friend class Model_Session;
197   friend class Model_Update;
198   friend class Model_AttributeReference;
199   friend class Model_AttributeRefAttr;
200   friend class Model_AttributeRefList;
201 };
202
203 #endif