Tuk Bredsdorff avatar Tuk Bredsdorff committed 53480e9 Draft

Struts are now properly counted and reported!

Comments (0)

Files changed (1)

 PRIMARY_COLORS = ["Red", "Green", "Blue", "Yellow"]
 STRUT_NAMES = ["A", "B", "C", "D"]
 
-$strut_lengths_sorted = nil
-
 
 def edges_in_selection
 	selection = Sketchup.active_model.selection
 	return strut_group
 end
 
-# Look up colors and names given a strut length
-def strut_properties length
-	index = $strut_lengths_sorted.index length.to_s
-	color = PRIMARY_COLORS[index]
-	name = STRUT_NAMES[index]
-	
-	return {'color'=>color, 'name'=>name}
-end
-
 # Loops through all edges in the current selection and
 # creates struts. And adds all the struts to a group.
 def create_frame edges, center, size
+	strut_lengths_sorted = find_strut_lengths.keys.sort
 	all_struts = []
-	edges.each_with_index { | edge, index |
+	for edge in edges
 		strut = create_strut edge, center, size
-		props = strut_properties edge.length
-		strut.name = props["name"]
-		strut.material = props["color"]
+		index = strut_lengths_sorted.index round(edge.length)
+		strut.name = STRUT_NAMES[index]
+		strut.material = PRIMARY_COLORS[index]
 		all_struts << strut
-	}
+	end
 	frame_group = Sketchup.active_model.active_entities.add_group all_struts
 	frame_group.name = "Geodome frame"
 end
 	end
 end
 
-# Which strut lengths are this dome composed of?
+def round f
+	return (f*10000).round/10000.0
+end
+
+# Which strut lengths  and counts are this dome composed of?
+# Returns an hash table like this:
+# {
+#   48.0376=>{"length"=>48.037567357133, "count"=>30},
+#   56.8284=>{"length"=>56.8283548261602, "count"=>80}
+# }
+# The length keys are Length objects and we store them to
+# so we can display nicely formatted lengths using .to_s later.
 def find_strut_lengths
-	strut_lengths = {}
+	strut_info = {}
 	
 	for e in edges_in_selection
-		# Uses Length.to_s to get the length in
-		# in the users current units and precision :-) .
-		rounded_str = e.length.to_s 
-		if not strut_lengths[rounded_str]
-			strut_lengths[rounded_str] = 1
+		# We need to round the lengths as they
+		# can differ in the last very insignificant decimals.
+		rounded_float = round e.length
+		
+		if not strut_info[rounded_float]
+			strut_info[rounded_float] = {'count' => 1, 'length' => e.length}
 		else
-			strut_lengths[rounded_str]+= 1
+			strut_info[rounded_float]['count'] += 1
 		end
 	end
 	
-	return strut_lengths
+	return strut_info
 end
 
+# Print an overview of the struts
 def print_strut_lengths
-	puts "#{$strut_counts.nitems} strut lengths:"
-	find_strut_lengths.each do |length, count|
-		props = strut_properties length
-		name = props["name"]
-		color = props["color"]
+	strut_info = find_strut_lengths
+	strut_lengths_sorted = strut_info.keys.sort
+	puts "#{strut_info.size} strut lengths:"
+	
+	strut_lengths_sorted.each_with_index do | length_rounded, index |
+		count = strut_info[length_rounded]['count']
+		length = strut_info[length_rounded]['length'].to_s
+		name = STRUT_NAMES[index]
+		color = PRIMARY_COLORS[index]
 		puts "#{count} x #{name} (#{color}): #{length}"
 	end
 end
 				prompts = ["Width:", "Depth:"]
 				size = inputbox prompts, STRUT_SIZE, "Strut Dimensions"
 				if size
-					$strut_lengths_sorted = find_strut_lengths.keys.sort
-					
 					create_frame edges, center, size
 					print_strut_lengths
 				end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.