Easy Box Maker gadget (v2.0) suggested code improvement

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

Easy Box Maker gadget (v2.0) suggested code improvement

Post by adze_cnc »

Hello sharkcutup and jimandi5000:

A recent post showing that SpindleSpeed didn't get transferred from a Milling.MillTool to the toolpath Tool() got me to thinking how to slim down the copying of the MillTool values over to the Tool() fields when creating the toolpaths to avoid such errors. (That and I needed such a function for my own V-Carve Last Pass gadget if I ever finish it.)

In EasyBoxMaker v2.0, around line 2038, there is this 13 lines of code:

Code: Select all

local tool = Tool(Milling.MillTool2.Name, Tool.END_MILL)
tool.InMM = Milling.MillTool2.InMM
...
--  tool.VBitAngle = Milling.MillTool2.VBitAngle
--  tool.ClearStepover = Milling.MillTool2.ClearStepover
That code can be replaced with (similar code exits at 2090, 2140, 2190, and 2236):

Code: Select all

local tool = Tool(Milling.MillTool2.Name, Tool.END_MILL)
tool = loadToolValues(Milling.MillTool2, tool, readOnlyKeys)
This needs two things:

1. The following function:

Code: Select all

function loadToolValues(source, destination, excluded)
  local toolToLoad = destination

  for key, value in pairs(source) do
    if not excluded[key] then
      toolToLoad[key] = value
    end
  end
  return toolToLoad
end
2. This table entered outside the function main() as a global table:

Code: Select all

readOnlyKeys = {
  ["Name"] = true, ["RateUnitsText"] = true,
  ["ToolDBId"] = true, ["ToolDB_Location"] = true,
  ["ToolType"] = true, ["ToolTypeText"] = true
}
That table is a list of read-only values in the Vectric Tool() object the can't be updated.

This will shorten the code by quite a bit and hopefully make it easier to maintain. Note that the field key namess in Milling.MillTool{n} tables need to match those in the Vectric SDK Tool() object.

By the way: xxx.VBitAngle (both the Vectric Tool() instances and the MillTool ones) need to be changed to VBit_Angle everywhere or you will always get a bit of 90 degrees. V-Bits don't appear to be used in the Box Maker Gadget but it's good to change it in case this sort of code is re-used in another gadget that does use v-bits or engraving bits.

Attached is a sample Lua file that loads two tools and creates a sample profile toolpath for each.

Note that going from a Vectric tool to the Milling.MillTool is possible to. We just need the following minimum exclusion table:

Code: Select all

excludedKeys = {
  ["ToolDBId"] = true, ["ToolDB_Location"] = true
}
And this entry could also be added to the excludedKeys table if you’re not using the name within the Tool():

Code: Select all

  ["Name"] = true,
I hope that this is of some use.

Steven
Attachments
tool information copier.txt
Sample gadget code. Rename to .lua
(5.88 KiB) Downloaded 58 times

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: Easy Box Maker gadget (v2.0) suggested code improvement

Post by sharkcutup »

Thank You, Steven!

I have looked at this and yes I can see where that coding could shorten the overall code. I am going to make the adjustments to the Box Maker Gadget to verify/validate the difference. We are always looking at various ways to shorten the code but are also looking at the efficiency and logic of the code along with the overall layout of the code. In my short period of working with Lua I have seen that all these variables play a big part in how a gadget functions. Thank You again for your diligence in providing your knowledge of the Lua coding to enhance these gadgets!

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

Post Reply