Get access/..and write attributes only to selected components


#1

…i read a bunch of threads but still got no anser, would be glad if you could give me some assistance…

My aim is to select a couple of Components and Groups per Mous then process a ruby code that manipulates only the component_attributes only of the Components. Not selected entities / Components / Groups should not be manipulated.

First i wrote this but the attributes of all components in the model are beeing manipulated

modell = Sketchup.active_model
    sel = modell.selection
    entities = modell.active_entities
    # entitylist = aentities
    
    sel.each {|entity|
        if entity is_a? (Sketchup::Group)
            nil
        elsif entity.is_a? (Sketchup::ComponentInstance)
            entity.definition.set_attribute 'dynamic_attributes',
            'material', 'blue'
            entity.definition.set_attribute 'dynamic_attributes',
            '_material_formula', '"blue"'
            dcs = $dc_observers.get_latest_class
            dcs.redraw_with_undo(entity)
        end
    }

…but I want only the selected ones. Then I wrote this:

modell = Sketchup.active_model
    sel = modell.selection
    entities = modell.active_entities
    # entitylist = aentities
    
    sel.each {|entity|
        if entity is_a? (Sketchup::Group)
            nil
        elsif entity.is_a? (Sketchup::ComponentInstance)
            entity.definition.set_attribute 'dynamic_attributes',
            'material', 'blue'
            entity.definition.set_attribute 'dynamic_attributes',
            '_material_formula', '"blue"'
            dcs = $dc_observers.get_latest_class
            dcs.redraw_with_undo(entity)
        end
    }

but error undifined method “entity”

I read this:


and this:
http://www.sketchup.com/intl/en/developer/docs/ourdoc/entity
and the article from thomthom:

I still get no glue to it and would be glad about help

greets from southern bavaria
SPB


#2

I don’t see that happening - you are iterating over sel which is the model selection - which sounds like what is what you want? You only want to modify the selected components, right?

Can you provide full error please, with the backtrace?

Can you also upload a sample model where you see the unexpected behaviour when you run the snippet you posted?


#3

should be

entity.is_a?


#4

!Thank you 4 your ansers!
Sorry, I should have explained my developement intension more precise: My aim is to select an area of my model that contains a bunch of entities in extremely case all possible entities: components, of course, and the other stuff, groups, labels, lines, faces etc. BUT in doing so only the components (AND only the selected ones) should get modified / get attribute properties.
While formulating my anser I thougt about perhaps another approach.

  1. First make the (rougth selection) perhaps selection of the area of an upper floor of a woodframing house (posts,studs, girder, plates)
  2. Unselect all entitys that are NOT a Component (==> all other in 1. sleceted things)
  3. Assignt only to the selected components dynamic_attributes like name, coulour, material, etc…

How do I automatically unselect all other stuff (entities) exept the components within my afore done selection? / how to express in ruby?


#5

Uuh thats right. Thank you pcmoor. Now it works for my enclosed working file with this code

modell = Sketchup.active_model
    sel = modell.selection
    entities = modell.active_entities
    # entitylist = aentities
    
    sel.each {|entity|
        if entity.is_a? (Sketchup::Group)
            nil
        elsif entity.is_a? (Sketchup::ComponentInstance)
            entity.definition.set_attribute 'dynamic_attributes',
            'material', 'blue'
            entity.definition.set_attribute 'dynamic_attributes',
            '_material_formula', '"blue"'
            dcs = $dc_observers.get_latest_class
            dcs.redraw_with_undo(entity)
        end
    }

#6

Thank you ThomThom,
enclosed my workingfile with the better explained intension1602wf1.skp (1.4 MB)
also


#7

Oh - so what you are trying to do is figuring out which components in the selection is a Dynamic Component?

I though you only had selected the objects of interests and I couldn’t figure out why your snippet didn’t work as you expected. The image made it clearer what you intended.

model = Sketchup.active_model
model.selection.grep(Sketchup::ComponentInstance) { |entity|
    # This will check if there is an dictionary with the given name. The second
    # argument tell SU to not create it if it's missing.
    next unless entity.definition.attribute_dictionary('dynamic_attributes', false)
    # Now we can be sure the component has the DC dictionary.
    # NOTE: When you do this you end up creating multiple undo operations.
    #       If you plan on releasing this you should make sure you wrap the
    #       code up on model.start/commit_operation such that everything can be
    #       undone in one step. (Requirement for Extension Warehouse)
    entity.definition.set_attribute('dynamic_attributes', 'material', 'blue')
    entity.definition.set_attribute('dynamic_attributes', '_material_formula', '"blue"')
    dcs = $dc_observers.get_latest_class
    dcs.redraw_with_undo(entity)
}

#8

Wow didn’ know the .grep Method, is not containet in the sketchup ruby api
http://www.sketchup.com/intl/en/developer/docs/ourdoc/selection

Whith your code you brougtht me to my secend step where I wanted to alter or add some dynamic attributes. Thank you so far.

My initial intension was to first of all set some dynamic_attributes only to components that are part of /included in my “rought selection” (up to now they didnt have even some, and if they haves some they should be overriden).

Thanks


#9

Here is a screenshot to underständ


#10

That’s because it’s part of Ruby itself. You need to supplement the Ruby documentation:
Ruby 2.0 Core: http://ruby-doc.org/core-2.0.0/
Ruby 2.0 StdLib: http://ruby-doc.org/stdlib-2.0.0/

The SketchUp API docs only documents what the SketchUp API adds.


#11

Do not insert space character between method names and their parameters lists when surrounded by parenthesis:

WRONG:

object.method_call (param1,param2,param3)

CORRECT:

object.method_call(param1,param2,param3)

Otherwise these spaces can cause excessive warnings to be output to $stdout.


Only global Kernel and Object methods should be called without parenthesis around the parameter list. (These global methods are said to have “keyword status”.)


#12

Changing the attributes of a component definition, will change ALL of it’s component instances,… even those that are not selected.