Redraw a dynamic component

Redraw a dynamic component

Postby TNTDAVID » Sun Oct 22, 2017 3:51 pm

Hello, :D

The code below redraws the instance 0 named with the definition "BOX".

Code: Select all
model = Sketchup.active_model 
model_definition = model.definitions
model_def = model_definition['BOX']
$dc_observers.get_latest_class.redraw_with_undo(model_def.instances[0])


How to redraw all the instances being selected without taking their definitions into account?

Thank you

David
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Sun Oct 22, 2017 3:57 pm

Something like
Code: Select all
Sketchup.active_model.selection.grep(Sketchup::ComponentInstance){|i|
  next unless i.definition.attribute_dictionaries["dynamic_attributes"]
  $dc_observers.get_latest_class.redraw_with_undo(i)
}
?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Sun Oct 22, 2017 4:01 pm

I get the following error message with your TIG example:

Error: #<ArgumentError: wrong number of arguments (1 for 0)>
<main>:2:in `attribute_dictionaries'
<main>:2:in `block in <main>'
<main>:1:in `each'
<main>:1:in `grep'
<main>:1:in `<main>'
-e:1:in `eval'
nil
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Sun Oct 22, 2017 4:05 pm

Sorry, I adjusted the () to []. I can't actual test this code as I type it !
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Sun Oct 22, 2017 6:09 pm

Thank you TIG! :D
This solution will give me even more possibilities in the future with the dynamic components. :thumb:
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TNTDAVID » Tue Oct 24, 2017 1:53 pm

I found with some tests that it is not possible to add dynamic attributes to neutral components.

The component must have at least an existing attribute for it to be possible to add attributes in ruby.

If not, we have this error message:

Error: #<NoMethodError: undefined method `[]' for nil:NilClass>
<main>:1:in `block in <main>'
<main>:in `each'
<main>:in `grep'
<main>:in `<main>'
-e:1:in `eval'
nil


The goal is certainly to protect the creation of dynamic components, which is only available in Pro version of SketchUp?
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Tue Oct 24, 2017 1:55 pm

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.
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TIG » Tue Oct 24, 2017 2:06 pm

You can create a new dictionary thus
Code: Select all
some_definition.set_attribute("dynamic_attributes", "test", "1")
p some_definition.attribute_dictionaries["dynamic_attributes"]
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Tue Oct 24, 2017 8:58 pm

Sorry TIG for not having specified the method used.

I tried these 2 methods:

Code: Select all
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)
}





Code: Select all
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.

Error: #<NoMethodError: undefined method `[]' for nil:NilClass>
<main>:1:in `block in <main>'
<main>:in `each'
<main>:in `grep'
<main>:in `<main>'
-e:1:in `eval'
nil


But these 2 solutions work on components already have dynamic attributes.
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Tue Oct 24, 2017 9:26 pm

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...
Code: Select all
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 !!!!!
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Tue Oct 24, 2017 9:56 pm

This still does not work for components without attributes:

Code: Select all
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)
}
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Tue Oct 24, 2017 10:34 pm

WTF !
You really must learn basic principles...
consider this:
Code: Select all
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 !!!!!!
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Wed Oct 25, 2017 5:53 pm

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
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TNTDAVID » Thu Oct 26, 2017 4:53 pm

Hello, :enlight:

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:

Code: Select all
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
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TNTDAVID » Sat Oct 28, 2017 2:54 pm


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.

Code: Select all
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.

Code: Select all
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
0
Last edited by TNTDAVID on Sun Oct 29, 2017 3:01 pm, edited 1 time in total.
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TIG » Sat Oct 28, 2017 8:41 pm

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.
Code: Select all
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
0
TIG
User avatar
TIG 
Global Moderator
 

Re: Redraw a dynamic component

Postby TNTDAVID » Sun Oct 29, 2017 3:17 am

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.
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TNTDAVID » Sun Oct 29, 2017 1:33 pm

Here I am in a paradox!

If I apply your TIG method, the handles selected in Method 2 will also be redrawn.

Code: Select all
 
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
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

Re: Redraw a dynamic component

Postby TNTDAVID » Mon Oct 30, 2017 4:51 pm

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.

Code: Select all
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. :thumb:
0
* Nouveau !!!
Découvrez notre nouveau Plugin Click-Cuisine 2, pour créer vos cuisines 3D !
User avatar
TNTDAVID 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: benj59380, neviath and 21 guests

Visit our sponsors: