What's new

Use a variable for the Units

oldfox

Alibre Super User
In the statement "Units.Current = UnitTypes.Inches" I would like to use a variable instead of the constants (i.e. Inches) I want to do this in a script with drop-down selection boxes so I can choose those units later as the needs change.

I've tried everything I could think of. It may be that the "units" are hard coded and the statement will not take any variables. Can someone give me the answer, either way?

TIA
oldfox
 

idslk

Alibre Super User
Hello Chris,

Code:
#ScriptName = 'Change Units'
#Version = 0.1
#Date = 20190131
#Startdate = 20190131
#author = 'IDSLK'

Win = Windows()
CP = CurrentPart()
Variable_Names = []
ScriptName = 'Change Units'
Unit_Names = ['Millimeters','Centimeters','Inches']

# create Window with Stringlist
def Userwindow_1():
  Values=[]
  Options = []
  Options.append(['UnitTypes', WindowsInputTypes.StringList, Unit_Names])
  Values = Win.OptionsDialog(ScriptName, Options, 200)
  if Values == None:
    print 'Canceled'
    sys.exit()
  elif Values[0] == 0:
    Units.Current = UnitTypes.Millimeters
  elif Values[0] == 1:
    Units.Current = UnitTypes.Centimeters
  elif Values[0] == 2:
    Units.Current = UnitTypes.Inches
  else:
    print 'Nothing Choosen'

#+++++Main++++

Userwindow_1()

Win.InfoDialog(str(Units.Current),ScriptName)


Regards
Stefan
 

oldfox

Alibre Super User
Hi Stefan,
Thanks.
I've been playing around with it. It is a totally different direction than I was going/thinking.
This script works but it doesn't change the "File Properties" to the new UnitTypes.
If it did, then that is exactly what I was looking for.

Also it errors out ("not defined") if I try to change the Unit_Names list. I really don't understand that one.

I keep trying to find answers in Python 2.7 tutorials but so far no luck. I think I may need a Python glossary for the jargon.:rolleyes::eek:

Thanks again,
Chris
 

idslk

Alibre Super User
Hello Chris,

at the moment it is not possible to change the File-Properties. It's a pity... (i hope the future will bring us this functionality).
To Change the list you have to do two things:
  1. you have to add (or change) a name like ['Millimeter','Centimeter','Inches','Chris']
    The name has to be in quotes to be treated as a string. The name could be choosen free. It is only to be read from the user. (The program works like: names -> list -> index -> action)
  2. if you've added a new name, you have to add an interpretation criteria. If you use the
    'WindowsInputTypes.StringList', every entry in the list has an index number. The return value of the 'WindowsOptionDialog' is regarding to this.
    In our example the returned value Value[0] will be 0 from 'Millimeters' and 3 from 'Chris'.
    To check for 'Chris' you have to add:
elif Values[0] == 3:
Units.Current = UnitTypes.Millimeters


There are only 3 types to choose from (mm,cm,in), so an additional list entry does not give you more opportunities...
upload_2019-1-31_15-24-42.png

If you use the UnitTypes inside the script, you can define Values in script units and in the design they will be automaticaly recalculated to the design units. So you can use inches in "your" script and if i use your script in "my" design (mm) the "dimensions" will be shown for me in mm.
Hope it helps.

Regards
Stefan
 

NateLiquidGravity

Alibre Super User
I've been playing around with it. It is a totally different direction than I was going/thinking.
This script works but it doesn't change the "File Properties" to the new UnitTypes.
If it did, then that is exactly what I was looking for.
Units.Current sets the units for the script only not the file.

Also it errors out ("not defined") if I try to change the Unit_Names list. I really don't understand that one.
The Unit_Names list only matters for what strings are displayed to the user. But they must be strings enclosed in single or double quotes.

The if and elif checks what item number was selected and assigns the UnitTypes after. This is not dynamic - it does not care what the strings were in the dropdown box.

edit: Stefan beat me to it.
 

oldfox

Alibre Super User
at the moment it is not possible to change the File-Properties. It's a pity...
BUMMER!!

There are only 3 types to choose from (mm,cm,in),
This I missed. Still don't know where to be looking in the "reference". That explains it. In the "File Properties" list there are 6 options.

Thanks Stefan. And to you too, Nate. It's really encouraging when there are such knowledgeable folks around, willing to help us neophytes.
I really appreciate the time y'all took to answer this.
 
Top