What's new

Motion script needs update for V27

DavidJ

Administrator
Staff member
Not immediately - The script does run in v27, but display doesn't update (I added a print statement to show the changing value).

I'll ask QA to take a look - issue could be in various places....
 

DavidJ

Administrator
Staff member
Code:
Win = Windows()
WT = 'IDSLK'
ScriptName = 'DBC_move'
wheeltravel = 180
speed = 360/wheeltravel
ca = CurrentAssembly()
for i in range(len(ca.Parameters)):
  if ca.Parameters[i].Name == 'A1':
    M = i
Posi_x = ca.Parameters[M].Value
for r in range(wheeltravel):
  Posi_x = Posi_x + speed
  ca.Parameters[M].Value = Posi_x
  ca.Regenerate()
#for r in range(wheeltravel):
  #Posi_x = Posi_x + speed
  #ca.Parameters[M].Value = Posi_x
print 'done'
 

NateLiquidGravity

Alibre Super User
Wasn't me - that script was originally written by @idslk

On my machine ca.Regenerate() makes it super slow as it regenerates deeper.
A Stopwatch was added to see how long the first loop takes. Estimated time with ca.Regenerate(): 0 Hours, 10 Minutes, and 8 Seconds.
I have changed it to use the API IADDesignSession.RegenerateDesign method without deepRegenerate. That was much faster. Estimated time: 0 Hours, 0 Minutes, and 19 Seconds.

The Assembly.SaveSnapshot AlibreScript method was added to the loop to automatically save a .bmp of the screen to a user specified folder.
Added a question to give the user a total time estimate and a way out if that is too long.
I tried to unselect the objects that Alibre Design automatically selects after regen, but I couldn't find a good way. Sometimes putting a new constraint on objects out of view helped.
I condensed the various options into the top of file and added/changed some variable names to fit that.

Python:
Win = Windows()
WT = 'IDSLK + NateLiqGrav'
ScriptName = 'Animation Example'
steps = 32
step_angle = 360/steps
parameter_name = 'A1'
save_folder = r'C:\Users\Gamer\Desktop\Images'

ca = CurrentAssembly()
AD_ca = ca._Assembly #This gets the API version of the assembly

param = ca.GetParameter(parameter_name)
current_val = param.Value

first = 1
from System.Diagnostics import Stopwatch
from System import TimeSpan
watch = Stopwatch.StartNew()
for r in range(steps):
  ca.SaveSnapshot( str(save_folder) + '\image_' + str(r) + '.bmp' , 0, 0, 1, 1)
  current_val = current_val + step_angle
  param.Value = current_val
  AD_ca.RegenerateDesign(0) #This does a basic regen using the API version of the assembly
  #ca.Regenerate()
  if first:
    first = 0
    watch.Stop()
    ts = watch.Elapsed
    total_ts = TimeSpan.FromTicks(ts.Ticks * float(steps))
    print 'Estimated time: ' + str(total_ts.Hours) + ' Hours, ' + str(total_ts.Minutes) + ' Minutes, and ' + str(total_ts.Seconds) + ' Seconds.'
    value = Win.QuestionDialog('It took ' + str(ts.Seconds) + ' Seconds for the first movement.\nEstimated time to finish is ' + str(total_ts.Hours) + ' Hours, ' + str(total_ts.Minutes) + ' Minutes, and ' + str(total_ts.Seconds) + ' Seconds to complete ' + str(steps) + ' Steps\nAre you sure you want to continue?' , 'Are you sure you want to continue?')
    if not value:
      sys.exit()
    else:
      watch.Restart()
watch.Stop()
final_ts = ts.Add(watch.Elapsed)
print 'Final time: ' + str(final_ts.Hours) + ' Hours, ' + str(final_ts.Minutes) + ' Minutes, and ' + str(final_ts.Seconds) + ' Seconds.'
print 'Done'
Then I went to https://ezgif.com/maker and created this animation.
animation_optimized.gif
 

idslk

Alibre Super User
i added a little UI...

1692534466563.png

Python:
win = Windows()
author = '(C) IDSLK / NateLiqGrav'
scriptversion = 0.20230820
scriptversiontext = str(scriptversion)
scriptname = 'Motion in Assembly + Capture '
message = 'Script is not suitable for running in parts!'
error = 'Error! '
outputfolder = ''
snapname = ''
saving = False
reso = 0
resolutionlist = ['800*600','1024*768','1920*1080']
resolutions = [[800,600],[1024,768],[1920,1080]]

parametername = 'A2' # parametername from formeleditor which is will be used for animation
M = -1 # will be used if parameter check failes / (parameter is not available in parameterset)
steps = 10 #number of animation steps

try:
  co = CurrentAssembly()
  print 'Running in Assembly: ',co
  AD_co = co._Assembly #This gets the API version of the assembly
 
except:
  co = CurrentPart()
  print 'Running in Part',co
  win.InfoDialog(message,scriptname)
  sys.exit()
 
# called when an input changes in the dialog window
def input_changed(index, value):
  if index == 0 and value == False:
    print 'off'
    win.DisableInput(1)
    win.DisableInput(2)
    win.DisableInput(3)
  elif index == 0 and value == True:
    print 'on'
    win.EnableInput(1)
    win.EnableInput(2)
    win.EnableInput(3)
  pass
 
# called when user confirms selections
def selection_made():
  win.DisableInput(1)
  win.DisableInput(2)
  win.DisableInput(3)
  pass

def SaveLocation():
   options = []
   options.append(['Set checkmark for saving snapshots', WindowsInputTypes.Boolean, False]) # 0
   options.append(['please choose the directory for the snapshots', WindowsInputTypes.Folder, 'C:\Users\Default\Desktop']) # 1
   options.append(['please enter a name for the snapshots', WindowsInputTypes.String, co.Name]) # 2
   options.append(['please choose the resolution for the snapshots', WindowsInputTypes.StringList, resolutionlist]) # 3
   options.append(['please enter a number of animation steps', WindowsInputTypes.Integer, steps]) # 4
   values = win.OptionsDialog(author+scriptname+scriptversiontext, options,230, input_changed, selection_made)
   if values == None:
      sys.exit('User canceled')
   # or return OutputFolder
   global outputfolder,snapname,reso,steps,saving
   outputfolder = values[1]
   snapname = values[2]
   reso = values[3]
   steps = values[4]
   saving = values[0]

#main-------------------
SaveLocation()

for i in range(len(co.Parameters)):
  if co.Parameters[i].Name == parametername:
    M = i
    print 'actual internal parameter number: ', M
if M == -1:
  text = 'No Parameter with name'+parametername+' has been found! Script will be terminated...'
  win.InfoDialog(text,scriptname)
  print 'No Parameter with name',parametername,' has been found! Script will be terminated...'
  sys.exit()

Posi_x = co.Parameters[M].Value
print 'actual parameter value: ',Posi_x

print 'animating...'

first = 1
from System.Diagnostics import Stopwatch
from System import TimeSpan
watch = Stopwatch.StartNew()

for r in range(steps):
  Posi_x = Posi_x - 1
  co.Parameters[M].Value = Posi_x
  #co.Regenerate()
  AD_co.RegenerateDesign(0) #This does a basic regen using the API version of the assembly
 
  if first:
    first = 0
    watch.Stop()
    ts = watch.Elapsed
    total_ts = TimeSpan.FromTicks(ts.Ticks * float(steps))
    print 'Estimated time: ' + str(total_ts.Hours) + ' Hours, ' + str(total_ts.Minutes) + ' Minutes, and ' + str(total_ts.Seconds) + ' Seconds.'
    value = win.QuestionDialog('It took ' + str(ts.Seconds) + ' Seconds for the first movement.\nEstimated time to finish is ' + str(total_ts.Hours) + ' Hours, ' + str(total_ts.Minutes) + ' Minutes, and ' + str(total_ts.Seconds) + ' Seconds to complete ' + str(steps) + ' Steps\nAre you sure you want to continue?' , 'Are you sure you want to continue?')
    if not value:
      sys.exit()
    else:
      watch.Restart()
 
  if saving == True:
    co.SaveSnapshot(outputfolder+'\_'+snapname+str(r)+'.bmp',resolutions[reso][0],resolutions[reso][1],0,0)

watch.Stop()
final_ts = ts.Add(watch.Elapsed)
print 'Final time: ' + str(final_ts.Hours) + ' Hours, ' + str(final_ts.Minutes) + ' Minutes, and ' + str(final_ts.Seconds) + ' Seconds.'

if saving == True:
  infotext = 'Pictures saved in folder: '+outputfolder
  print infotext
  win.InfoDialog(infotext,scriptname)

print 'done :-)'

Regards
Stefan
 

NateLiquidGravity

Alibre Super User
Nice additions!
A few changes I would make to add saving the full view:
Python:
resolutionlist = ['800*600','1024*768','1920*1080','Full View']
resolutions = [[800,600,0,0],[1024,768,0,0],[1920,1080,0,0],[0,0,1,1]]
Python:
co.SaveSnapshot(outputfolder+'\_'+snapname+str(r)+'.bmp',resolutions[reso][0],resolutions[reso][1],resolutions[reso][2],resolutions[reso][3])
 

JimCad

Senior Member
Easy with inventor.
I really need to spend more time on this to learn the new (for me) way of doing things.
Sorry to harp on about Inventor but it's what I'm used to.
No way I'd buy a license or subscription from THEM!
Alibre will do just nicely thanks. :)
 
Top