Salome HOME
d8743f0994d03ffe707df3d26605251531c81f6f
[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   MODEL_EXPORT virtual void close();
53
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();
65
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();
74
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);
78
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);
82
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);
86
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);
90
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);
93
94   //! Internal sub-document by ID
95   MODEL_EXPORT virtual boost::shared_ptr<Model_Document> subDoc(std::string theDocID);
96
97   ///! Returns the id of hte document
98   MODEL_EXPORT virtual const std::string& id() const
99   {
100     return myID;
101   }
102
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);
110
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);
114
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);
124   /// Creates a group results
125   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultGroup> createGroup(
126       const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
127
128   //! Returns a feature by result (owner of result)
129   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature>
130     feature(const boost::shared_ptr<ModelAPI_Result>& theResult);
131
132   ///! Returns true if parametric updater need to execute feature on recomputartion
133   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
134   bool executeFeatures() {return myExecuteFeatures;}
135
136  protected:
137
138   //! Returns (creates if needed) the features label
139   TDF_Label featuresLabel();
140
141   //! Initializes feature with a unique name in this group (unique name is generated as 
142   //! feature type + "_" + index
143   void setUniqueName(FeaturePtr theFeature);
144
145   //! Synchronizes myFeatures list with the updated document
146   //! \param theMarkUpdated causes the "update" event for all features
147   //! \param theUpdateReferences causes the update of back-references
148   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences);
149   //! Synchronizes the BackReferences list in Data of Features and Results
150   void synchronizeBackRefs();
151
152   //! Creates new document with binary file format
153   Model_Document(const std::string theID, const std::string theKind);
154
155   Handle_TDocStd_Document document()
156   {
157     return myDoc;
158   }
159
160   //! performs compactification of all nested operations into one
161   //! \returns true if resulting transaction is not empty and can be undoed
162   bool compactNested();
163
164   //! Initializes the data fields of the feature
165   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
166
167   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
168   MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
169                                         boost::shared_ptr<ModelAPI_Result> theResult,
170                                         const int theResultIndex = 0);
171
172   //! returns the label of result by index; creates this label if it was not created before
173   TDF_Label resultLabel(const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
174
175   //! Updates the results list of the feature basing on the current data tree
176   void updateResults(FeaturePtr theFeature);
177
178   //! Returns all sub documents
179   const std::set<std::string>& subDocuments() const {return mySubs;}
180
181   friend class Model_Application;
182   friend class Model_Session;
183   friend class Model_AttributeReference;
184   friend class DFBrowser;
185
186  private:
187   std::string myID;  ///< identifier of the document in the application
188   std::string myKind;  ///< kind of the document in the application
189   Handle_TDocStd_Document myDoc;  ///< OCAF document
190   /// number of transactions after the last "save" call, used for "IsModified" method
191   int myTransactionsAfterSave;
192   /// number of nested transactions performed (or -1 if not nested)
193   int myNestedNum;
194   /// All features managed by this document (not only in history of OB)
195   /// For optimization mapped by labels
196   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
197
198   ///< set of identifiers of sub-documents of this document
199   std::set<std::string> mySubs;
200   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
201   std::map<int, bool> myIsEmptyTr;
202   /// If it is true, features are not executed on update (on abort, undo, redo)
203   bool myExecuteFeatures;
204 };
205
206 #endif