…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
}
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?
!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.
First make the (rougth selection) perhaps selection of the area of an upper floor of a woodframing house (posts,studs, girder, plates)
Unselect all entitys that are NOT a Component (==> all other in 1. sleceted things)
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?
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)
}
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).
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”.)