1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Document.h
4 // Created: 28 Feb 2014
5 // Author: Mikhail PONIKAROV
7 #ifndef Model_Document_H_
8 #define Model_Document_H_
11 #include <ModelAPI_Document.h>
12 #include <ModelAPI_Feature.h>
13 #include <ModelAPI_Result.h>
14 #include <ModelAPI_ResultParameter.h>
16 #include <TDocStd_Document.hxx>
20 class Handle_Model_Document;
22 class ModelAPI_AttributeSelectionList;
24 /**\class Model_Document
26 * \brief Document for internal data structure of any object storage.
27 * Document contains all data that must be stored/retrived in the file.
28 * Also it provides acces to this data: open/save, transactions management etc.
30 class Model_Document : public ModelAPI_Document
33 //! Returns the kind of the document: "PartSet", "Part", or something else.
34 //! This kind is used for feature buttons enable/disable depending on active document
35 //! (it uses workbench "document" identifier in XML configuration file for this)
36 MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
38 //! Loads the OCAF document from the file.
39 //! \param theDirName directory of the loaded file
40 //! \param theFileName a name of the file to load
41 //! \param theThis the common shared pointer to the document to manage with it later
42 //! \returns true if file was loaded successfully
43 MODEL_EXPORT virtual bool load(
44 const char* theDirName, const char* theFileName, DocumentPtr theThis);
46 //! Saves the OCAF document to the file.
47 //! \param theDirName directory where the document will be saved
48 //! \param theFileName a name of the document 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(
52 const char* theDirName, const char* theFileName, std::list<std::string>& theResults);
54 //! Removes document data
55 //! \param theForever if it is false, document is just hiden (to keep possibility make it back on Undo/Redo)
56 MODEL_EXPORT virtual void close(const bool theForever = false);
58 //! Starts a new operation (opens a tansaction)
59 MODEL_EXPORT virtual void startOperation();
60 //! Finishes the previously started operation (closes the transaction)
61 //! \returns true if transaction in this document is not empty and really was performed
62 MODEL_EXPORT virtual bool finishOperation();
63 //! Aborts the operation
64 MODEL_EXPORT virtual void abortOperation();
65 //! Returns true if operation has been started, but not yet finished or aborted
66 MODEL_EXPORT virtual bool isOperation() const;
67 //! Returns true if document was modified (since creation/opening)
68 MODEL_EXPORT virtual bool isModified();
70 //! Returns True if there are available Undos
71 MODEL_EXPORT virtual bool canUndo();
72 //! Undoes last operation
73 MODEL_EXPORT virtual void undo();
74 //! Returns True if there are available Redos
75 MODEL_EXPORT virtual bool canRedo();
76 //! Redoes last operation
77 MODEL_EXPORT virtual void redo();
79 //! Adds to the document the new feature of the given feature id
80 //! \param theID creates feature and puts it in the document
81 //! \param theMakeCurrent to make current this new feature in this document
82 MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
84 //! Return a list of features, which refers to the feature
85 //! \param theFeature a feature
86 //! \param theRefs a list of reference features
87 //! \param isSendError a flag whether the error message should be send
88 MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
89 std::set<FeaturePtr>& theRefs,
90 const bool isSendError = true);
92 //! Removes the feature from the document (with result)
93 //! It is necessary to flush REDISPLAY signal manually after this method because
94 //! the method sends it, but for the performance purpose does not flush it
95 //! \param theFeature a removed feature
96 MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
98 //! Moves the feature to make it after the given one in the history.
99 MODEL_EXPORT virtual void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
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);
108 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
109 //! \param theObject object of this document
110 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
111 MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject);
113 //! Internal sub-document by ID
114 MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(int theDocID);
116 ///! Returns the id of the document
117 MODEL_EXPORT virtual const int id() const
122 //! Returns the feature in the group by the index (started from zero)
123 //! \param theGroupID group that contains a feature
124 //! \param theIndex zero-based index of feature in the group
125 MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID, const int theIndex);
127 //! Returns the number of features in the group
128 MODEL_EXPORT virtual int size(const std::string& theGroupID);
130 //! Returns the feature that is currently edited in this document, normally
131 //! this is the latest created feature
132 //! \param theVisible use visible features only: flag is true for Object Browser functionality
133 //! \returns null if next created feature must be the first
134 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
136 //! Sets the current feature: all features below will be disabled, new features
137 //! will be appended after this one.
138 //! \param theCurrent the selected feature as current: blow it everythin become disabled
139 //! \param theVisible use visible features only: flag is true for Object Browser functionality
140 MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
141 const bool theVisible);
143 //! Makes the current feature one feature upper
144 MODEL_EXPORT virtual void setCurrentFeatureUp();
146 //! Returns the number of all features: in the history or not
147 MODEL_EXPORT virtual int numInternalFeatures();
148 //! Returns the feature by zero-based index: features in the history or not
149 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
150 //! Performs synchronization of transactions with the module document:
151 //! If some document is not active (by undo of activation) but in memory,
152 //! on activation the transactions must be synchronised because all redos performed
153 //! wihtout this participation
154 MODEL_EXPORT virtual void synchronizeTransactions();
156 //! Returns feature by the id of the feature (produced by the Data "featureId" method)
157 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> featureById(const int theId);
160 /// Creates a construction cresults
161 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
162 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
163 /// Creates a body results
164 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
165 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
166 /// Creates a part results
167 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
168 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
169 //! Copies a part result, keeping the reference to origin
170 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> copyPart(
171 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
172 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
173 /// Creates a group result
174 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
175 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
176 /// Creates a parameter result
177 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
178 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
180 //! Returns a feature by result (owner of result)
181 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
182 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
184 ///! Returns true if parametric updater need to execute feature on recomputartion
185 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
186 bool& executeFeatures() {return myExecuteFeatures;}
188 //! Registers the name of the shape for the topological naming needs
189 void addNamingName(const TDF_Label theLabel, std::string theName);
190 //! Returns the label, keeper of the name for the topological naming needs
191 TDF_Label findNamingName(std::string theName);
192 //! Returns the result by name of the result (names of results must be unique, used for naming
193 //! selection by name.
194 ResultPtr findByName(const std::string theName);
196 ///! Returns all features of the document including the hidden features which are not in
197 ///! history. Not very fast method, for calling once, not in big cycles.
198 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
200 /// Returns the global identifier of the current transaction (needed for the update algo)
201 MODEL_EXPORT virtual int transactionID();
202 /// Increases the transaction ID
203 MODEL_EXPORT virtual void incrementTransactionID();
204 /// Decreases the transaction ID
205 MODEL_EXPORT virtual void decrementTransactionID();
207 /// Returns true if document is opened and valid
208 MODEL_EXPORT virtual bool isOpened();
210 /// Returns the last feature in the document (even not visible or disabled)
211 /// \returns null if there is no features
212 FeaturePtr lastFeature();
214 /// Returns the feature that produced the given face of the given result.
215 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> producedByFeature(
216 std::shared_ptr<ModelAPI_Result> theResult,
217 const std::shared_ptr<GeomAPI_Shape>& theShape);
219 /// Returns true if theLater is in history of features creation later than theCurrent
220 MODEL_EXPORT virtual bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
223 //! Returns (creates if needed) the general label
224 TDF_Label generalLabel() const;
226 //! Creates new document with binary file format
227 Model_Document(const int theID, const std::string theKind);
229 //! Returns the internal OCCT document of this interface
230 Handle_TDocStd_Document document()
235 //! performs compactification of all nested operations into one
236 //! \returns true if resulting transaction is not empty and can be undoed
237 void compactNested();
239 //! Returns all loaded sub documents
240 const std::set<int> subDocuments() const;
242 //! The implementation of undo: with or without recoursive calls in the sub-documents
243 void undoInternal(const bool theWithSubs, const bool theSynchronize);
245 //! Stores the Id of the current operation (normally is called for the root document)
246 void operationId(const std::string& theId);
248 //! Returns the list of Ids of the operations that can be undoed (called for the root document)
249 std::list<std::string> undoList() const;
251 //! Returns the list of Ids of the operations that can be redoed (called for the root document)
252 std::list<std::string> redoList() const;
254 //! Internally makes document know that feature was removed or added in history after creation
255 virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
256 //! Internally makes document know that feature was removed or added in history after creation
257 virtual void updateHistory(const std::string theGroup);
259 //! Returns true if the document is root module document
262 //! Sets shared pointer to this
263 void setThis(DocumentPtr theDoc);
265 //! Returns the objects manager
266 Model_Objects* objects() {return myObjs;}
268 ///! Informs the document that it becomes active and some actions must be performed
269 virtual void setActive(const bool theFlag);
270 //! Returns true if this document is currently active
271 virtual bool isActive() const;
273 //! Returns the selection attribute that is used for calculation of selection externally from the document
274 std::shared_ptr<ModelAPI_AttributeSelectionList> selectionInPartFeature();
276 friend class Model_Application;
277 friend class Model_Session;
278 friend class Model_Update;
279 friend class Model_AttributeReference;
280 friend class Model_AttributeRefAttr;
281 friend class Model_AttributeRefList;
282 friend class Model_AttributeRefAttrList;
283 friend class Model_AttributeSelection;
284 friend class Model_ResultPart;
285 friend class Model_ResultCompSolid;
286 friend class DFBrowser;
289 int myID; ///< identifier of the document in the application
290 std::string myKind; ///< kind of the document in the application
291 Handle_TDocStd_Document myDoc; ///< OCAF document
293 Model_Objects *myObjs; ///< data manager of this document
295 //! counter value of transaction on the last "save" call, used for "IsModified" method
296 int myTransactionSave;
297 //! number of nested transactions performed (list becasue may be nested inside of nested)
298 //! the list is empty if not nested transaction is performed
299 std::list<int> myNestedNum;
301 //! Information related to the every user-transaction
303 int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
304 std::string myId; ///< user-identifier string of transaction
305 //! default constructor with default Id
306 Transaction(): myOCAFNum(0), myId("") {}
309 //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
310 //! in myDocument connected to this operation (may be zero for empty transaction)
311 std::list<Transaction> myTransactions;
312 //! list of info about transactions undone (first is oldest undone)
313 std::list<Transaction> myRedos;
315 //! Optimization for finding the shape-label by topological naming names
316 std::map<std::string, TDF_Label> myNamingNames;
317 //! If it is true, features are not executed on update (on abort, undo, redo)
318 bool myExecuteFeatures;
320 bool myIsActive; ///< flag that stores the active/not active state
322 //! The selection feature, if needed
323 FeaturePtr mySelectionFeature;