Code Snippets

Random, useful code snippets I want here for easy reference.


Find large folders

And sort in descending order...

Just cd to the folder you want to examine. This snippet only goes one level deep:

du -sh * | sort -hr

Create Sketchup components from CSV

Input is .csv with 4 columns: name, width, depth, height.

No need for column names:

Boxy box 2 4 6
Another box 3 7 5

Copy & paste this into the Ruby console (get the file path right):


require 'csv'
rows = CSV.read("/Users/joelgaeddert/test.csv")
  
rows.each do |row|
  # Get component name and make sure it is unique
  name = row[0]
  component = Sketchup.active_model.definitions.add(name + "#{Sketchup.active_model.definitions.size + 1}")
  
  # Set the dimensions for the component based on the values in the row
  box_width = row[1].to_f
  box_depth = row[2].to_f
  box_height = row[3].to_f
  
  # Draw the box
  pt1 = [0, 0, 0]
  pt2 = [box_width, 0, 0]
  pt3 = [box_width, box_depth, 0]
  pt4 = [0, box_depth, 0]
  pt5 = [0, 0, box_height]
  pt6 = [box_width, 0, box_height]
  pt7 = [box_width, box_depth, box_height]
  pt8 = [0, box_depth, box_height]
  component.entities.add_face(pt1, pt2, pt3, pt4)
  component.entities.add_face(pt5, pt6, pt7, pt8)
  component.entities.add_face(pt1, pt5, pt8, pt4)
  component.entities.add_face(pt2, pt6, pt7, pt3)
  component.entities.add_face(pt1, pt2, pt6, pt5)
  component.entities.add_face(pt4, pt3, pt7, pt8)
  
  # Add 3D Text to top of box
  string = name
  align = TextAlignLeft
  font = "Arial"
  is_bold = true
  is_italic = false
  height = 1.0
  tolerance = 0.0
  z_position = box_height
  is_filled = true
  depth = 0.5
  component.entities.add_3d_text(string, align, font, is_bold, is_italic, height, tolerance, z_position, is_filled, depth)
  
end