Automatically load a component.
-
Also have a look at this section in the picture.
My suggestion is: add more
puts
statements into your code to quickly find the problems (yourself).Edit: where are you declaring
definition
anyway??
-
Sorry about the typo - substitute Dan's
.each{
correction.
I have edited the original post to suit !Also I used the reference '
definition
' BUT you then used 'component_definition
'
Please try to match such references when you are given 'generic' code...Why did you not follow my example more closely [except for the stupid typo!] ???
PS: @Kaas is actually wrong it IS
=
NOT==
because
if definition = definitions[...
With the=
test you are trying to set the reference 'definition
' to be pointing to something 'by name' indefinitions
- if that fails then you getnil
returned, so then theelse
part of the test runs...
A==
test is for 'equality' of two items, and not for testing if setting a reference works... -
I took note of all your comments.
This is not very important but the code, do not remove the "cube", if there is more than one.
elsif component_definition.instances.length> 1 component_definition.instances [1 .. -1] .each {| i | i.erase! } component_definition.instances instance = [0]
You know why?
Thank you
David
-
It's wrongly formed, reread what you have typed it does not make sense.
Try this...elsif component_definition.instances.length > 1 component_definition.instances[1..-1].each{|i| i.erase! } instance = component_definition.instances[0]
I am assuming that all of your references to the cube's definition now use 'component_definition' rather than 'definition' ?
You cannot copy / paste snippets of code in a different sequence and hope that it will somehow work.
The first part of your code is also carried over from your earlier version and does NOT do what I asked you to do.
It always loads the path, but you ought to check if the definition exists and then load it if not.
Try following my code more closely, and remove your earlier steps which pre-load the cube before checking if it's loaded ! -
@tig said:
...PS: @Kaas is actually wrong it IS
=
NOT==
because
if definition = definitions[...
With the=
test you are trying to set the reference 'definition
' to be pointing to something 'by name' indefinitions
- if that fails then you getnil
returned, so then theelse
part of the test runs...
A==
test is for 'equality' of two items, and not for testing if setting a reference works...Thanks TIG. Learned something here.
-
@tig said:
I am assuming that all of your references to the cube's definition now use 'component_definition' rather than 'definition' ?
I think yes :
def self.methode00 path = Sketchup.find_support_file("cube.skp", "Plugins/TesteCube/Components/") model = Sketchup.active_model definitions = model.definitions component_definition = definitions.load(path) point = Geom;;Point3d.new(0, 0, 0) transformation = Geom;;Transformation.new(point) if component_definition = definitions['cube'] else component_definition = definitions.load(path_to_cube.skp) end if component_definition.instances.length == 1 instance = component_definition.instances[0] elsif component_definition.instances.length > 1 component_definition.instances [1..-1] .each {| i | i.erase! } instance = component_definition.instances[0] else instance = model.entities.add_instance(component_definition, transformation) status = instance.locked = true layers = model.layers new_layer = layers.add "cube" new_layer.visible = false instance.layer = new_layer instance.locked = true instance.hidden = true end end
@tig said:
You cannot copy / paste snippets of code in a different sequence and hope that it will somehow work.
Sorry for silly mistakes, but i am just beginning to understand the logic of the ruby.
I'm a simple Carpenter by training, then learn the ruby is not easy for me.
I Thank You for your understanding and your indulgence.
David
-
The initial
component_definition = definitions.load(path)
is NOT needed.
remove it, and retry. -
@tig said:
The initial
component_definition = definitions.load(path)
is NOT needed.
remove it, and retry.If I delete this line, the cube is no longer imported.
Another area of research:
Is it possible to activate a tool SketchUp, with an icon from my toolbar?
I would like to activate the tool, "Interact with Dynamic Components" and the window of "Components Options".
How to change the value of an attribute of a dynamic component, by clicking on an icon?
Thank you in advance for your lights.
David
-
The reason it's not loading is that the
if...else...end
test fails because you have not edited it properly:
The line
component_definition = definitions.load(path_to_cube.skp)
should actually be
component_definition = definitions.load(path)
Of course you can do all of what you outline by setting up appropriate code.
e.g. if you have a selected DC instance then a button click could change an attribute and then refresh the DC, but all of this is complex - we already have enough issues getting the cube.skp loaded !
-
Do not be so pessimistic TIG, because thanks to you I made very big progress in Ruby.
Here is an example of evolution, thanks to the ruby script:
Notice that the "Control Panel", is the "Click-Window 3D" layer are imported along with the "Hung Windows".
Other advanced thanks to ruby, the "Control Panel" always positions façe the camera when you call.
I want to believe you that my requests are complicated, but I will not give up before trying.
You can at least guide me by giving me the methods to use?
And if you are generous as usual, a small sample code?
Thank you in advance.
David
-
Try this to open the Options dialog for the currently selected DC.
$dc_observers.get_latest_class.show_configure_dialog
Here are some other clues to update a DC after you change its attributes in Ruby...
With
dins
set to an array of DC instances...
dins.each{|di| $dc_observers.get_latest_class.redraw(din, false, false) if din && din.valid?}
Alternatively
dins.each{|sel| $dc_observers.get_latest_class.redraw_with_undo(din) if din && din.valid? }
DC attributes are normally attached to the DC instance's definition,although some are on the instance itself.
Assuming thatdin
is the DC instance...
dda = din.definition.attribute_dictionary("dynamic_attributes") ida = din.attribute_dictionary("dynamic_attributes")
To see these, when testing etc, use
dda.each_pair{|k, v| puts "#{k} = #{v}" }
and so on...To get a particular 'key' value use this format.
Note how 'built-in' attributes start with an _
puts dda["_name"]
To set a particular 'key' to a new value use this format.
First see original name...
puts dda["_name"] **dda["_name"]="Banane"** puts dda["_name"]
which should show the new name...BUT remember that you need to do a '
redraw
' - as explained above to see the changes and update the DC's Options etc.Also consider how attribute values are stored - as strings, with dimensions in inches etc...
-
@tig said:
$dc_observers.get_latest_class.show_configure_dialog
TIG big thank you, this code works perfectly.
I read on another forum, some character like "$" are refused by the bank of extensions and can lead to a rejection of the Plugin.
Is it true?
How close the window component options, with another icon?
I'm sorry to ask twisted questions, but your experience is a great help to me.
I can not activate the icon "Interact with Dynamic Component".
Yet I am the logical steps:
-
I select the tool "Interact with Dynamic Components".
-
I open the Ruby console and write this code:
= tools Sketchup.active_model.tools if tools.active_tool_id.equal? 0 puts "Tools collection is empty" else name = tools.active_tool_name end
The result is: "RubyTool" the name of the tool.
As a result I write the following code:
result = Sketchup.send_action "selectRubyTool:"
The result is: False!
If that means that "RubyTool" is not the right name.I so understand, that the solution is too hard to find for me!
Do you have the solution?
Thank you for explanations on changing the Dynamic Component with the ruby script.
I'll take my time to study in detail all the information you give me.
Thank you for your patience.
David
-
-
You can always write code that does not need to use 'global' variable reference - i.e. avoid the form
$myCompo
These globals are seen by and affect all Ruby code running, so it's not a good idea, as one day your code and someone else's code will clash - e.g. imagine if you used$x
, and changed its value in your script, then someone else's code that also ill-advisedly uses that same reference is messed up afterwards - so just don't go there !
There are several useful built-in Ruby globals like$:
etc that are useful.
SketchUp itself chose to break the rule with how it handles DC references.
If your code only uses the SketchUp and Ruby globals it should at least pass those EW tests...
However, it's not a certainty - if that is a reason appeal - I once wrote a method containing a Constant namedTEMP
pointing at a temporary folder location.
It was rejected by EW because it was reassigning a system Constant - of course it was not as it was within it's own module - so after an appeal they relented and allowed it - they had simply misread the code...A variable is only available in the method it is made - e.g.
x
ormyCompo
An instance variable is available in all methods within a module or those within one instance of a class - e.g.@x
or@myCompo
; so it's remembered across Module use, during that session.
A class variable is initialized outside of any methods within a class and is available in all methods in any use of that class, during that session - e.g.@@x
or@@myCompo
Another form of reference is a Constant, these must start with a Capital letter, often they are in all capitals, but it's not compulsory. You can reset the value of a Constant in code but it produces a Ruby Console warning message, there are ways of changing a Constant in code to avoid the message, but since a constant is set up to remain the same it seems perverse to do so. There are built-in Constants likeORIGIN
,X_AXIS
and so on, you can set up your own unchanging ones to be remembered across a module that session - e.g.IMAGES_FOLDER
,TEMP_FOLDER
etc.
To keep values per key across sessions you can write custom attributes to almost anything - from the model itself through collections like Layers and Materials, to elements like a definition, or individual instances etc.
This way you can remember the last user inputs for each model.
To remember the last user inputs for your tool globally across models consider usingSketchup.write_default()
and [ruby:3rngofgq]read_default()[/ruby:3rngofgq]
You can of course write data to a file and read it back later [like an ini file]...I gave you the code to open the DC Options dialog...
[ruby:3rngofgq]$dc_observers.get_latest_class.show_configure_dialog[/ruby:3rngofgq]
Using
[ruby:3rngofgq]$dc_observers.get_latest_class.methods.sort[/ruby:3rngofgq]
prints a long list of ALL methods accessed by that DC class's function...The obvious candidate for closing it is...
[ruby:3rngofgq]$dc_observers.get_latest_class.close_configure_dialog[/ruby:3rngofgq]Which does indeed work !
-
Please don't capitalize code inappropriately, it can break it.
Also never put a space between a module/class and its method.
So it's NOT:
$Dc_observers .get_latest_class.show_reporter_dialog
but
$dc_observers.get_latest_class.show_reporter_dialog
And also try to use
()
around passed arguments - with no intervening space...
NOT
Some_Module.some_method 1, 2, 3
use
Some_Module.some_method(1, 2, 3)
-
$dc_observers.get_latest_class.methods.sort
.!
.!=
.!~
.<=>
.==
.===
.=~
.id
.send
.apply_instance_cache
.apply_relative_move
.attribute_dictionary_to_json
.can_be_configured
.children_have_behaviors
.chop_quotes
.class
.clean_entity_name
.clean_for_json
.clean_for_xml
.clean_up_report_data
.clear_forced_config_values
.clear_highlight
.clear_instance_cache
.clone
.clone_attributes
.close_configure_dialog
.close_manager_dialog
.close_reporter_dialog
.configure_dialog_is_visible
.convert
.copy_number_of
.copyless_name
.define_singleton_method
.delete_all_attributes
.delete_attribute
.delete_cascading_attribute
.delete_onclick_state
.delete_template
.destroy_highlight_instance
.determine_movetool_behaviors
.display
.display_attribute?
.dup
.entity_to_json
.enum_for
.eql?
.equal?
.escape
.explode_if_3d_warehouse
.extend
.find_entity_by_sheet_name
.fix_float
.freeze
.frozen?
.generate_report
.generate_report_string
.get_attribute_access
.get_attribute_base_units
.get_attribute_error
.get_attribute_formlabel
.get_attribute_formula
.get_attribute_formulaunits
.get_attribute_label
.get_attribute_list
.get_attribute_options
.get_attribute_units
.get_attribute_value
.get_cascading_attribute
.get_classifier_data
.get_configure_dialog
.get_decimal_delimiter
.get_default_authoring_units
.get_default_units
.get_definition_attribute
.get_entity_formula_name
.get_entity_lengthunits
.get_forced_config_value
.get_formula_result
.get_instance_attribute
.get_instance_attribute_list
.get_instance_cache_list
.get_length_format
.get_length_unit
.get_live_value
.get_nested_attribute
.get_temp_path
.get_template_dictionary
.handle_place_component
.has_behaviors
.has_dc_attribute_dictionary
.has_forced_config_values
.has_instance_cache
.has_movetool_behaviors
.hash
.inspect
.instance_eval
.instance_exec
.instance_of?
.instance_variable_defined?
.instance_variable_get
.instance_variable_set
.instance_variables
.is_3d_warehouse
.is_a?
.is_free?
.is_mac
.is_online
.kind_of?
.make_unique_if_needed
.manager_dialog_is_visible
.method
.methods
.name_of_copy
.name_of_entity_is
.nil?
.object_id
.parse_attribute_path
.parse_formula
.parse_reference
.parse_subformula
.private_methods
.protected_methods
.public_method
.public_methods
.public_send
.pull_attribute_tree
.pull_information
.pull_selection_ids
.push_attribute_set
.query_to_hash
.read_templates
.redraw
.redraw_with_undo
.refresh_configure_dialog
.refresh_dialogs
.refresh_manager_dialog
.refresh_thumbnails
.remove_instance_variable
.rename_attribute
.replace_formula_references
.reporter_dialog_is_visible
.reset_last_highlight
.respond_to?
.run_all_formulas
.run_facearea_formulas
.run_formula
.save_configure_thumbnail
.save_report
.save_report_to_file
.second_if_empty
.second_if_nan
.send
.set_attribute
.set_attribute_error
.set_attribute_formula
.set_dialog_properties
.set_forced_config_value
.set_nested_attribute
.show_configure_dialog
.show_dialog
.show_dialog_orig
.show_manager_dialog
.show_reporter_dialog
.singleton_class
.singleton_methods
.store_instance_cache
.store_instance_cache_attribute
.store_nominal_size
.strip_operator
.swap_component
.taint
.tainted?
.tap
.to_enum
.to_json
.to_s
.translate
.trust
.unescape
.untaint
.untrust
.untrusted?
.update_last_sizes
.update_template
.url_escape
.url_unescape -
Sketchup.active_model.select_tool(DCInteractTool.new($dc_observers))
Perhaps ?
-
TIG thank you for having brought clarity!
I will try to pass the exam of the extension warehouse with the "$" symbol.
@tig said:
The obvious candidate for closing it is...
$dc_observers.get_latest_class.close_configure_dialogThe closing code of the window components options, works fine.
@tig said:
$dc_observers.get_latest_class.methods.sort
prints a long list of ALL methods accessed by that DC class's function...I tried all the methods, but none can activate the tool "Interact with Dynamic Components".
Do you know how to activate this tool?
For information, here the line of code to open the reports window:
$Dc_observers.get_latest_class.show_reporter_dialog
To see all of the methods, this code also works:
$Dc_observers.get_latest_class.methods
Thank you
-
Component Option
result = Sketchup.send_action 50168
Component Attribute
result = Sketchup.send_action 50170
Do not ask me how I found the ID, because you will not believe me.
Edit : I just noticed that the ID of the toolbar "Dynamic Component" is random and changes each computer.
I must again begin my research.
-
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.
Advertisement