1. Tuk Bredsdorff
  2. Geodome frames for SU

Commits

Tuk Bredsdorff  committed 0573986 Draft

Printing radius and diameter. Fixing method names so they works within a module.

  • Participants
  • Parent commits c02c2e4
  • Branches default

Comments (0)

Files changed (1)

File geodome_frame.rb

View file
 	PRIMARY_COLORS = ["Red", "Blue", "Green", "Yellow"]
 	STRUT_NAMES = ["A", "B", "C", "D"]
 
-	def edges_in_selection
+	def self.edges_in_selection
 		selection = Sketchup.active_model.selection
 		return selection.find_all { |e| e.kind_of?(Sketchup::Edge) }
 	end
 
 	# Create a strut (as a box in a group)
 	# along an edge, facing a center point.
-	def create_strut edge, center, size
+	def self.create_strut edge, center, size
 		entities = Sketchup.active_model.active_entities
 		width, depth = size
 	
 
 	# 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
+	def self.create_frame edges, center, size
+		strut_lengths_sorted = self.find_strut_lengths.keys.sort
 		all_struts = []
 		for edge in edges
-			strut = create_strut edge, center, size
-			index = strut_lengths_sorted.index round(edge.length)
+			strut = self.create_strut edge, center, size
+			index = strut_lengths_sorted.index self.round(edge.length)
 			strut.name = STRUT_NAMES[index]
 			strut.material = PRIMARY_COLORS[index]
 			all_struts << strut
 
 	# Print all entities.
 	# TODO: Fails. Test properly for entities attribute?
-	def print_entities entities=nil
+	def self.print_entities entities=nil
 		if not entities
 			entities = Sketchup.active_model.entities
 		end
 		end
 	end
 
-	def round f
+	def self.round f
 		return (f*10000).round/10000.0
 	end
 
 	# }
 	# 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
+	def self.find_strut_lengths
 		strut_info = {}
 	
-		for e in edges_in_selection
+		for e in self.edges_in_selection
 			# We need to round the lengths as they
 			# can differ in the last very insignificant decimals.
-			rounded_float = round e.length
+			rounded_float = self.round e.length
 		
 			if not strut_info[rounded_float]
 				strut_info[rounded_float] = {'count' => 1, 'length' => e.length}
 		return strut_info
 	end
 
+	def self.dome_radius
+		# Find a dome edge
+		edge = self.edges_in_selection[0]
+		
+		# The distance of any edge end to origo is the radius
+		# (with the assumtion that the dome has its center in the origin).
+		radius = edge.start.position.distance [0, 0, 0]
+		
+		# Return the radius as a Length
+		return radius.to_l
+	end
+	
 	# Print an overview of the struts
-	def print_strut_lengths
-		strut_info = find_strut_lengths
+	def self.print_strut_lengths
+		strut_info = self.find_strut_lengths
 		strut_lengths_sorted = strut_info.keys.sort
+		radius = self.dome_radius
+		
+		puts "Radius: #{radius}, diameter: #{(2*radius).to_l}"
 		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
 		end
 	end
 
-	def main
+	def self.main
 		Sketchup.active_model.start_operation "Geodome frame", true
 			selection = Sketchup.active_model.selection
 		
 			center = [0, 0, 0]
 		
 			if selection.count > 0
-				edges = edges_in_selection
+				edges = self.edges_in_selection
 				if edges.nitems > 0
 					prompts = ["Width:", "Depth:"]
 					size = inputbox prompts, STRUT_SIZE, "Strut Dimensions"
 					if size
-						create_frame edges, center, size
-						print_strut_lengths
+						self.create_frame edges, center, size
+						self.print_strut_lengths
 					end
 				else
 					UI.messagebox "No edges in selection to build from."
 	end
 
 	unless file_loaded? __FILE__
-		UI.menu("Plugins").add_item("Geodome frame") { main }
+		UI.menu("Plugins").add_item("Geodome frame") { self.main }
 	end
-
+	
 	file_loaded __FILE__
 end