Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom into Dev_1.1.0
[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   //! \returns true if file was loaded successfully
44   MODEL_EXPORT virtual bool load(const char* theFileName);
45
46   //! Saves the OCAF document to the file.
47   //! \param theFileName full name of the file to store
48   //! \param theResults the result full file names that were stored by "save"
49   //! \returns true if file was stored successfully
50   MODEL_EXPORT virtual bool save(const char* theFileName, std::list<std::string>& theResults);
51
52   //! Removes document data
53   //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
54   MODEL_EXPORT virtual void close(const bool theForever = false);
55
56   //! Starts a new operation (opens a tansaction)
57   MODEL_EXPORT virtual void startOperation();
58   //! Finishes the previously started operation (closes the transaction)
59   //! \returns true if transaction in this document is not empty and really was performed
60   MODEL_EXPORT virtual bool finishOperation();
61   //! Aborts the operation 
62   MODEL_EXPORT virtual void abortOperation();
63   //! Returns true if operation has been started, but not yet finished or aborted
64   MODEL_EXPORT virtual bool isOperation() const;
65   //! Returns true if document was modified (since creation/opening)
66   MODEL_EXPORT virtual bool isModified();
67
68   //! Returns True if there are available Undos
69   MODEL_EXPORT virtual bool canUndo();
70   //! Undoes last operation
71   MODEL_EXPORT virtual void undo();
72   //! Returns True if there are available Redos
73   MODEL_EXPORT virtual bool canRedo();
74   //! Redoes last operation
75   MODEL_EXPORT virtual void redo();
76
77   //! Adds to the document the new feature of the given feature id
78   //! \param theID creates feature and puts it in the document
79   MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID);
80
81   //! Return a list of features, which refers to the feature
82   //! \param theFeature a feature
83   //! \param theRefs a list of reference features
84   //! \param isSendError a flag whether the error message should be send
85   MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
86                                           std::set<FeaturePtr>& theRefs,
87                                           const bool isSendError = true);
88
89   //! Removes the feature from the document (with result)
90   //! \param theFeature a removed feature
91   MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
92
93   //! Returns the existing feature by the label
94   //! \param theLabel base label of the feature
95   MODEL_EXPORT virtual FeaturePtr feature(TDF_Label& theLabel) const;
96
97   //! Returns the existing object: result or feature
98   //! \param theLabel base label of the object
99   MODEL_EXPORT virtual ObjectPtr object(TDF_Label theLabel);
100
101   //! Returns the first found object in the group by the object name
102   //! \param theGroupID group that contains an object
103   //! \param theName name of the object to search
104   //! \returns null if such object is not found
105   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
106     const std::string& theGroupID, const std::string& theName);
107
108
109   //! Adds a new sub-document by the identifier, or returns existing one if it is already exist
110   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Document> subDocument(std::string theDocID);
111
112   //! Internal sub-document by ID
113   MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(std::string theDocID);
114
115   ///! Returns the id of hte document
116   MODEL_EXPORT virtual const std::string& id() const
117   {
118     return myID;
119   }
120
121   //! Returns the feature in the group by the index (started from zero)
122   //! \param theGroupID group that contains a feature
123   //! \param theIndex zero-based index of feature in the group
124   //! \param theHidden if it is true, it counts also the features that are not in tree
125   MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex,
126                                         const bool theHidden = false);
127
128   //! Returns the number of features in the group
129   //! If theHidden is true, it counts also the features that are not in tree
130   MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theHidden = false);
131
132   /// Creates a construction cresults
133   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
134       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
135   /// Creates a body results
136   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
137       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
138   /// Creates a part results
139   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
140       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
141   /// Creates a group results
142   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
143       const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
144
145   //! Returns a feature by result (owner of result)
146   MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
147     feature(const std::shared_ptr<ModelAPI_Result>& theResult);
148
149   ///! Returns true if parametric updater need to execute feature on recomputartion
150   ///! On abort, undo or redo it is not necessary: results in document are updated automatically
151   bool executeFeatures() {return myExecuteFeatures;}
152
153   //! Registers the name of the shape for the topological naming needs
154   void addNamingName(const TDF_Label theLabel, std::string theName);
155   //! Returns the label, keeper of the name  for the topological naming needs
156   TDF_Label findNamingName(std::string theName);
157   //! Returns the result by name of the result (names of results must be unique, used for naming
158   //! selection by name.
159   ResultPtr findByName(const std::string theName);
160
161  protected:
162
163   //! Returns (creates if needed) the features label
164   TDF_Label featuresLabel() const;
165
166   //! Initializes feature with a unique name in this group (unique name is generated as 
167   //! feature type + "_" + index
168   void setUniqueName(FeaturePtr theFeature);
169
170   //! Synchronizes myFeatures list with the updated document
171   //! \param theMarkUpdated causes the "update" event for all features
172   //! \param theUpdateReferences causes the update of back-references
173   void synchronizeFeatures(const bool theMarkUpdated, const bool theUpdateReferences);
174   //! Synchronizes the BackReferences list in Data of Features and Results
175   void synchronizeBackRefs();
176
177   //! Creates new document with binary file format
178   Model_Document(const std::string theID, const std::string theKind);
179
180   //! Returns the internal OCCT document of this interface
181   Handle_TDocStd_Document document()
182   {
183     return myDoc;
184   }
185
186   //! performs compactification of all nested operations into one
187   //! \returns true if resulting transaction is not empty and can be undoed
188   void compactNested();
189
190   //! Initializes the data fields of the feature
191   void initData(ObjectPtr theObj, TDF_Label theLab, const int theTag);
192
193   //! Allows to store the result in the data tree of the document (attaches 'data' of result to tree)
194   MODEL_EXPORT virtual void storeResult(std::shared_ptr<ModelAPI_Data> theFeatureData,
195                                         std::shared_ptr<ModelAPI_Result> theResult,
196                                         const int theResultIndex = 0);
197
198   //! returns the label of result by index; creates this label if it was not created before
199   TDF_Label resultLabel(const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theResultIndex);
200
201   //! Updates the results list of the feature basing on the current data tree
202   void updateResults(FeaturePtr theFeature);
203
204   //! Returns all sub documents
205   const std::set<std::string> subDocuments(const bool theActivatedOnly) const;
206
207   //! The implementation of undo: with or without recoursive calls in the sub-documents
208   void undoInternal(const bool theWithSubs, const bool theSynchronize);
209
210   //! Stores the Id of the current operation (normally is called for the root document)
211   void operationId(const std::string& theId);
212
213   //! Returns the list of Ids of the operations that can be undoed (called for the root document)
214   std::list<std::string> undoList() const;
215
216   //! Returns the list of Ids of the operations that can be redoed (called for the root document)
217   std::list<std::string> redoList() const;
218
219   /// Internally makes document know that feature was removed or added in history after creation
220   MODEL_EXPORT virtual void addToHistory(const std::shared_ptr<ModelAPI_Object> theObject);
221
222   friend class Model_Application;
223   friend class Model_Session;
224   friend class Model_Update;
225   friend class Model_AttributeReference;
226   friend class DFBrowser;
227
228  private:
229   std::string myID;  ///< identifier of the document in the application
230   std::string myKind;  ///< kind of the document in the application
231   Handle_TDocStd_Document myDoc;  ///< OCAF document
232
233   /// counter value of transaction on the last "save" call, used for "IsModified" method
234   int myTransactionSave;
235   /// number of nested transactions performed (list becasue may be nested inside of nested)
236   /// the list is empty if not nested transaction is performed
237   std::list<int> myNestedNum;
238
239   /// Information related to the every user-transaction
240   struct Transaction {
241     int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
242     std::string myId; ///< user-identifier string of transaction
243     /// default constructor with default Id
244     Transaction(): myOCAFNum(0), myId("") {}
245   };
246
247   /// transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
248   /// in myDocument connected to this operation (may be zero for empty transaction)
249   std::list<Transaction> myTransactions;
250   /// list of info about transactions undone (first is oldest undone)
251   std::list<Transaction> myRedos;
252   /// All features managed by this document (not only in history of OB)
253   /// For optimization mapped by labels
254   NCollection_DataMap<TDF_Label, FeaturePtr> myObjs;
255   /// Optimization for finding the shape-label by topological naming names
256   std::map<std::string, TDF_Label> myNamingNames;
257   /// If it is true, features are not executed on update (on abort, undo, redo)
258   bool myExecuteFeatures;
259 };
260
261 #endif