I wanted a routine that I can use in my stair maker that will convert individual connected edges into a polyline.
I looked at weld.rb and I thought there is another way to weld things together more efficiently.
I have a small benchmark file that I can upload if requested. It has 308 edges.
running my combine.rb against it took this time.
0.036
0.037
running weld.rb against it took this time.
0.187
0.197
Please do note that I am currently not interested in a closed polyline. Nor am I interested in filling in faces.
require 'sketchup.rb'
def combine_edges()
model = Sketchup.active_model
entities = model.active_entities
selection = model.selection
edges = [] # array of user selected edges
right = [] # right hand chain
left = [] # left hand chain
t = Time.now
selection.each { |val| edges << val if ( val.typename == "Edge" ) }
check to make sure user has selected at least 2 edges
return UI.messagebox( "Please select at least 2 edges" ) if ( edges.length < 2 )
remove first edge and split into both chains
edge = edges.shift
right << edge.end
left << edge.start
found = false
loop through the edges and add to right or left chain
while ( edges.length > 0 )
found = false
edges.each_with_index do |edge, idx|
if ( right.last == edge.start )
right << edge.end
elsif ( right.last == edge.end )
right << edge.start
elsif ( left.last == edge.start )
left << edge.end
elsif ( left.last == edge.end )
left << edge.start
else
next
end
found = true
edges[idx] = nil
break
end
edges.compact! if ( found )
return UI.messagebox( "Polyline requires all edges to connect. Please try again!" ) if ( ! found )
end
concatenate chains - since we pushed vertices on the left chain they are in reverse and need to be fixed
left.reverse!
left += right
puts Time.now - t
create the new polyline "curve", explode the container group and clear the selection
model.start_operation "polyline"
group = ( container = entities.add_group ).entities
group.add_curve( left )
container.explode
model.commit_operation
selection.clear
end
#-----------------------------------------------------------------------------
if ( not file_loaded?( "combine.rb" ) )
add_separator_to_menu( "Plugins" )
UI.menu( "Plugins" ).add_item( "Combine Edges" ) { combine_edges() }
file_loaded( "combine.rb" )
end
#-----------------------------------------------------------------------------