Extending gadget objects by creating your own methods

This section is for general discussion about Gadgets
Post Reply
User avatar
adze_cnc
Vectric Wizard
Posts: 4325
Joined: Sat Jul 27, 2013 10:08 pm
Model of CNC Machine: AXYZ 4008
Location: Vancouver, BC, Canada

Extending gadget objects by creating your own methods

Post by adze_cnc »

I'm good at knowing that 2 plus 2 equals 4. It just take me quite a while figure out when I need to use that knowledge.

I was wrestling with a problem that needed a function to act upon a tool but it was becoming unwieldy passing tool instances to and from that function. I was needing temporary instances with their own names and things were getting out of hand.

I realised (had an epiphany?) that you can extend the Vectric gadget API's objects by adding your own methods [those ":" commands. e.g. the existing Tool:ConvertRateUnitsTo(new_rate_code) ].

For example the API has a Tool() object that has various constructors and methods. The following adds a simple ":ShowToolRPM" method. To test it yourself copy the code. Load or create a new job. Select Edit > Paste:

Code: Select all

-- VECTRIC LUA SCRIPT

function Tool:ShowToolRPM()
  DisplayMessageBox("You spin me right round at " .. self.SpindleSpeed .. " rpm")
end

function main(script_path)
  local endMill = Tool("Lua End Mill", Tool.END_MILL)
  endMill.SpindleSpeed = 10000
  endMill:ShowToolRPM()

  local vbit = Tool("Lua VBit Mill", Tool.VBIT)
  vbit.SpindleSpeed = 17500
  vbit:ShowToolRPM()

  return true
end
jimandi5000: This could make some of your gadgets less verbose and perhaps simpler(?) to maintain. For example in the file "EasyMDFDoorRegistry_Ver11.9.xlua" for "Easy MDF Cabinet Door Maker" the 96 lines in the "function RegistryWrite()" from about line 528 to 623:

Code: Select all

RegValue = RegistryWriter:SetDouble("MillTool1.FeedRate" ,     MillTool1.FeedRate)
RegValue = RegistryWriter:SetBool("MillTool1.InMM",            MillTool1.InMM)
...
RegValue = RegistryWriter:SetDouble("MillTool8.ClearStepover", MillTool8.ClearStepover)
RegValue = RegistryWriter:SetInt("MillTool8.ToolNumber",       MillTool8.ToolNumber)
could be replaced with 8 lines plus the function definition for Tool:RegWrite

Code: Select all

function Tool:RegWrite(registryrWriter, keyPrefix)
  local k = keyPrefix
  local w = registryWriter

  w:SetDouble(k .. "FeedRate",      self.FeedRate)
  w:SetBool(k ..   "InMM",          self.InMM)
  w:SetString(k .. "Name",          self.Name)
  w:SetDouble(k .. "PlungeRate",    self.PlungeRate)
  w:SetInt(k ..    "RateUnits",     self.RateUnits)
  w:SetInt(k ..    "SpindleSpeed",  self.SpindleSpeed)
  w:SetDouble(k .. "Stepdown",      self.Stepdown)
  w:SetDouble(k .. "Stepover" ,     self.Stepover)
  w:SetDouble(k .. "ToolDia" ,      self.ToolDia)
  w:SetDouble(k .. "VBit_Angle",    self.VBit_Angle)
  w:SetDouble(k .. "ClearStepover", self.ClearStepover)
  w:SetInt(k ..    "ToolNumber",    self.ToolNumber)
end

function RegistryWrite()
  local RegistryWriter = Registry(RegName)
  -- ...
  local key = "MillTool"
  MillTool1:RegWrite(RegWriter, key.."1.")
  MillTool2:RegWrite(RegWriter, key.."2.")
  MillTool3:RegWrite(RegWriter, key.."3.")
  MillTool4:RegWrite(RegWriter, key.."4.")
  MillTool5:RegWrite(RegWriter, key.."5.")
  MillTool6:RegWrite(RegWriter, key.."6.")
  MillTool7:RegWrite(RegWriter, key.."7.")
  MillTool8:RegWrite(RegWriter, key.."8.")
  -- ...
end
A similar replacement could be made for "function RegistryRead()" by adding a similar Tool:RegRead function.

User avatar
sharkcutup
Vectric Wizard
Posts: 2885
Joined: Sat Mar 26, 2016 3:48 pm
Model of CNC Machine: Shark HD3 Pro Extended Bed with Spindle
Location: U.S.A.

Re: Extending gadget objects by creating your own methods

Post by sharkcutup »

Interesting stuff there Adze_cnc

That sure can shorten up the LUA coding script considerably!

Sharkcutup
V-Carve Pro Tips, Gadget Tips & Videos
YouTube Channel - Sharkcutup CNC
V-Carve Pro 11.554

User avatar
sharkcutup
Vectric Wizard
Posts: 2885
Joined: Sat Mar 26, 2016 3:48 pm
Model of CNC Machine: Shark HD3 Pro Extended Bed with Spindle
Location: U.S.A.

Re: Extending gadget objects by creating your own methods

Post by sharkcutup »

adze_cnc wrote:
Mon Sep 19, 2022 6:27 pm
I'm good at knowing that 2 plus 2 equals 4. It just take me quite a while figure out when I need to use that knowledge.

I was wrestling with a problem that needed a function to act upon a tool but it was becoming unwieldy passing tool instances to and from that function. I was needing temporary instances with their own names and things were getting out of hand.

I realised (had an epiphany?) that you can extend the Vectric gadget API's objects by adding your own methods [those ":" commands. e.g. the existing Tool:ConvertRateUnitsTo(new_rate_code) ].

For example the API has a Tool() object that has various constructors and methods. The following adds a simple ":ShowToolRPM" method. To test it yourself copy the code. Load or create a new job. Select Edit > Paste:

Code: Select all

-- VECTRIC LUA SCRIPT

function Tool:ShowToolRPM()
  DisplayMessageBox("You spin me right round at " .. self.SpindleSpeed .. " rpm")
end

function main(script_path)
  local endMill = Tool("Lua End Mill", Tool.END_MILL)
  endMill.SpindleSpeed = 10000
  endMill:ShowToolRPM()

  local vbit = Tool("Lua VBit Mill", Tool.VBIT)
  vbit.SpindleSpeed = 17500
  vbit:ShowToolRPM()

  return true
end
jimandi5000: This could make some of your gadgets less verbose and perhaps simpler(?) to maintain. For example in the file "EasyMDFDoorRegistry_Ver11.9.xlua" for "Easy MDF Cabinet Door Maker" the 96 lines in the "function RegistryWrite()" from about line 528 to 623:

Code: Select all

RegValue = RegistryWriter:SetDouble("MillTool1.FeedRate" ,     MillTool1.FeedRate)
RegValue = RegistryWriter:SetBool("MillTool1.InMM",            MillTool1.InMM)
...
RegValue = RegistryWriter:SetDouble("MillTool8.ClearStepover", MillTool8.ClearStepover)
RegValue = RegistryWriter:SetInt("MillTool8.ToolNumber",       MillTool8.ToolNumber)
could be replaced with 8 lines plus the function definition for Tool:RegWrite

Code: Select all

function Tool:RegWrite(registry[b]r[/b]Writer, keyPrefix)
  local k = keyPrefix
  local w =[b] registryWriter[/b]

  w:SetDouble(k .. "FeedRate",      self.FeedRate)
  w:SetBool(k ..   "InMM",          self.InMM)
  w:SetString(k .. "Name",          self.Name)
  w:SetDouble(k .. "PlungeRate",    self.PlungeRate)
  w:SetInt(k ..    "RateUnits",     self.RateUnits)
  w:SetInt(k ..    "SpindleSpeed",  self.SpindleSpeed)
  w:SetDouble(k .. "Stepdown",      self.Stepdown)
  w:SetDouble(k .. "Stepover" ,     self.Stepover)
  w:SetDouble(k .. "ToolDia" ,      self.ToolDia)
  w:SetDouble(k .. "VBit_Angle",    self.VBit_Angle)
  w:SetDouble(k .. "ClearStepover", self.ClearStepover)
  w:SetInt(k ..    "ToolNumber",    self.ToolNumber)
end

function [b]RegistryWrite[/b]()
  local [b]RegistryWriter[/b] = Registry(RegName)
  -- ...
  local key = "MillTool"
  MillTool1:[b]RegWrite(RegWriter[/b], key.."1.")
  MillTool2:RegWrite(RegWriter, key.."2.")
  MillTool3:RegWrite(RegWriter, key.."3.")
  MillTool4:RegWrite(RegWriter, key.."4.")
  MillTool5:RegWrite(RegWriter, key.."5.")
  MillTool6:RegWrite(RegWriter, key.."6.")
  MillTool7:RegWrite(RegWriter, key.."7.")
  MillTool8:RegWrite(RegWriter, key.."8.")
  -- ...
end
A similar replacement could be made for "function RegistryRead()" by adding a similar Tool:RegRead function.

Got a problem with that script (I marked in BOLD). Not all may be causing a problem but I believe there may be a conflict with the different spellings of Function Registry Write

I am not an expert in LUA by any means but I have done some coding in my distant past and word variations can sometimes create havoc!

Sharkcutup
V-Carve Pro Tips, Gadget Tips & Videos
YouTube Channel - Sharkcutup CNC
V-Carve Pro 11.554

User avatar
adze_cnc
Vectric Wizard
Posts: 4325
Joined: Sat Jul 27, 2013 10:08 pm
Model of CNC Machine: AXYZ 4008
Location: Vancouver, BC, Canada

Re: Extending gadget objects by creating your own methods

Post by adze_cnc »

Argh!

Here's a case of problems with:
  1. an editor that "helpfully" changes all instances of something at once. Great when it works, horrendous when it doesn't.
  2. too many variables, functions, etc. with similar names
  3. trying to maintain consistency with another person's naming structure
  4. not sacrificing the right things to the coding gods...
In the above code:

Code: Select all

function Tool:RegWrite(registryWriter, keyPrefix)
...
end

function RegistryWrite()
  local RegistryWriter = Registry(RegName)
  -- ...
  local key = "MillTool"
  MillTool1:RegWrite(RegWriter, key.."1.")
The parameter name for the ":RegWrite" is OK as it stands. What is wrong is that each "MillToo1:RegWrite" needs to have "(RegistryWriter, key" substituted for "(RegWriter, key".

If'n it were me and I had carte blache to alter everything I'd actually rename "function RegistryWrite()" to "function updateRegistry()" [you could also capitalize "update" but I like the convention that when using multiple words in camel-case you don't capitalize the first. My exception is in creating ":" functions as in "Point:SetPoint(x, y)"].** I think verb+object names are better than noun names.

So, my revised excerpt (again not trying match the existing naming convention) would be:

Code: Select all

function Tool:SaveRegistry(keyPrefix)
  local k = keyPrefix
  local w = Registry(RegName)

  w:SetDouble(k .. "FeedRate", self.FeedRate)
  ...
  w:SetInt(k .. "ToolNumber", self.ToolNumber)
end

function updateRegistry()
  local RegistryWriter = Registry(RegName) -- see note below
  ...
  local toolKey = "MillTool"
  MillTool1:SaveRegistry(toolKey .. "1.")
  MillTool2:SaveRegistry(toolKey .. "2.")
  MillTool3:SaveRegistry(toolKey .. "3.")
  MillTool4:SaveRegistry(toolKey .. "4.")

  MillTool5:SaveRegistry(toolKey .. "5.")
  MillTool6:SaveRegistry(toolKey .. "6.")
  MillTool7:SaveRegistry(toolKey .. "7.")
  MillTool8:SaveRegistry(toolKey .. "8.")
  ...
end
The "local RegistryWriter = Registry(RegName)" is kept to maintain compatibility with the other entry writing statements. Note that since "RegValue" is never used all the "RegValue = " text can be safely removed.

"toolKey" a noun name is OK because it doesn't do anything. It just is a thing.

** I notice that in the Circle from 3 Points gadget that I actually broke this "rule" by using "Point:setPoint(x,y)"

User avatar
sharkcutup
Vectric Wizard
Posts: 2885
Joined: Sat Mar 26, 2016 3:48 pm
Model of CNC Machine: Shark HD3 Pro Extended Bed with Spindle
Location: U.S.A.

Re: Extending gadget objects by creating your own methods

Post by sharkcutup »

Hey Stephen,

Is it true that coders need to have a sense of humor? Got to admit you never know what to expect as an end result of the coding!!!

Thank you for your input/comments in Jim Andi's Easy Gadgets. I know that he definitely appreciates all input whether it is good or bad. Without this type of input/comments/remarks a gadget could not be turned into a useful and or successful tool.

Thank You,

Sharkcutup
V-Carve Pro Tips, Gadget Tips & Videos
YouTube Channel - Sharkcutup CNC
V-Carve Pro 11.554

Post Reply