1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef Model_Document_H_
21 #define Model_Document_H_
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_ResultParameter.h>
29 #include <TDocStd_Document.hxx>
33 class Handle_Model_Document;
35 class ModelAPI_AttributeSelectionList;
37 /**\class Model_Document
39 * \brief Document for internal data structure of any object storage.
40 * Document contains all data that must be stored/retrieved in the file.
41 * Also it provides access to this data: open/save, transactions management etc.
43 class Model_Document : public ModelAPI_Document
46 //! Returns the kind of the document: "PartSet", "Part", or something else.
47 //! This kind is used for feature buttons enable/disable depending on active document
48 //! (it uses workbench "document" identifier in XML configuration file for this)
49 MODEL_EXPORT virtual const std::string& kind() const {return myKind;}
51 //! Loads the OCAF document from the file.
52 //! \param theDirName directory of the loaded file
53 //! \param theFileName a name of the file to load
54 //! \param theThis the common shared pointer to the document to manage with it later
55 //! \returns true if file was loaded successfully
56 MODEL_EXPORT virtual bool load(
57 const char* theDirName, const char* theFileName, DocumentPtr theThis);
59 //! Saves the OCAF document to the file.
60 //! \param theDirName directory where the document will be saved
61 //! \param theFileName a name of the document file to store
62 //! \param theResults the result full file names that were stored by "save"
63 //! \returns true if file was stored successfully
64 MODEL_EXPORT virtual bool save(
65 const char* theDirName, const char* theFileName, std::list<std::string>& theResults);
67 //! Removes document data
68 //! \param theForever if it is false, document is just hidden
69 //! (to keep possibility make it back on Undo/Redo)
70 MODEL_EXPORT virtual void close(const bool theForever = false);
72 //! Starts a new operation (opens a transaction)
73 MODEL_EXPORT virtual void startOperation();
74 //! Finishes the previously started operation (closes the transaction)
75 //! \returns true if transaction in this document is not empty and really was performed
76 MODEL_EXPORT virtual bool finishOperation();
77 //! Aborts the operation
78 MODEL_EXPORT virtual void abortOperation();
79 //! Returns true if operation has been started, but not yet finished or aborted
80 MODEL_EXPORT virtual bool isOperation() const;
81 //! Returns true if document was modified (since creation/opening)
82 MODEL_EXPORT virtual bool isModified();
84 //! Returns True if there are available Undo-s
85 MODEL_EXPORT virtual bool canUndo();
86 //! Undoes last operation
87 MODEL_EXPORT virtual void undo();
88 //! Clean the undo list
89 MODEL_EXPORT virtual void clearUndos();
90 //! Returns True if there are available Redo-s
91 MODEL_EXPORT virtual bool canRedo();
92 //! Redoes last operation
93 MODEL_EXPORT virtual void redo();
95 //! Adds to the document the new feature of the given feature id
96 //! \param theID creates feature and puts it in the document
97 //! \param theMakeCurrent to make current this new feature in this document
98 MODEL_EXPORT virtual FeaturePtr addFeature(std::string theID, const bool theMakeCurrent = true);
100 //! Return a list of features, which refers to the feature
101 //! \param theFeature a feature
102 //! \param theRefs a list of reference features
103 //! \param isSendError a flag whether the error message should be send
104 MODEL_EXPORT virtual void refsToFeature(FeaturePtr theFeature,
105 std::set<FeaturePtr>& theRefs,
106 const bool isSendError = true);
108 //! Removes the feature from the document (with result)
109 //! It is necessary to flush REDISPLAY signal manually after this method because
110 //! the method sends it, but for the performance purpose does not flush it
111 //! \param theFeature a removed feature
112 MODEL_EXPORT virtual void removeFeature(FeaturePtr theFeature);
114 //! Moves the feature to make it after the given one in the history.
115 MODEL_EXPORT virtual void moveFeature(FeaturePtr theMoved, FeaturePtr theAfterThis);
117 //! Returns the first found object in the group by the object name
118 //! \param theGroupID group that contains an object
119 //! \param theName name of the object to search
120 //! \returns null if such object is not found
121 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> objectByName(
122 const std::string& theGroupID, const std::string& theName);
124 //! Returns the object index in the group. Object must be visible. Otherwise returns -1.
125 //! \param theObject object of this document
126 //! \param theAllowFolder take into account grouping feature by folders
127 //! \returns index started from zero, or -1 if object is invisible or belongs to another document
128 MODEL_EXPORT virtual const int index(std::shared_ptr<ModelAPI_Object> theObject,
129 const bool theAllowFolder = false);
131 //! Internal sub-document by ID
132 MODEL_EXPORT virtual std::shared_ptr<Model_Document> subDoc(int theDocID);
134 ///! Returns the id of the document
135 MODEL_EXPORT virtual const int id() const
140 //! Returns the feature in the group by the index (started from zero)
141 //! \param theGroupID group that contains a feature
142 //! \param theIndex zero-based index of feature in the group
143 //! \param theAllowFolder take into account grouping feature by folders
144 MODEL_EXPORT virtual ObjectPtr object(const std::string& theGroupID,
146 const bool theAllowFolder = false);
148 //! Returns the number of features in the group
149 //! \param theGroupID group of objects
150 //! \param theAllowFolder take into account grouping feature by folders
151 MODEL_EXPORT virtual int size(const std::string& theGroupID, const bool theAllowFolder = false);
153 //! Returns the parent object of this child. This may be result or feature, parent of a
154 //! top result. Fast method, that uses internal data structure specifics.
155 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Object> parent(
156 const std::shared_ptr<ModelAPI_Object> theChild);
158 //! Returns the feature that is currently edited in this document, normally
159 //! this is the latest created feature
160 //! \param theVisible use visible features only: flag is true for Object Browser functionality
161 //! \returns null if next created feature must be the first
162 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> currentFeature(const bool theVisible);
164 //! Sets the current feature: all features below will be disabled, new features
165 //! will be appended after this one.
166 //! \param theCurrent the selected feature as current: below it everything becomes disabled
167 //! \param theVisible use visible features only: flag is true for Object Browser functionality
168 MODEL_EXPORT virtual void setCurrentFeature(std::shared_ptr<ModelAPI_Feature> theCurrent,
169 const bool theVisible);
171 //! Makes the current feature one feature upper
172 MODEL_EXPORT virtual void setCurrentFeatureUp();
174 //! Returns the number of all features: in the history or not
175 MODEL_EXPORT virtual int numInternalFeatures();
176 //! Returns the feature by zero-based index: features in the history or not
177 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> internalFeature(const int theIndex);
178 //! Performs synchronization of transactions with the module document:
179 //! If some document is not active (by undo of activation) but in memory,
180 //! on activation the transactions must be synchronized because all redo-s performed
181 //! without this participation
182 MODEL_EXPORT virtual void synchronizeTransactions();
184 /// Creates construction results
185 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultConstruction> createConstruction(
186 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
187 /// Creates a body results
188 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultBody> createBody(
189 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
190 /// Creates a part results
191 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> createPart(
192 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
193 //! Copies a part result, keeping the reference to origin
194 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultPart> copyPart(
195 const std::shared_ptr<ModelAPI_ResultPart>& theOrigin,
196 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
197 /// Creates a group result
198 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultGroup> createGroup(
199 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
200 /// Creates a field result
201 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultField> createField(
202 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
203 /// Creates a parameter result
204 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_ResultParameter> createParameter(
205 const std::shared_ptr<ModelAPI_Data>& theFeatureData, const int theIndex = 0);
207 //! Returns a feature by result (owner of result)
208 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature>
209 feature(const std::shared_ptr<ModelAPI_Result>& theResult);
211 //! Creates a folder (group of the features in the object browser)
212 //! \param theAddBefore a feature, the folder is added before
213 //! (if empty, the folder is added after the last feature)
214 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> addFolder(
215 std::shared_ptr<ModelAPI_Feature> theAddBefore = std::shared_ptr<ModelAPI_Feature>());
216 //! Removes the folder from the document (all features in the folder will be kept).
217 MODEL_EXPORT virtual void removeFolder(std::shared_ptr<ModelAPI_Folder> theFolder);
218 //! Search a folder above the list of features applicable to store them
219 //! (it means the list of features stored in the folder should be consequential)
220 //! \return Empty pointer if there is no applicable folder
221 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> findFolderAbove(
222 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
223 //! Search a folder below the list of features applicable to store them
224 //! (it means the list of features stored in the folder should be consequential)
225 //! \return Empty pointer if there is no applicable folder
226 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> findFolderBelow(
227 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures);
228 //! Search a folder containing the given feature.
229 //! Additionally calculates a zero-based index of the feature in this folder.
230 //! \param theFeature feature to search
231 //! \param theIndexInFolder zero-based index in the folder or -1 if the feature is top-level.
232 //! \return the folder containing the feature or empty pointer if the feature is top-level.
233 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Folder> findContainingFolder(
234 const std::shared_ptr<ModelAPI_Feature>& theFeature,
235 int& theIndexInFolder);
236 //! Add a list of features to the folder. The correctness of the adding is not performed
237 //! (such checks have been done in corresponding find.. method).
238 //! \return \c true if the movement is successful
239 MODEL_EXPORT virtual bool moveToFolder(
240 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
241 const std::shared_ptr<ModelAPI_Folder>& theFolder);
242 //! Remove features from the folder
243 //! \param theFeatures list of features to be removed
244 //! \param theBefore extract features before the folder (this parameter is applicable only
245 //! when all features in the folder are taking out,
246 //! in other cases the direction is taken automatically)
247 //! \return \c true if the features have been moved out
248 MODEL_EXPORT virtual bool removeFromFolder(
249 const std::list<std::shared_ptr<ModelAPI_Feature> >& theFeatures,
250 const bool theBefore = true);
252 ///! Returns true if parametric updater need to execute feature on recomputation
253 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
254 bool executeFeatures() {return myExecuteFeatures;}
256 ///! On abort, undo or redo it is not necessary: results in document are updated automatically
257 void setExecuteFeatures(const bool theFlag);
259 //! Registers the name of the shape for the topological naming needs
260 void addNamingName(const TDF_Label theLabel, std::string theName);
261 //! Updates the name of some object
262 void changeNamingName(std::string theOldName, const std::string theNewName,
263 const TDF_Label& theLabel);
264 //! Returns the label, keeper of the name for the topological naming needs
265 TDF_Label findNamingName(std::string theName, ResultPtr theContext);
266 //! Returns the number of the name in the history relatively to the given object (by label).
267 //! Start from 1 (this object).
268 int numberOfNameInHistory(const ObjectPtr& theNameObject, const TDF_Label& theStartFrom);
269 //! Returns the result by name of the result (names of results must be unique, used for naming
270 //! selection by name.
271 ResultPtr findByName(std::string& theName, std::string& theSubShapeName, bool& theUniqueContext);
273 ///! Returns all features of the document including the hidden features which are not in
274 ///! history. Not very fast method, for calling once, not in big cycles.
275 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Feature> > allFeatures();
277 //! Returns all objects of the document including the hidden features which are not in
278 //! history. Not very fast method, for calling once, not in big cycles.
279 MODEL_EXPORT virtual std::list<std::shared_ptr<ModelAPI_Object> > allObjects();
281 /// Returns the global identifier of the current transaction (needed for the update algo)
282 MODEL_EXPORT virtual int transactionID();
283 /// Increases the transaction ID
284 MODEL_EXPORT virtual void incrementTransactionID();
286 /// Returns true if document is opened and valid
287 MODEL_EXPORT virtual bool isOpened();
289 /// Returns the last feature in the document (even not visible or disabled)
290 /// \returns null if there is no features
291 FeaturePtr lastFeature();
293 /// Returns the feature that produced the given face of the given result.
294 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> producedByFeature(
295 std::shared_ptr<ModelAPI_Result> theResult,
296 const std::shared_ptr<GeomAPI_Shape>& theShape);
298 /// Returns true if theLater is in history of features creation later than theCurrent
299 MODEL_EXPORT virtual bool isLater(FeaturePtr theLater, FeaturePtr theCurrent) const;
301 /// Just removes all features without touching the document data (to be able undo)
302 MODEL_EXPORT virtual void eraseAllFeatures();
304 /// Returns the next (from the history point of view) feature, any: invisible or disabled
305 /// \param theCurrent previous to the resulting feature
306 /// \param theReverse if it is true, iterates in reversed order (next becomes previous)
307 MODEL_EXPORT virtual std::shared_ptr<ModelAPI_Feature> nextFeature(
308 std::shared_ptr<ModelAPI_Feature> theCurrent, const bool theReverse = false) const;
311 //! Returns (creates if needed) the general label
312 TDF_Label generalLabel() const;
314 //! Creates new document with binary file format
315 Model_Document(const int theID, const std::string theKind);
317 //! Returns the internal OCCT document of this interface
318 Handle_TDocStd_Document document()
323 //! performs compactification of all nested operations into one
324 //! \returns true if resulting transaction is not empty and can be undone
325 void compactNested();
327 //! Returns all loaded sub documents
328 const std::set<int> subDocuments() const;
330 //! The implementation of undo: with or without recursive calls in the sub-documents
331 void undoInternal(const bool theWithSubs, const bool theSynchronize);
333 //! Stores the Id of the current operation (normally is called for the root document)
334 void operationId(const std::string& theId);
336 //! Returns the list of Ids of the operations that can be undone (called for the root document)
337 std::list<std::string> undoList() const;
339 //! Returns the list of Ids of the operations that can be redone (called for the root document)
340 std::list<std::string> redoList() const;
342 //! Internally makes document know that feature was removed or added in history after creation
343 virtual void updateHistory(const std::shared_ptr<ModelAPI_Object> theObject);
344 //! Internally makes document know that feature was removed or added in history after creation
345 virtual void updateHistory(const std::string theGroup);
347 //! Returns true if the document is root module document
350 //! Sets shared pointer to this
351 void setThis(DocumentPtr theDoc);
353 //! Returns the objects manager
354 Model_Objects* objects() {return myObjs;}
356 ///! Informs the document that it becomes active and some actions must be performed
357 virtual void setActive(const bool theFlag);
358 //! Returns true if this document is currently active
359 virtual bool isActive() const;
361 //! Returns the selection attribute that is used
362 //! for calculation of selection externally from the document
363 std::shared_ptr<ModelAPI_AttributeSelectionList> selectionInPartFeature();
365 /// Stores in the document boolean flags: states of the nodes in the object browser.
366 /// Normally is called outside of the transaction, just before "save".
367 virtual void storeNodesState(const std::list<bool>& theStates);
369 /// Returns the stored nodes states. Normally it is called just after "open".
370 /// Appends the values to theStates list.
371 virtual void restoreNodesState(std::list<bool>& theStates) const;
373 /// Label that contains structures for selection of constructions of another document
374 TDF_Label extConstructionsLabel() const;
376 /// searches in this document feature that contains this label
377 FeaturePtr featureByLab(const TDF_Label& theLab);
378 /// searches in this document result that contains this label
379 ResultPtr resultByLab(const TDF_Label& theLab);
381 /// returns true if theThis is later in the features tree and dependencies than theOther
382 bool isLaterByDep(FeaturePtr theThis, FeaturePtr theOther);
384 /// appends the latest transaction to the previous one (used for AutoUpdate enabling transaction)
385 void appendTransactionToPrevious();
387 /// Sets the automatic recomputation flag: true means enabled
388 void setAutoRecomutationState(const bool theState);
389 /// Returns the current automatic recomputation flag: true means enabled
390 bool autoRecomutationState() const;
392 friend class Model_Application;
393 friend class Model_Session;
394 friend class Model_Update;
395 friend class Model_AttributeReference;
396 friend class Model_AttributeRefAttr;
397 friend class Model_AttributeRefList;
398 friend class Model_AttributeRefAttrList;
399 friend class Model_AttributeSelection;
400 friend class Model_AttributeSelectionList;
401 friend class Model_ResultPart;
402 friend class Model_ResultBody;
403 friend class Model_ResultConstruction;
404 friend class Model_SelectionNaming;
405 friend class Model_BodyBuilder;
406 friend class DFBrowser;
409 int myID; ///< identifier of the document in the application
410 std::string myKind; ///< kind of the document in the application
411 Handle_TDocStd_Document myDoc; ///< OCAF document
413 Model_Objects *myObjs; ///< data manager of this document
415 //! counter value of transaction on the last "save" call, used for "IsModified" method
416 int myTransactionSave;
417 //! number of nested transactions performed (list because may be nested inside of nested)
418 //! the list is empty if not nested transaction is performed
419 std::list<int> myNestedNum;
421 //! Information related to the every user-transaction
423 int myOCAFNum; ///< number of OCAF transactions related to each "this" transaction, may be 0
424 std::string myId; ///< user-identifier string of transaction
425 //! default constructor with default Id
426 Transaction(): myOCAFNum(0), myId("") {}
429 //! transaction indexes (related to myTransactionsAfterSave) and info about the real transactions
430 //! in myDocument connected to this operation (may be zero for empty transaction)
431 std::list<Transaction> myTransactions;
432 //! list of info about transactions undone (first is oldest undone)
433 std::list<Transaction> myRedos;
435 //! Optimization for finding the shape-label by topological naming names
436 //! The name -> list of labels where this name is appeared (the last created name is last here)
437 std::map<std::string, std::list<TDF_Label> > myNamingNames;
438 //! If it is true, features are not executed on update (on abort, undo, redo)
439 bool myExecuteFeatures;
441 bool myIsActive; ///< flag that stores the active/not active state
443 //! The selection feature, if needed
444 FeaturePtr mySelectionFeature;
446 bool myIsSetCurrentFeature; ///< flag that my current feature is changed right now (recursion)