Looking to make a script, but dunno how.
-
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...
-
@martinrinehart said:
@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 >
But I can't... it's not a group or component. It's a line.... tried "current_entity = current_entity.entities", no go.
-
You can't convert any raw geometry to a Group.
But...my_group=model.active_entities.add_group(my_line)
might work if the line has no face etc, then
my_instance=my_group.to_component my_definition=my_instance.definition
Otherwise clone the line into a group and leave the original alone with
my_group=model.active_entities.add_group() my_group.entities.add_line(my_line.start.position,my_line.end.position)
which recreates the line inside the new group...
-
Ugh. Maybe it's just better to use Chris's unsorted geometry->groups script first.
-
Okay, script 1 is finished. It could use some expansion though, possibly. (eg: (1) convert areas instead of just lines, groups, and components [hard] and (2) don't make identical unique components as options [very doable, but might not be worth it]) The only real problems now are cosmetic:
-
I am not sure that "Convert objects randomly" is a very good name for the script. I can't think of anything else that is both short and also semi-self-explanatory, though.
-
The dropdown in the "options" is too big! I don't know how to set its width.
Code:
# ---------------------------------- # ================================== # ---------------------------------- # # "Convert objects randomly" script # Released into the public domain by Agamemnus. # Version 1; November 10, 2009 # Contact the author; sketchup at flyingsoft dot phatcode dot net # # Purpose; # # Easily converts a set of objects (target) into another set (base) randomly, # distributed amongst members of the base set. # # Use; # 1) Select a set of objects -- currently only components and/or groups work. # 1b) Pick "Select base objects" in "Plugins">"Convert Objects Randomly". # 2) Select a target set of objects -- currently only components, groups, or lines. # 2b) Pick "Convert selected to random base objects" in "Plugins">"Convert Objects Randomly". # 2c) Done! You may pick "Convert selected to random base objects" again if you wish -- # your previous base and target object sets apply if you did not fundamentally change # (explode, convert to group, etc-- geometry editing is fine) the target components. # # 3) Set "Plugins">"Convert Objects Randomly">"Settings">"Convert lines to base objects?" to "YES" # if you want ungrouped lines in the target set to be affected. Set "NO" otherwise. # The default is "YES". # # Behavior of result objects in version 1; # If the picked "base" object is a group, the target object is converted into a unique component. # If the picked "base" object is a component, the target object is converted into a component # instance of that component type. # # # ==Miscellaneous Notes;== # 1.) Much thanks to the Sketchup forum regulars in helping with problems in this script. # # 2.) Looking for Work ;( # ...in the fields of; # economic analysis # business strategy # marketing # UI design # web based programming # 2D visual art # 3D visual art. # general programming. # M.A. in International Economics and Finance at Brandeis IBS, BA in economics at Brandeis IBS and Brandeis University. # 16 years of experience in amateur programming in BASIC-based languages (Basic, Freebasic, VB, etc.) # Several years of experience in PHP and Javascript. Experience in C-LISP, Matlab, and "some" # experience in Ruby (eh!) and C. # Work environment must at a _minimum_ contain penguins performing tricks, live. # # ---------------------------------- # ================================== # ---------------------------------- require 'sketchup.rb' module Convert_objects_randomly @base_entities = [] @target_entities = [] @convert_lines = 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 elsif (@convert_lines == true) current_entity = Sketchup.active_model.active_entities.add_group(current_entity) 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_lines == true) default = "yes" else default = "no" end settings_query1 = inputbox(["Convert lines to base objects? "],[default], ["yes|no"], "Convert Objects Randomly Settings") if (settings_query1[0] == "yes") @convert_lines = true else @convert_lines = 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}
-
-
Here is a file with both scripts (random color and object), and another file that shows the result of using SketchyPhysics 3, these two scripts, and Chris Fullmer's "Scale and Rotate Multiple" and "Loose Geometry to Groups" scripts. (Gems are from the 3D Warehouse)
So now ... what if I wanted to make the result groups instead of component instances? So far the script is only converting everything to unique component instances and changing the definition. Ideally I want to just change the geometry of the groups without making unique component instances. So, assuming I could do one of these, which one is more efficient (faster), and can I even do any of these (and how) --?
-
Convert "target" objects to component instances based on "base" objects, then convert "target" objects back to groups and delete the components.
-
Convert "target" objects straight to groups based on "base" objects.
(If anyone thinks my question is unclear I would rephrase it.. I needs answers! )
-
-
A few things. I'm getting errors when I try to run the script. I have not looked at all the code, so I'm not sure what I'm doing wrong yet. But, I'll try to look it over tonight.
I'm also not entirely sure what your questions are about groups and components and component instances. Maybe it will make more sense when I get the script to run all the way through. I'll try to get back to this in a few hours,
Chris
-
@chris fullmer said:
A few things. I'm getting errors when I try to run the script. I have not looked at all the code, so I'm not sure what I'm doing wrong yet. But, I'll try to look it over tonight.
I'm also not entirely sure what your questions are about groups and components and component instances. Maybe it will make more sense when I get the script to run all the way through. I'll try to get back to this in a few hours,
Chris
I don't know why you are getting errors. I am not getting any errors. I did not use any globals AFAIK, so that can't be it. No idea.
I'll try to rehash the question. I'm trying to convert the "target" objects into "base" objects but so far the only way to do that is to convert the "targets", if they are groups or lines, to new/unique component instances and then change the definition of that instance. Instead, I would like to either change the unique component instances back into groups (and delete the component definition), or to simply change the objects right away into groups, depending on whether any of these methods are possible and which one is faster. (see "Convert_objects_randomly.choosetarget_changeobjects")
-
I am getting
Error: #<NoMethodError: undefined method `definition' for #Sketchup::Group:0xa0636b0>
I am guessing you have a plugin installed that created the Group.definition method. Probably somethin by Thom or TIG? let me know what it is and I'll intall it.
Chris
-
You can't get a group's definition without an extra 'method'... thomthom has one - I can't find it on the forum so here it is...
class Sketchup;;Group # Some times the group.entities.parent refer to the wrong definition. This method checks # for this error and locates the correct parent definition. def real_parent if self.entities.parent.instances.include?(self) return self.entities.parent else Sketchup.active_model.definitions.each { |definition| return definition if definition.instances.include?(self) } end return nil # Should not happen. end end
The
group.entities.parent
should return the group's definition BUT sometimes it fails... so usegroup.real_parent
to replacegroup.definition
and it should work...Hope this helps...
-
Ok. I didn't know.. I thought it was native. Is there some way to stash this inside my script so that the method is local? Or perhaps I shouldn't worry about that? (How do I check if a method is defined? If I can't, are duplicates OK?)
Advertisement