App: API changes for document recompute/save/restore/import/export
This patch adds support of recomputation with external linked object, as well as external document auto loading and partial loading. Application: * Modified new/openDocument()/signalNewDocument to choose whether to signal GUI for creating a view for the document. This makes it possible to suppress view creation when opening external documents. * New API openDocuments() which does the actual job of loading the document together with any external dependencies. There are afew extra arguments to allow setting FileName property differently from the actual file path, which are required when auto loading dependencies during document recovery (with future patch to Gui::DocumentRecovery) * openDocumentPrivate() is an internal helper for opening individual document. * New signalStart/FinishOpenDocument to be signaled before and after opening a document. There may be multiple depending documents actually opened in between these two signals. * New signalBeforeRecomputeDocument signaled before recompute a document. * New API addPendingDocument() for use by external capable link properties' to queue up external documents. * isRestoring/isClosingAll(), for convenience status reporting. Document: * signalFinishImport/RestoreObjects, new signal triggered after imported or restored all input objects * signalBeforeRecompute, signaled before start recomputing this document * Modified signalRecomputed with additional recomputed objects, this is to make it more efficient for Gui::TreeWidget to check recomputation result. * signalSkipRecompute, signal to inform which objects are skipped during recomputation because of their owner document SkipRecompute setting. * restore/save/read/writeObjects() modified to suport partial loading. See [here](https://git.io/fj6PY) for more information. * afterRestore(), internal function called to finish restore. The function is separated from restore() because there is quite a few critical steps needed to fully restore a document with external linking. See [here](https://git.io/fj6P4) for more information. * DocumentP::_RecomputeLog is modified to store more accurate object recomputation error, including those happened during restore/import. * isExporting(), new API for checking if an object is exporting. External linking properties will use this function to decide how to export. * copyObject(), modified to support external linking objects, and accepts multiple input objects. * moveObject(), modified to support arbitary object moves. The original implementation may cause crash if undo/redo is enabled. Furthermore, because the original information fakes the object's deletion to break its dependency, it does not work for objects that may auto delete their children when being deleted. The new implementation copy the object, and than paste it to the other document. It then deletes the input objects from the original document. In case of recursive move, it only deletes the depending object if it has an empty in list. * importLinks(), new API to import any external object linked by the input objects into this document. It will auto correct all link references after importing. * getDependencyList/_rebuildDependencyList(), these two APIs are unified and implemented by an internal function _buildDependencyList() with a new algorithm to handle external objects. The returned dependency list will now include objects from external documents. In case of cyclic dependencies, getDpendencyList() will report the actual objects involved in dependency loops. * mustExecute(), new API to check if there are any object requires recomputation. This function will call _buildDependencyList() and check for external objects as well. * addRecomputeObject(), new API for marking changes during document restore. It only marks the object but does not actually recompute them for performance reason. One use case is for geo feature to request for recomputation to generate geometry topological names. * recompute(), support partial, external, and inverse dependency recomputation. Improve error handling during recomputation. See [here](https://git.io/fj6PO) for more information. * _recomputeFeature(), suppoert user abort. * getDependentDocuments/getInList(), new API to obtain an optional dependency sorted list of depending documents. DocumentObject: * Add various ObjectStatus flags * isExporting/getExportName(), return a safe name for exporting, in the form of <ObjName>@<DocName>, which is guarrenteed to be unique. Various link property will save linked object using this name if the the linked object is exported together with the owner object, see [PropertyLinkBase::restoreLabelReference()](https://git.io/fj6XO) for more information. * recomputeFeature(), add option to recompute this object together with all its dependent objects. * canLoadPartial(), new API for [partial document loading](https://git.io/fj6PY). MergeDocuments: * Move object name mapping logic to various link properties. See Base::Sequencer: * Add new API checkAbort() for checking user abort.
This commit is contained in:
@@ -45,6 +45,7 @@
|
||||
#include <Base/Factory.h>
|
||||
#include <Base/FileInfo.h>
|
||||
#include <Base/UnitsApi.h>
|
||||
#include <Base/Sequencer.h>
|
||||
|
||||
//using Base::GetConsole;
|
||||
using namespace Base;
|
||||
@@ -132,8 +133,8 @@ PyMethodDef Application::Methods[] = {
|
||||
"Get a document by its name or raise an exception\n"
|
||||
"if there is no document with the given name."},
|
||||
{"listDocuments", (PyCFunction) Application::sListDocuments, METH_VARARGS,
|
||||
"listDocuments() -> list\n\n"
|
||||
"Return a list of names of all documents."},
|
||||
"listDocuments(sort=False) -> list\n\n"
|
||||
"Return a list of names of all documents, optionally sort in dependency order."},
|
||||
{"addDocumentObserver", (PyCFunction) Application::sAddDocObserver, METH_VARARGS,
|
||||
"addDocumentObserver() -> None\n\n"
|
||||
"Add an observer to get notified about changes on documents."},
|
||||
@@ -151,6 +152,12 @@ PyMethodDef Application::Methods[] = {
|
||||
"getLinksTo(obj,options=0,maxCount=0) -- return the objects linked to 'obj'\n\n"
|
||||
"options: 1: recursive, 2: check link array. Options can combine.\n"
|
||||
"maxCount: to limit the number of links returned\n"},
|
||||
{"getDependentObjects", (PyCFunction) Application::sGetDependentObjects, METH_VARARGS,
|
||||
"getDependentObjects(obj|[obj,...], options=0)\n"
|
||||
"Return a list of dependent objects including the given objects.\n\n"
|
||||
"options: can have the following bit flags,\n"
|
||||
" 1: to sort the list in topological order.\n"
|
||||
" 2: to exclude dependency of Link type object."},
|
||||
{"setActiveTransaction", (PyCFunction) Application::sSetActiveTransaction, METH_VARARGS,
|
||||
"setActiveTransaction(name, persist=False) -- setup active transaction with the given name\n\n"
|
||||
"name: the transaction name\n"
|
||||
@@ -164,6 +171,14 @@ PyMethodDef Application::Methods[] = {
|
||||
"getActiveTransaction() -> (name,id) return the current active transaction name and ID"},
|
||||
{"closeActiveTransaction", (PyCFunction) Application::sCloseActiveTransaction, METH_VARARGS,
|
||||
"closeActiveTransaction(abort=False) -- commit or abort current active transaction"},
|
||||
{"isRestoring", (PyCFunction) Application::sIsRestoring, METH_VARARGS,
|
||||
"isRestoring() -> Bool -- Test if the application is opening some document"},
|
||||
{"checkAbort", (PyCFunction) Application::sCheckAbort, METH_VARARGS,
|
||||
"checkAbort() -- check for user abort in length operation.\n\n"
|
||||
"This only works if there is an active sequencer (or ProgressIndicator in Python).\n"
|
||||
"There is an active sequencer during document restore and recomputation. User may\n"
|
||||
"abort the operation by pressing the ESC key. Once detected, this function will\n"
|
||||
"trigger a BaseExceptionFreeCADAbort exception."},
|
||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
@@ -213,6 +228,12 @@ PyObject* Application::sLoadFile(PyObject * /*self*/, PyObject *args)
|
||||
}
|
||||
}
|
||||
|
||||
PyObject* Application::sIsRestoring(PyObject * /*self*/, PyObject *args) {
|
||||
if (!PyArg_ParseTuple(args, ""))
|
||||
return NULL;
|
||||
return Py::new_reference_to(Py::Boolean(GetApplication().isRestoring()));
|
||||
}
|
||||
|
||||
PyObject* Application::sOpenDocument(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
char* Name;
|
||||
@@ -645,22 +666,26 @@ PyObject* Application::sGetHomePath(PyObject * /*self*/, PyObject *args)
|
||||
|
||||
PyObject* Application::sListDocuments(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
|
||||
PyObject *sort = Py_False;
|
||||
if (!PyArg_ParseTuple(args, "|O",&sort)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
PY_TRY {
|
||||
PyObject *pDict = PyDict_New();
|
||||
PyObject *pKey;
|
||||
Base::PyObjectBase* pValue;
|
||||
|
||||
for (std::map<std::string,Document*>::const_iterator It = GetApplication().DocMap.begin();
|
||||
It != GetApplication().DocMap.end();++It) {
|
||||
std::vector<Document*> docs = GetApplication().getDocuments();;
|
||||
if(PyObject_IsTrue(sort))
|
||||
docs = Document::getDependentDocuments(docs,true);
|
||||
|
||||
for (auto doc : docs) {
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
pKey = PyUnicode_FromString(It->first.c_str());
|
||||
pKey = PyUnicode_FromString(doc->getName());
|
||||
#else
|
||||
pKey = PyString_FromString(It->first.c_str());
|
||||
pKey = PyString_FromString(doc->getName());
|
||||
#endif
|
||||
// GetPyObject() increments
|
||||
pValue = static_cast<Base::PyObjectBase*>(It->second->getPyObject());
|
||||
pValue = static_cast<Base::PyObjectBase*>(doc->getPyObject());
|
||||
PyDict_SetItem(pDict, pKey, pValue);
|
||||
// now we can decrement again as PyDict_SetItem also has incremented
|
||||
pValue->DecRef();
|
||||
@@ -819,6 +844,41 @@ PyObject *Application::sGetLinksTo(PyObject * /*self*/, PyObject *args)
|
||||
}PY_CATCH;
|
||||
}
|
||||
|
||||
PyObject *Application::sGetDependentObjects(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
PyObject *obj;
|
||||
int options = 0;
|
||||
if (!PyArg_ParseTuple(args, "O|i", &obj,&options))
|
||||
return 0;
|
||||
|
||||
std::vector<App::DocumentObject*> objs;
|
||||
if(PySequence_Check(obj)) {
|
||||
Py::Sequence seq(obj);
|
||||
for(size_t i=0;i<seq.size();++i) {
|
||||
if(!PyObject_TypeCheck(seq[i].ptr(),&DocumentObjectPy::Type)) {
|
||||
PyErr_SetString(PyExc_TypeError, "Expect element in sequence to be of type document object");
|
||||
return 0;
|
||||
}
|
||||
objs.push_back(static_cast<DocumentObjectPy*>(seq[i].ptr())->getDocumentObjectPtr());
|
||||
}
|
||||
}else if(!PyObject_TypeCheck(obj,&DocumentObjectPy::Type)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"Expect first argument to be either a document object or sequence of document objects");
|
||||
return 0;
|
||||
}else
|
||||
objs.push_back(static_cast<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
|
||||
|
||||
PY_TRY {
|
||||
auto ret = App::Document::getDependencyList(objs,options);
|
||||
|
||||
Py::Tuple tuple(ret.size());
|
||||
for(size_t i=0;i<ret.size();++i)
|
||||
tuple.setItem(i,Py::Object(ret[i]->getPyObject(),true));
|
||||
return Py::new_reference_to(tuple);
|
||||
} PY_CATCH;
|
||||
}
|
||||
|
||||
|
||||
PyObject *Application::sSetActiveTransaction(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
char *name;
|
||||
@@ -862,3 +922,13 @@ PyObject *Application::sCloseActiveTransaction(PyObject * /*self*/, PyObject *ar
|
||||
} PY_CATCH;
|
||||
}
|
||||
|
||||
PyObject *Application::sCheckAbort(PyObject * /*self*/, PyObject *args)
|
||||
{
|
||||
if (!PyArg_ParseTuple(args, ""))
|
||||
return 0;
|
||||
|
||||
PY_TRY {
|
||||
Base::Sequencer().checkAbort();
|
||||
Py_Return;
|
||||
}PY_CATCH
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user