Add_group ( how to do )

Hello, i’m starting ruby on sketchup
just tring to make a simple volume and convert it into a group

it doesn’t work… how to do? thanks for your help

def panne_delard(largeur,hauteur,pente,plat)
model=Sketchup.active_model
entities=model.entities

pt1 = Geom::Point3d.new(0,0,0)
pt3 = Geom::Point3d.new(largeur,0,0)
pt4 = Geom::Point3d.new(largeur,20.cm,0)
pt5 = Geom::Point3d.new(plat,hauteur,0)
pt6 = Geom::Point3d.new(0,hauteur,0)

entities.add_cpoint pt1
entities.add_cpoint pt3
entities.add_cpoint pt4
entities.add_cpoint pt5
entities.add_cpoint pt6
section_panne = entities.add_face pt1,pt3,pt4,pt5,pt6
section_panne.reverse!
panne=section_panne.pushpull 1.m

end

@vmsremy66

The following should do what you want.

entities = model.active_entities
group = entities.add_group panne

@yogesh
doesn’t work

here is my ruby console

here is my code

There are several problems:

  • pushpull returns a status indicator for the operation, not the entities created by the operation, so panne is nothing you can add to a group. That’s the cause of the error message.
  • you should create the group first then add and manipulate your geometry within the group’s entities collection (this order of operations is recommended in the API docs).
  • the line entities=model.active_entities likely doesn’t do what you expect. It returns the edit context (model, group’s entities, or component’s entities) currently open for edit in the model. So, the group you are creating will be nested in whatever context is open at the time you invoke your method.
1 Like

@slbaumgartner
thank you for your answer
it’s true i’ve read in the API to create an empty group and then to add the geometry inside.

the point is : i don’t find in the API or in samples how to add my geometry inside a group.

i will investigate

To consolidate the replies…

 def panne_delard(largeur, hauteur, pente, plat)
        model=Sketchup.active_model
        aents=model.active_entities
        group=aents.add_group()
        entities=group.entities
        pt1 = Geom::Point3d.new(0,0,0)
        # why no pt2 ??
        pt3 = Geom::Point3d.new(largeur,0,0)
        pt4 = Geom::Point3d.new(largeur,20.cm,0)
        pt5 = Geom::Point3d.new(plat,hauteur,0)
        pt6 = Geom::Point3d.new(0,hauteur,0)
        entities.add_cpoint(pt1)
        entities.add_cpoint(pt3)
        entities.add_cpoint(pt4)
        entities.add_cpoint(pt5)
        entities.add_cpoint(pt6)
        section_panne = entities.add_face(pt1, pt3, pt4, pt5, pt6)
        section_panne.reverse!
        section_panne.pushpull(1.m)
        # rest of the code...
    end

thanks @TIG

here is what i’ve just tried while you were writing your reply

and it works!

@TIG
going further…
is there somewhere a simple tutorial for creating an icon to call my panne_delard function with a window in which i can fill for the parameters ( without opening the ruby console ) ?

@TIG
i initially used pt2 but i deleted it because it wasn’t usefull.
but as it was on my paper sheet, i decided not to renumber the others points

my way of working… to avoid changes that can introduce bugs hard to fix ( in my experience )

See inputbox:

In your case something like:

prompts=['largeur', 'hauter', 'pente', 'plat']
values=[10.cm, 10.cm, 0.0, 0.0] # default values - you choose
pops=['', '', '', '']
title='panne_delard'
results=inputbox(prompts, values, pops, title)
return nil unless results # if user cancels
largeur, hauteur, pente, plat = results

I assume largeur and hauter are dimensions [aka ‘lengths’] ?
And if pente is an angle in degrees - to use that = pente.degrees, which turns it into radians for coding use…
Not sure what ‘plat’ represents ? If it’s Z ‘level’ use a ‘length’ like 0.0.cm ??

@TIG
Here is the result. pente is in percentage not in degrees ( wood carpentry usage )

A subtle “gotcha” for future reference:

SketchUp’s Ruby API is very aggressive about deleting empty groups. So, you should have something (typically a cpoint) ready before you create the group and add that something to the group’s entities immediately after creating the group. If it was purely a temporary for this purpose, you can erase the something later after you have added the real content. Doing it that way avoids any possibility that SU will kill the group before you get around to adding content.

So it’s:
values=[10.cm, 10.cm, 0.0, 0.cm] # default values - you choose
The 3rd value % needs to be a ‘float’, the rest are ‘lengths’.

@TIG
inputbox works perfectly for my use

thank you very much

and finally, i’ve just created my own toolbar with Extension | SketchUp Extension Warehouse
for calling my ruby functions