For some reason, the vertices are floating. Do you know any way to fix this? Also, I’m essentially trying to deposit m1 on top of the existing top faces uniformly and I am open to any suggestions on how to do that if this way seems to not be the best.
SKETCHUP_CONSOLE.clear
module Practice
model = Sketchup.active_model # Open model
manager = model.options
provider = manager["UnitsOptions"]
provider["LengthUnit"] = 2
sub_t = 2.mm
via_t = 5.mm
via_into = 1.mm
insulator_t = 2.mm
m1_t = 2.mm
current_z = 0.mm
prev_z = 0.mm
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"
m1_tag = layers_array.add "m1"
# create substrate
substrate = ents.add_group # add substrate group
sub_face = substrate.entities.add_face [0,0,0], [5.mm,0,0], [5.mm,8.mm,0], [0,8.mm,0] # add face
if sub_face.normal.z == 1 # want to extrude in negative direction
sub_face.reverse!
end
sub_face.pushpull sub_t # extrude 10mm below
# now do via and insulator layer
via = ents.add_group
via.name = "via"
via.entities.add_face([1.mm,1.mm,0], [4.mm,1.mm,0], [4.mm,3.mm,0], [1.mm,3.mm,0])
via.entities.add_face([1.mm,5.mm,0], [4.mm,5.mm,0], [4.mm,7.mm,0], [1.mm,7.mm,0])
via_faces = via.entities.grep(Sketchup::Face) # very fast!
points = via_faces.flat_map(&:vertices).uniq.map(&:position)
# go up by 25 mm
vector = Geom::Vector3d.new(0, 0, via_t)
top_face_pts=[]
via_faces.each{|f| top_face_pts.push(f.vertices.map(&:position).map{|p| p.offset(vector)})}
# adds the top face
top_face_pts.each{|pts| via.entities.add_face pts}
# create a new edge for each point
points.each{|pt| via.entities.add_edges(pt,pt.offset(vector))}
current_z = via_t - via_into
# add faces
via_edges = via.entities.grep(Sketchup::Edge)
via_edges.each(&:find_faces)
#new_via_edges.each(&:find_faces)
new_transform = Geom::Transformation.new([0,0,-via_into])
via.transformation = new_transform
substrate=via.trim(substrate)
# now do insulator
insulator = ents.add_group
ins_face = insulator.entities.add_face [0,0,0], [5.mm,0,0], [5.mm,8.mm,0], [0,8.mm,0]
if ins_face.normal.z == -1 # want to extrude in negative direction
ins_face.reverse!
end
ins_face.pushpull insulator_t
# changed!
insulator = via.trim(insulator)
top_ins_pts = insulator.entities.grep(Sketchup::Face).first.vertices.map(&:position)
m1 = ents.add_group
groups = ents.grep(Sketchup::Group)
faces = groups.flat_map{|g| g.entities.grep(Sketchup::Face)}
positions = groups.flat_map{|g| g.entities.grep(Sketchup::Face)}.flat_map(&:vertices).map(&:position)
top_points = positions.find_all{|p| p.z > prev_z && p.z < current_z+m1_t}
top_points = top_points.map(&:to_a).uniq
vector = Geom::Vector3d.new(0, 0, m1_t)
top_points.each{|pt| m1.entities.add_edges(pt,pt.offset(vector))}
m1_edges = m1.entities.grep(Sketchup::Edge)
m1_edges.each(&:find_faces)
#name groups
substrate.name = "substrate" # name substrate group
via.name = "via"
insulator.name = "insulator"
m1.name = "m1"
via.layer = via_tag
insulator.layer = insulator_tag
substrate.layer=substrate_tag # add layer tag.
m1.layer = m1_tag
model.rendering_options['DisplayColorByLayer']= true # colors each layer
end