How can I get the positions and add a face to the via layer where I added edges?

I’m trying to make it to where after I add the edges the faces will form automatically. I’m not really sure if I am doing that right though. Also after forming the faces I intend to bring the insulator layer up 10 mm and subtract the via from it.

Thanks!

module Practice
  model = Sketchup.active_model # Open model
  ents = model.active_entities
  layers_array = model.layers
  
  # add layers to the array.
  substrate_tag = layers_array.add "substrate"
  insulator_tag = layers_array.add "insulator"
  via_tag = layers_array.add "via"
  
  # create substrate
  substrate = ents.add_group # add substrate group
  substrate.name = "substrate" # name substrate group
  sub_face = substrate.entities.add_face [0,0,0], [5,0,0], [5,8,0], [0,8,0] # add face
  if sub_face.normal.z == 1 # want to extrude in negative direction
    sub_face.reverse!
  end
  sub_face.pushpull 10.mm # extrude 10mm below
  substrate.layer=substrate_tag # add layer tag.
  
  # now do via and insulator layer
  via = ents.add_group
  via.name = "via"
  via_faces=[]
  via_faces.push(via.entities.add_face [1,1,0], [4,1,0],[4,3,0],[1,3,0])
  via_faces.push(via.entities.add_face [1,5,0], [4,5,0],[4,7,0],[1,7,0])
  # get the positions for each vertex [DOESN'T WORK]
  #via_faces.each{|face| face.vertices.each{|vertex| vertex.position}}
  
  # go up by 25 mm 
  vector = Geom::Vector3d.new(0, 0, 25.mm)
  # create a new edge for each point
  for face in via_faces do
    for vertex in face.vertices
      via.entities.add_edges(vertex.position,vertex.position+vector)
    end
  end
  via.layer = via_tag
  #via_faces.each{|face| face.reverse! if face.normal.z == -1}
  #via_faces.each{|face| face.pushpull(thickness.mm)}
  #top_via_faces=via.entities.find_all{|ent| ent.typename == "Face" &&    ent.normal == [0, 0, 1]}
  # access vertices
  #top_via_faces.each{|face| p face.vertices.position}
  #verts = top_via_faces.each{|face| p face.vertices.each{|vertex| p vertex.position}}
  #via_copy = via.parent.entities.add_instance(via.definition, via.transformation)
    
   # now do insulator
  insulator = ents.add_group
  insulator.name = "insulator"
  ins_face = insulator.entities.add_face [0,0,0], [5,0,0], [5,8,0], [0,8,0]
  if ins_face.normal.z == -1 # want to extrude in negative direction
    ins_face.reverse!
  end
  ins_face.pushpull 10.mm
  #insulator = via.subtract(insulator)
  insulator.layer = insulator_tag
  
  
  model.rendering_options['DisplayColorByLayer']= true # colors each layer
end



You don’t usually need to start with an empty array like this and push stuff into it. Instead …

  via.entities.add_face([1,1,0], [4,1,0], [4,3,0], [1,3,0])
  via.entities.add_face([1,5,0], [4,5,0], [4,7,0], [1,7,0])
  via_faces = via.entities.grep(Sketchup::Face) # very fast!
  points = via_faces.flat_map(&:vertices).uniq.map(&:position)

which is shorthand for …

points = via_faces.flat_map{|face| face.vertices }.uniq.map{|vert| vert.position }

Your topic title is misleading. It asks about adding geometry to a layer. SketchUp layer/tag objects are not geometric collections. They are nothing but a shared property sheet holding display behaviors that can be shared among many objects.

You add geometry to Entities collections.

Most API and Ruby collection type objects have the core Enumerable mixin library mixed in (using include,) so be aware of the nifty methods that this library module adds to Ruby collection classes.

To answer the general question of “how to add a face where you created edges” …

… you use the Edge#find_faces method.

So let us say that you have an array of “new_via_edges”.

new_via_edges.each(&:find_faces)

… which is shorthand for …

new_via_edges.each {|edge| edge.find_faces }