What's new

Troubleshooting Please - This script used to work

albie0803

Alibre Super User
My keyway in a bore script is now throwing the following error

Code:
System.Collections.Generic.KeyNotFoundException: KeyError
   at IronPython.Runtime.PythonDictionary.GetItem(Object key)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at Microsoft.Scripting.Interpreter.DynamicInstruction`3.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
   at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
   at AlibreScript.UI.IronTextBoxControl.#ijb(Object #9A)

Can someone please have a look for me? It dies after entering the face and edge detail.

Code:
print "******************** INSTRUCTIONS ********************\n"

print "1. Open part in Alibre Design."
print "2. Launch AlibreScript."
print "3. Open this script."
print "4. Fill in the prompts as presented."
print "5. Repeat 4 as required"
print "6. VOILA! And there is/are your keyseat/keyseats."

print "\n\nFor this script to function properly you will need to enter the following information..."
print "#1 The edge number of the hole in which you will be inserting the keyseat. The script generates the plane."
print "#2 The face number the key sketch is to be created on. The script generates the plane."
print "#3 The length of the key."

Units.Current = UnitTypes.Millimeters

try:
    Ass = CurrentAssembly()
    for P in Ass.Parts:
        PName = str(P)
        if "Boss" in PName:
            PN = PName + "<1>"
            Hub = Ass.GetPart(PN)
except:
    Hub = CurrentPart()

Hub.Regenerate()

Finished = Hub.AddConfiguration('Finished')
Finished.SetLocks(LockTypes.SuppressNewFeatures)
Finished.Activate()

Win = Windows()

if Win.QuestionDialog('Do you want to cut finished bore size?', 'Optional Extra') == True:
    Options = []
    Options.append(["Finished Bore Diameter", WindowsInputTypes.Real, 0])
    Values = Win.OptionsDialog("Finished Bore Diameter", Options, 250)
    BD = float(Values[0])
    # AddPlane and Sketch
    S1 = Hub.AddSketch('BoreProfile', Hub.GetPlane("ZX-Plane"))
    S1.AddCircle(0, 0, BD, False)
    Hub.AddExtrudeCut('Bore', S1, 0, True, Part.EndCondition.ThroughAll, None, 0, Part.DirectionType.Normal, None, 0, False)
    Win.InfoDialog('Activate Finished config to see key', 'Note')

TH = 0
LG = 0
WD = 0

# called when an input changes in the dialog window
def InputChanged(Index, Value):
  # size changed
  if Index == 0:
    global KS
    KS = KeySizes[Value]

# called when user confirms selections
def SelectionMade(Values):
  # get values
  global KS
  KS = KeySizes[Values[0]]

KeySizes = ['2x2', '3x3', '4x4', '5x5', '6x6', '8x7', '10x8', '12x8', '14x9', '16x10', '18x11', '20x12', '22x14', '25x14', '28x16', '32x18', '36x20', '40x22', '45x25', '50x28']
 
Options = []
Options.append(["Choose Hole Edge", WindowsInputTypes.Edge, None])
Options.append(['Choose Key Face', WindowsInputTypes.Face, None])
Options.append(["Length: 0 for Through All", WindowsInputTypes.Real, LG])
Options.append([None, WindowsInputTypes.Image, 'HKey.png', 100])

Values = Win.OptionsDialog("Metric Hole Keyway Generator", Options, 100)
if Values == None:
  sys.exit()
 
E = Values[0]
F = Values[1]
LG = Values[2]

Length = float(LG)

CylDia = E.Diameter
CylRad = CylDia / 2
CylDiaRanCho = 0

if CylDia > 6 and CylDia <= 8:
    CylDiaRanCho = 1
elif CylDia > 8 and CylDia <= 10:
    CylDiaRanCho = 2
elif CylDia > 10 and CylDia <= 12:
    CylDiaRanCho = 3
elif CylDia > 12 and CylDia <= 17:
    CylDiaRanCho = 4
elif CylDia > 17 and CylDia <= 22:
    CylDiaRanCho = 5
elif CylDia > 22 and CylDia <= 30:
    CylDiaRanCho = 6
elif CylDia > 30 and CylDia <= 38:
    CylDiaRanCho = 7
elif CylDia > 38 and CylDia <= 44:
    CylDiaRanCho = 8
elif CylDia > 44 and CylDia <= 50:
    CylDiaRanCho = 9
elif CylDia > 50 and CylDia <= 58:
    CylDiaRanCho = 10
elif CylDia > 58 and CylDia <= 65:
    CylDiaRanCho = 11
elif CylDia > 65 and CylDia <= 75:
    CylDiaRanCho = 12
elif CylDia > 75 and CylDia <= 85:
    CylDiaRanCho = 13
elif CylDia > 85 and CylDia <= 95:
    CylDiaRanCho = 14
elif CylDia > 95 and CylDia <= 110:
    CylDiaRanCho = 15
elif CylDia > 110 and CylDia <= 130:
    CylDiaRanCho = 16
elif CylDia > 130 and CylDia <= 150:
    CylDiaRanCho = 17
elif CylDia > 150 and CylDia <= 170:
    CylDiaRanCho = 18
elif CylDia > 170 and CylDia <= 200:
    CylDiaRanCho = 19
elif CylDia > 200 and CylDia <= 230:
    CylDiaRanCho = 20

# KW = Key Width, KH = Square Key Height, KD = Square Key Depth, KR = Keyseat Radius
#  KW  KH KD KR
KeySeatData = {}
KeySeatData[1] = [2, 2, 1, 0.16, '2x2']
KeySeatData[2] = [3, 3, 1.6, 0.16,'3x3']
KeySeatData[3] = [4, 4, 2.5, 0.16,'4x4']
KeySeatData[4] = [5, 5, 3, 0.25,'5x5']
KeySeatData[5] = [6, 6, 3.5, 0.25,'6x6']
KeySeatData[6] = [8, 7, 4, 0.25,'8x7']
KeySeatData[7] = [10, 8, 5, 0.4,'10x8']
KeySeatData[8] = [12, 8, 5, 0.4,'12x8']
KeySeatData[9] = [14, 9, 5.5, 0.4,'14x9']
KeySeatData[10] = [16, 10, 6, 0.4,'16x10']
KeySeatData[11] = [18, 11, 7, 0.4,'18x11']
KeySeatData[12] = [20, 12, 7.5, 0.6,'20x12']
KeySeatData[13] = [22, 14, 9, 0.6,'22x14']
KeySeatData[14] = [25, 14, 9, 0.6,'25x14']
KeySeatData[15] = [28, 16, 10, 0.6,'28x16']
KeySeatData[16] = [32, 18, 11, 0.6,'32x18']
KeySeatData[17] = [36, 20, 12, 1,'36x20']
KeySeatData[18] = [40, 22, 13, 1,'40x22']
KeySeatData[19] = [45, 25, 15, 1,'45x25']
KeySeatData[20] = [50, 28, 17, 1,'50x28']

KN = KeySeatData[CylDiaRanCho][4] #KN key name

Options = []
Options.append(['Key Size', WindowsInputTypes.StringList, KeySizes, KN])
ValueC = Win.UtilityDialog('Select Key Size', 'Apply', SelectionMade, InputChanged, Options, 150)

sum = 0
for i in KeySizes:
    if i == KS:
      print i
      print sum
      readTh = sum
    sum += 1

CylDiaRanCho = readTh + 1

CR = CylRad
KW = float(KeySeatData[CylDiaRanCho][0])
KH = float(KeySeatData[CylDiaRanCho][1])
KD = float(KeySeatData[CylDiaRanCho][2])
KR = float(KeySeatData[CylDiaRanCho][3])

X1 = CylRad + KD - KH
X2 = CylRad + KD
Y1 = KW / 2
print Y1
    
# AddPlane and Sketch
S = Hub.AddSketch('KeyseatProfile', Hub.AddPlane('KeyseatPlane', F, 0))

# draw lines
S.AddLine(X1, Y1, X2 - KR, Y1, False)
S.AddLine(X1, -Y1, X2 - KR, -Y1, False)
S.AddLine(X1, Y1, X1, -Y1, False)
S.AddLine(X2, Y1 - KR, X2, -Y1 + KR, False)

# dwaw arcs
S.AddArcCenterStartEnd(X2 - KR, Y1 - KR, X2, Y1 - KR, X2 - KR, Y1, False)
S.AddArcCenterStartEnd(X2 - KR, -Y1 + KR, X2 - KR, -Y1, X2, -Y1 + KR, False)

if Length == 0:
    Hub.AddExtrudeCut('KeyCut %dx%d' % (KW,KH), S, Length , True, Part.EndCondition.ThroughAll, None, 0, Part.DirectionType.Normal, None, 0, False)
else:
    Hub.AddExtrudeCut('KeyCut %dx%d' % (KW,KH), S, Length , True)

Hub.Regenerate()

Win.InfoDialog("Activate Finished config to see key", 'Note')
 

idslk

Alibre Super User
Hello Albie ,

you know, i have a german installation of AD...(including 9h of timediff Brisbane to Germany ...)
Here a list of things which can happen on a different system then your own...

First Error:
- System.Exception: Plane 'ZX-Plane' not found
I have a german installation. My standard planes are named "ZX-Ebene", the script can not find planes named in an other language ...
If you adress the plane with MyPart.ZXPlane it is language independent.
S1 = Hub.AddSketch('BoreProfile', Hub.GetPlane("ZX-Plane"))
change to S1 = Hub.AddSketch('BoreProfile', Hub.ZXPlane)

Script Result:
Bore_1.JPG
I think this was not intended by you ...
It might be better to let the user choose the Plane ...(In my loaded part the Cylinder is based on XY not on ZX)

Second Error:
System.Exception: Failed to understand default value for input description : Unable to determine location of image 'HKey.png'. If the script is not saved please save it and retry.
No Picture ...

Third Error:
System.Runtime.InteropServices.COMException (0x80040223): Input Face can't be a Curved face
May you choose an other Text for your userinterface "Please Choose Startface of Bore" or something. because i've been to silly to understand what face is needed ;-)

Fourth Error:
IronPython.Runtime.UnboundNameException: global name 'KS' is not defined
There is no variable named 'KS' outside of your function def InputChanged.
Simply add in your main skript before defining the funktion a KS=0 like :
TH = 0
LG = 0
WD = 0
KS = 0
KN = 0
readTh = 0


Fifth Error:
IronPython.Runtime.UnboundNameException: name 'readTh' is not defined
see fourth.

Hopefully i am on the right way here:
I followed your discription: print "5. Repeat 4 as required"
And then the main Error seems to occur if you try to add a second keyseat to an existing one:
Your function E.Diameter seems to be not able to get an diameter greater than zero from an not closed circle (the first keyseat has "disrupted" the circle)
You may add something like this:
if E.Diameter <= 0:
Win.InfoDialog('Sorry, I can not create a second keyseat in the same hole.','Error occured')
sys.exit()


I hope i have found the error and helped you.
If not, ask again and of course it will be a pleasure for me to try again.
Keep on scripting.

Regards
Stefan
 
Last edited:

albie0803

Alibre Super User
Hi Stefan, I think you nailed it here as I was idly fiddling just before work finished with a part that had a keyway in it. I had forgotten that this script can only run once. I will definitely add the diameter value check. I couldn't debug it as the line it fails on doesn't show at the moment and I don't know if there is a way to step through a script like I do in VB.

I will look at your suggestions about plane naming too.

Hello Albie ,
I followed your discription: print "5. Repeat 4 as required"
And then the main Error seems to occur if you try to add a second keyseat to an existing one:
Your function E.Diameter seems to be not able to get an diameter greater than zero from an not closed circle (the first keyseat has "disrupted" the circle)
You may add something like this:
if E.Diameter <= 0:
Win.InfoDialog('Sorry, I can not create a second keyseat in the same hole.','Error occured')
sys.exit()

I tend to write scripts to do what I want, to suit the way I work. This script is mainly used with existing templates where the ZX plane will always be correct. The parts the script is used on will have an existing bore but may be a fabrication size (undersize) in its base configuration, hence the optional question about the finished bore. The edge to be selected is the edge of the bore to be keyed (which sets the size) and the face is the face the key plane will sit on.

Thanks again for your help, most appreciated.
 

albie0803

Alibre Super User
Hopefully the corrected script should be in the part.
 

Attachments

  • Keyway Bore.AD_PRT
    374 KB · Views: 2
  • Metric Hole Keyways.py
    6.2 KB · Views: 2

idslk

Alibre Super User
Hello Albie,

i downloaded the part, started the script and clicked "ok" at "Do you want to cut finished bore size?"
At the following window i changed my mind and clicked "Cancel", what throws the following error:

IronPython.Runtime.Exceptions.TypeErrorException: 'NoneType' object is not subscriptable

to get rid of this, add a
if values!=None:
PL = Values[0]
BD = float(Values[1])
# AddPlane and Sketch
S1 = Hub.AddSketch('BoreProfile', Hub.GetPlane(PL.Name))
S1.AddCircle(0, 0, BD, False)
Hub.AddExtrudeCut('Bore', S1, 0, True, Part.EndCondition.ThroughAll, None, 0, Part.DirectionType.Normal, None, 0, False)
Win.InfoDialog('Activate Finished config to see key', 'Note')
else:
Win.InfoDialog('Ok. You changed your mind','Very windy today')

The rest seems to play ok.
Congrats well done

Regards
Stefan
 
Last edited:

albie0803

Alibre Super User
I don't understand why this works

Code:
    if Values != None:
        PL = Values[0]
        BD = float(Values[1])
        # AddPlane and Sketch
        S1 = Hub.AddSketch('BoreProfile', Hub.GetPlane(PL.Name))
        S1.AddCircle(0, 0, BD, False)
        Hub.AddExtrudeCut('Bore', S1, 0, True, Part.EndCondition.ThroughAll, None, 0, Part.DirectionType.Normal, None, 0, False)
        Win.InfoDialog('Activate Finished config to see key', 'Note')
#    else:
#        Win.InfoDialog('Ok. You changed your mind','Create Finished Bore')
#        sys.exit('Ok. You changed your mind')

but take out the hashes and it throws an error
 

idslk

Alibre Super User
Hello albie,

i've copied this on my PC into your keyway skript, erased the hashes and it works. What kind of error do you get?

Regards
Stefan
 

albie0803

Alibre Super User
Maybe tomorrow or monday before I get to this.

Hmmm....I ran it at home and it worked fine. I will try it on monday at work. Maybe Alibre had corrupted a bit and needed to be restarted.

Thanks for persevering with this for me.
 
Last edited:
Top