Salome HOME
Issue #83: rename and transfer some methods related to document
[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
35   //! Loads the OCAF document from the file.
36   //! \param theFileName full name of the file to load
37   //! \param theStudyID identifier of the SALOME study to associate with loaded file
38   //! \returns true if file was loaded successfully
39   MODEL_EXPORT virtual bool load(const char* theFileName);
40
41   //! Saves the OCAF document to the file.
42   //! \param theFileName full name of the file to store
43   //! \param theResults the result full file names that were stored by "save"
44   //! \returns true if file was stored successfully
45   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
46
47   //! Removes document data
48   MODEL_EXPORT virtual void close();
49
50   //! Starts a new operation (opens a tansaction)
51   MODEL_EXPORT virtual void startOperation();
52   //! Finishes the previously started operation (closes the transaction)
53   //! Returns true if transaction in this document is not empty and really was performed
54   MODEL_EXPORT virtual void finishOperation();
55   //! Aborts the operation 
56   MODEL_EXPORT virtual void abortOperation();
57   //! Returns true if operation has been started, but not yet finished or aborted
58   MODEL_EXPORT virtual bool isOperation();
59   //! Returns true if document was modified (since creation/opening)
60   MODEL_EXPORT virtual bool isModified();
61
62   //! Returns True if there are available Undos
63   MODEL_EXPORT virtual bool canUndo();
64   //! Undoes last operation
65   MODEL_EXPORT virtual void undo();
66   //! Returns True if there are available Redos
67   MODEL_EXPORT virtual bool canRedo();
68   //! Redoes last operation
69   MODEL_EXPORT virtual void redo();
70
71   //! Adds to the document the new feature of the given feature id
72   //! \param creates feature and puts it in the document
73   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
74
75   //! Removes the feature from the document (with result)
76   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
77
78   //! Returns the existing feature by the label
79   //! \param theLabel base label of the feature
80   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel);
81
82   //! Returns the existing object: result or feature
83   //! \param theLabel base label of the object
84   MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
85
86   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
87   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
88
89   //! Internal sub-document by ID
90   MODEL_EXPORT virtual boost::shared_ptr<Model_Document> subDoc(std::string theDocID);
91
92   ///! Returns the id of hte document
93   MODEL_EXPORT virtual const std::string& id() const
94   {
95     return myID;
96   }
97
98   //! Returns the feature in the group by the index (started from zero)
99   //! \param theGroupID group that contains a feature
100   //! \param theIndex zero-based index of feature in the group
101   //! \param isOperation if it is true, returns feature (not Object)
102   //! \param theHidden if it is true, it counts also the features that are not in tree
103   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
104                                         const bool theHidden = false);
105
106   //! Returns the number of features in the group
107   //! If theHidden is true, it counts also the features that are not in tree
108   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
109
110   /// Creates a construction cresults
111   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
112       const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
113   /// Creates a body results
114   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultBody> createBody(
115       const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
116   /// Creates a part results
117   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultPart> createPart(
118       const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
119
120   //! Returns a feature by result (owner of result)
121   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature>
122     feature(const boost::shared_ptr<ModelAPI_Result>& theResult);
123
124   ///! Returns true if parametric updater need to execute feature on recomputartion
125   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
126   bool executeFeatures() {return myExecuteFeatures;}
127
128  protected:
129
130   //! Returns (creates if needed) the features label
131   TDF_Label featuresLabel();
132
133   //! Initializes feature with a unique name in this group (unique name is generated as 
134   //! feature type + "_" + index
135   void setUniqueName(FeaturePtr theFeature);
136
137   //! Synchronizes myFeatures list with the updated document
138   void synchronizeFeatures(const bool theMarkUpdated = false);
139
140   //! Creates new document with binary file format
141   Model_Document(const std::string theID);
142
143   Handle_TDocStd_Document document()
144   {
145     return myDoc;
146   }
147
148   //! performas compactification of all nested operations into one
149   void compactNested();
150
151   //! Initializes the data fields of the feature
152   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
153
154   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
155   MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
156                                         boost::shared_ptr<ModelAPI_Result> theResult,
157                                         const int theResultIndex = 0);
158
159   //! returns the label of result by index; creates this label if it was not created before
160   TDF_Label resultLabel(const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
161
162   //! Updates the results list of the feature basing on the current data tree
163   void updateResults(FeaturePtr theFeature);
164
165   friend class Model_Application;
166   friend class Model_Session;
167   friend class DFBrowser;
168
169  private:
170   std::string myID;  ///< identifier of the document in the application
171   Handle_TDocStd_Document myDoc;  ///< OCAF document
172   /// number of transactions after the last "save" call, used for "IsModified" method
173   int myTransactionsAfterSave;
174   /// number of nested transactions performed (or -1 if not nested)
175   int myNestedNum;
176   /// All features managed by this document (not only in history of OB)
177   /// For optimization mapped by labels
178   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
179
180   ///< set of identifiers of sub-documents of this document
181   std::set<std::string> mySubs;
182   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
183   std::map<int, bool> myIsEmptyTr;
184   /// If it is true, features are not executed on update (on abort, undo, redo)
185   bool myExecuteFeatures;
186 };
187
188 #endif