help with first time gadget

This section is for general discussion about Gadgets
Post Reply
smithori
Posts: 3
Joined: Sun May 14, 2023 1:44 pm
Model of CNC Machine: 3 axis 2021

help with first time gadget

Post by smithori »

Hi all,

We have been trying to build our first gadget to help us create drilling only for cabinet fronts we receive from outside source

The gadget should import all DXF from a directory one by one. for each file it should move the vectors to a specific location, recalculate all toolpaths, save the nc file, delete all vectors and open a new dxf

at the moment we could not find how to delete all vectors before we import a new dxf, can you please help?

also I am sharing what we wrote so far a would love your comments and improvements

-- VECTRIC LUA SCRIPT

-- Function to display a message box
function MessageBox(title, message)
local mb = wx.wxMessageBox(message, title, wx.wxOK + wx.wxICON_INFORMATION)
end

-- Function to get the selected directory
function GetDirectory()
local openFileDialog = wx.wxDirDialog(nil, "Choose a directory", "", wx.wxDD_DEFAULT_STYLE + wx.wxDD_DIR_MUST_EXIST)
if openFileDialog:ShowModal() == wx.wxID_CANCEL then
return ""
end
return openFileDialog:GetPath()
end

-- Main function
function main()


-- Job Validation --
local job = VectricJob()
if not job.Exists then
DisplayMessageBox("Error: No job loaded")
return false ;
end


--selecting directory
local file_dialog = FileDialog()
file_dialog:FileOpen("","","")
local path = file_dialog.Directory
DisplayMessageBox(path)




--checking for valid directory
local directory = path

if directory == "" then
return
end


--going over selectet folder to map files

local fileList = ""

for file in io.popen("dir \""..directory.."\" /b"):lines() do
fileList = fileList..file
DisplayMessageBox(fileList)


local mr_file = path.. "\\"..fileList


DisplayMessageBox(mr_file)
job:ImportDxfDwg(mr_file)

--selecting vectors
local selection =job.Selection
-------- local selection_index = selection.count
DisplayMessageBox(tostring(selection.Count))

local vocter = Vector2D(50,50)
local trans = TranslationMatrix2D(vocter)
selection:Transform(trans)

local toolpath_mgr = ToolpathManager()


--checking selection on cad objects to retreive size data
local pos = selection:GetTailPosition()
local object
local uid_list = UUID_List()

object, pos = selection:GetPrev(pos)
local bb2d = Box2D(object:GetBoundingBox())
DisplayMessageBox(tostring(bb2d.XLength))
DisplayMessageBox(tostring(bb2d.YLength))

pos = selection:GetTailPosition()
--saving localy the x y values
for i = selection.Count,1,-1 do

object, pos = selection:GetPrev(pos)
uid_list:AddHead(object.RawId)

end

local x_value = tostring(bb2d.XLength)
local y_value = tostring(bb2d.YLength)
local name_is = x_value.. "X".. y_value
DisplayMessageBox(name_is)
toolpath_mgr:RecalculateAllToolpaths()
DisplayMessageBox(tostring(toolpath_mgr.Count))

--add the toolpath to saver
local pos_tool = toolpath_mgr:GetTailPosition()
local toolpath
local tps = ToolpathSaver()

for i=toolpath_mgr.Count,1,-1 do
toolpath, pos_tool = toolpath_mgr:GetNext(pos_tool)
--DO SOMETHING WITH TOOLPATH ....
tps:AddToolpath(toolpath)
end

--saving to nc
local post_pro = tps.DefaultPost
tps:SaveToolpaths(post_pro,directory.. name_is..".nc",false)

--------------------------------------------------------------

--------------------------------------------------------------






fileList = ""

--locating the dxf and saving
--sjob.Selection

-- DisplayMessageBox(tostring(job.Selection))



---clear the used vector



end



return true
end

-- function end
-- ===================================================]]

User avatar
jimandi5000
Vectric Wizard
Posts: 1050
Joined: Wed Mar 11, 2015 6:50 pm
Model of CNC Machine: Home Made 60 x 120
Location: North Houston Tx.
Contact:

Re: help with first time gadget

Post by jimandi5000 »

smithori wrote:
Thu May 25, 2023 5:24 am
Hi all,

We have been trying to build our first gadget to help us create drilling only for cabinet fronts we receive from outside source

The gadget should import all DXF from a directory one by one. for each file it should move the vectors to a specific location, recalculate all toolpaths, save the nc file, delete all vectors and open a new dxf

at the moment we could not find how to delete all vectors before we import a new dxf, can you please help?

also I am sharing what we wrote so far a would love your comments and improvements

-- VECTRIC LUA SCRIPT

-- Function to display a message box
function MessageBox(title, message)
local mb = wx.wxMessageBox(message, title, wx.wxOK + wx.wxICON_INFORMATION)
end

-- Function to get the selected directory
function GetDirectory()
local openFileDialog = wx.wxDirDialog(nil, "Choose a directory", "", wx.wxDD_DEFAULT_STYLE + wx.wxDD_DIR_MUST_EXIST)
if openFileDialog:ShowModal() == wx.wxID_CANCEL then
return ""
end
return openFileDialog:GetPath()
end

-- Main function
function main()


-- Job Validation --
local job = VectricJob()
if not job.Exists then
DisplayMessageBox("Error: No job loaded")
return false ;
end


--selecting directory
local file_dialog = FileDialog()
file_dialog:FileOpen("","","")
local path = file_dialog.Directory
DisplayMessageBox(path)




--checking for valid directory
local directory = path

if directory == "" then
return
end


--going over selectet folder to map files

local fileList = ""

for file in io.popen("dir \""..directory.."\" /b"):lines() do
fileList = fileList..file
DisplayMessageBox(fileList)


local mr_file = path.. "\\"..fileList


DisplayMessageBox(mr_file)
job:ImportDxfDwg(mr_file)

--selecting vectors
local selection =job.Selection
-------- local selection_index = selection.count
DisplayMessageBox(tostring(selection.Count))

local vocter = Vector2D(50,50)
local trans = TranslationMatrix2D(vocter)
selection:Transform(trans)

local toolpath_mgr = ToolpathManager()


--checking selection on cad objects to retreive size data
local pos = selection:GetTailPosition()
local object
local uid_list = UUID_List()

object, pos = selection:GetPrev(pos)
local bb2d = Box2D(object:GetBoundingBox())
DisplayMessageBox(tostring(bb2d.XLength))
DisplayMessageBox(tostring(bb2d.YLength))

pos = selection:GetTailPosition()
--saving localy the x y values
for i = selection.Count,1,-1 do

object, pos = selection:GetPrev(pos)
uid_list:AddHead(object.RawId)

end

local x_value = tostring(bb2d.XLength)
local y_value = tostring(bb2d.YLength)
local name_is = x_value.. "X".. y_value
DisplayMessageBox(name_is)
toolpath_mgr:RecalculateAllToolpaths()
DisplayMessageBox(tostring(toolpath_mgr.Count))

--add the toolpath to saver
local pos_tool = toolpath_mgr:GetTailPosition()
local toolpath
local tps = ToolpathSaver()

for i=toolpath_mgr.Count,1,-1 do
toolpath, pos_tool = toolpath_mgr:GetNext(pos_tool)
--DO SOMETHING WITH TOOLPATH ....
tps:AddToolpath(toolpath)
end

--saving to nc
local post_pro = tps.DefaultPost
tps:SaveToolpaths(post_pro,directory.. name_is..".nc",false)

--------------------------------------------------------------

--------------------------------------------------------------






fileList = ""

--locating the dxf and saving
--sjob.Selection

-- DisplayMessageBox(tostring(job.Selection))



---clear the used vector



end



return true
end

-- function end
-- ===================================================]]
Hi smithori,

Good to hear you are trying your hand at writing your own Gadget. I am sure you can do it and if you need help send me a PM.

I do not think you can Delete Vectors with a gadget.

JimAndi
Thanks,
Jim

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

Re: help with first time gadget

Post by adze_cnc »

You might want to enclose code posted here using the code tags (the icon with the </> on it). Quote this message to see what you'd need to type.

Code: Select all

-- sample code
local a = 1
print(a)

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

Re: help with first time gadget

Post by adze_cnc »

As well, have you looked at the built-in "DXF Batch Processor" gadget? To locate it from VCarve / Aspire's File menu do:
 
01-menu.ong.png
 
02-gadgets.png

smithori
Posts: 3
Joined: Sun May 14, 2023 1:44 pm
Model of CNC Machine: 3 axis 2021

Re: help with first time gadget

Post by smithori »

adze_cnc wrote:
Thu May 25, 2023 9:09 am
You might want to enclose code posted here using the code tags (the icon with the </> on it). Quote this message to see what you'd need to type.

Code: Select all

-- sample code
local a = 1
print(a)
Thanks! Will use it next time

Do you know anything on how to delete vectors from a gadget? There must be a way

smithori
Posts: 3
Joined: Sun May 14, 2023 1:44 pm
Model of CNC Machine: 3 axis 2021

Re: help with first time gadget

Post by smithori »

adze_cnc wrote:
Fri May 26, 2023 10:44 pm
As well, have you looked at the built-in "DXF Batch Processor" gadget? To locate it from VCarve / Aspire's File menu do:
 
01-menu.ong.png
 
02-gadgets.png
We will look into it, thanks!

gregk
Vectric Staff
Posts: 373
Joined: Mon Mar 05, 2018 12:34 pm
Model of CNC Machine: None

Re: help with first time gadget

Post by gregk »

Do you know anything on how to delete vectors from a gadget? There must be a way
For each vector that you want to remove, you need to get a CadLayer object that contains it and then use RemoveObject or RemoveAt method:

Code: Select all

local object = layer:GetAt(pos)
layer:RemoveObject(object)
-- or
layer:RemoveAt(pos)
I hope this helps,

Greg K

Post Reply