Button that creates a group and then a component

this routine happens a lot here at work, create a group and then create a component, is it possible to create a button that I select the objects and when clicking on the button, it creates a group and then create a component of that group?

Do you mean you want a component, with a nested group, containing the entities you select ?

1 Like

We work with dynamic components. We have a large volume here.

Before creating a component, we always create a group. So it is an action that we repeat a lot. I want to switch to a button that does this in one click, it is better than two shortcuts, or use the right mouse button.

So I wanted to select an object,
Create a group
Create a component

Why not create the component directly from the selected geometry?

I don’t understand the purpose of creating a group first.

What does that add to the process?

1 Like

Try something like …

# encoding: UTF-8

module Cabarsa
  module WrapSelectionForDC

    extend self

    def wrap_selection(ents)
      actents = Sketchup.active_model.active_entities
      grp_inner = actents.add_group(ents)
      grp_outer = actents.add_group(grp_inner)
      grp_outer.to_component
    end
    
    if !@loaded
      @loaded = true
      
      wrap_cmd ||= UI::Command.new("Wrap selection for DC") {
        sel = Sketchup.active_model.selection
        inst = wrap_selection(sel.to_a)
        sel.add(inst)
      }.set_validation_proc {
        Sketchup.active_model.selection.empty? ? MF_DISABLED | MF_GRAYED : MF_ENABLED
      }
      wrap_cmd.large_icon = "wrap_btn.png"
      wrap_cmd.small_icon = "wrap_btn.png"
      
      wrap_cmd.tooltip = "Wrap selection within group within component."
      wrap_cmd.status_bar_text = "Wrap selection within group within component."
      wrap_cmd.menu_text = "Wrap selection for DC"
      toolbar = UI::Toolbar.new("Cabarsa")
      toolbar.add_item(wrap_cmd)
      toolbar.show

    end
  
  end
end
1 Like

Added a sel.add(inst) statement to the command proc so it puts the new component instance into the selection after wrapping.

perfect, that was it !!!

thks very much

1 Like

You’ll have to make the button image yourself.

You can also add statements to name the groups and the component definition if you like.

hello @DanRathbun , so I need to make several buttons.
o First you helped, create the component, ok

I need to create several other buttons on the bar, which add attributes to the component that is selected.
But I have several problems:
-this second button, only works once, if you try another component, it does nothing.
-When I add these attributes, I need to redraw the component for the formula to work.

and is giving a bug when creating an attribute with “_” in the name,
Attr_def.set_attribute “dynamic_attributes”, “c_mn”, “”

when I try to put a value in the created c_mn field, it simply automatic creates another field named “c” and places the value in it. Screenshot_39

how do I put an attribute with that name “a_bc” without the bug happening?

module modelagem
 
  module WrapSelectionForDC

    extend self

    def wrap_selection(ents)
      actents = Sketchup.active_model.active_entities
      grp_inner = actents.add_group(ents)
      grp_dados = grp_inner.to_component
      grp_atrib = grp_dados.definition
      
      grp_atrib.set_attribute "dynamic_attributes","_lengthunits","CENTIMETERS"
      grp_atrib.set_attribute "dynamic_attributes","_name","camada"
      grp_atrib.set_attribute "dynamic_attributes","name",""
      grp_atrib.set_attribute "dynamic_attributes","description",""
            
      grp_atrib.set_attribute "dynamic_attributes","lenx",""
      grp_atrib.set_attribute "dynamic_attributes","leny",""
      grp_atrib.set_attribute "dynamic_attributes","lenz",""
      
      grp_atrib.set_attribute "dynamic_attributes","x",""
      grp_atrib.set_attribute "dynamic_attributes","y",""
      grp_atrib.set_attribute "dynamic_attributes","z",""
      
      grp_atrib.set_attribute "dynamic_attributes","x",""
      grp_atrib.set_attribute "dynamic_attributes","y",""
      grp_atrib.set_attribute "dynamic_attributes","z",""
      
      grp_atrib.set_attribute "dynamic_attributes","material",""
      
    end
    
    if !@loaded
      @loaded = true
      
    wrap_cmd ||= UI::Command.new("Wrap selection for DC") {
        sel = Sketchup.active_model.selection
        inst = wrap_selection(sel.to_a)
        sel.add(inst)
      }.set_validation_proc {
        Sketchup.active_model.selection.empty? ? MF_DISABLED | MF_GRAYED : MF_ENABLED
      }
      wrap_cmd.large_icon = "wrap_btn.png"
      wrap_cmd.small_icon = "wrap_btn.png"
      
      wrap_cmd.tooltip = "Wrap selection within group within component."
      wrap_cmd.status_bar_text = "Wrap selection within group within component."
      wrap_cmd.menu_text = "Wrap selection for DC"
      toolbar = UI::Toolbar.new("Modelagem")
      toolbar.add_item(wrap_cmd)
      toolbar.show

    end
  
  end
 
 
 module Atribmaxmin

    extend self

    def max_min(ents)
       
       
      atrib = Sketchup.active_model.entities[0]
      atrib_def = atrib.definition
      
      atrib_def.set_attribute "dynamic_attributes","a_bc",""
      atrib_def.set_attribute "dynamic_attributes","a_cd",""
      
      atrib_def.set_attribute "dynamic_attributes","a_mn",""
      atrib_def.set_attribute "dynamic_attributes","a_mx",""
      
      atrib_def.set_attribute "dynamic_attributes","bsx",""
      atrib_def.set_attribute "dynamic_attributes","_bsx_formula","LenX" 
      
    end
    
    if !@loaded
      @loaded = true
      
    wrap_cmd ||= UI::Command.new("Atributos Max Min") {
        sel = Sketchup.active_model.selection
        inst = max_min(sel.to_a)
        sel.add(inst)
      }.set_validation_proc {
        Sketchup.active_model.selection.empty? ? MF_DISABLED | MF_GRAYED : MF_ENABLED
      }
      wrap_cmd.large_icon = "wrap_btn.png"
      wrap_cmd.small_icon = "wrap_btn.png"
      
      wrap_cmd.tooltip = "Wrap selection within group within component."
      wrap_cmd.status_bar_text = "Wrap selection within group within component."
      wrap_cmd.menu_text = "Wrap selection for DC"
      toolbar = UI::Toolbar.new("Modelagem")
      toolbar.add_item(wrap_cmd)
      toolbar.show

    end
  
  end

I only used a local reference to point at the toolbar object.
So you need to do this all from within the same extension submodule.

Each button will need it’s own image file, and it’s own UI::Command object that fires it’s own command method.

You kinda did this, but in a separate top level module (The only toplevel module you should use is your unique company or author namespace module. All of your extensions must be defined inside this namespace module.)

There is another topic on redrawing DC instances.

… but I’ll paste the code into a new example (below.)

DC attributes that begin with "_" have a special meaning. The end user never sees these attributes as they are really variables used by a DC property and the DC code. (I use the word “property” to mean one of the options that end users can see and change.)

There are “secret” DC attributes you are not adding to the component definition.
Also, the instance must get a small DC attribute dictionary.

More on this in the next post with an example …

Rather than use a zillion literal “dynamic_attributes” strings, set a reference to the name once, and reuse this …

# near top of submodule ...
DCDICT ||= "dynamic_attributes"

# Then later in the code ...
obj.set_attribute(DCDICT, "c_mn", "")

Many DC attributes are kept in the definition’s “dynamic_attributes” dictionary, not the instance’s “dynamic_attributes” dictionary. There are only specific situations where the attributes from the instance’s “dynamic_attributes” dictionary are used.

Also nested dynamic groups always have the DC attributes attached to the group instance.

So the general rule is to first check the instance for an attribute, and if it does not exist (returns nil) then check the definition for the DC attribute.

The DC definition will have all the default values for all the property attributes. The instance will only have an property attribute if it differs from the default value in the definition.

1 Like

… new example in progress. Please be patient …

1 Like

@DanRathbun you are a genius.
thank you very much for all the support, attention and patience you show in answering all these questions.
Thank you very much for your generosity and for your time.
God bless you

1 Like

Hey Dan, how are you?
where do I find all possible attributes in a component?
= parent
= shell
this things? I never saw a place talking about parent for example.

You will need to go to the Extension Warehouse and get Aerilius’ Attribute Inspector extension, so you can interrogate attribute dictionaries.


I am not going to be able to write or post open code that accesses the DC classes or objects.
It is too complex a job, and it is a Pro only feature, and the DC code is proprietary. So any library would need to be encrypted. But it’s just too big a job. It would take several weeks to a month.

I would suggest perhaps looking at Curic Studio and his new Curic DCUI.

1 Like

Hey Dan, you still do this? =D

what I meant is that some commands we use to make formulas in the components, I don’t know where people found this … the “parent” for example, which references the component above, that kind of thing, doesn’t have in any place?

Bookmark the DC Resource lists …

Yea, I cannot find it in the DC User Guide either.

1 Like

I don’t remember where or how I learned of =parent! because it was 12 years ago, but there is a tutorial that explains it.

It’s Paul Thompson’s DC Tutorial, lesson 6 …

Now, when you open “Component Options”, you should see a menu named “Colour” where we can select a colour (and click Apply). Of course, it doesn’t work at the moment because we haven’t linked the Sub1 material to the custom attribute. So enter this formula in the Material attribute of Sub1.

=Parent!SubMaterial

What’s that? Shouldn’t it be “DC1!SubMaterial”? Well it could be but this is a little tip I thought I’d share. If you use “Parent” in any subcomponent, it always refer to the component or group which contains it – useful if you have a set of formula snippets you can use in any model without changing the names. (Note there is no equivalent “Child” term since a parent can have many children but a child can have only one parent (in the SketchUp world, at least).

1 Like

Thats nice, thks!!!
o = parent! helps development a lot.
I’m curious if there are other things hidden like that, the other day I saw someone using = shell. I don’t know what you do either.

I don’t want to bother you anymore, but you managed to solve it:
“… New example in progress. Please be patient…”

Yes I need to clean up that example … been busy the past few days.

1 Like

Hey, Dan, how are you?
Can I create a button that positions the component’s axis in the lower left rear corner?