Salome HOME
Reanud's patch for modern Cpp11 compilers applied
[modules/shaper.git] / src / Model / Model_Document.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Document.h
4 // Created:     28 Feb 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef Model_Document_H_
8 #define Model_Document_H_
9
10 #include <Model.h>
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14
15 #include <TDocStd_Document.hxx>
16 #include <NCollection_DataMap.hxx>
17 #include <TDF_Label.hxx>
18 #include <map>
19 #include <set>
20
21 class Handle_Model_Document;
22
23 // for TDF_Label map usage
24 static Standard_Integer HashCode(const TDF_Label& theLab, const Standard_Integer theUpper);
25 static Standard_Boolean IsEqual(const TDF_Label& theLab1, const TDF_Label& theLab2);
26
27 /**\class Model_Document
28  * \ingroup DataModel
29  * \brief Document for internal data structure of any object storage.
30  * Document contains all data that must be stored/retrived in the file.
31  * Also it provides acces to this data: open/save, transactions management etc.
32  */
33 class Model_Document : public ModelAPI_Document
34 {
35  public:
36   //! Returns the kind of the document: "PartSet", "Part", or something else.
37   //! This kind is used for feature buttons enable/disable depending on active document
38   //! (it uses workbench "document" identifier in XML configuration file for this)
39   MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
40
41   //! Loads the OCAF document from the file.
42   //! \param theFileName full name of the file to load
43   //! \param theStudyID identifier of the SALOME study to associate with loaded file
44   //! \returns true if file was loaded successfully
45   MODEL_EXPORT virtual bool load(const char* theFileName);
46
47   //! Saves the OCAF document to the file.
48   //! \param theFileName full name of the file to store
49   //! \param theResults the result full file names that were stored by "save"
50   //! \returns true if file was stored successfully
51   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
52
53   //! Removes document data
54   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
55   MODEL_EXPORT virtual void close(const bool theForever = false);
56
57   //! Starts a new operation (opens a tansaction)
58   MODEL_EXPORT virtual void startOperation();
59   //! Finishes the previously started operation (closes the transaction)
60   //! Returns true if transaction in this document is not empty and really was performed
61   MODEL_EXPORT virtual void finishOperation();
62   //! Aborts the operation 
63   MODEL_EXPORT virtual void abortOperation();
64   //! Returns true if operation has been started, but not yet finished or aborted
65   MODEL_EXPORT virtual bool isOperation();
66   //! Returns true if document was modified (since creation/opening)
67   MODEL_EXPORT virtual bool isModified();
68
69   //! Returns True if there are available Undos
70   MODEL_EXPORT virtual bool canUndo();
71   //! Undoes last operation
72   MODEL_EXPORT virtual void undo();
73   //! Returns True if there are available Redos
74   MODEL_EXPORT virtual bool canRedo();
75   //! Redoes last operation
76   MODEL_EXPORT virtual void redo();
77
78   //! Adds to the document the new feature of the given feature id
79   //! \param creates feature and puts it in the document
80   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
81
82   //! Removes the feature from the document (with result)
83   //! \param theCheck if it is false, do not check the references
84   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature, const bool theCheck = true);
85
86   //! Returns the existing feature by the label
87   //! \param theLabel base label of the feature
88   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel) const;
89
90   //! Returns the existing object: result or feature
91   //! \param theLabel base label of the object
92   MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
93
94   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
95   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
96
97   //! Internal sub-document by ID
98   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
99
100   ///! Returns the id of hte document
101   MODEL_EXPORT virtual const std::string& id() const
102   {
103     return myID;
104   }
105
106   //! Returns the feature in the group by the index (started from zero)
107   //! \param theGroupID group that contains a feature
108   //! \param theIndex zero-based index of feature in the group
109   //! \param isOperation if it is true, returns feature (not Object)
110   //! \param theHidden if it is true, it counts also the features that are not in tree
111   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
112                                         const bool theHidden = false);
113
114   //! Returns the number of features in the group
115   //! If theHidden is true, it counts also the features that are not in tree
116   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
117
118   /// Creates a construction cresults
119   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
120       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
121   /// Creates a body results
122   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
123       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
124   /// Creates a part results
125   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
126       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
127   /// Creates a group results
128   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
129       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
130
131   //! Returns a feature by result (owner of result)
132   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
133     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
134
135   ///! Returns true if parametric updater need to execute feature on recomputartion
136   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
137   bool executeFeatures() {return myExecuteFeatures;}
138
139   //! Registers the name of the shape for the topological naming needs
140   void addNamingName(const TDF_Label theLabel, std::string theName);
141   //! Returns the label, keeper of the name  for the topological naming needs
142   TDF_Label findNamingName(std::string theName);
143   //! Returns the result by name of the result (names of results must be unique, used for naming
144   //! selection by name.
145   ResultPtr findByName(const std::string theName);
146
147  protected:
148
149   //! Returns (creates if needed) the features label
150   TDF_Label featuresLabel() const;
151
152   //! Initializes feature with a unique name in this group (unique name is generated as 
153   //! feature type + "_" + index
154   void setUniqueName(FeaturePtr theFeature);
155
156   //! Synchronizes myFeatures list with the updated document
157   //! \param theMarkUpdated causes the "update" event for all features
158   //! \param theUpdateReferences causes the update of back-references
159   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences);
160   //! Synchronizes the BackReferences list in Data of Features and Results
161   void synchronizeBackRefs();
162
163   //! Creates new document with binary file format
164   Model_Document(const std::string theID, const std::string theKind);
165
166   Handle_TDocStd_Document document()
167   {
168     return myDoc;
169   }
170
171   //! performs compactification of all nested operations into one
172   //! \returns true if resulting transaction is not empty and can be undoed
173   void compactNested();
174
175   //! Initializes the data fields of the feature
176   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
177
178   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
179   MODEL_EXPORT virtual void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
180                                         std::shared_ptr<ModelAPI_Result> theResult,
181                                         const int theResultIndex = 0);
182
183   //! returns the label of result by index; creates this label if it was not created before
184   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
185
186   //! Updates the results list of the feature basing on the current data tree
187   void updateResults(FeaturePtr theFeature);
188
189   //! Returns all sub documents
190   const std::set<std::string> subDocuments(const bool theActivatedOnly) const;
191
192   friend class Model_Application;
193   friend class Model_Session;
194   friend class Model_Update;
195   friend class Model_AttributeReference;
196   friend class DFBrowser;
197
198  private:
199   std::string myID;  ///< identifier of the document in the application
200   std::string myKind;  ///< kind of the document in the application
201   Handle_TDocStd_Document myDoc;  ///< OCAF document
202
203   /// counter value of transaction on the last "save" call, used for "IsModified" method
204   int myTransactionSave;
205   /// number of nested transactions performed (list becasue may be nested inside of nested)
206   /// the list is empty if not nested transaction is performed
207   std::list<int> myNestedNum;
208
209   /// transaction indexes (related to myTransactionsAfterSave) and number of real transactions 
210   /// in myDocument connected to this operation (may be zero for empty transaction)
211   std::list<int> myTransactions;
212   /// list of numbers of real document transactions undone (first is oldest undone)
213   std::list<int> myRedos;
214   /// All features managed by this document (not only in history of OB)
215   /// For optimization mapped by labels
216   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
217   /// Optimization for finding the shape-label by topological naming names
218   std::map<std::string, TDF_Label> myNamingNames;
219   /// If it is true, features are not executed on update (on abort, undo, redo)
220   bool myExecuteFeatures;
221 };
222
223 #endif