Auto create 2D floor

How can I generate a 2D floor depending on components width and length using Ruby API

Something like following:

Any suggestion?

I’m hardly the right person to give suggestions but I figured I’d at least give you some food for thought.

The first potential solution to come to my head was to create circles around the bottom of each component and then erase all but the outer loop.

Here’s the code to do that:

# select each entity you want a circle made around and then put this in the ruby console
def create_circles(ent, ss)
  ss.each do |group|
    bb = group.bounds
    center = bb.center
    center.z = 0
    radius = Math.sqrt(bb.width ** 2 + bb.height ** 2) / 2
    edges = ent.add_circle center, Z_AXIS, radius
    edges.each{|edge| edge.find_faces}
  end
  ent.intersect_with(false, IDENTITY, ent,
                     IDENTITY, false, ent.grep(Sketchup::Drawingelement) - ent.grep(Sketchup::Group) - ent.grep(Sketchup::ComponentInstance))
end

def delete_inner_loops(ent)
  ent.grep(Sketchup::Edge).each do |edge|
    next if edge.deleted?
    
    unless edge.faces.length == 1
      edge.erase!
    end
    
  end
end

mod = Sketchup.active_model
ent = mod.entities
ss = mod.selection

mod.start_operation('Create Circle', true)
create_circles(ent, ss)
delete_inner_loops(ent)
mod.commit_operation

This isn’t going to give you the exactly look you’re going for(if it even works) but it’s a start.

EDIT: I just now tested it and it seems to work alright. looks a little more goofy than I had imagined… I think I might have made an incorrect assumption somewhere. Anyway here’s what the test looked like.

Before I run the script:

After:
image

1 Like

Thanks a lot @gnebster

Is there a way of creating rectangles with rounded corners instead of circles?

Thank you again

I meant using Ruby API

Of course there is. You write a method to create 4 arcs connected by 4 edges. Lastly (to fill the loop with a face) you call #find_faces() upon one of the edges in this edge loop. (Note that arcs are really a collection of joined edges.)

I do recall a topic here on creating rounded rectangles. Ah, … here it is …