I can not remove bottom circular face of a nut model

I used below code for creating a nut. It’s almost a success. But I can not remove bottom circular face of the nut model. Thank you in advance.

ruby
# *************************************************
model = Sketchup.active_model
entities = model.active_entities

# Parameters
outer_radius = 45.mm  # Radius of the circumscribed circle around the hexagon
inner_radius = outer_radius - 22.mm  # Wall thickness is 22 mm
height = 30.mm  # Height of the nut

# Create a new group
group = entities.add_group
group_entities = group.entities

# Center and vector for the base hexagon
center = [0, 0, 0]
vector = [0, 0, 1]

# Function to create hexagonal points
def hexagon_points(center, radius)
  points = []
  6.times do |i|
    angle = i * Math::PI / 3
    x = center[0] + radius * Math.cos(angle)
    y = center[1] + radius * Math.sin(angle)
    points << Geom::Point3d.new(x, y, center[2])
  end
  points
end

# Create the outer hexagon
outer_hex_points = hexagon_points(center, outer_radius)
outer_hex_face = group_entities.add_face(outer_hex_points)

# Create the inner circle for hollowing out
inner_circle = group_entities.add_circle(center, vector, inner_radius, 100)
inner_face = group_entities.add_face(inner_circle)

# Check if the inner face is created successfully and erase it
if inner_face
  inner_face.erase!
end

# Reverse the face orientation to ensure correct extrusion direction
outer_hex_face.reverse!

# Extrude the outer hex face to the specified height
outer_hex_face.pushpull(height)

# *************************************************
'''

![image|256x500](upload://tcR4ZVVzU6GYqy4N8bZBFukqhhJ.png)

Please edit your post and put three backticks on an otherwise empty line before your code and another one after it. As is, it gets processed as markup and a mess results!

Ie …


Try reversing the order of the pushpulls. Do the outer hex pushpull first, then draw the inner circle and pushpull it.

1 Like

Try the following, tested in SketchUp 2023 & 2024

model = Sketchup.active_model
entities = model.active_entities

# Parameters
outer_radius = 45.mm # Radius of the circumscribed circle around the hexagon
inner_radius = outer_radius - 22.mm # Wall thickness is 22 mm
height = 30.mm # Height of the nut

# Create a new group
group = entities.add_group
group_entities = group.entities

# Center and vector for the base hexagon
center = [0, 0, 0]
vector = [0, 0, 1]

# Function to create hexagonal points
def hexagon_points(center, radius)
  points = []
  6.times do |i|
    angle = i * Math::PI / 3
    x = center[0] + radius * Math.cos(angle)
    y = center[1] + radius * Math.sin(angle)
    points << Geom::Point3d.new(x, y, center[2])
  end
  points
end

# Create the outer hexagon
outer_hex_points = hexagon_points(center, outer_radius)
outer_hex_face = group_entities.add_face(outer_hex_points)

# Create the inner circle for hollowing out
inner_circle = group_entities.add_circle(center, vector, inner_radius, 100)

# Inner face has one loop, outer has two
inner_face = inner_circle.first.faces.find { |f| f.loops.length == 1 }
inner_face.erase! if inner_face

# Reverse the face orientation to ensure correct extrusion direction
outer_hex_face.reverse!

# Extrude the outer hex face to the specified height
outer_hex_face.pushpull(height)
1 Like

It solved my problem, as you can see in the picture below. Thank you, MSP_Greg. I appreciate it.

image

1 Like

Glad that helped.

Adding entities using the standard way (Sketchup::Entities#add_*) uses the SU inference engine, so adding edge ‘loops’ that contain other existing loops can do unexpected things with faces. Hence, the code to determine which face is the inner one.

It’s only available in SU 2022 and later, but you could also use Sketchup::EntitiesBuilder.

EDIT: I checked using Sketchup::EntitiesBuilder, and until its methods match Sketchup::Entities#add_*, it doesn’t really provide any benefit.

1 Like