How to delete the selected surface?

I want to delete my selected side, but I tried several ways but it doesn’t work.

To delete the selected entities:

model = Sketchup.active_model
model.active_entities.erase_entities(model.selection)
2 Likes

You should be more precise by wording… in SketchUp and its Ruby API the surface can be a collection of faces like e.g. a cylinder…

The face has two “sides”: the “front” and the “back”. The sentence above can also be interpreted as meaning that you “only” want to remove the material from one side of the face. If this is your case, let me know…

Never mind, I assuming you want to delete a face(s) completely.

Next time would be nice to see what you have been tried, and how that “doesn’t work”.


To delete the face(s) you can try:

The Entities #erase_entities method is used to erase one or more entities from the model.

The Drawingelement #erase! method is used to erase an element from the model.

These are giving different result if you select e.g. a surface in cylinder for deletion.

I’ll leave it up to you to discover the difference. (Draw a cylinder select everything, then try the different methods in a snippet below)

def delete_faces
  model = Sketchup.active_model
  entities = model.active_entities
  selection = model.selection
  #do nothing if no selection
  return if selection.empty?
  
  # One - fast - method to collect the faces in selection
  faces = selection.grep(Sketchup::Face)
  
  # Other method to select the faces in selection
  # faces = selection.select{|ent| ent.is_a?(Sketchup::Face)}
  
  puts "There are #{faces.size} face(s) to delete."
  #do nothing if no faces in selection
  return unless faces.first
  
  # wrap into one undo operation
  model.start_operation("Delete Faces", true)
    #Method 1 to delete/erase
    entities.erase_entities(faces)
    
    # Or mehtod 2
    # faces.each{|face| face.erase! if face.valid?}
    
    # Or mehtod 2 alternative syntax, no validity check:
    # faces.each(&:erase!)
    #
  model.commit_operation
end
delete_faces
2 Likes

image
Let’s select a cilinder surface:

result:
image


BUT:

model = Sketchup.active_model
model.selection.to_a.each(&:erase!)

result:
image

1 Like

Thanks!

Thank you!

Also, erase_entities will be faster than calling erase! in a collection of entities.

1 Like

Sure. But still, with a different result… :wink:

The main thing besides leaving the edges behind, is that erase_entities creates a single undo operation.