Ruby Script for Adding up length of Edges
-
I write a script to display the sum of a set of edges that a user has selected. I understand how to use ruby to get access to the selection set. I have not yet figured out how to get this sum of lengths. I would like to create in script a component that I can pass this value to that the user does not see in the viewport for listing through the SketchUp Generate report command.
Any help will be appreciated thank you!
One last question, is it possible to parse a layer for a specific set of entities like edges and the do the same as I described above?
I have not found any ruby commands for parsing a specific layer for all edges contained in that set.
Thank you,
-
Haven't rubied'in a while, so i'll have to remember the syntax, but until then:
For each entity in selection array, if it's a sketchup edge, totalLength = totalLength + edge.length ?
-
You can only really parse through entities 'collections': model.entities/model.active_entities and group.entities/definition.entities AND model.selection...
However, once you have a collected set from that, you can parse that set to find just the things on a certain layer, with a certain material etc...
Recently the use of 'grep' has been found to speed up entity parsing significantly, I recommend you try it...
So to find all groups in a selection use:
gps=model.selection.grep(Sketchup::Group)
Now 'gps' is an array of all groups in the current selection.
To find every selected group that's assigned layer 'XXX' use
xxx=[]; gps.each{|gp| xxx << gp if gp.layer.name=='XXX' }
If you want to find absolutely everything in the model using that layer, then:
gps=model.entities.grep(Sketchup::Group)
and process 'gps'...To get matches within a group use:
gps=group.entities.grep(Sketchup::Group)
and process 'gps'...To find everything in the model using a certain layer try:
xxx=[]; model.entities.each{|e| xxx << e if e.layer.name=='XXX' }
There are several alternative methods, like 'collect', but here I just show you make an empty array 'xxx=[]' and then we add to it using 'push' [<<] when we get a match...
-
@th3lurker said:
Haven't rubied'in a while, so i'll have to remember the syntax, but until then:
For each entity in selection array, if it's a sketchup edge, totalLength = totalLength + edge.length ?
Thank you for this bit of information - as I get this project rolling - I will post here the development of it.
-
@tig said:
You can only really parse through entities 'collections': model.entities/model.active_entities and group.entities/definition.entities AND model.selection...
However, once you have a collected set from that, you can parse that set to find just the things on a certain layer, with a certain material etc...
Recently the use of 'grep' has been found to speed up entity parsing significantly, I recommend you try it...
So to find all groups in a selection use:
gps=model.selection.grep(Sketchup::Group)
Now 'gps' is an array of all groups in the current selection.
To find every selected group that's assigned layer 'XXX' use
xxx=[]; gps.each{|gp| xxx << gp if gp.layer.name=='XXX' }
If you want to find absolutely everything in the model using that layer, then:
gps=model.entities.grep(Sketchup::Group)
and process 'gps'...To get matches within a group use:
gps=group.entities.grep(Sketchup::Group)
and process 'gps'...To find everything in the model using a certain layer try:
xxx=[]; model.entities.each{|e| xxx << e if e.layer.name=='XXX' }
There are several alternative methods, like 'collect', but here I just show you make an empty array 'xxx=[]' and then we add to it using 'push' [<<] when we get a match...
Hello TIG,
This is once again very helpful. I think I understand your examples to experiment a bit and report back what I develop using this information.
Advertisement