Salome HOME
Make result part as a container of the 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_HeaderFile
6 #define Model_Document_HeaderFile
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 <map>
15 #include <set>
16
17 class Handle_Model_Document;
18
19 /**\class Model_Document
20  * \ingroup DataModel
21  * \brief Document for internal data structure of any object storage.
22  * Document contains all data that must be stored/retrived in the file.
23  * Also it provides acces to this data: open/save, transactions management etc.
24  */
25 class Model_Document: public ModelAPI_Document
26 {
27 public:
28
29   //! Loads the OCAF document from the file.
30   //! \param theFileName full name of the file to load
31   //! \param theStudyID identifier of the SALOME study to associate with loaded file
32   //! \returns true if file was loaded successfully
33   MODEL_EXPORT virtual bool load(const char* theFileName);
34
35   //! Saves the OCAF document to the file.
36   //! \param theFileName full name of the file to store
37   //! \returns true if file was stored successfully
38   MODEL_EXPORT virtual bool save(const char* theFileName);
39
40   //! Removes document data
41   MODEL_EXPORT virtual void close();
42
43   //! Starts a new operation (opens a tansaction)
44   MODEL_EXPORT virtual void startOperation();
45   //! Finishes the previously started operation (closes the transaction)
46   //! Returns true if transaction in this document is not empty and really was performed
47   MODEL_EXPORT virtual void finishOperation();
48   //! Aborts the operation 
49   MODEL_EXPORT virtual void abortOperation();
50   //! Returns true if operation has been started, but not yet finished or aborted
51   MODEL_EXPORT virtual bool isOperation();
52   //! Returns true if document was modified (since creation/opening)
53   MODEL_EXPORT virtual bool isModified();
54
55   //! Returns True if there are available Undos
56   MODEL_EXPORT virtual bool canUndo();
57   //! Undoes last operation
58   MODEL_EXPORT virtual void undo();
59   //! Returns True if there are available Redos
60   MODEL_EXPORT virtual bool canRedo();
61   //! Redoes last operation
62   MODEL_EXPORT virtual void redo();
63
64   //! Adds to the document the new feature of the given feature id
65   //! \param creates feature and puts it in the document
66   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
67
68   //! Removes the feature from the document (with result)
69   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
70
71   //! Returns the existing feature by the label
72   //! \param theLabel base label of the feature
73   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel);
74
75   //! Returns the existing object: result or feature
76   //! \param theLabel base label of the object
77   MODEL_EXPORT virtual ObjectPtr object(TDF_Label& theLabel);
78
79   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
80   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
81
82   ///! Returns the id of hte document
83   MODEL_EXPORT virtual const std::string& id() const {return myID;}
84
85   //! Returns the feature in the group by the index (started from zero)
86   //! \param theGroupID group that contains a feature
87   //! \param theIndex zero-based index of feature in the group
88   //! \param isOperation if it is true, returns feature (not Object)
89   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex);
90
91   //! Returns the number of features in the group
92   MODEL_EXPORT virtual int size(const std::string& theGroupID);
93
94   /// Creates a construction cresults
95   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
96     const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
97   /// Creates a body results
98   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultBody> createBody(
99     const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
100   /// Creates a part results
101   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_ResultPart> createPart(
102     const boost::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
103
104   //! Returns a feature by result (owner of result)
105   MODEL_EXPORT virtual boost::shared_ptr<ModelAPI_Feature> 
106     feature(const boost::shared_ptr<ModelAPI_Result>& theResult);
107
108
109 protected:
110
111   //! Returns (creates if needed) the group label
112   TDF_Label groupLabel(const std::string theGroup);
113
114   //! Initializes feature with a unique name in this group (unique name is generated as 
115   //! feature type + "_" + index
116   void setUniqueName(FeaturePtr theFeature);
117
118   //! Synchronizes myFeatures list with the updated document
119   void synchronizeFeatures(const bool theMarkUpdated = false);
120
121   //! Creates new document with binary file format
122   Model_Document(const std::string theID);
123
124   Handle_TDocStd_Document document() {return myDoc;}
125
126   //! performas compactification of all nested operations into one
127   void compactNested();
128
129   //! Initializes the data fields of the feature
130   void Model_Document::initData(ObjectPtr theObj, TDF_Label& theLab, const int theTag);
131
132   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
133   MODEL_EXPORT virtual void storeResult(boost::shared_ptr<ModelAPI_Data> theFeatureData,
134     boost::shared_ptr<ModelAPI_Result> theResult, const int theResultIndex = 0);
135
136
137   friend class Model_Application;
138   friend class Model_PluginManager;
139   friend class DFBrowser;
140
141 private:
142   std::string myID; ///< identifier of the document in the application
143   Handle_TDocStd_Document myDoc; ///< OCAF document
144   /// number of transactions after the last "save" call, used for "IsModified" method
145   int myTransactionsAfterSave;
146   /// number of nested transactions performed (or -1 if not nested)
147   int myNestedNum;
148   /// All objects managed by this document (not only in history of OB)
149   std::map<std::string, std::vector<ObjectPtr> > myObjs;
150
151   ///< set of identifiers of sub-documents of this document
152   std::set<std::string> mySubs;
153   /// transaction indexes (related to myTransactionsAfterSave) which were empty in this doc
154   std::map<int, bool> myIsEmptyTr;
155 };
156
157 #endif