Invalid DAG! when removing object with expressions

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Post Reply
mlampert
Veteran
Posts: 1771
Joined: Fri Sep 16, 2016 9:28 pm

Invalid DAG! when removing object with expressions

Post by mlampert »

The script below creates a spreadsheet with an alias and a box with an expression referencing that alias in one of its properties. If one deletes Box the following error gets issued - although Box seems to be removed:

Code: Select all

Document::topologicalSort: cyclic dependency detected (no root object)
App::Document::recompute(): topological sort fails, invalid DAG!
If one were to ignore the error, and continue on with deleting the remaining SetupSheet the user gets prompted with the dialog that this might "break Box". Although it still seems to exist somewhere I can't find a reference to it. Note that saving the document and loading it again the error is gone, it seems to be an in memory issue.

Code: Select all

import FreeCAD

doc = FreeCAD.newDocument('labels')
ss = doc.addObject('Spreadsheet::Sheet', 'SetupSheet')
ss.set('A1', '3 mm')
ss.setAlias('A1', 'Height')
doc.recompute()

box = doc.addObject('Part::Box', 'Box')
box.setExpression('Height', "%s.Height" % ss.Label)
doc.recompute()
wmayer
Founder
Posts: 20074
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Invalid DAG! when removing object with expressions

Post by wmayer »

Confirmed.

This is another bug raised with the recently introduced back links. When setting an expression that references another object a back link is set. Now on deletion of the box feature this back link is still kept (which can become a dangling pointer too) and thus the algorithm to topologically sort the objects on recompute will fail.

When performing the deletion in a Python script you can clear the expression first (as a work around):

Code: Select all

box.setExpression('Height', None)
Feel free to open a bug report referencing this thread.
mlampert
Veteran
Posts: 1771
Joined: Fri Sep 16, 2016 9:28 pm

Re: Invalid DAG! when removing object with expressions

Post by mlampert »

wmayer wrote: Wed Oct 11, 2017 12:32 pm When performing the deletion in a Python script you can clear the expression first (as a work around):

Code: Select all

box.setExpression('Height', None)
That's what I added to the upcoming change for Path in the onDelete callbacks. Thanks for confirming the validity of the work around.

Created issue #3214, hope I got the title and description right.
wmayer
Founder
Posts: 20074
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Invalid DAG! when removing object with expressions

Post by wmayer »

Perfect! Thank you!
danidr
Posts: 36
Joined: Thu Aug 25, 2016 10:46 am

Re: Invalid DAG! when removing object with expressions

Post by danidr »

mlampert wrote: Wed Oct 11, 2017 5:54 pm
wmayer wrote: Wed Oct 11, 2017 12:32 pm When performing the deletion in a Python script you can clear the expression first (as a work around):

Code: Select all

box.setExpression('Height', None)
That's what I added to the upcoming change for Path in the onDelete callbacks. Thanks for confirming the validity of the work around.

Created issue #3214, hope I got the title and description right.
Hello,
I am using this workaround on one of my scripts. Basically, I do this:

Code: Select all

for expression in obj.ExpressionEngine:
  obj.setExpression(expression[0], None)
  for i in obj.OutList:
    for expression in i.ExpressionEngine:
       i.setExpression(expression[0], None)
To clean up any expression associated with the object.
And only then,

Code: Select all

FreeCAD.ActiveDocument.getObject(obj.Name).removeObjectsFromDocument()
FreeCAD.ActiveDocument.removeObject(obj.Name)
Despite this, apparently, on FreeCAD.ActiveDocument.recompute() , it does not update the references correctly. The error, "Invalid DAG!", does not trigger.
May be due to some backlinks not resetting even when manually setting the expression to none? Is this a separate bug?
chrisb
Veteran
Posts: 52150
Joined: Tue Mar 17, 2015 9:14 am

Re: Invalid DAG! when removing object with expressions

Post by chrisb »

As a workaround I save, close and reopen the model.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply