Looking to make a script, but dunno how.
-
No way Chris, that would be waste of resources. Like if (instead of working hard on your next, cool plugin) you dealt with stupid spammers and kept banning them.
everyone does his own job. Mine in this subforum is to be amazed and enjoy every new gem.
-
are you looking for a script to do what you ask for, or are you asking for a tutorial on how to do it?
-
Either. Anyway, here's what I have now. I already have a problem I can't figure out-- I can't get both IF statements to work in the second def. It will skip the second if statement as of now... dunno why.
require 'sketchup.rb' module Convert_components_randomly def Convert_components_randomly.choosebase base_entities = Sketchup.active_model.selection if base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end end def Convert_components_randomly.choosetarget target_entities = Sketchup.active_model.selection if target_entities.empty? results = UI.messagebox "Please select target groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end if Convert_components_randomly.choosebase.base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end end end #Convert_components_randomly module convert_components_randomly_menu = UI.menu("Plugins").add_submenu("Convert Components Randomly") convert_components_randomly_menu.add_item("Select base components") {Convert_components_randomly.choosebase} convert_components_randomly_menu.add_item("Select components to convert") {Convert_components_randomly.choosetarget}
-
The if statement is invalid:
if Convert_components_randomly.choosebase.base_entities.empty?
That is calling a method (which does not exist). It needs to looking at an array. So in your choosebase method, you should make an array that holds all the selected components.
Then in this method, you can determine if that array is empty. You might need to make your component selection be a global variables. So change base_entities to $base_entities in the first method. Then change the if statement to:
if $base_entities.empty?
That might begin to get you somewhere. But I have a feeling you are going to run into some more problems with the selection set. base_entities is currently pointing at the selection object - it is not pointing to an array of objects that are selected. Which is different. So maybe change line 6 to read:
$base_entities = Sketchup.active_model.selection.to_a
Now your base_entities is an array of SketchUp entities. And hopefully now when you get to the "if" statement, it will recognize your if statement as a valid statement, and parse it correctly.
But I have not tested any of this, so it might need tweaks still.
Chris
-
global variable? I'd recommend an module variable instead.
-
I never played with the @ and @@ variables inside of modules. Do they work as expected? Will his script remember the value over multiple script sessions?
Chris
-
Use @ variable within a module and it's global within that module and it doesn't interfere with any other scripts.
module MyModule @module my_var = 'Hello World' def self.foo return @my_var end end
For classes - have a look at this: http://sporkmonger.com/2007/2/19/instance-variables-class-variables-and-inheritance-in-ruby
-
Agamemnus,
Having Chris Fullmer help you with Ruby programming is like having Roger Federer help you with your tennis. Beyond what money could buy.
Sorry about the off topic stuff, but here's some more:
@chris fullmer said:
oh Gaieus, you can do it! Its not too late to build some way cool Gaieus Toolset for SU
Gaieus, Chris is so right! I'm working on my tutorial's Ruby half (yes, half!). See if you can read this script:
# /t/stairs1.rb - a stairway in SketchUp Ruby Talk (aka SketchTalk) =begin The following bit of SketchTalk builds a 36"-wide stairway. Rise is 7 inches; run is 9. Built from 1.5" thick lumber. It has 15 steps. It has a 1.5 inch square banister on the left. The banister is supported by 1x1 balusters. =end require '/t/sketch_talk' n # my keyboard shortcut for File/New box [0,0,0], [36,1.5,0], 5.5 # riser box [0,0,5.5], [36,10.5,5.5], 1.5 # tread box [1.25,1,7], [2.25,2,7], 38 # ballusters box [1.25,5.5,7], [2.25,6.5,7], 38+(7.0/2) all # select all step = g 'step' # turn selection into a component named 'step' mc step, [0,9,7], 14 # Move/Copy, 14x box [1,0,36+7], [2.5,15*9,36+7+15*7], 1.5 # bannister
I'll bet you can read that script. I'll bet you can't build the model half that fast any other way!
-
The first script is almost done. I have a few problems --
-
I could not find a way to convert groups to components so I changed them to unique components and then changed the definition of THOSE to the randomly selected base component. Isn't there a more efficient way?
-
I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.
-
When I do "@target_entities = target_entities", and I already had a non-empty @target_entities, am I leaking?
-
(mini-gripe) The rand function is glitchy. Sometimes it gives several (3-4) repeats in a 90-size or so list, which has an incredibly low chance of occurring.
require 'sketchup.rb' module Convert_objects_randomly @base_entities = [] @target_entities = [] def Convert_objects_randomly.choosebase @base_entities = Sketchup.active_model.selection.to_a if @base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end end def Convert_objects_randomly.choosetarget target_entities = Sketchup.active_model.selection.to_a if @base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end if target_entities.empty? if @target_entities.empty? results = UI.messagebox "Please select target groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end target_entities = @target_entities end target_entities.each do |current_entity| if (current_entity.typename == "Group") current_entity.to_component end end Sketchup.active_model.commit_operation target_entities.each do |current_entity| if (current_entity.typename == "Group") or (current_entity.typename == "ComponentInstance") current_entity.make_unique current_entity.definition = @base_entities[rand(@base_entities.length)].definition end end @target_entities = target_entities Sketchup.active_model.commit_operation end end #Convert_objects_randomly module Convert_objects_randomly_menu = UI.menu("Plugins").add_submenu("Convert Objects Randomly") Convert_objects_randomly_menu.add_item("Select base objects") {Convert_objects_randomly.choosebase} Convert_objects_randomly_menu.add_item("Convert selected to random base objects") {Convert_objects_randomly.choosetarget} file_loaded(__FILE__)
-
-
Thanks. I couldn't do it with "@module base_entities = '' "-- that gave me an error. I used " @base_entities = '' ". And looks like I need that " @base_entities ='' " or else something weird happens (maybe undefineds everywhere?) and the if statement for base_entities did not work. (it does now, of course)
So now I have to figure out how to iterate through target_entities and convert each to a random base_entities ..
There isn't a native function I could use?
Edit: Figured it out mostly... See post 1 below.
-
#1 groups have the .to_component method that will turn them into a component. But it makes each group a unique component, because there is no built in way to determine if a group is an identical copy of another group. There is some work arounds that can be done on certain groups that are exact copies of another group, but generally speaking, groups can't be turned easily into a single component and instances.
#2 - I'll have to look at later
#3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a
@taget_entities = []
to the end of your script. That is my gess as to what is happening.#4 - Yeah, that happens. Adam Billyard once took some time to explain about how random functions work in programming. It was eye opening....or rather, my eyes popped out from all the terms he used that didn't make any sense to me. But essentially random is a tricky thing to nail perfectly I guess.
Sounds like its coming along well!
Chris
-
@chris fullmer said:
#3 - The @target_entities will hold its value until SketchUp is closed, after it is given a value once. So you should clear it after your script is run each time. Add a
@taget_entities = []
to the end of your script. That is my gess as to what is happening.Well, I know that it holds its value -- that was a way to re-randomize if the user wanted to without selecting those same groups again. But, if I set @target_entities to something else and it (@target_entities) is already set to something, that won't cause a leak, right? (the array it was previously set to will be cleared from memory?)
-
Ahh, correct. If you set it to seomthing new, then it clears its old value.
-
Ruby uses garbage collection - so it keeps track of all the junk we throw about and sweeps it all away!
-
@agamemnus said:
- I can't seem to convert the target groups to components fast enough -- I used 'if (current_entity.typename == "Group") // current_entity.to_component // end', but that only applies at the end of the script so I have to run it twice.. dunno how to make it convert instantly. Tried shoving "Sketchup.active_model.commit_operation" after and then converting the unique components to base components but it won't budge.
the reason why is because .to_component creates a new entity.
Try current_entity = current_entity.to_component. -
Also
.typename
is very slow.
Instead ofcurrent_entity.typename == "Group"
usecurrent_entity.is_a?(Sketchup::Group)
.
http://forums.sketchucation.com/viewtopic.php?f=180&t=19576&start=0#p162235 -
Thanks guys. Got myself two more issues now, though.
- The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")
- Is there any way to make a yes/no dropdown, and if not how do I change the size (width) of the inputbox text?
require 'sketchup.rb' module Convert_objects_randomly @base_entities = [] @target_entities = [] @convert_only_groupsinstances = true def Convert_objects_randomly.choosebase @base_entities = Sketchup.active_model.selection.to_a if @base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end end def Convert_objects_randomly.choosetarget target_entities = Sketchup.active_model.selection.to_a if @base_entities.empty? results = UI.messagebox "Please select base groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end if target_entities.empty? if @target_entities.empty? results = UI.messagebox "Please select target groups and/or components first." Sketchup.send_action "selectSelectionTool;" return end target_entities = @target_entities end target_entities.each do |current_entity| if current_entity.is_a?(Sketchup;;Group) current_entity = current_entity.to_component current_entity.definition = @base_entities[rand(@base_entities.length)].definition elsif current_entity.is_a?(Sketchup;;ComponentInstance) current_entity = current_entity.make_unique current_entity.definition = @base_entities[rand(@base_entities.length)].definition else current_entity = current_entity.add_group current_entity = current_entity.to_component current_entity.definition = @base_entities[rand(@base_entities.length)].definition end end @target_entities = target_entities Sketchup.active_model.commit_operation end def Convert_objects_randomly.settings if (@convert_only_groupsinstances == true) default = "yes" else default = "no" end settings_query1 = inputbox(["Convert only groups/components to base objects?"],[default],"Convert Objects Randomly Settings") if (settings_query1[0] == "yes") @convert_only_groupsinstances = true else @convert_only_groupsinstances = false end end end #Convert_objects_randomly module Convert_objects_randomly_menu = UI.menu("Plugins").add_submenu("Convert Objects Randomly") Convert_objects_randomly_menu.add_item("Select base objects") {Convert_objects_randomly.choosebase} Convert_objects_randomly_menu.add_item("Convert selected to random base objects") {Convert_objects_randomly.choosetarget} Convert_objects_randomly_menu.add_item("Settings") {Convert_objects_randomly.settings} file_loaded(__FILE__)
-
@agamemnus said:
- The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")
Check the API on that method. It only belongs to Entities collections. Not single Entity
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html@agamemnus said:
- Is there any way to make a yes/no dropdown
Check the second example in the manual:
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/ui.html#inputbox -
@agamemnus said:
Ok, thanks. How do I convert an entity to an entities collection?
You don't. Use an entities collection returned by the entities method:
Sketchup.active_model.entities myGroup.entities someComponent.entities
-
@thomthom said:
@agamemnus said:
- The code refuses to convert lines to groups!! (I have "current_entity = current_entity.add_group")
Check the API on that method. It only belongs to Entities collections. Not single Entity
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/entities.html@agamemnus said:
- Is there any way to make a yes/no dropdown
Check the second example in the manual:
http://code.google.com/intl/nb/apis/sketchup/docs/ourdoc/ui.html#inputboxOk, thanks. How do I convert an entity to an entities collection?
Edit: The textbox width is still really long, even with the dropdown now...
Advertisement