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 9ad8913110..06b4107d3c 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -2758,7 +2758,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 diff --git a/src/Mod/Test/CMakeLists.txt b/src/Mod/Test/CMakeLists.txt index 5fc8d5f7c5..acc7140276 100644 --- a/src/Mod/Test/CMakeLists.txt +++ b/src/Mod/Test/CMakeLists.txt @@ -4,6 +4,7 @@ SET(Test_SRCS Init.py BaseTests.py Document.py + GuiDocument.py Metadata.py StringHasher.py Menu.py diff --git a/src/Mod/Test/GuiDocument.py b/src/Mod/Test/GuiDocument.py new file mode 100644 index 0000000000..f5980e959b --- /dev/null +++ b/src/Mod/Test/GuiDocument.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +"""************************************************************************** + * * + * Copyright (c) 2024 Ondsel * + * * + * This file is part of FreeCAD. * + * * + * FreeCAD is free software: you can redistribute it and/or modify it * + * under the terms of the GNU Lesser General Public License as * + * published by the Free Software Foundation, either version 2.1 of the * + * License, or (at your option) any later version. * + * * + * FreeCAD is distributed in the hope that it will be useful, but * + * WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * + * Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with FreeCAD. If not, see * + * . * + * * + ***************************************************************************/""" + +import FreeCAD, FreeCADGui, unittest + +# --------------------------------------------------------------------------- +# define the functions to test the FreeCAD Gui Document code +# --------------------------------------------------------------------------- + + +class TestGuiDocument(unittest.TestCase): + def setUp(self): + # Create a new document + self.doc = FreeCAD.newDocument("TestDoc") + + def tearDown(self): + # Close the document + FreeCAD.closeDocument("TestDoc") + + def testGetTreeRootObject(self): + # Create objects at the root level + group1 = self.doc.addObject("App::DocumentObjectGroup", "Group1") + group2 = self.doc.addObject("App::DocumentObjectGroup", "Group2") + obj1 = self.doc.addObject("App::FeaturePython", "RootObject1") + part1 = self.doc.addObject("App::Part", "Part1") + + # Create App::Parts and groups with objects in them + part1_obj = part1.newObject("App::FeaturePython", "Part1_Object") + group3 = group2.newObject("App::DocumentObjectGroup", "Group1") + group1_obj = group3.newObject("App::FeaturePython", "Group1_Object") + + # Fetch the root objects using getTreeRootObjects + root_objects = FreeCADGui.getDocument("TestDoc").TreeRootObjects + + # Check if the new function returns the correct root objects + expected_root_objects = [group1, group2, obj1, part1] + self.assertEqual(set(root_objects), set(expected_root_objects)) diff --git a/src/Mod/Test/InitGui.py b/src/Mod/Test/InitGui.py index 61fec8c7a8..9ce7ca0809 100644 --- a/src/Mod/Test/InitGui.py +++ b/src/Mod/Test/InitGui.py @@ -92,4 +92,10 @@ class TestWorkbench(Workbench): Gui.addWorkbench(TestWorkbench()) # Base system tests -FreeCAD.__unit_test__ += ["Workbench", "Menu", "Menu.MenuDeleteCases", "Menu.MenuCreateCases"] +FreeCAD.__unit_test__ += [ + "Workbench", + "Menu", + "Menu.MenuDeleteCases", + "Menu.MenuCreateCases", + "GuiDocument", +]