footprintgrid.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # usage:
  2. # footprintgrid.py -o [outputfile] -u ['in' or 'mm' units] -l [left padding (units)] -t [top padding (units)] -r [right padding (units)] -b [bottom padding (units)] -x [horizontal pin count] -y [vertical pin count] -g [grid size (units)] -h [hole diameter (units)] -r [ring thickness (units)]
  3. #
  4. # writes an svg footprint file to [outputfile]
  5. # units are either all in inches or millimeters
  6. # padding left and top is the distance from the edge of the part to the center of the top left pin
  7. # padding right and bottom is the distance from the edge of the part to the center of the bottom right pin
  8. # x and y define the number of pins
  9. # gridsize is the distance from the center of one pin to the center of another pin
  10. # drillhole is the diameter of the pin's drillhole
  11. # ring thickness is the width of the ring (not including the drillhole)
  12. #
  13. # example: footprintgrid.py -o arduino_mini.svg -u in -l 0.1 -t 0.1 -r0.1 -b 0.1 -x 12 -y 7 -g 0.1 -c 0.02 -d 0.035
  14. import getopt, sys, os, re
  15. def usage():
  16. print """
  17. usage:
  18. footprintgrid.py -o [outputfile] -u ['in' or 'mm' units] -l [left padding (units)] -t [top padding (units)] -r [right padding (units)] -b [bottom padding (units)] -x [horizontal pin count] -y [vertical pin count] -g [grid size (units)] -d [drillhole diameter (units)] -c [copper thickness (units)]
  19. writes an svg footprint file to [outputfile]
  20. units are either all in inches or millimeters
  21. padding left and top is the distance from the edge of the part to the center of the top left pin
  22. padding right and bottom is the distance from the edge of the part to the center of the bottom right pin
  23. x and y define the number of pins
  24. g is the distance from the center of one pin to the center of another pin
  25. drillhole is the diameter of the pin's drillhole
  26. copper thickness is the width of the copper ring (not including the drillhole)
  27. """
  28. def main():
  29. try:
  30. opts, args = getopt.getopt(sys.argv[1:], "ho:u:l:t:r:b:x:y:g:d:c:", ["help", "output", "units", "left", "top", "right", "bottom", "x", "y", "grid", "drillhole", "copper"])
  31. except getopt.GetoptError, err:
  32. # print help information and exit:
  33. print str(err) # will print something like "option -a not recognized"
  34. usage()
  35. sys.exit(2)
  36. outputFile = None
  37. units = None
  38. left = None
  39. right = None
  40. top = None
  41. bottom = None
  42. x = None
  43. y = None
  44. gridSize = None
  45. drillHole = None
  46. ringThickness = None
  47. for o, a in opts:
  48. if o in ("-o", "--output", "--outputfile"):
  49. outputFile = a
  50. elif o in ("-h", "--help"):
  51. usage()
  52. sys.exit(2)
  53. elif o in ("-u", "--units"):
  54. units = a
  55. elif o in ("-l", "--left"):
  56. left = float(a)
  57. elif o in ("-r", "--right"):
  58. right = float(a)
  59. elif o in ("-t", "--top"):
  60. top = float(a)
  61. elif o in ("-b", "--bottom"):
  62. bottom = float(a)
  63. elif o in ("-x"):
  64. x = int(a)
  65. elif o in ("-y"):
  66. y = int(a)
  67. elif o in ("-g", "--grid", "--gridsize"):
  68. gridSize = float(a)
  69. elif o in ("-d", "--drill", "--drillhole"):
  70. drillHole = float(a)
  71. elif o in ("-c", "--ring", "--ringthickness", "--thickness", "--copper", "--copperthickness"):
  72. ringThickness = float(a)
  73. else:
  74. print ("params %s %s" % (o, a))
  75. assert False, "unhandled option "
  76. for thing in (outputFile, units, left, right, top, bottom, x, y, gridSize, drillHole, ringThickness):
  77. if(not(thing)):
  78. usage()
  79. sys.exit(2)
  80. if units == 'mm':
  81. left /= 25.4
  82. right /= 25.4
  83. top /= 25.4
  84. bottom /= 25.4
  85. gridSize /= 25.4
  86. drillHole /= 25.4
  87. ringThickness /= 25.4
  88. width = left + right + ((x - 1) * gridSize)
  89. width000 = width * 1000
  90. height = top + bottom + ((y - 1) * gridSize)
  91. height000 = height * 1000
  92. radius = (drillHole + ringThickness) / 2.0
  93. svg = "<?xml version='1.0' encoding='UTF-8' standalone='no'?>\n"
  94. svg += "<svg xmlns='http://www.w3.org/2000/svg' version='1.2' baseProfile='tiny' x='0in' y='0in' width='%fin' height='%fin' viewBox='0 0 %f %f'>\n" % (width, height, width000, height000)
  95. svg += "<g id='silkscreen'>\n"
  96. #across the top:
  97. svg += "<line x1='4' y1='4' x2='%f' y2='4' stroke='#ffffff' stroke-width='8' stroke-linecap='round' />\n" % (width000 - 8)
  98. #across the bottom:
  99. svg += "<line x1='4' y1='%f' x2='%f' y2='%f' stroke='#ffffff' stroke-width='8' stroke-linecap='round' />\n" % (height000 - 8, width000 - 8, height000 - 8)
  100. #down left with a break in the middle
  101. svg += "<line x1='4' y1='4' x2='4' y2='%f' stroke='#ffffff' stroke-width='8' stroke-linecap='round' />\n" % ((height000 / 2.0) - (1000 * gridSize / 2.0))
  102. svg += "<line x1='4' y1='%f' x2='4' y2='%f' stroke='#ffffff' stroke-width='8' stroke-linecap='round' />\n" % ((height000 / 2.0) + (1000 * gridSize / 2.0), height000 - 8)
  103. #down right
  104. svg += "<line x1='%f' y1='4' x2='%f' y2='%f' stroke='#ffffff' stroke-width='8' stroke-linecap='round' />\n" % (width000 - 8, width000 - 8, height000 - 8)
  105. svg += "</g>\n"
  106. svg += "<g id='copper0'>\n"
  107. cy = top
  108. for iy in range(y):
  109. cx = left
  110. for ix in range(x):
  111. svg += "<circle id='connector%dpin' fill='none' cx='%f' cy='%f' r='%f' stroke='rgb(255, 191, 0)' stroke-width='%f' />\n" % (ix + ((y - iy -1) * x), cx * 1000, cy * 1000, radius * 1000, ringThickness * 1000)
  112. if (ix == 0 and iy == y - 1):
  113. svg += "<rect x='%f' y='%f' width='%f' height='%f' fill='none' stroke='rgb(255, 191, 0)' stroke-width='%f'/>\n" % ((cx - radius) * 1000, (cy - radius) * 1000, radius * 1000 * 2, radius * 1000 * 2, ringThickness * 1000)
  114. cx += gridSize
  115. cy += gridSize
  116. svg += "</g>\n"
  117. svg += "</svg>"
  118. infile = open(outputFile, "w")
  119. infile.write(svg);
  120. infile.close();
  121. if __name__ == "__main__":
  122. main()