Plan to classify parts for laser cutting

First, sorry for my English.
I work with physical model, I’m not a programmer, but with a lot of struggle I managed to create an extension that has helped me a lot in my work.
I have a script to plan the parts, it works very well, but I would like it to create a classification by layer, like a space of 2mm of parts of the same layer, and a space of 100mm of parts in layers differences, something similar to what happens with the ABF extension.

def self.planificar
mod = Sketchup.active_model
ent = mod.active_entities
sel = mod.selection
SKETCHUP_CONSOLE.clear
org = Geom::Point3d.new(); spc = 2.mm
cdn = ent.grep(Sketchup::ComponentInstance).map{|ci|ci.definition.name}.uniq.sort

cdn.each{|n|
cis = ent.grep(Sketchup::ComponentInstance).each{|ci|

next unless ci.definition.name==n
ci.transform! ci.transformation.inverse
ci.transform! Geom::Transformation.translation(org-ci.transformation.origin)
if ci.bounds.width>ci.bounds.height
ci.transform! Geom::Transformation.translation(org-ci.bounds.corner(2))
ci.transform! Geom::Transformation.rotation(org,Z_AXIS,90.degrees)
end

if ci.bounds.depth>ci.bounds.width && ci.bounds.depth>ci.bounds.height
ci.transform! Geom::Transformation.rotation(org,X_AXIS,90.degrees)
end

if ci.bounds.min.y < org.y
ci.transform! Geom::Transformation.rotation(org,X_AXIS,180.degrees)
ci.transform! Geom::Transformation.translation(org-ci.bounds.corner(0))
end

org.offset!([ci.bounds.width+spc,0,0])
}
}
end

This is what I have.

This is what I wish I had.

you could go check on the side of deepnest

and the online version, svgnest

they do what you want to do, at least partially, I’ve used them before lasercutting.
They are open source, so you might poke into the code to see how they work and how to work with them in an extension

Thank you friend for your help, so I love this community, I already make use of these tools, especially Deepnest, but it does not serve me, I have information in the pieces, numbering and recordings, in addition to the tags that are lost in it.