Core: Add Gui::Document::getTreeRootObjects()

This commit is contained in:
PaddleStroke
2024-05-30 12:20:35 +02:00
parent 238fd6a5fc
commit fb32579498
7 changed files with 57 additions and 3 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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>

View File

@@ -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

View File

@@ -2754,7 +2754,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) {

View File

@@ -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):

View File

@@ -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