How to add group in this case

def create_rectangle
    if( @pts[0] != @pts[3] )
        face = Sketchup.active_model.active_entities.add_face @pts
        entities = model.active_entities
        group = entities.add_group(face)
    end
    self.reset
end

this group method doesn’t work

The problem might be that @pts isn’t acceptable to add_face, not with add_group. You should check that face is not nil.

1 Like

Your partial code is a mess.
Let’s us assume that create_rectangle gets called when something happens to an array of points you are collecting…
e.g. @pts[3] is defined as the final corner.
How do we get ‘here’ ?

Next,

face = Sketchup.active_model.active_entities.add_face(@pts)

Should work, provided that the 4 points are coplanar…
But then you make a reference to ‘model’ without having defined it !
This would be far more logical…

  entities = Sketchup.active_model.active_entities
  face = entities.add_face(@pts)
  group = entities.add_group(face)

I think you are at it again ! not reading what you write…
Look at each line and see if it will work…
If something doesn’t work read the error-message.
Tell us the message if you don’t understand it…

Again making the group before adding the face means that the danger of merging with existing geometry is avoided…

  entities = Sketchup.active_model.active_entities
  group = entities.add_group()
  face = group.entities.add_face(@pts)

In this case a simple fix is possible - always assuming that @pts is getting made properly beforehand…

3 Likes

Commentators could make themselves familiar with the Trimble Team’s Rotated Rectangle Tool Example extension.
https://extensions.sketchup.com/extension/22a5ad0d-b612-4f51-8f48-f1f1b2d69575/rotated-rectangle-tool-example