Change parameters and see the results during loop

48 views
Skip to first unread message

Steven Williams

unread,
May 25, 2022, 5:05:09 PM5/25/22
to RevitPythonShell
I would like to iterate through changes in a parameter value and see the results. Here is an example to run against the attached .rfa.

doc = __revit__.ActiveUIDocument.Document
fm = uidoc.Document.FamilyManager
wid = pars[2]

def ChangeParameter(p,w):
  with Transaction(doc) as t:
    t.Start("Change Parameter")
    fm.Set(p, w)
    doc.Regenerate() # doesn't actually do anything -- I was hoping this would refresh the view
    t.Commit()

for i in range(4,10):
  ChangeParameter(wid, i)


Calling ChangeParameter() on a single line works and the display is updated accordingly, but not each time in the loop. What am I missing?
Table Test.rfa

Maycon Freitas

unread,
May 25, 2022, 5:43:48 PM5/25/22
to RevitPythonShell
Try to put  doc.Regenerate()  after t.Commit(), or after  ChangeParameter(wid, i), inside the loop block.

Steven Williams

unread,
May 26, 2022, 10:44:47 AM5/26/22
to RevitPythonShell
Oops... I just realized that my variable pars was not defined in the example above. Here is the corrected version:

doc = __revit__.ActiveUIDocument.Document
fm = uidoc.Document.FamilyManager
pars = fm.GetParameters()

wid = pars[2]

def ChangeParameter(p,w):
  with Transaction(doc) as t:
    t.Start("Change Parameter")
    fm.Set(p, w)
    t.Commit()
  with Transaction(doc) as t:
    t.Start("Refresh View")

    doc.Regenerate() # doesn't actually do anything -- I was hoping this would refresh the view
    t.Commit()

for i in range(4,10):
  ChangeParameter(wid, i)


Unfortunately, putting doc.Regenerate() anywhere doesn't change the result. It has to be wrapped in a Transaction, but not even that fixes it. The interface freezes until then end of the whole script, then updates to show the final result. I want it to step through each change. This would make it so I can be sure my constraints do not break for different parameter values when building a family.

Callum

unread,
May 26, 2022, 5:16:49 PM5/26/22
to RevitPythonShell
Having the Revit interface update while youre editing the database is a tricky one - generally code will want to lock you out untill its finished its business. 

Ive got around this in the past by having an winforms (or WPF) interface that has a 'next' button - but I wonder if there is a console-only way using Pythons 'raw_input()' function to pause at each step?

Steven Williams

unread,
May 29, 2022, 5:57:53 PM5/29/22
to RevitPythonShell
I got it -- uidoc.RefreshActiveView() works.

from time import sleep

fm = uidoc.Document.FamilyManager
pars = fm.GetParameters()
wid = pars[2]

def ChangeParameter(p,w):
  with Transaction(doc) as t:
    t.Start("Change Parameter")
    fm.Set(p, w)
    uidoc.RefreshActiveView()
    t.Commit()

for i in range(4,9):
  ChangeParameter(wid, i)
  sleep(0.2)   


Reply all
Reply to author
Forward
0 new messages