Methods from Support files
-
I am trying to move some def's that are common to most of my joint scripts to a support file. I am already using Arithmetic parsing.rb which I think Jim sent me to convert equations into calculated lengths. I thought I could add other def's there and use them from the main program. I get this error message
Error: #<NoMethodError: undefined methodcal_xCL_points' for ArithmeString:Module> C:\Users\Keith\Documents\ShopProjects\TestPlugins\K2WS_Tools\KK_Tools_Template.rb:214:in
onLButtonDown'The code in the support file is:
def ArithmeString.cal_xCL_points(x1,e_len,dir_x,spacing,qty) @xcl=Array.new qty.to_i case spacing when "Symetrical" #Debug UI.messagebox("Spacing = " + spacing.to_s) if qty == 1 @xcl[0]=x1+(end2CL*dir_x) #Debug UI.messagebox("@xcl[0] = " + @xcl[0].to_s) #Debug UI.messagebox("Completed qty = " + qty.to_s) elsif qty == 2 @xcl[0]=x1+(end2CL*dir_x) @xcl[1]=x1+((e_len-end2CL)*dir_x) #Debug UI.messagebox("Completed qty = " + qty.to_s) elsif qty == 3 @xcl[0]=x1+(end2CL*dir_x) @xcl[1]=x1+((e_len/2)*dir_x) @xcl[2]=x1+((e_len-end2CL)*dir_x) #Debug UI.messagebox("Completed qty = " + qty.to_s) elsif qty >= 4 #Debug UI.messagebox("Started qty = " + qty.to_s) @xcl[0]=x1+(end2CL*dir_x) @xcl[qty-1]=x1+((e_len-end2CL)*dir_x) @xcl[qty/2]=x1+((e_len/2)*dir_x) n =(qty-2)/2 #loop number sn = 1 #Starting number i = 0 while n > 0 temp_offset=cl_offset[i] #Debug UI.messagebox("CL Offset = "+ temp_offset.to_s) @xcl[sn]=@xcl[sn-1]+((temp_offset)*dir_x) @xcl[qty-1-sn]=@xcl[qty-sn]-((temp_offset)*dir_x) sn = sn + 1 n = n -1 i = i + 1 end # while n > 0 end #if qty == #Debug @xcl.each {|e| UI.messagebox("Symetrical CL point = "+ e.to_s)} when "Equal" #Debug UI.messagebox("Case Spacing = " + spacing.to_s) equal_offset=(e_len-(end2CL*2))/(qty-1) @xcl[0]=x1+(end2CL*dir_x) @xcl[qty-1]=x1+((e_len-end2CL)*dir_x) n =(qty-2) #loop number sn = 1 #Starting number while n > 0 @xcl[sn]=@xcl[sn-1]+((equal_offset)*dir_x) sn = sn + 1 n = n -1 end # while n > 0 #Debug @xcl.each {|e| UI.messagebox("Equal CL point = "+ e.to_s)} when "User" #Debug UI.messagebox("Case Spacing = " + spacing.to_s) @xcl[0]=x1+(end2CL*dir_x) n =(qty-1) #loop number sn = 1 #Starting number i = 0 while n > 0 user_offset=cl_offset[i] #user_offset=user_offset.to_l #Debug UI.messagebox("user_offset = "+ user_offset.to_s) @xcl[sn]=@xcl[sn-1]+((user_offset)*dir_x) sn = sn + 1 i = i + 1 n = n -1 end # while n > 0 #Debug @xcl.each {|e| UI.messagebox("User CL point = "+ e.to_s)} when "Fixed" #Debug UI.messagebox("Case Spacing = " + spacing.to_s) @xcl[0]=x1+(end2CL*dir_x) n =(qty-1) #loop number sn = 1 #Starting number while n > 0 fixed_offset=cl_offset[0] @xcl[sn]=@xcl[sn-1]+((fixed_offset)*dir_x) sn = sn + 1 n = n -1 end # while n > 0 #Debug @xcl.each {|e| UI.messagebox("Fixed CL point = "+ e.to_s)} end #case spacing #Debug UI.messagebox("End Spacing = " + spacing.to_s) end #cal_xCL_points
the code in the main file is:
UI.messagebox"Calculations and then Add Features for first part" x1=0; e_len=6.0; dir_x=1;spacing="Symetrical"; qty=3 ArithmeString.cal_xCL_points(x1,e_len,dir_x,spacing,qty) UI.messagebox("Center line points = " + @xcl[1].to_s)
I know the file is read because in earlier statements I used:
todist_input=ArithmeString.string_to_length(results[1].to_s) if !todist_input then todist_input=results[1] end @@todist=todist_input; ans[1] = @@todist
and this works properly
As always I appreciate the help. [I do use the SketchUp Book, examples and api code and try lots of things before asking].
Keith -
Where is this 'support-file'... is it auto-loaded or do you 'require' it or 'load' it from a subfolder?
To test if it's loading add a line in the start of the
def ArithmeString.cal_xCL_points(x1, e_len, dir_x, spacing, qty)
asputs "ArithmeString.cal_xCL_points is here!"
Then in the Ruby Console type
ArithmeString.cal_xCL_points(1, 1, 1, 1, 1)
I know what's passed to it will likely break it... but at least we'll get to the "ArithmeString.cal_xCL_points is here!" in the Ruby Console, IF it's been loaded and it runs...
-
Thanks TIG that got me going. I found that my program was using the Arithmetic methods from an already loaded file.
However as I got into i farther I found that I was going to have to rewrite to pass several more variables and I am going to leave that for another time. I revised the names and added KK_ to the front so I would know where the methods were that I was using.Thanks
Keith -
Your trying to add a module function to an existing module.
Your method def needs to be wrapped within a:
module ArithmeString #---- paste method def here end
block, otherwise the instance var @xcl will be inaccessible. -
THIS thread needs to be moved to the Developer's forum.
-
Moved.
Advertisement