What's new

DataBase is falling through to last line.

oldfox

Alibre Super User
This script runs just fine with the exception that it falls through to the last line in the so-called DataBase.
This can be proven by commenting out lines starting at the last line in the DB.
What am I doing wrong or missing????

# This is a troubleshooter script only.

Units.Current = UnitTypes.Millimeters
DefaultName = 'None'
def InputChanged(Index, Value):
if Index == 0:
global Name
Name = MetricNames[Value]
print Name
def SelectionMade(Values):
global Name
Name = MetricNames[Values[0]]
print Name
Win = Windows()
MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
Options = []
Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
DialogWidth = 400
Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
print 'You chose %s' % Name

if Name == 'M2':
Choice = 1
print '\nChoice is %i\n' % Choice
elif Name == 'M2.5':
Choice = 2
print '\nChoice is %i\n' % Choice
elif Name == 'M3':
Choice = 3
print '\nChoice is %i\n' % Choice
elif Name == 'M4':
Choice = 4
print '\nChoice is %i\n' % Choice

Length = 5.0

MetricSizeData = {} # 0 1 2 3 4 5 6 7 8 9
MetricSizeData[Choice] = ['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]

Name = MetricSizeData[Choice][0]
BodyDiameter = MetricSizeData[Choice][1]
HeadDiameter = MetricSizeData[Choice][2]
HeadHeight = MetricSizeData[Choice][3]
HexSocketSize = MetricSizeData[Choice][4]
HexHoleDepth = MetricSizeData[Choice][5]
TransitionDiameter = MetricSizeData[Choice][6]
RimChamfer = MetricSizeData[Choice][7]
EndChamfer = MetricSizeData[Choice][8]
Length = MetricSizeData[Choice][9]

print 'Name is %s' % Name
print 'Body Diameter is %.2f' % BodyDiameter
print 'Head Diameter is %.2f' % HeadDiameter
print 'Head Height is %.2f' % HeadHeight

TIA
 

idslk

Alibre Super User
Hello oldfox,

i tried the script and it runs...(but will never give you your desired result...)
if you mean the obvious failure:

>>>
None
You chose None
IronPython.Runtime.UnboundNameException: name 'Choice' is not defined
bei IronPython.Runtime.Operations.PythonOps.GetVariable(CodeContext context, String name, Boolean isGlobal, Boolean lightThrow)
bei IronPython.Compiler.LookupGlobalInstruction.Run(InterpretedFrame frame)
...

it will help to catch the choosen "NONE" from your WinInput....

It would be easier to read and copy a script if you would use the insert code function of the forum ;-)
upload_2019-2-17_22-16-35.png

Regards
Stefan
 
Last edited:

oldfox

Alibre Super User
Hi Stefan,

It runs without failure for me. You can see in the "Console" that it always ends with "M4" and the corresponding variables,
and not the size actually chosen. The variable for the size (i.e. M4) is "Name".
Here is it reposted using the Insert Code function.

it will help to catch the choosen "NONE" from your WinInput....
I never choose "None", only the Mx screw size numbers called "Name"

This is just a piece of a larger script I'm working on. So far this is the only stumbling block I haven't been able to scramble over.

Code:
#  This is a troubleshooter script only.

Units.Current = UnitTypes.Millimeters
DefaultName = 'None'
def InputChanged(Index, Value):
   if Index == 0:
      global Name
      Name = MetricNames[Value]
      print Name
def SelectionMade(Values):
   global Name
   Name = MetricNames[Values[0]]
   print Name
Win = Windows()
MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
Options = []
Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
DialogWidth = 400
Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
print 'You chose %s' % Name

if Name == 'M2':
   Choice = 1
   print '\nChoice is %i\n' % Choice
elif Name == 'M2.5':
   Choice = 2
   print '\nChoice is %i\n' % Choice
elif Name == 'M3':
   Choice = 3
   print '\nChoice is %i\n' % Choice
elif Name == 'M4':
   Choice = 4
   print '\nChoice is %i\n' % Choice

Length = 5.0

MetricSizeData = {}    #   0    1    2    3    4    5    6    7    8    9
MetricSizeData[Choice] = ['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]

Name = MetricSizeData[Choice][0]
BodyDiameter = MetricSizeData[Choice][1]
HeadDiameter = MetricSizeData[Choice][2]
HeadHeight = MetricSizeData[Choice][3]
HexSocketSize = MetricSizeData[Choice][4]
HexHoleDepth = MetricSizeData[Choice][5]
TransitionDiameter = MetricSizeData[Choice][6]
RimChamfer = MetricSizeData[Choice][7]
EndChamfer = MetricSizeData[Choice][8]
Length = MetricSizeData[Choice][9]

print 'Name is %s' % Name
print 'Body Diameter is %.2f' % BodyDiameter
print 'Head Diameter is %.2f' % HeadDiameter
print 'Head Height is %.2f' % HeadHeight

I hope this helps.
 

oldfox

Alibre Super User
Nope. That didn't make any difference. I think "None" has more meaning than just a word. The script won't take it as a valid input. Consequently,
"Choice" is never assigned a number if "None" is selected.

It really is hard to troubleshoot scripts without the handy-dandy line numbers in the error messages. Albeit this one being pretty obvious.
 

idslk

Alibre Super User
Hello oldfox,

1) if you click on 'close' in your window the variable 'Name' won't be assigned...
2) if you leave the selection at 'None' (your defined string) the variable 'Choice' won't be assigned...
3) if you choose any other list entry, you will allways get the values for 'M4' (the latest assigning in your data list)...
4) the 'Apply'-button is useless

If you want to use your structure further, you can try the following:
Code:
# This is a troubleshooter script only.

Units.Current = UnitTypes.Millimeters
DefaultName = 'None'

def InputChanged(Index, Value):
  if Index == 0:
    global Name
    Name = MetricNames[Value]
    print Name

def SelectionMade(Values):
  global Name
  Name = MetricNames[Values[0]]
  print Name

Win = Windows()
MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
Options = []
Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
DialogWidth = 400
Values = Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
#this helps to catch the 'close' selection...
if Values == None:
  try:
    # if a selection was made the Name will be valid
    print Name
  except:
    # if 'close' is choosen and no selection was made, set the value of 'Name' to the Value of 'DefaultName'
    Name = DefaultName

print 'You chose %s' % Name

if Name == 'None':
  Choice = 0
  print 'your Choise was invalid...'
if Name == 'M2':
  Choice = 1
  print '\nChoice is %i\n' % Choice
elif Name == 'M2.5':
  Choice = 2
  print '\nChoice is %i\n' % Choice
elif Name == 'M3':
  Choice = 3
  print '\nChoice is %i\n' % Choice
elif Name == 'M4':
  Choice = 4
  print '\nChoice is %i\n' % Choice

Length = 5.0

MetricSizeData = []
MetricSizeData.append (['None', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
MetricSizeData.append (['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])

Name = MetricSizeData[Choice][0]
BodyDiameter = MetricSizeData[Choice][1]
HeadDiameter = MetricSizeData[Choice][2]
HeadHeight = MetricSizeData[Choice][3]
HexSocketSize = MetricSizeData[Choice][4]
HexHoleDepth = MetricSizeData[Choice][5]
TransitionDiameter = MetricSizeData[Choice][6]
RimChamfer = MetricSizeData[Choice][7]
EndChamfer = MetricSizeData[Choice][8]
Length = MetricSizeData[Choice][9]

print 'Name is %s' % Name
print 'Body Diameter is %.2f' % BodyDiameter
print 'Head Diameter is %.2f' % HeadDiameter
print 'Head Height is %.2f' % HeadHeight

Not the best, but should give you the desired result ;-)

Regards
Stefan
 
Last edited:

NateLiquidGravity

Alibre Super User
In addition to what Stefan explained:
Code:
MetricSizeData[Choice] = ['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
MetricSizeData[Choice] = ['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length]
Right here you are assigning MetricSizeData values to the same index (whatever the value of Choice was) and therefore overwriting them resulting in the last override being the only one remaining. See Stefan's code for the fix.
 

oldfox

Alibre Super User
Hi Guys,
Well I've been playing with this. Stefan, your fix, does indeed, work as desired. Nate, I understand what is happening, just not why. Not being
a programmer probably doesn't help any.:confused:

The following 2 scripts are one and the same. The ONLY difference is that the indentation has been removed ("Work Copy is NOT Function") and the other IS a function with a call from so-called "Main". (Future implementation)

One works the other not.

Code:
#def MetricSeries():
Units.Current = UnitTypes.Millimeters
DefaultName = 'None'
 
def InputChanged(Index, Value):
   if Index == 0:
      global Name
      Name = MetricNames[Value]
      print Name
 
def SelectionMade(Values):
   global Name
   Name = MetricNames[Values[0]]
   print Name
 
Win = Windows()
MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
Options = []
Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
DialogWidth = 400
Values = Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
#this helps to catch the 'close' selection...
if Values == None:
   try:
      # if a selection was made the Name will be valid
      print Name
   except:
      # if 'close' is choosen and no selection was made, set the value of 'Name' to the Value of 'DefaultName'
      Name = DefaultName
 
print 'You chose %s' % Name
 
if Name == 'None':
   Choice = 0
   print 'your Choise was invalid...'
if Name == 'M2':
   Choice = 1
   print '\nChoice is %i\n' % Choice
elif Name == 'M2.5':
   Choice = 2
   print '\nChoice is %i\n' % Choice
elif Name == 'M3':
   Choice = 3
   print '\nChoice is %i\n' % Choice
elif Name == 'M4':
   Choice = 4
 
print '\nChoice is %i\n' % Choice
 
Length = 5.0
 
MetricSizeData = []
MetricSizeData.append (['None', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
MetricSizeData.append (['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
MetricSizeData.append (['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])

Name = MetricSizeData[Choice][0]
BodyDiameter = MetricSizeData[Choice][1]
HeadDiameter = MetricSizeData[Choice][2]
HeadHeight = MetricSizeData[Choice][3]
HexSocketSize = MetricSizeData[Choice][4]
HexHoleDepth = MetricSizeData[Choice][5]
TransitionDiameter = MetricSizeData[Choice][6]
RimChamfer = MetricSizeData[Choice][7]
EndChamfer = MetricSizeData[Choice][8]
Length = MetricSizeData[Choice][9]
 
print 'Name is %s' % Name
print 'Body Diameter is %.2f' % BodyDiameter
print 'Head Diameter is %.2f' % HeadDiameter
print 'Head Height is %.2f' % HeadHeight

#MetricSeries()

****************************************************************************

Code:
def MetricSeries():
   Units.Current = UnitTypes.Millimeters
   DefaultName = 'None'
 
   def InputChanged(Index, Value):
      if Index == 0:
         global Name
         Name = MetricNames[Value]
         print Name
 
   def SelectionMade(Values):
      global Name
      Name = MetricNames[Values[0]]
      print Name
 
   Win = Windows()
   MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
   Options = []
   Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
   DialogWidth = 400
   Values = Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
   #this helps to catch the 'close' selection...
   if Values == None:
      try:
         # if a selection was made the Name will be valid
         print Name
      except:
         # if 'close' is choosen and no selection was made, set the value of 'Name' to the Value of 'DefaultName'
         Name = DefaultName
 
   print 'You chose %s' % Name
 
   if Name == 'None':
      Choice = 0
      print 'your Choise was invalid...'
   if Name == 'M2':
      Choice = 1
      print '\nChoice is %i\n' % Choice
   elif Name == 'M2.5':
      Choice = 2
      print '\nChoice is %i\n' % Choice
   elif Name == 'M3':
      Choice = 3
      print '\nChoice is %i\n' % Choice
   elif Name == 'M4':
      Choice = 4
 
   print '\nChoice is %i\n' % Choice
 
   Length = 5.0
 
   MetricSizeData = []
   MetricSizeData.append (['None', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
   MetricSizeData.append (['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
   MetricSizeData.append (['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
   MetricSizeData.append (['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
   MetricSizeData.append (['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
 
   Name = MetricSizeData[Choice][0]
   BodyDiameter = MetricSizeData[Choice][1]
   HeadDiameter = MetricSizeData[Choice][2]
   HeadHeight = MetricSizeData[Choice][3]
   HexSocketSize = MetricSizeData[Choice][4]
   HexHoleDepth = MetricSizeData[Choice][5]
   TransitionDiameter = MetricSizeData[Choice][6]
   RimChamfer = MetricSizeData[Choice][7]
   EndChamfer = MetricSizeData[Choice][8]
   Length = MetricSizeData[Choice][9]
 
   print 'Name is %s' % Name
   print 'Body Diameter is %.2f' % BodyDiameter
   print 'Head Diameter is %.2f' % HeadDiameter
   print 'Head Height is %.2f' % HeadHeight

#******  MAIN  ******

MetricSeries()

I'm gonna go pick up my hair off the floor now. It will probably be all grown back by the time I get this thing working. :rolleyes:
 
Last edited:

NateLiquidGravity

Alibre Super User
Proper indentation is one of the most important things in Python. In the second one you can see almost the entire thing is indented because it is the function being defined and then called at the end.
 

oldfox

Alibre Super User
Proper indentation is one of the most important things in Python.

Hi Nate.
Yes. I struggled with this in the beginning trying to shoehorn Stefan's fix into my existing function.

I really don't understand why the function won't work. It is still picking up "NONE".
 

NateLiquidGravity

Alibre Super User
Try this arrangement.
Code:
def MetricSeries():
   Units.Current = UnitTypes.Millimeters
   DefaultName = 'None'


   def InputChanged(Index, Value):
      if Index == 0:
         Name = MetricNames[Value]
         print Name

   def SelectionMade(Values):
      Name = MetricNames[Values[0]]
      
      try:
         # if a selection was made the Name will be valid
         print Name
      except:
         # if 'close' is choosen and no selection was made, set the value of 'Name' to the Value of 'DefaultName'
         Name = DefaultName

      print 'You chose %s' % Name
      
      if Name == 'None':
         Choice = 0
         print 'your Choise was invalid...'
         return
      if Name == 'M2':
         Choice = 1
         print '\nChoice is %i\n' % Choice
      elif Name == 'M2.5':
         Choice = 2
         print '\nChoice is %i\n' % Choice
      elif Name == 'M3':
         Choice = 3
         print '\nChoice is %i\n' % Choice
      elif Name == 'M4':
         Choice = 4
         print '\nChoice is %i\n' % Choice
      
      Length = 5.0
      
      MetricSizeData = []
      MetricSizeData.append (['None', 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
      MetricSizeData.append (['M2', 2.0, 5.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
      MetricSizeData.append (['M2.5', 2.5, 6.5, 2.5, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
      MetricSizeData.append (['M3', 3.0, 6.5, 3.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
      MetricSizeData.append (['M4', 4.0, 6.5, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, Length])
      
      Name = MetricSizeData[Choice][0]
      BodyDiameter = MetricSizeData[Choice][1]
      HeadDiameter = MetricSizeData[Choice][2]
      HeadHeight = MetricSizeData[Choice][3]
      HexSocketSize = MetricSizeData[Choice][4]
      HexHoleDepth = MetricSizeData[Choice][5]
      TransitionDiameter = MetricSizeData[Choice][6]
      RimChamfer = MetricSizeData[Choice][7]
      EndChamfer = MetricSizeData[Choice][8]
      Length = MetricSizeData[Choice][9]
      
      print 'Name is %s' % Name
      print 'Body Diameter is %.2f' % BodyDiameter
      print 'Head Diameter is %.2f' % HeadDiameter
      print 'Head Height is %.2f' % HeadHeight



   Win = Windows()
   MetricNames = ['None', 'M2', 'M2.5', 'M3', 'M4']
   Options = []
   Options.append(['Size', WindowsInputTypes.StringList, MetricNames, DefaultName])
   DialogWidth = 400
   Values = Win.UtilityDialog('Metric Sizes', 'Apply', SelectionMade, InputChanged, Options, DialogWidth)
   #this helps to catch the 'close' selection...
   if Values == None:
      print("User Canceled")
      sys.exit()
      


#******  MAIN  ******

MetricSeries()

Since the functions are wrapped in the MetricSeries function they don't need global to access its variables. (I didn't even know we could do that)

I moved the "action" into the Apply button's SelectionMade function.

I added a return after if Name == 'None': to prevent further execution off the function.

I changed it to exit if the close button was pressed.

There is still some redundancy here. You are currently assigning a name based on the index number of the dropdown.
Code:
Name = MetricNames[Values[0]]

And then assigning an index number based on the name.
Code:
if Name == 'M2':
   Choice = 1

And then assigning a name based on that new index number.
Code:
Name = MetricSizeData[Choice][0]
 

idslk

Alibre Super User
Hello oldfox,

as far as i don't know the structure of your whole script, your script will get a little patchy...
not fine, but ...

globalize the variable name in your uppermost function:

Code:
def MetricSeries():
   Units.Current = UnitTypes.Millimeters
   DefaultName = 'None'
   global Name

   def InputChanged(Index, Value):
     if Index == 0:
     ...

Regards
Stefan
 

oldfox

Alibre Super User
Since the functions are wrapped in the MetricSeries function they don't need global to access its variables.

Correct. I am planning on using "Name" and "Length" in the file name later on for the finished part. That's why global. All this really is a WIP.

globalize the variable name in your uppermost function:

Would it work if I globalize in "Main"? That is really from where MetricSeries(): is called... Along with NumberSeries(): , FractionSeries(): and
BritAssocSeries(): . Or do I need to put something inside the parens of the function Names?

I'm working on this script as though it will work for 4 different screw series or families. I still don't know how long it will be once it is finished.
I may very well just opt to break it down into 4 separate scripts: One for each series. I'm kinda leaning toward the latter.

For now the selection process of the size works (if I don't break it), thanks to Stefan and Nate both. I will continue on with it.

Thanks again, both of you.
 
Top