Skip to content

Commit

Permalink
Core: Add getRootObjectsIgnoreLinks and fix bugs in tree.cpp and Asse…
Browse files Browse the repository at this point in the history
…mblyObject.cpp, CommandInsertLink.py, UtilsAssembly.py
  • Loading branch information
PaddleStroke authored and yorikvanhavre committed May 13, 2024
1 parent 4cd8b2a commit 344b125
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 5 deletions.
26 changes: 26 additions & 0 deletions src/App/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4005,6 +4005,32 @@ std::vector<App::DocumentObject*> Document::getRootObjects() const
return ret;
}

std::vector<App::DocumentObject*> Document::getRootObjectsIgnoreLinks() const
{
std::vector<App::DocumentObject*> ret;

for (auto objectIt : d->objectArray) {
auto list = objectIt->getInList();
bool noParents = list.empty();

if (!noParents) {
// App::Document getRootObjects returns the root objects of the dependency graph.
// So if an object is referenced by a App::Link, it will not be returned by that function.
// So here, as we want the tree-root level objects,
// we check if all the parents are links. In which case its still a root object.
noParents = std::all_of(list.cbegin(), list.cend(), [](App::DocumentObject* obj) {
return obj->isDerivedFrom<App::Link>();
});
}

if (noParents) {
ret.push_back(objectIt);
}
}

return ret;
}

void DocumentP::findAllPathsAt(const std::vector <Node> &all_nodes, size_t id,
std::vector <Path> &all_paths, Path tmp)
{
Expand Down
2 changes: 2 additions & 0 deletions src/App/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ class AppExport Document : public App::PropertyContainer
std::vector<App::DocumentObject*> topologicalSort() const;
/// get all root objects (objects no other one reference too)
std::vector<App::DocumentObject*> getRootObjects() const;
/// get all tree root objects (objects that are at the root of the object tree)
std::vector<App::DocumentObject*> getRootObjectsIgnoreLinks() const;
/// get all possible paths from one object to another following the OutList
std::vector<std::list<App::DocumentObject*> > getPathsByOutList
(const App::DocumentObject* from, const App::DocumentObject* to) const;
Expand Down
6 changes: 6 additions & 0 deletions src/App/DocumentPy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ sort: whether to topologically sort the return list
</Documentation>
<Parameter Name="RootObjects" Type="List" />
</Attribute>
<Attribute Name="RootObjectsIgnoreLinks" ReadOnly="true">
<Documentation>
<UserDocu>The list of root objects in this document ignoring references from links.</UserDocu>
</Documentation>
<Parameter Name="RootObjectsIgnoreLinks" Type="List" />
</Attribute>
<Attribute Name="UndoMode" ReadOnly="false">
<Documentation>
<UserDocu>The Undo mode of the Document (0 = no Undo, 1 = Undo/Redo)</UserDocu>
Expand Down
12 changes: 12 additions & 0 deletions src/App/DocumentPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,18 @@ Py::List DocumentPy::getRootObjects() const
return res;
}

Py::List DocumentPy::getRootObjectsIgnoreLinks() const
{
std::vector<App::DocumentObject*> objs = getDocumentPtr()->getRootObjectsIgnoreLinks();
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;
}

Py::Int DocumentPy::getUndoMode() const
{
return Py::Int(getDocumentPtr()->getUndoMode());
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,7 @@ void TreeWidget::sortDroppedObjects(TargetItemInfo& targetInfo, std::vector<App:
propGroup->setValue(sortedObjList);
}
else if (targetInfo.targetItem->type() == TreeWidget::DocumentType) {
objList = targetInfo.targetDoc->getRootObjects();
objList = targetInfo.targetDoc->getRootObjectsIgnoreLinks();
// First we need to sort objList by treeRank.
std::sort(objList.begin(), objList.end(),
[](App::DocumentObject* a, App::DocumentObject* b) {
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Assembly/App/AssemblyObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ Base::Placement AssemblyObject::getGlobalPlacement(App::DocumentObject* targetOb
App::DocumentObject* container)
{
bool inContainerBranch = (container == nullptr);
auto rootObjects = App::GetApplication().getActiveDocument()->getRootObjects();
auto rootObjects = App::GetApplication().getActiveDocument()->getRootObjectsIgnoreLinks();
for (auto& part : rootObjects) {
Base::Placement foundPlc;
bool found =
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/Assembly/CommandInsertLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def process_objects(objs, item):
):
process_objects(obj.OutList, objItem)

process_objects(doc.RootObjects, docItem)
process_objects(doc.RootObjectsIgnoreLinks, docItem)
self.form.partList.expandAll()

def onFilterChange(self):
Expand Down
4 changes: 2 additions & 2 deletions src/Mod/Assembly/UtilsAssembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def getGlobalPlacement(targetObj, container=None):
return App.Placement()

inContainerBranch = container is None
for rootObj in App.activeDocument().RootObjects:
for rootObj in App.activeDocument().RootObjectsIgnoreLinks:
foundPlacement = getTargetPlacementRelativeTo(
targetObj, rootObj, container, inContainerBranch
)
Expand All @@ -308,7 +308,7 @@ def getGlobalPlacement(targetObj, container=None):


def isThereOneRootAssembly():
for part in App.activeDocument().RootObjects:
for part in App.activeDocument().RootObjectsIgnoreLinks:
if part.TypeId == "Assembly::AssemblyObject":
return True
return False
Expand Down

0 comments on commit 344b125

Please sign in to comment.