Redraw a dynamic component
-
Without us seeing your code it's impossible to say what raises your error message.
Perhaps adding an extra test before testing for the DC attribute dictionary [] entry
next unless i.definition.attribute_dictionaries
as the first test will stop it failing, since the DC dictionary can't be tested for without the existence of the attribute dictionaries entry. -
You can create a new dictionary thus
some_definition.set_attribute("dynamic_attributes", "test", "1") p some_definition.attribute_dictionaries["dynamic_attributes"]
-
**Sorry TIG for not having specified the method used.
I tried these 2 methods:
Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i| next unless i.definition.attribute_dictionaries["dynamic_attributes"] i.set_attribute 'dynamic_attributes','teste', '1' $dc_observers.get_latest_class.redraw_with_undo(i) }
Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i| next unless i.definition.attribute_dictionaries["dynamic_attributes"] i.set_attribute("dynamic_attributes", "test", "1") i.attribute_dictionaries["dynamic_attributes"] $dc_observers.get_latest_class.redraw_with_undo(i) }
None of the 2 solutions work on a component without dynamic attributes.
@unknownuser said:
Error: #<NoMethodError: undefined method
[]' for nil:NilClass> <main>:1:in
block in <main>'
<main>:ineach' <main>:in
grep'
<main>:in<main>' -e:1:in
eval'
nilBut these 2 solutions work on components already have dynamic attributes.**
-
The attributes belong to the instance's defintion NOT the instance.
You mess up trying to set things to the instance, NOT the definition !
Try substituting something like...i.definition.set_attribute('dynamic_attributes', 'teste', '1') $dc_observers.get_latest_class.redraw_with_undo(i)
Please learn the difference between a definition and its instance !!!!!
-
This still does not work for components without attributes:
Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i| next unless i.definition.attribute_dictionaries["dynamic_attributes"] i.definition.set_attribute('dynamic_attributes', 'teste', '1') $dc_observers.get_latest_class.redraw_with_undo(i) }
-
WTF !
You really must learn basic principles...
consider this:Sketchup.active_model.selection.grep(Sketchup;;ComponentInstance){|i| ###next unless i.definition.attribute_dictionaries["dynamic_attributes"] i.definition.set_attribute('dynamic_attributes', 'teste', '1') $dc_observers.get_latest_class.redraw_with_undo(i) }
The added
###
skips the check for the dictionary [ which you do NOT need ! ]... so IF a definition does NOT have that dictionary set then it makes it anyway !!!!!! -
**OK TIG, I understand!
The Ruby is very wide and has many instructions such as:
!,&&,||,and,box,else,elsif,eql?,equal,if,then,not,gold,require,unless,wheen,true, false...
I had not yet read "unless".
I realize that it takes many years of Ruby to totally understand him master it.
As I learn according to my needs, this undoubtedly slows down my general understanding of ruby.
Promised, next time I would make more effort with your examples before publishing it does not work.
Ps: Your solution works wonderfully TIG
Thanks a lot for your help.
David**
-
**Hello,
I now want to refresh the visible dynamic components.
If not, the answer must be "nil".
The methde below works for components that are not nested:
m=Sketchup.active_model; s=m.selection; m.definitions.each{|d|s.add d.instances if d.name=~/#{"BOUTON"}/} s.grep(Sketchup;;ComponentInstance){|i| next if i.hidden? $dc_observers.get_latest_class.redraw_with_undo(i) }
Oddly if the components with the "BOUTON" definitions, are nested in another component, they will be considered as visible even if they are hidden.
Do you know what is the problem?
Thank you**
-
Please stop adding entities into the selection.
What happens if it already contains some selected entities ?
You haven't cleared it ??
Also these additions might not be in the current context and cause various issue down the line...Simply create an empty array and add entities to that if they meet a certain test.
e.g.def handlesCI(ents) array = [] ents.grep(Sketchup;;ComponentInstance).each{|e| ### collect only visible instances next unless e.visible? array << e if e.definition.name =~ /#{"HANDLES"}/ handlesCI(e.definition.entities) ### repeat for nested entities in visibles } array.each{|i| ### process this array of instances $dc_observers.get_latest_class.redraw_with_undo(i) } end
-
**Your method works TIG, but it does not keep the "Handles" definitions selected.
The goal is to update all Invisible Handles, then select any definition "Handles" without visibility restrictions.**
-
**Here I am in a paradox!
If I apply your TIG method, the handles selected in Method 2 will also be redrawn.
def methodeTIG(ents) sel = Sketchup.active_model.selection array = [] ents.grep(Sketchup;;ComponentInstance).each{|e| ### collect only visible instances next unless e.visible? array << e if e.definition.name =~ /#{"HANDLES"}/ methodeTIG(e.definition.entities) ### repeat for nested entities in visibles } array.each{|i| ### process this array of instances $dc_observers.get_latest_class.redraw_with_undo(i) } end def methode2(ents) sel = Sketchup.active_model.selection ents.each do |e| if e.is_a? Sketchup;;ComponentInstance sel.add e if e.definition.name=~/#{"HANDLES"}/ methode2(e.definition.entities) end end end def methode_end methodeTIG(instances) methode2(instances) ### The handles of this method are drawn by the methodTIG! end
How can I avoid this?
Thank you**
-
**I solved the problem of my previous post!
Now, I'm facing another problem that is much more difficult to solve.
The code below adds an attribute to all the "Handles" definitions that are visible in the components being selected.
def handlesCI(ents) sel = Sketchup.active_model.selection ents.each do |e| if e.is_a? Sketchup;;ComponentInstance sel.add e if e.definition.name=~/#{"HANDLES"}/ handlesCI(e.definition.entities) sel.grep(Sketchup;;ComponentInstance){|i| next unless i.visible? i.set_attribute 'dynamic_attributes','attribut', '1' } end end end
This method does not work to refresh only visible components.
def handlesCI(ents) sel = Sketchup.active_model.selection ents.each do |e| if e.is_a? Sketchup;;ComponentInstance sel.add e if e.definition.name=~/#{"HANDLES"}/ handlesCI(e.definition.entities) sel.grep(Sketchup;;ComponentInstance){|i| next unless i.visible? $dc_observers.get_latest_class.redraw_with_undo(i) } end end end
I need your help to understand the problem.
Thank you
David**
-
**I ended up bypassing the problem by selecting a different definition than "handles" in method 2.
I asked to select the IKEA definition, which corresponds to the furniture that contains the facades and the handle inside.
def ikeaCI(ents) sel = Sketchup.active_model.selection ents.each do |e| if e.is_a? Sketchup;;ComponentInstance sel.add e if e.definition.name=~/#{"IKEA"}/ ikeaCI(e.definition.entities) end end end
This is possible because I specified the definition of the highest components of the hierachie.
Is it possible to select the highest components of the hierarchy without specifying the name of that definition?
Thank you TIG for your method that allows me to redraw the visible handles. **
Advertisement