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 MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
82 //! Returns the existing feature by the label
83 //! \param theLabel base label of the feature
84 MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel);
86 //! Returns the existing object: result or feature
87 //! \param theLabel base label of the object
88 MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
90 //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
91 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
93 //! Internal sub-document by ID
94 MODEL_EXPORT virtual boost::shared_ptr<Model_Document> subDoc(std::string theDocID);
96 ///! Returns the id of hte document
97 MODEL_EXPORT virtual const std::string& id() const
102 //! Returns the feature in the group by the index (started from zero)
103 //! \param theGroupID group that contains a feature
104 //! \param theIndex zero-based index of feature in the group
105 //! \param isOperation if it is true, returns feature (not Object)
106 //! \param theHidden if it is true, it counts also the features that are not in tree
107 MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
108 const bool theHidden = false);
110 //! Returns the number of features in the group
111 //! If theHidden is true, it counts also the features that are not in tree
112 MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
114 /// Creates a construction cresults
115 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
116 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
117 /// Creates a body results
118 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultBody> createBody(
119 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
120 /// Creates a part results
121 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultPart> createPart(
122 const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
124 //! Returns a feature by result (owner of result)
125 MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature>
126 feature(const boost::shared_ptr<ModelAPI_Result>& theResult);
128 ///! Returns true if parametric updater need to execute feature on recomputartion
129 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
130 bool executeFeatures() {return myExecuteFeatures;}
132 ///! Reutrns true is result was conecaled because of usage it by other object
133 virtual bool isConcealed(const boost::shared_ptr<ModelAPI_Object>& theResult) {
134 return myConcealedResults.find(boost::dynamic_pointer_cast<ModelAPI_Result>(theResult))
135 != myConcealedResults.end();
140 //! Returns (creates if needed) the features label
141 TDF_Label featuresLabel();
143 //! Initializes feature with a unique name in this group (unique name is generated as
144 //! feature type + "_" + index
145 void setUniqueName(FeaturePtr theFeature);
147 //! Synchronizes myFeatures list with the updated document
148 void synchronizeFeatures(const bool theMarkUpdated = false);
150 //! Creates new document with binary file format
151 Model_Document(const std::string theID, const std::string theKind);
153 Handle_TDocStd_Document document()
158 //! performs compactification of all nested operations into one
159 //! \returns true if resulting transaction is not empty and can be undoed
160 bool compactNested();
162 //! Initializes the data fields of the feature
163 void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
165 //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
166 MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
167 boost::shared_ptr<ModelAPI_Result> theResult,
168 const int theResultIndex = 0);
170 //! returns the label of result by index; creates this label if it was not created before
171 TDF_Label resultLabel(const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
173 //! Updates the results list of the feature basing on the current data tree
174 void updateResults(FeaturePtr theFeature);
176 //! Stores information that there is a reference to this object
177 void objectIsReferenced(const ObjectPtr& theObject);
178 //! Removes information that there is a reference to this object
179 void objectIsNotReferenced(const ObjectPtr& theObject);
181 //! Returns all sub documents
182 const std::set<std::string>& subDocuments() const {return mySubs;}
184 friend class Model_Application;
185 friend class Model_Session;
186 friend class Model_AttributeReference;
187 friend class DFBrowser;
190 std::string myID; ///< identifier of the document in the application
191 std::string myKind; ///< kind of the document in the application
192 Handle_TDocStd_Document myDoc; ///< OCAF document
193 /// number of transactions after the last "save" call, used for "IsModified" method
194 int myTransactionsAfterSave;
195 /// number of nested transactions performed (or -1 if not nested)
197 /// All features managed by this document (not only in history of OB)
198 /// For optimization mapped by labels
199 NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
200 /// Results that are referenced and must be concealed for object browser
201 std::set<ResultPtr> myConcealedResults;
203 ///< set of identifiers of sub-documents of this document
204 std::set<std::string> mySubs;
205 /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
206 std::map<int, bool> myIsEmptyTr;
207 /// If it is true, features are not executed on update (on abort, undo, redo)
208 bool myExecuteFeatures;