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