diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index 410a193ff7..e6f01ef63a 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -2531,3 +2531,32 @@ void Document::slotChangePropertyEditor(const App::Document &doc, const App::Pro getMainWindow()->setUserSchema(doc.UnitSystem.getValue()); } } + +std::vector Document::getTreeRootObjects() const +{ + std::vector docObjects = d->_pcDocument->getObjects(); + std::unordered_map rootMap; + for (auto it : docObjects) { + rootMap[it] = true; + } + + for (auto obj : docObjects) { + ViewProvider* vp = Application::Instance->getViewProvider(obj); + if (!vp) { + continue; + } + + std::vector children = vp->claimChildren(); + for (auto child : children) { + rootMap[child] = false; + } + } + + std::vector rootObjs; + for (const auto& it : rootMap) { + if (it.second) { + rootObjs.push_back(it.first); + } + } + return rootObjs; +} diff --git a/src/Gui/Document.h b/src/Gui/Document.h index 73df51510d..8d65a4b88f 100644 --- a/src/Gui/Document.h +++ b/src/Gui/Document.h @@ -298,6 +298,9 @@ public: const char *getCameraSettings() const; bool saveCameraSettings(const char *) const; + /// get all tree root objects (objects that are at the root of the object tree) + std::vector getTreeRootObjects() const; + protected: // pointer to the python class Gui::DocumentPy *_pcDocPy; diff --git a/src/Gui/DocumentPy.xml b/src/Gui/DocumentPy.xml index 8a33f9c212..d3fdeada96 100644 --- a/src/Gui/DocumentPy.xml +++ b/src/Gui/DocumentPy.xml @@ -243,5 +243,11 @@ obj : Gui.ViewProvider + + + The list of tree root objects. + + + diff --git a/src/Gui/DocumentPyImp.cpp b/src/Gui/DocumentPyImp.cpp index 28451f14ee..c53832fdf1 100644 --- a/src/Gui/DocumentPyImp.cpp +++ b/src/Gui/DocumentPyImp.cpp @@ -509,6 +509,20 @@ void DocumentPy::setModified(Py::Boolean arg) getDocumentPtr()->setModified(arg); } +Py::List DocumentPy::getTreeRootObjects() const +{ + std::vector objs = getDocumentPtr()->getTreeRootObjects(); + Py::List res; + + for (auto obj : objs) { + //Note: Here we must force the Py::Object to own this Python object as getPyObject() increments the counter + res.append(Py::Object(obj->getPyObject(), true)); + } + + return res; +} + + PyObject *DocumentPy::getCustomAttributes(const char* attr) const { // Note: Here we want to return only a document object if its diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index d73edb1978..a68931ce7a 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -2754,7 +2754,8 @@ void TreeWidget::sortDroppedObjects(TargetItemInfo& targetInfo, std::vectorsetValue(sortedObjList); } else if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { - objList = targetInfo.targetDoc->getRootObjectsIgnoreLinks(); + Gui::Document* guiDoc = Gui::Application::Instance->getDocument(targetInfo.targetDoc->getName()); + objList = guiDoc->getTreeRootObjects(); // First we need to sort objList by treeRank. std::sort(objList.begin(), objList.end(), [](App::DocumentObject* a, App::DocumentObject* b) { diff --git a/src/Mod/Assembly/CommandInsertLink.py b/src/Mod/Assembly/CommandInsertLink.py index 743da1289b..d4e6e14f02 100644 --- a/src/Mod/Assembly/CommandInsertLink.py +++ b/src/Mod/Assembly/CommandInsertLink.py @@ -203,7 +203,8 @@ class TaskAssemblyInsertLink(QtCore.QObject): ): process_objects(obj.OutList, objItem) - process_objects(doc.RootObjectsIgnoreLinks, docItem) + guiDoc = Gui.getDocument(doc.Name) + process_objects(guiDoc.TreeRootObjects, docItem) self.form.partList.expandAll() def onFilterChange(self): diff --git a/src/Mod/Assembly/UtilsAssembly.py b/src/Mod/Assembly/UtilsAssembly.py index a775018369..927c683864 100644 --- a/src/Mod/Assembly/UtilsAssembly.py +++ b/src/Mod/Assembly/UtilsAssembly.py @@ -308,7 +308,7 @@ def getGlobalPlacement(targetObj, container=None): def isThereOneRootAssembly(): - for part in App.activeDocument().RootObjectsIgnoreLinks: + for part in Gui.activeDocument().TreeRootObjects: if part.TypeId == "Assembly::AssemblyObject": return True return False