Salome HOME
e67556cc966d41a3d46a3cfd19b92056bdb322d3
[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   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
81
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);
85
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);
89
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);
92
93   //! Internal sub-document by ID
94   MODEL_EXPORT virtual boost::shared_ptr<Model_Document> subDoc(std::string theDocID);
95
96   ///! Returns the id of hte document
97   MODEL_EXPORT virtual const std::string& id() const
98   {
99     return myID;
100   }
101
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);
109
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);
113
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);
123
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);
127
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;}
131
132  protected:
133
134   //! Returns (creates if needed) the features label
135   TDF_Label featuresLabel();
136
137   //! Initializes feature with a unique name in this group (unique name is generated as 
138   //! feature type + "_" + index
139   void setUniqueName(FeaturePtr theFeature);
140
141   //! Synchronizes myFeatures list with the updated document
142   void synchronizeFeatures(const bool theMarkUpdated = false);
143
144   //! Creates new document with binary file format
145   Model_Document(const std::string theID, const std::string theKind);
146
147   Handle_TDocStd_Document document()
148   {
149     return myDoc;
150   }
151
152   //! performs compactification of all nested operations into one
153   //! \returns true if resulting transaction is not empty and can be undoed
154   bool compactNested();
155
156   //! Initializes the data fields of the feature
157   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
158
159   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
160   MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
161                                         boost::shared_ptr<ModelAPI_Result> theResult,
162                                         const int theResultIndex = 0);
163
164   //! returns the label of result by index; creates this label if it was not created before
165   TDF_Label resultLabel(const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
166
167   //! Updates the results list of the feature basing on the current data tree
168   void updateResults(FeaturePtr theFeature);
169
170   //! Stores information that there is a reference to this object
171   void objectIsReferenced(const ObjectPtr& theObject);
172   //! Removes information that there is a reference to this object
173   void objectIsNotReferenced(const ObjectPtr& theObject);
174
175   friend class Model_Application;
176   friend class Model_Session;
177   friend class Model_AttributeReference;
178   friend class DFBrowser;
179
180  private:
181   std::string myID;  ///< identifier of the document in the application
182   std::string myKind;  ///< kind of the document in the application
183   Handle_TDocStd_Document myDoc;  ///< OCAF document
184   /// number of transactions after the last "save" call, used for "IsModified" method
185   int myTransactionsAfterSave;
186   /// number of nested transactions performed (or -1 if not nested)
187   int myNestedNum;
188   /// All features managed by this document (not only in history of OB)
189   /// For optimization mapped by labels
190   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
191   /// Results that are referenced and must be concealed for object browser
192   std::set<ResultPtr> myConcealedResults;
193
194   ///< set of identifiers of sub-documents of this document
195   std::set<std::string> mySubs;
196   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
197   std::map<int, bool> myIsEmptyTr;
198   /// If it is true, features are not executed on update (on abort, undo, redo)
199   bool myExecuteFeatures;
200 };
201
202 #endif