Merge pull request #14388 from Ondsel-Development/core_rootObjects
Core: Add Gui::Document::getTreeRootObjects() Fixes #14373
This commit is contained in:
@@ -2531,3 +2531,32 @@ void Document::slotChangePropertyEditor(const App::Document &doc, const App::Pro
|
||||
getMainWindow()->setUserSchema(doc.UnitSystem.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<App::DocumentObject*> Document::getTreeRootObjects() const
|
||||
{
|
||||
std::vector<App::DocumentObject*> docObjects = d->_pcDocument->getObjects();
|
||||
std::unordered_map<App::DocumentObject*, bool> rootMap;
|
||||
for (auto it : docObjects) {
|
||||
rootMap[it] = true;
|
||||
}
|
||||
|
||||
for (auto obj : docObjects) {
|
||||
ViewProvider* vp = Application::Instance->getViewProvider(obj);
|
||||
if (!vp) {
|
||||
continue;
|
||||
}
|
||||
|
||||
std::vector<App::DocumentObject*> children = vp->claimChildren();
|
||||
for (auto child : children) {
|
||||
rootMap[child] = false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<App::DocumentObject*> rootObjs;
|
||||
for (const auto& it : rootMap) {
|
||||
if (it.second) {
|
||||
rootObjs.push_back(it.first);
|
||||
}
|
||||
}
|
||||
return rootObjs;
|
||||
}
|
||||
|
||||
@@ -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<App::DocumentObject*> getTreeRootObjects() const;
|
||||
|
||||
protected:
|
||||
// pointer to the python class
|
||||
Gui::DocumentPy *_pcDocPy;
|
||||
|
||||
@@ -243,5 +243,11 @@ obj : Gui.ViewProvider</UserDocu>
|
||||
</Documentation>
|
||||
<Parameter Name="Modified" Type="Boolean" />
|
||||
</Attribute>
|
||||
<Attribute Name="TreeRootObjects" ReadOnly="true">
|
||||
<Documentation>
|
||||
<UserDocu>The list of tree root objects.</UserDocu>
|
||||
</Documentation>
|
||||
<Parameter Name="TreeRootObjects" Type="List" />
|
||||
</Attribute>
|
||||
</PythonExport>
|
||||
</GenerateModel>
|
||||
|
||||
@@ -509,6 +509,20 @@ void DocumentPy::setModified(Py::Boolean arg)
|
||||
getDocumentPtr()->setModified(arg);
|
||||
}
|
||||
|
||||
Py::List DocumentPy::getTreeRootObjects() const
|
||||
{
|
||||
std::vector<App::DocumentObject*> 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
|
||||
|
||||
@@ -2758,7 +2758,8 @@ void TreeWidget::sortDroppedObjects(TargetItemInfo& targetInfo, std::vector<App:
|
||||
propGroup->setValue(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) {
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,6 +4,7 @@ SET(Test_SRCS
|
||||
Init.py
|
||||
BaseTests.py
|
||||
Document.py
|
||||
GuiDocument.py
|
||||
Metadata.py
|
||||
StringHasher.py
|
||||
Menu.py
|
||||
|
||||
57
src/Mod/Test/GuiDocument.py
Normal file
57
src/Mod/Test/GuiDocument.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
"""**************************************************************************
|
||||
* *
|
||||
* Copyright (c) 2024 Ondsel <development@ondsel.com> *
|
||||
* *
|
||||
* 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 *
|
||||
* <https://www.gnu.org/licenses/>. *
|
||||
* *
|
||||
***************************************************************************/"""
|
||||
|
||||
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))
|
||||
@@ -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",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user