Accessing entities of a group for applying / changing materials


As my knowledge, it is possible to add entity inside a group. is there is anyway to access a particular face of the group ( ex: a cube of size 1m,1m,1m, consist of 6 faces) and apply different material to each face.


It is quite possible, though the technique you will need to use depends on how much you know about the Face(s) you want to paint. Some examples:

  • If you created the Faces yourself using
    face = my_group.entities.add_face(points)
    you can immediately apply a Material via
    face.material = some_material
  • If you know the Faces of interest are already in the Group, you can retrieve them using
    array_of_faces = my_group.entities.grep(Sketchup::Face)
    You then need to decide which face in the array is to receive which material. That might be done by testing the direction of each face’s normal. Note that grep will find all faces in the group, which might include more than you want to work on if there are others present too. In that case, you need to figure out a test that will uniquely select the ones of interest. The test might again involve Face normal directions, but without more information, I can’t suggest a specific test for other situations.

Hi slbaumgartner,

Thanks for the info. I am creating a face with four points in an array, with each point has a set of x,y,z values defined. Then using push pull to make it as panel. Then grouping it all connected faces as a single group. Now would like to access each face for applying the materials like laminate on the vertical surface (I.e two sides, which has bigger area)and rest of faces with edgeband.

The code follows something similar

 Default code, use or delete...
mod = Sketchup.active_model # Open model
ent = mod.entities # All entities in model
sel = mod.selection # Current selection
#declaration of variables
height = #Height of the cabinet
depth = #depth of the cabinet
wd = #width of the cabinet
thickness #thickness of the panel
# Left side Panel
pt = [0,1,2,3]
pt[0] = [0,0,0]
pt[1] = [thickness,0,0]
pt[2] = [thickness,depth,0]
pt[3] = [0,depth,0]

#creation of face - LH Side panel
face = ent.add_face(pt)
face.pushpull(height, true)
lh_group = ent.add_group face.all_connected "LH Panel"

This workflow is backwards. If you draw in the model entities context, your new geometry will interact and intersect with other geometry that you (or the user) has drawn in that context.

Instead, create an empty group, and add your geometry to the group’s definition’s entities.
(The group class has a shortcut method to get at their definition’s entities collection.)

# Assume laminate_matl and edgeband_matl already point at the
# desired material objects in the model's materials collection.

# Creation of Side panel
def create_side_panel(pts, height, laminate_matl, edgeband_matl, name= "Panel")
  group = Sketchup.active_model.active_entities.add_group
  face = group.entities.add_face(pts)
  fail "Error creating face from points." if !face
  face.pushpull(height, true) name
  grp_faces = group.entities.grep(Sketchup::Face)
  fail "Error pushpulling face." if grp_faces.size <= 1
  big_faces = grp_faces.max(2) {|a,b| a.area <=> b.area }
  lil_faces = grp_faces - big_faces
  big_faces.each {|face| face.material= laminate_matl }
  lil_faces.each {|face| face.material= edgeband_matl }
  return group
end # Check that group is valid in calling code!

Once the group is successfully created, use group.transform! to move, rotate or scale the group instance to the position desired.

Thanks Dan, will check it out

As @DanRathbun says - group = entities.add_group() [i.e. it’s empty] and then immediately add your geometry into that group.entities.
Also add this group to the model.active_entities context rather than the model.entities context.
This allows the code to work logically, even if the user is inside an edit…

1 Like

Thanx, fixed the example.