1 // File: Model_Document.h
2 // Created: 28 Feb 2014
3 // Author: Mikhail PONIKAROV
5 #ifndef Model_Document_H_
6 #define Model_Document_H_
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Feature.h>
11 #include <ModelAPI_Result.h>
13 #include <TDocStd_Document.hxx>
14 #include <NCollection_DataMap.hxx>
15 #include <TDF_Label.hxx>
19 class Handle_Model_Document;
21 // for TDF_Label map usage
22 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
23 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
25 /**\class Model_Document
27 * \brief Document for internal data structure of any object storage.
28 * Document contains all data that must be stored/retrived in the file.
29 * Also it provides acces to this data: open/save, transactions management etc.
31 class Model_Document : public ModelAPI_Document
34 //! Returns the kind of the document: "PartSet", "Part", or something else.
35 //! This kind is used for feature buttons enable/disable depending on active document
36 //! (it uses workbench "document" identifier in XML configuration file for this)
37 MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
39 //! Loads the OCAF document from the file.
40 //! \param theFileName full name of the file to load
41 //! \param theStudyID identifier of the SALOME study to associate with loaded file
42 //! \returns true if file was loaded successfully
43 MODEL_EXPORT virtual bool load(const char* theFileName);
45 //! Saves the OCAF document to the file.
46 //! \param theFileName full name of the file to store
47 //! \param theResults the result full file names that were stored by "save"
48 //! \returns true if file was stored successfully
49 MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
51 //! Removes document data
52 MODEL_EXPORT virtual void close();
54 //! Starts a new operation (opens a tansaction)
55 MODEL_EXPORT virtual void startOperation();
56 //! Finishes the previously started operation (closes the transaction)
57 //! Returns true if transaction in this document is not empty and really was performed
58 MODEL_EXPORT virtual void finishOperation();
59 //! Aborts the operation
60 MODEL_EXPORT virtual void abortOperation();
61 //! Returns true if operation has been started, but not yet finished or aborted
62 MODEL_EXPORT virtual bool isOperation();
63 //! Returns true if document was modified (since creation/opening)
64 MODEL_EXPORT virtual bool isModified();
66 //! Returns True if there are available Undos
67 MODEL_EXPORT virtual bool canUndo();
68 //! Undoes last operation
69 MODEL_EXPORT virtual void undo();
70 //! Returns True if there are available Redos
71 MODEL_EXPORT virtual bool canRedo();
72 //! Redoes last operation
73 MODEL_EXPORT virtual void redo();
75 //! Adds to the document the new feature of the given feature id
76 //! \param creates feature and puts it in the document
77 MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
79 //! Removes the feature from the document (with result)
80 //! \param theCheck if it is false, do not check the references
81 MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature, const bool theCheck = true);
83 //! Returns the existing feature by the label
84 //! \param theLabel base label of the feature
85 MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel);
87 //! Returns the existing object: result or feature
88 //! \param theLabel base label of the object
89 MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
91 //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
92 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
94 //! Internal sub-document by ID
95 MODEL_EXPORT virtual boost::shared_ptr<Model_Document> subDoc(std::string theDocID);
97 ///! Returns the id of hte document
98 MODEL_EXPORT virtual const std::string& id() const
103 //! Returns the feature in the group by the index (started from zero)
104 //! \param theGroupID group that contains a feature
105 //! \param theIndex zero-based index of feature in the group
106 //! \param isOperation if it is true, returns feature (not Object)
107 //! \param theHidden if it is true, it counts also the features that are not in tree
108 MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
109 const bool theHidden = false);
111 //! Returns the number of features in the group
112 //! If theHidden is true, it counts also the features that are not in tree
113 MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
115 /// Creates a construction cresults
116 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
117 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
118 /// Creates a body results
119 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultBody> createBody(
120 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
121 /// Creates a part results
122 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultPart> createPart(
123 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
125 //! Returns a feature by result (owner of result)
126 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature>
127 feature(const boost::shared_ptr<ModelAPI_Result>& theResult);
129 ///! Returns true if parametric updater need to execute feature on recomputartion
130 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
131 bool executeFeatures() {return myExecuteFeatures;}
135 //! Returns (creates if needed) the features label
136 TDF_Label featuresLabel();
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 void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences);
146 //! Synchronizes the BackReferences list in Data of Features and Results
147 void synchronizeBackRefs();
149 //! Creates new document with binary file format
150 Model_Document(const std::string theID, const std::string theKind);
152 Handle_TDocStd_Document document()
157 //! performs compactification of all nested operations into one
158 //! \returns true if resulting transaction is not empty and can be undoed
159 bool compactNested();
161 //! Initializes the data fields of the feature
162 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
164 //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
165 MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
166 boost::shared_ptr<ModelAPI_Result> theResult,
167 const int theResultIndex = 0);
169 //! returns the label of result by index; creates this label if it was not created before
170 TDF_Label resultLabel(const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
172 //! Updates the results list of the feature basing on the current data tree
173 void updateResults(FeaturePtr theFeature);
175 //! Returns all sub documents
176 const std::set<std::string>& subDocuments() const {return mySubs;}
178 friend class Model_Application;
179 friend class Model_Session;
180 friend class Model_AttributeReference;
181 friend class DFBrowser;
184 std::string myID; ///< identifier of the document in the application
185 std::string myKind; ///< kind of the document in the application
186 Handle_TDocStd_Document myDoc; ///< OCAF document
187 /// number of transactions after the last "save" call, used for "IsModified" method
188 int myTransactionsAfterSave;
189 /// number of nested transactions performed (or -1 if not nested)
191 /// All features managed by this document (not only in history of OB)
192 /// For optimization mapped by labels
193 NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
195 ///< set of identifiers of sub-documents of this document
196 std::set<std::string> mySubs;
197 /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
198 std::map<int, bool> myIsEmptyTr;
199 /// If it is true, features are not executed on update (on abort, undo, redo)
200 bool myExecuteFeatures;