Looping though entities on a layer and putting in a group

This forum has been created for users to submit suggestions for Gadgets
Post Reply
reggieaj
Posts: 8
Joined: Sat Jun 18, 2022 11:09 pm
Model of CNC Machine: X-Carve

Looping though entities on a layer and putting in a group

Post by reggieaj »

I'm simply want to loop through all entities on a layer and put them in a group.
I went through the SDK last night and couldn't find an example.
I must be over looking it.
Can someone post some lua to just go through all entities on a layer and put them in a group please?

User avatar
Adrian
Vectric Archimage
Posts: 13159
Joined: Thu Nov 23, 2006 2:19 pm
Model of CNC Machine: ShopBot PRS Alpha 96x48
Location: Surrey, UK

Re: Looping though entities on a layer and putting in a group

Post by Adrian »

On my phone at the moment so can't check but from memory the Chamfer Gadget has code to do that - https://gadgets.vectric.com/V11/chamfer

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

Re: Looping though entities on a layer and putting in a group

Post by jimandi5000 »

reggieaj wrote:
Mon Jun 20, 2022 9:54 pm
I'm simply want to loop through all entities on a layer and put them in a group.
I went through the SDK last night and couldn't find an example.
I must be over looking it.
Can someone post some lua to just go through all entities on a layer and put them in a group please?
Hi Reggieaj,

Here is a function I use to group objects to a layer

Code: Select all

-- =====================================================]]
function AddGroupToJob(job, group, layer_name)
--[[  --------------- AddGroupToJob --------------------
|
|  Add passed group to the job - returns object created
|  Parameters:
|     job              -- job we are working with
|     group            -- group of contours to   add to document
|     layer_name       -- name of layer group will be created on|
|  Return Values:
|     object created to represent group in document
]]
   --  create a CadObject to represent the  group
  local cad_object = CreateCadGroup(group);
   -- create a layer with passed name if it doesnt already exist
  local layer = job.LayerManager:GetLayerWithName(layer_name)
   -- and add our object to it
  layer:AddObject(cad_object, true)
  return cad_object
end
-- =====================================================]]
And I call it like this:

Code: Select all


local Group = ContourGroup(true)
local job = VectricJob()

local line = Contour(0.0)
  line:AppendPoint(pB1) 
  line:LineTo(pE2)
  line:LineTo(pE3) 
  line:LineTo(pB4) 
  line:LineTo(pB5) 
  group:AddTail(line) 
  
  line = Contour(0.0) 
  line:AppendPoint(pB3)
  line:LineTo(pC3)
  line:LineTo(pK8)
  line:LineTo(pB8)
  line:LineTo(pE3) 
  group:AddTail(line)   

AddGroupToJob(job, Group, "Jim")

This will make two line and Group them.
Thanks,
Jim

reggieaj
Posts: 8
Joined: Sat Jun 18, 2022 11:09 pm
Model of CNC Machine: X-Carve

Re: Looping though entities on a layer and putting in a group

Post by reggieaj »

My goal is to not have to select geometry or create geometry in or to group.
I see from the SDK how to add to group if entities are selected or created but not if they already exist.

What I want to do is start looping through all of the existing geometry on a layer (without having to select anything)
and add all of the geometry on the layer to a group.

I could not find a way to loop through existing geometry on a layer!!!
Granted I only started looking last night. But I would have thought this would be easy to find.

Pseudo Code:

Open Vcarve
Load File
Start Gadget

For each layer in file
Create a new group
For each entity on a layer 'No selection. Just traverse the entities without selecting 1st
Add the entity to a group
Loop End
Loop End

Create Tool Path

Simple enough. But I couldn't find way to do it!!
I think it's just my inexperience with Lua. Surely this has been done before.
Please help. Trying to impress boss in the morning with some automation.

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

Re: Looping though entities on a layer and putting in a group

Post by adze_cnc »

One of the sample files in the SDK (under "Samples - General”) is a layer example. In that example it counts the number of objects on the layer. It’s not a large step from counting objects to adding them to a layer as you count them.

reggieaj
Posts: 8
Joined: Sat Jun 18, 2022 11:09 pm
Model of CNC Machine: X-Carve

Re: Looping though entities on a layer and putting in a group

Post by reggieaj »

adze_cnc,

I'll take a closer look at that. It was the most promising from last night.
I wasn't sure it was exposing the object ID. If it is then I should be good.
Thanks.

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

Re: Looping though entities on a layer and putting in a group

Post by jimandi5000 »

You can also select geometry on a layer by using the select vectors on layer function (listed below) you can find the code in the SDK

Code: Select all

function SelectVectorsOnLayer(layer, selection, select_closed, select_open, select_groups)
    -- Please Note: SelectVectorsOnLayer is provided by Vectric and can be found in the SDK and Sample Gadget files.
    --[[  ---------------- SelectVectorsOnLayer ----------------
    -- |   SelectVectorsOnLayer("Stringer Profile", selection, true, falus, falus)
    -- |   Add all the vectors on the layer to the selection
    -- |     layer,            -- layer we are selecting vectors on
    -- |     selection         -- selection object
    -- |     select_closed     -- if true  select closed objects
    -- |     select_open       -- if true  select open objects
    -- |     select_groups     -- if true select grouped vectors (irrespective of open / closed state of member objects)
    -- |  Return Values:
    -- |     true if selected one or more vectors|
    --]]
    local objects_selected = false
    local warning_displayed = false
    local pos = layer:GetHeadPosition()
    while pos ~= nil do
      local object
      object, pos = layer:GetNext(pos)
      local contour = object:GetContour()
      if contour == nil then
        if (object.ClassName == "vcCadObjectGroup") and select_groups then
          selection:Add(object, true, true)
          objects_selected = true
        else
          if not warning_displayed then
            local message = "Object(s) without contour information found on layer - ignoring"
            if not select_groups then
              message = message ..  "\r\n\r\n" ..
              "If layer contains grouped vectors these must be ungrouped for this script"
            end -- if end
            DisplayMessageBox(message)
            warning_displayed = true
          end -- if end
        end -- if end
      else  -- contour was NOT nil, test if Open or Closed
        if contour.IsOpen and select_open then
          selection:Add(object, true, true)
          objects_selected = true
        elseif select_closed then
          selection:Add(object, true, true)
          objects_selected = true
        end -- if end
      end -- if end
    end -- while end
    -- to avoid excessive redrawing etc we added vectors to the selection in 'batch' mode
    -- tell selection we have now finished updating
    if objects_selected then
      selection:GroupSelectionFinished()
    end -- if end
    return objects_selected
  end -- function end
-- =====================================================]]

Thanks,
Jim

reggieaj
Posts: 8
Joined: Sat Jun 18, 2022 11:09 pm
Model of CNC Machine: X-Carve

Re: Looping though entities on a layer and putting in a group

Post by reggieaj »

Ah. function SelectVectorsOnLayer(layer, selection, select_closed, select_open, select_groups) looks like exactly what I need.
I'll give a go.
Thank you very much!!!!!!!!!!!!

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

Re: Looping though entities on a layer and putting in a group

Post by jimandi5000 »

Okay!
Attachments
MyCode.jpg
Thanks,
Jim

Post Reply