Automatically load a component.
-
To turn the tool "Interact with the Dynamic Component", it is very easy:
result = Sketchup.send_action 50167
As against the "Component Options" is much more difficult!
Someone has a solution?
Thank you
-
@tig said:
Please don't capitalize code inappropriately, it can break it.
Sorry, this is due to my translator and my lack of attention!
I edited several of my messages to correct these silly mistakes.@tig said:
Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers))
Wooow gorgeous, this code it works great!
How did you find this solution?
I have some beginner questions:
1 - How to activate several actions if a condition is met?
Example:
if new_layer.visible? puts new_layer.visible = false # action 1A puts Sketchup.send_action "selectSelectionTool;" # action 2A elsif puts new_layer.visible = true # action 1B puts Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers)) # action 2B end
Two "puts", it does not work!
Is there a way to acomplire both A shares, if the layer is visible or both B shares, otherwise?2 - How to check if a tool is selected?
For example :
Sketchup.active_model.select_tool (DCInteractTool.new ($ dc_observers))
3 - How to check if an " Existing " layer is visible?
For example:
The layer, "Click-Window 3D".
In advance, thank you for your answers.
-
- I messed on until I found some DC related code that worked - it was NOT simple...
- Try this alternative...
puts vis = new_layer.visible? if vis # 'true' = visible so we switch it off... new_layer.visible = false # action 1A puts Sketchup.send_action("selectSelectionTool;") # action 2A elsif # 'false' = not visible so we switch it on... new_layer.visible = true # action 1B puts Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers)) # action 2B end
It should return true OR false, then toggle the layer's visibility on/off, and exit to the select-tool or interact-tool as appropriate.
2. You could get the active_tool... BUT if it's Ruby based, just that - not it's name...
3.layer = Sketchup.active_model.layers.add("Click-Window 3D")
setslayer
to be the layer of that given name, and it makes it IF it doesn't already exist.
Then use
vis = layer.visible?
which as we have seen in 1. abovevis
returns true or false... -
@tig said:
puts vis = new_layer.visible?
if vis # 'true' = visible so we switch it off...
 new_layer.visible = false # action 1A
 puts Sketchup.send_action("selectSelectionTool:") # action 2A
elsif # 'false' = not visible so we switch it on...
 new_layer.visible = true # action 1B
 puts Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers)) # action 2B
endTIG thank you, your code works very well!
Out of curiosity, I tested variants to see if I could simplify your example.
I finally found it:
if new_layer.visible? new_layer.visible = false Sketchup.send_action("selectSelectionTool;") elsif new_layer.visible = true Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers)) end
The error in my first example is:
puts new_layer.visible = false
You just had to remove the "puts", which does not work with "=".
Through your example, I found my main mistake.
1.For another tool, such as "Move", how to tell if it is selected?
2.Even if the existing layer is, we are forced to use the "layers.add" method, to know whether the layer is visible?
3.If the layer exists, layers.add replaces the layer or it is ignored?
Come on we will move to supperior level.
In the sample code below, we will add 2 attributes to a component box.
The first defined a color for the "Material" attribute.
The second adds a fonctione "REDRAW" to attribute "Onclick".
Here is the example GIF:
Here's the code:
UI.menu("Plugins").add_item('MyBox Red') { sang = Sketchup.active_model sang_definition = sang.definitions sang_def = sang_definition['box'] sang_def.set_attribute 'dynamic_attributes','material', 'red' sang_def.set_attribute 'dynamic_attributes','_material_formula', '"red"' sang_def.set_attribute 'dynamic_attributes','onclick', 'REDRAW()' sang_def.set_attribute 'dynamic_attributes','_onclick_formula', '"REDRAW()"' dcs = $dc_observers.get_latest_class dcs.redraw_with_undo(sang_def) }
The "REDRAW" in OnClick, is the best way I've found to redraw the attributes of my component.
To redraw a dynamic component in ruby, I found this code written by DanRathbun in this forum:
def recalc_dc(inst) return nil unless defined?(DCObservers) if inst.is_a?(Sketchup;;ComponentInstance) && inst.attribute_dictionary('dynamic_attributes') if @dc_observers.nil? ObjectSpace.each_object(DCObservers) {|o| break @dc_observers = o } end return nil unless @dc_observers.respond_to?(;get_latest_class) latest_dc_class = @dc_observers.get_latest_class return nil unless latest_dc_class.respond_to?(;redraw_with_undo) latest_dc_class.redraw_with_undo(inst) end nil # (there is no particular return value) end
4.How to use the code of DanRathbunwith my example?
5. How to integrate my code in a method?
I tried this:
def Mybox sang = Sketchup.active_model sang_definition = sang.definitions sang_def = sang_definition['box'] sang_def.set_attribute 'dynamic_attributes','material', 'red' sang_def.set_attribute 'dynamic_attributes','_material_formula', '"red"' sang_def.set_attribute 'dynamic_attributes','onclick', 'REDRAW()' sang_def.set_attribute 'dynamic_attributes','_onclick_formula', '"REDRAW()"' dcs = $dc_observers.get_latest_class dcs.redraw_with_undo(sang_def) end
But it does not work!
Thank you in advance for your help.
PS: We are on a completely different subject, is it a good idea to open a new topic?
-
-
The active_tool etc should spot 'Move', but why do you need to find it ?
-
You do not need to use the
layer = layers.add("layer_name")
method, BUT it is a simple way to either add a new layer by that name, and get a reference to it, OR, if it already exists then it returns a reference to that existing layer... -
See 2. above - that does NOT 'replace' the layer, it simply returns a reference to that layer by name - either if it's newly made, or it's preexisting...
-
-
@tig said:
- Le active_tool etc devrait repérer 'Move', mais pourquoi avez - vous besoin de le trouver?
For my general culture ruby script.
I have the same question but an icon from my toolbar:
I need to check or uncheck a icon!
1. If the icon in "command18" and is checked, the icon "command17" is automatically unchecked.
We see the example in the SketchUp toolbar "styles".
2. I want to check the icon of the "command1" with the first click, then uncheck the 2nd click. And so on ...
Here is my code:
command17 = UI;;Command.new("Opens Settings Panel"){ CW_Pro_mm.panel } command17.small_icon = File.join(@@path_to_resources,"add_17.png") command17.large_icon = File.join(@@path_to_resources,"add_17.png") command17.tooltip = %Q(Opens Settings Panel) command17.status_bar_text = %Q(Opens Settings Panel) command17.menu_text = %Q(Opens Settings Panel) command17.set_validation_proc { if Sketchup.active_model # ..... ??? MF_ENABLED else MF_CHECKED end }
Thank you for your help.
David
-
I'm looking everywhere, I can not find a solution.
Like my icons have no name, should I use the method in my code?
By exeple:
if Sketchup.active_model.selection.panel?
Reminder, I wish to know the code to verify if my icon " command17 " which call the " panel " method, is "on" or "off"?
It is certainly not difficult, but I find the answer anywhere.
I need your helps.
Thank you
-
You are muddling up functions and methods again.
The 'selection
' has no method.panel?
I don't quite understand your terminology or what it is you hope to achieve.
If you have a toolbar with let's say two buttons and you want only one to be enabled at a time...
You have already found theset_validation_proc
method...
http://www.sketchup.com/intl/en/developer/docs/ourdoc/command#set_validation_proc
In the example in the API usage it checks:
if Sketchup.active_model.selection.length == 0
i.e. that nothing is selected.
If so, then it disables that command, otherwise it is enabled...
This works in menus and toolbars alike...To make a working example of this make your two commands and add
set_validation_proc
code to each command, make the test a simple one like if something is selected in the model [as in the API example] but reverse the logic in the second command...
there are several ways to do that...
Theif
becomesunless
orif !
orif not
, OR change the test from== 0
to!= 0
This way your menu/toolbar buttons are enabled/disabled depending on a selection...
You can of course make lots of different tests, what is it you need ?? -
I do not want to enable or disable a button, depending on a selection in the model.
I want to enable or disable a button, depending on the selection of another button.
Example Image:
-
You have started another thread and I've replied there.
I know you do not want to trigger it depending on a selection - I just used that as an example...
In the example you show the other buttons are not disabled.
When one is active it's shown 'depressed', when another is click that takes the focus and appears depressed.
It the other buttons were actually 'disabled', then they would not be clickable and you'd never get out of the first clicked button's state !
Advertisement