Salome HOME
0b7a618c68119b59f67e47c7a181e98639b7c256
[modules/shaper.git] / src / Model / Model_Document.h
1 // File:        Model_Document.h
2 // Created:     28 Feb 2014
3 // Author:      Mikhail PONIKAROV
4
5 #ifndef Model_Document_H_
6 #define Model_Document_H_
7
8 #include <Model.h>
9 #include <ModelAPI_Document.h>
10 #include <ModelAPI_Feature.h>
11 #include <ModelAPI_Result.h>
12
13 #include <TDocStd_Document.hxx>
14 #include <NCollection_DataMap.hxx>
15 #include <TDF_Label.hxx>
16 #include <map>
17 #include <set>
18
19 class Handle_Model_Document;
20
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);
24
25 /**\class Model_Document
26  * \ingroup DataModel
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.
30  */
31 class Model_Document : public ModelAPI_Document
32 {
33  public:
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;}
38
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);
44
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);
50
51   //! Removes document data
52   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
53   MODEL_EXPORT virtual void close(const bool theForever = false);
54
55   //! Starts a new operation (opens a tansaction)
56   MODEL_EXPORT virtual void startOperation();
57   //! Finishes the previously started operation (closes the transaction)
58   //! Returns true if transaction in this document is not empty and really was performed
59   MODEL_EXPORT virtual void finishOperation();
60   //! Aborts the operation 
61   MODEL_EXPORT virtual void abortOperation();
62   //! Returns true if operation has been started, but not yet finished or aborted
63   MODEL_EXPORT virtual bool isOperation();
64   //! Returns true if document was modified (since creation/opening)
65   MODEL_EXPORT virtual bool isModified();
66
67   //! Returns True if there are available Undos
68   MODEL_EXPORT virtual bool canUndo();
69   //! Undoes last operation
70   MODEL_EXPORT virtual void undo();
71   //! Returns True if there are available Redos
72   MODEL_EXPORT virtual bool canRedo();
73   //! Redoes last operation
74   MODEL_EXPORT virtual void redo();
75
76   //! Adds to the document the new feature of the given feature id
77   //! \param creates feature and puts it in the document
78   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
79
80   //! Removes the feature from the document (with result)
81   //! \param theCheck if it is false, do not check the references
82   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature, const bool theCheck = true);
83
84   //! Returns the existing feature by the label
85   //! \param theLabel base label of the feature
86   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel);
87
88   //! Returns the existing object: result or feature
89   //! \param theLabel base label of the object
90   MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
91
92   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
93   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
94
95   //! Internal sub-document by ID
96   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
97
98   ///! Returns the id of hte document
99   MODEL_EXPORT virtual const std::string& id() const
100   {
101     return myID;
102   }
103
104   //! Returns the feature in the group by the index (started from zero)
105   //! \param theGroupID group that contains a feature
106   //! \param theIndex zero-based index of feature in the group
107   //! \param isOperation if it is true, returns feature (not Object)
108   //! \param theHidden if it is true, it counts also the features that are not in tree
109   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
110                                         const bool theHidden = false);
111
112   //! Returns the number of features in the group
113   //! If theHidden is true, it counts also the features that are not in tree
114   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
115
116   /// Creates a construction cresults
117   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
118       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
119   /// Creates a body results
120   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
121       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
122   /// Creates a part results
123   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
124       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
125   /// Creates a group results
126   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
127       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
128
129   //! Returns a feature by result (owner of result)
130   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
131     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
132
133   ///! Returns true if parametric updater need to execute feature on recomputartion
134   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
135   bool executeFeatures() {return myExecuteFeatures;}
136
137  protected:
138
139   //! Returns (creates if needed) the features label
140   TDF_Label featuresLabel();
141
142   //! Initializes feature with a unique name in this group (unique name is generated as 
143   //! feature type + "_" + index
144   void setUniqueName(FeaturePtr theFeature);
145
146   //! Synchronizes myFeatures list with the updated document
147   //! \param theMarkUpdated causes the "update" event for all features
148   //! \param theUpdateReferences causes the update of back-references
149   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences);
150   //! Synchronizes the BackReferences list in Data of Features and Results
151   void synchronizeBackRefs();
152
153   //! Creates new document with binary file format
154   Model_Document(const std::string theID, const std::string theKind);
155
156   Handle_TDocStd_Document document()
157   {
158     return myDoc;
159   }
160
161   //! performs compactification of all nested operations into one
162   //! \returns true if resulting transaction is not empty and can be undoed
163   bool compactNested();
164
165   //! Initializes the data fields of the feature
166   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
167
168   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
169   MODEL_EXPORT virtual void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
170                                         std::shared_ptr<ModelAPI_Result> theResult,
171                                         const int theResultIndex = 0);
172
173   //! returns the label of result by index; creates this label if it was not created before
174   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
175
176   //! Updates the results list of the feature basing on the current data tree
177   void updateResults(FeaturePtr theFeature);
178
179   //! Returns all sub documents
180   const std::set<std::string>& subDocuments() const {return mySubs;}
181
182   friend class Model_Application;
183   friend class Model_Session;
184   friend class Model_Update;
185   friend class Model_AttributeReference;
186   friend class DFBrowser;
187
188  private:
189   std::string myID;  ///< identifier of the document in the application
190   std::string myKind;  ///< kind of the document in the application
191   Handle_TDocStd_Document myDoc;  ///< OCAF document
192   /// counter of transactions
193   int myTransactionsCounter;
194   /// counter value of transaction on the last "save" call, used for "IsModified" method
195   int myTransactionSave;
196   /// number of nested transactions performed (or -1 if not nested)
197   int myNestedNum;
198   /// All features managed by this document (not only in history of OB)
199   /// For optimization mapped by labels
200   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
201
202   ///< set of identifiers of sub-documents of this document
203   std::set<std::string> mySubs;
204   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
205   std::map<int, bool> myIsEmptyTr;
206   /// If it is true, features are not executed on update (on abort, undo, redo)
207   bool myExecuteFeatures;
208 };
209
210 #endif