Ruby: Group from existing entities

Hi, I have finally taken the plunge and joined the community. So I don’t know whether I’m doing it right and please excuse my blunders (and put me right).
I have a working Ruby programme to slice my wooden toy models. Each slice can contain more than one sub-section and I’m trying to isolate them for printing etc (automated, of course). My code does seem to achieve this and makes groups out of the separate sub-sections, but I get a bugslpat when trying to do an UNDO of the program or load another model. I have identified the faces defining the sub-sections and try to use them to make them into groups (or components) each, preferably within the slice group s for further processing. The bug is generated when I include this code section:

_faces.each_with_index {|ff, j|
  _main_grp = s.entities.add_group(ff.all_connected.to_a)
  grp_trans = _main_grp.transformation
  grp_def = _main_grp.definition

  _inst = s.entities.add_instance grp_def, grp_trans
  _inst.name = "Slice%02d_%02d" % [i, j]
  _main_grp.entities.clear!
  _main_grp.erase!
}

What am I doing wrong? Any help would be appreciated. Thanks very much.

Grouping from existing entities only works in the active drawing context, not inside of groups/components that are not open for editing.

If you only need edges and faces, not materials, texture positioning and attributes, you can somewhat easily recreate the geometry inside the groups after they have been created.

Btw, in Ruby a leading underscore is typically used to denote a parameter isn’t being used, which makes your code quite hard to read as half the lines start off by saying they should be ignored.

Thank you very much for the quick reply. Sorry about the underscore. I was under the wrong impression that it denoted a local variable. I will of course correct that. On your first comment I will have to do some more research to understand why my entities are not active(??)

I have finally found a way of achieving what I wanted. The code snippet is probably not elegant enough for others but I thought I share it any way. Thanks again eneroth3, you put me on the right path.

faces.each_with_index {|face, j|
  sub_slice = []
  sub_slice = face.all_connected.grep(Sketchup::Edge).to_a
  #sub_slice = face.all_connected.to_a
  main_grp = Sketchup.active_model.active_entities.add_group(sub_slice)
  grp_trans = main_grp.transformation
  grp_def = main_grp.entities.parent
  inst = sub_slice_grp.entities.add_instance grp_def, grp_trans
  inst.name = "Slice%02d_%02d" % [i, j]
  main_grp.entities.clear!
  main_grp.erase!
}
# tidy any left-over entities which are unwanted
puts sub_slice_grp.typename
sub_slice_grp.entities.to_a.each {|ssg| 
  next if ssg.deleted?
  if ssg.is_a? (Sketchup::Group)
    ssg.entities.each {|edg| orient_face(edg)} # fill the faces in the group again
  end
  ssg.erase! if !ssg.is_a? (Sketchup::Group)
}

Btw, the line as a comment was the one that caused SU to bugsplat. Have not had any of them since I removed that statement. Glad to be part of this group now.

A general tip to make code more elegant and readable is to break out chunks into individual shorter methods. Some say a method shouldn’t be more than 5 lines, other says 10 and some 20. I like to make every chunk that can be given a descriptive name into a method, even if it is just a single line. If the extracted method doesn’t contain to a single easily identifiable “task” and the calling method doesn’t get easier to understand when replacing the chunk with a method name, the code probably fit better where it was.

Thanks for your encouragement. It just shows what a rookie I am.