1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Objects.h
4 // Created: 15 May 2015
5 // Author: Mikhail PONIKAROV
7 #ifndef Model_Objects_H_
8 #define Model_Objects_H_
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
16 #include <TDocStd_Document.hxx>
17 #include <NCollection_DataMap.hxx>
18 #include <TDF_Label.hxx>
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);
27 /**\class Model_Objects
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.
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);
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);
50 //! Removes the feature from the document (with result)
51 //! \param theFeature a removed feature
52 void removeFeature(FeaturePtr theFeature);
54 //! Moves the feature to make it after the given one in the history.
55 void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
57 //! Returns the existing feature by the label
58 //! \param theLabel base label of the feature
59 FeaturePtr feature(TDF_Label theLabel) const;
61 //! Returns the existing object: result or feature
62 //! \param theLabel base label of the object
63 ObjectPtr object(TDF_Label theLabel);
65 //! Returns the first found object in the group by the object name
66 //! \param theGroupID group that contains an object
67 //! \param theName name of the object to search
68 //! \returns null if such object is not found
69 std::shared_ptr<ModelAPI_Object> objectByName(
70 const std::string& theGroupID, const std::string& theName);
72 //! Returns the result by the result name
73 ResultPtr findByName(const std::string theName);
76 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
77 //! \param theObject object of this document
78 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
79 const int index(std::shared_ptr<ModelAPI_Object> theObject);
81 //! Returns the feature in the group by the index (started from zero)
82 //! \param theGroupID group that contains a feature
83 //! \param theIndex zero-based index of feature in the group
84 ObjectPtr object(const std::string& theGroupID, const int theIndex);
86 //! Returns the number of features in the group
87 int size(const std::string& theGroupID);
89 ///! Returns all (and disabled) results of the given type. Not fast method (iterates all features).
90 void allResults(const std::string& theGroupID, std::list<ResultPtr>& theResults);
92 //! Returns the number of all features: in the history or not
93 int numInternalFeatures();
94 //! Returns the feature by zero-based index: features in the history or not
95 std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
97 /// Creates a construction results
98 std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
99 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
100 /// Creates a body results
101 std::shared_ptr<ModelAPI_ResultBody> createBody(
102 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
103 /// Creates a body results
104 std::shared_ptr<ModelAPI_ResultCompSolid> createCompSolid(
105 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
106 /// Creates a part results
107 std::shared_ptr<ModelAPI_ResultPart> createPart(
108 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
109 /// Copies a part results, keeping the reference to origin
110 std::shared_ptr<ModelAPI_ResultPart> copyPart(
111 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
112 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
113 /// Creates a group results
114 std::shared_ptr<ModelAPI_ResultGroup> createGroup(
115 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
117 std::shared_ptr<ModelAPI_ResultParameter> createParameter(
118 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
120 //! Returns a feature by result (owner of result)
121 std::shared_ptr<ModelAPI_Feature>
122 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
124 //! Sets the owner of this manager
125 void setOwner(DocumentPtr theDoc);
127 //! Returns the owner of this manager
128 DocumentPtr owner() {return myDoc;}
130 //! Deletes all managed features wit hemmitting of corresponded signal
135 //! Returns (creates if needed) the features label
136 TDF_Label featuresLabel() const;
138 //! Initializes feature with a unique name in this group (unique name is generated as
139 //! feature type + "_" + index
140 void setUniqueName(FeaturePtr theFeature);
142 //! Synchronizes myFeatures list with the updated document
143 //! \param theMarkUpdated causes the "update" event for all features
144 //! \param theUpdateReferences causes the update of back-references
145 //! \param theFlush makes flush all events in the end of all modifications of this method
146 void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences,
147 const bool theFlush);
148 //! Synchronizes the BackReferences list in Data of Features and Results
149 void synchronizeBackRefs();
151 //! Creates manager on the OCAF document main label
152 Model_Objects(TDF_Label theMainLab);
154 //! Initializes the data fields of the feature
155 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
157 //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
158 void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
159 std::shared_ptr<ModelAPI_Result> theResult,
160 const int theResultIndex = 0);
162 //! returns the label of result by index; creates this label if it was not created before
163 TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
165 //! Updates the results list of the feature basing on the current data tree
166 void updateResults(FeaturePtr theFeature);
168 /// Internally makes document know that feature was removed or added in history after creation
169 void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
171 /// Internally makes document know that feature was removed or added in history after creation
172 void updateHistory(const std::string theGroup);
174 /// Clears the history arrays related to this object
175 void clearHistory(ObjectPtr theObj);
177 /// Creates the history: up to date with the current state
178 void createHistory(const std::string& theGroupID);
180 /// Returns to the next (from the history point of view) feature, any: invisible or disabled
181 /// \param theReverse if it is true, iterates in reverced order (next becomes previous)
182 FeaturePtr nextFeature(FeaturePtr theCurrent, const bool theReverse = false);
183 /// Returns to the first (from the history point of view) feature, any: invisible or disabled
184 FeaturePtr firstFeature();
185 /// Returns to the last (from the history point of view) feature, any: invisible or disabled
186 FeaturePtr lastFeature();
188 /// Returns the result group identifier of the given feature (for this at least one result must
189 /// be created before)
190 std::string featureResultGroup(FeaturePtr theFeature);
192 ///! Returns all features of the document including the hidden features which are not in
193 ///! history. Not very fast method, for calling once, not in big cycles.
194 std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
198 TDF_Label myMain; ///< main label of the data storage
200 DocumentPtr myDoc; ///< doc,ument, owner of this objects manager: needed for events creation
202 /// All managed features (not only in history of OB)
203 /// For optimization mapped by labels
204 NCollection_DataMap<TDF_Label, FeaturePtr> myFeatures;
206 /// Map from group id to the array that contains all objects located in history.
207 /// Each array is updated by demand from scratch, by browing all the features in the history.
208 std::map<std::string, std::vector<ObjectPtr> > myHistory;
210 friend class Model_Document;
211 friend class Model_Session;
212 friend class Model_Update;
213 friend class Model_AttributeReference;
214 friend class Model_AttributeRefAttr;
215 friend class Model_AttributeRefList;