Snippets

John Peck Create a pipe table of drill diameters and uses from a PCB drill file

Updated by Former user

File drilltable.tcl Modified

  • Ignore whitespace
  • Hide word diff
 	    set diameter [lindex $linelist 1]
 	    # Altium has F and S fields in their tool lists
 	    set toolstring [regexp -inline {T[0-9]*} $line]
-	    puts $toolstring
 	    # The tool code may contain leading zeros
 	    set toolnum [regexp -inline {[1-9][0-9]*} $toolstring]
 	    dict set tool_diameter_dict T$toolnum $diameter
Updated by Former user

File drilltable.tcl Modified

  • Ignore whitespace
  • Hide word diff
 	if {[string first T $line] == 0} {
 	    # This is a tool code line
 	    set linelist [split $line C]
-	    set toolcode [lindex $linelist 0]
 	    set diameter [lindex $linelist 1]
-	    dict set tool_diameter_dict $toolcode $diameter
+	    # Altium has F and S fields in their tool lists
+	    set toolstring [regexp -inline {T[0-9]*} $line]
+	    puts $toolstring
+	    # The tool code may contain leading zeros
+	    set toolnum [regexp -inline {[1-9][0-9]*} $toolstring]
+	    dict set tool_diameter_dict T$toolnum $diameter
 	}
     }
     return $tool_diameter_dict
 }
 
+proc format_tool_code {tool_string} {
+    # Returns a properly formatted tool code
+    
+}
+
 proc get_drill_units {raw_line_list} {
     # Returns either inch or mm for the drill diameter units
     #
 	}
 	if {[string first T $line] == 0 && $data_started} {
 	    # This is the start of a new tool section
-	    set current_tool $line
+	    set current_tool T[regexp -inline {[1-9][0-9]*} $line]
 	}
 	if {[string first X $line] == 0 && ![string equal $current_tool none]} {
 	    # This is a new drill location
Updated by Former user

File drilltable.tcl Modified

  • Ignore whitespace
  • Hide word diff
     return $drill_count_dict
 }
 
+proc get_total_drills {drill_count_dict} {
+    # Return the total number of drills in the drill count dictionary
+    #
+    # Arguments:
+    #   drill_count_dict -- Dictionary of (tool code) : (tool count)
+    set total_drills 0
+    foreach tool [dict keys $drill_count_dict] {
+	incr total_drills [dict get $drill_count_dict $tool]
+    }
+    return $total_drills
+}
+
+
+
 proc write_drill_table {drill_diameter_dict drill_count_dict drill_units} {
     # Write a pipe-delimited drill table
     #
     set size_width 20
     set count_width 10
     set filled_width 10
-    set separator "|-[string repeat - $tool_width]-"
+    set total_width 7
+    set separator [string repeat " " $total_width]
+    append separator "|-[string repeat - $tool_width]-"
     append separator "+-[string repeat - $size_width]-"
     append separator "+-[string repeat - $count_width]-"
     append separator "+-[string repeat - $filled_width]-"
     append separator "|"
     puts $separator
-    puts [format "| %-*s | %-*s | %-*s | %-*s | " \
-	      $tool_width "Tool" \
-	      $size_width "Hole size ($drill_units)" \
-	      $count_width "Count" \
-	      $filled_width "Filled?"
-	 ]
+    set header [string repeat " " $total_width]
+    append header [format "| %-*s | %-*s | %-*s | %-*s | " \
+		       $tool_width "Tool" \
+		       $size_width "Hole size ($drill_units)" \
+		       $count_width "Count" \
+		       $filled_width "Filled?"
+		  ]
+    puts $header
     puts $separator
     foreach tool [dict keys $drill_diameter_dict] {
 	set filled ""
 	if {[string first $tool $params(t)] != -1} {
 	    set filled x
 	}
-	puts [format "| %-*s | %-*s | %-*s | %-*s | " $tool_width $tool \
-		  $size_width [dict get $drill_diameter_dict $tool] \
-		  $count_width [dict get $drill_count_dict $tool] \
-		  $filled_width $filled
-	     ]
+	set table_row [string repeat " " $total_width]
+	append table_row [format "| %-*s | %-*s | %-*s | %-*s | " $tool_width $tool \
+			      $size_width [dict get $drill_diameter_dict $tool] \
+			      $count_width [dict get $drill_count_dict $tool] \
+			      $filled_width $filled
+			 ]
+	puts $table_row
     }
     puts $separator
+    set total_row [format "%-*s" $total_width "Total:"]
+    append total_row [format "| %-*s | %-*s | %-*s | %-*s | " \
+			  $tool_width [llength [dict keys $drill_diameter_dict]] \
+			  $size_width "" \
+			  $count_width [get_total_drills $drill_count_dict] \
+			  $filled_width $filled
+		     ]
+    puts $total_row
+    puts $separator
 }
 
 # Start processing the file
Updated by Former user

File drilltable.tcl Modified

  • Ignore whitespace
  • Hide word diff
 
 # Start processing the file
 set raw_line_list [read_file $datafile]
-puts $params(t)
 set drill_diameter_dict [get_drill_diameters $raw_line_list]
 set drill_count_dict [init_drill_count_dict $drill_diameter_dict]
 set drill_units [get_drill_units $raw_line_list]
Updated by Former user

File drilltable.tcl Modified

  • Ignore whitespace
  • Hide word diff
     #   drill_diameter_dict -- Dictionary of (tool code) : (tool diameter)
     #   drill_count_dict -- Dictionary of (tool code) : (tool count)
     #   drill_units -- Either inch or mm
+    global params
     set drill_diameter_dict [lsort -stride 2 -index 1 -real $drill_diameter_dict]
     set tool_width 10
     set size_width 20
     set count_width 10
+    set filled_width 10
     set separator "|-[string repeat - $tool_width]-"
     append separator "+-[string repeat - $size_width]-"
-    append separator "+-[string repeat - $count_width]-|"
+    append separator "+-[string repeat - $count_width]-"
+    append separator "+-[string repeat - $filled_width]-"
+    append separator "|"
     puts $separator
-    puts [format "| %-*s | %-*s | %-*s | " \
+    puts [format "| %-*s | %-*s | %-*s | %-*s | " \
 	      $tool_width "Tool" \
 	      $size_width "Hole size ($drill_units)" \
-	      $count_width "Count"]
+	      $count_width "Count" \
+	      $filled_width "Filled?"
+	 ]
     puts $separator
     foreach tool [dict keys $drill_diameter_dict] {
-	puts [format "| %-*s | %-*s | %-*s | " $tool_width $tool \
+	set filled ""
+	if {[string first $tool $params(t)] != -1} {
+	    set filled x
+	}
+	puts [format "| %-*s | %-*s | %-*s | %-*s | " $tool_width $tool \
 		  $size_width [dict get $drill_diameter_dict $tool] \
-		  $count_width [dict get $drill_count_dict $tool]
+		  $count_width [dict get $drill_count_dict $tool] \
+		  $filled_width $filled
 	     ]
     }
     puts $separator
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.