How do you really choose the first one for the first? sel[0] 如何确实为最先选择的为第一个呢?

image
image

mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
cdgrp=sel[0]
ss=[]
(1..sel.count-1).each { |i|
  cdgrp1=cdgrp.copy
  bcgrp=sel[i].copy
  ss << sel[i]
  bcgrp1=cdgrp1.subtract(bcgrp)
   bcgrp1.layer=sel[i].layer
   bcgrp1.name=sel[i].name
}
ent.erase_entities ss

Class: Sketchup::Selection:
“…Note that the order of entities (selection[0] , selection[1] and so on) in the set is in no particular order and should not be assumed to be in the same order as the user selected the entities.”

You need to define and use condition(s) - according what you want to achieve - to sort the entities in selection.
Unfortunately, your post does not reveal all the details in this regard.

1 Like

Also see this old topic thread:

对物体进行分标记(图层)重新做了标记进行判断,然后就可以了。
谢谢各位
Divide the object into sub-labels (layers) and re-mark them to judge, and then it’s OK.
Thank you