summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/kicad-scripts
diff options
context:
space:
mode:
authorwizard23 <wizard23>2008-11-09 06:49:34 +0000
committerwizard23 <wizard23@cb376a5e-1013-0410-a455-b6b1f9ac8223>2008-11-09 06:49:34 +0000
commit52a2c38427fc0138d180c7a1b68ed3b073832a7c (patch)
tree3ae5c48aa56dfad334a3064ff891cbd2705c0457 /trunk/users/metalab/kicad-scripts
parent14064f372cd6b6fa9e44b83828f5e7a92115dc02 (diff)
downloadreprap-52a2c38427fc0138d180c7a1b68ed3b073832a7c.tar.gz
reprap-52a2c38427fc0138d180c7a1b68ed3b073832a7c.zip
started wotrking on a python script that generates serpentine kicad tracks to generate heating traces
git-svn-id: https://reprap.svn.sourceforge.net/svnroot/reprap@2227 cb376a5e-1013-0410-a455-b6b1f9ac8223
Diffstat (limited to 'trunk/users/metalab/kicad-scripts')
-rw-r--r--trunk/users/metalab/kicad-scripts/python/component_generator.py97
-rw-r--r--trunk/users/metalab/kicad-scripts/python/generate_meander.py30
-rw-r--r--trunk/users/metalab/kicad-scripts/python/kicad.py220
-rw-r--r--trunk/users/metalab/kicad-scripts/python/kicad_templates.py73
-rw-r--r--trunk/users/metalab/kicad-scripts/python/kicad_templates_brd.py63
-rw-r--r--trunk/users/metalab/kicad-scripts/python/module_generator.py60
6 files changed, 543 insertions, 0 deletions
diff --git a/trunk/users/metalab/kicad-scripts/python/component_generator.py b/trunk/users/metalab/kicad-scripts/python/component_generator.py
new file mode 100644
index 00000000..212cebb0
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/component_generator.py
@@ -0,0 +1,97 @@
+from __future__ import with_statement
+import sys
+
+from kicad import *
+
+PINGROUP_SPACING = 200
+PIN_SPACING = 100
+COMPONENT_MARGIN = 400
+PIN_LENGTH = 300
+
+
+def countPins(groups):
+ l = 0
+ for group in groups:
+ l += len(group)
+ return l
+
+def reversePins(groups):
+ groups.reverse()
+ for group in groups:
+ group.reverse()
+
+pinGroupsLeft = [[]]
+pinGroupsRight = [[]]
+pinGroupsTop = [[]]
+pinGroupsBottom = [[]]
+
+allPinGroups = [pinGroupsRight, pinGroupsTop, pinGroupsLeft, pinGroupsBottom]
+
+activePinGroups = pinGroupsRight
+
+
+with open(sys.argv[1]) as f:
+ ComponentName = f.readline().strip()
+ component = kicad_component(ComponentName)
+ for line in f:
+ line = line.strip()
+ #print line
+
+ if (line == '[right]'):
+ activePinGroups = pinGroupsRight
+ elif (line == '[top]'):
+ activePinGroups = pinGroupsTop
+ elif (line == '[left]'):
+ activePinGroups = pinGroupsLeft
+ elif (line == '[bottom]'):
+ activePinGroups = pinGroupsBottom
+ elif (line == ''):
+ if (len(activePinGroups[-1]) > 0):
+ activePinGroups.append([])
+ else:
+ pinData = line.split()
+ pin = kicad_pin(pinData[0], int(pinData[1]), 0, 0, 0, pinData[2], PIN_LENGTH)
+ component.add(pin)
+ activePinGroups[-1].append(pin)
+
+
+def calculateGroupsSize(groups):
+ if (len(groups[-1]) == 0):
+ groups.pop()
+ groupCount = len(groups)
+ pinCount = countPins(groups)
+ return (pinCount-groupCount)*PIN_SPACING + (len(groups)-1)*PINGROUP_SPACING
+
+
+sideSizes = [calculateGroupsSize(groups) for groups in allPinGroups]
+maxSize = [max(sideSizes[0], sideSizes[2]), max(sideSizes[1], sideSizes[3])]
+
+side = 0
+for pinGroups in allPinGroups:
+
+ currentPos = -sideSizes[side]/2
+ otherDim = maxSize[(side+1)%2]/2 + COMPONENT_MARGIN + PIN_LENGTH
+
+ # we always want the pin ordering to be from left to right and top to bottom
+ if (pinGroups == pinGroupsRight or pinGroups == pinGroupsTop):
+ #print "REVERSING!!!"
+ #print pinGroups
+ reversePins(pinGroups)
+ #print pinGroups
+
+ for pinGroup in pinGroups:
+ for pin in pinGroup:
+ pin.PinPositionY = currentPos
+ pin.PinPositionX = otherDim
+ currentPos += PIN_SPACING
+ pin.rotateCCW(side)
+ currentPos += PINGROUP_SPACING - PIN_SPACING
+ side += 1
+
+rectangle = kicad_rectangle(-(maxSize[1]/2+COMPONENT_MARGIN), \
+ maxSize[0]/2+COMPONENT_MARGIN, \
+ maxSize[1]/2+COMPONENT_MARGIN, \
+ -(maxSize[0]/2+COMPONENT_MARGIN))
+component.add(rectangle)
+
+print component.render()
diff --git a/trunk/users/metalab/kicad-scripts/python/generate_meander.py b/trunk/users/metalab/kicad-scripts/python/generate_meander.py
new file mode 100644
index 00000000..e32fe9c7
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/generate_meander.py
@@ -0,0 +1,30 @@
+from kicad import *
+
+### ALL measurements are in mm
+
+
+class meander(kicad_container):
+ def __init__(self, dim1, dim2, width, n):
+ kicad_container.__init__(self)
+ self.length = 0
+ for i in range(0, n):
+ self.length += dim1 + dim2
+ if i % 2 == 0:
+ self.add(kicad_pcb_track(0, i*dim2, dim1, i*dim2, width, 0))
+ self.add(kicad_pcb_track(dim1, i*dim2, dim1, (i+1)*dim2, width, 0))
+ else:
+ self.add(kicad_pcb_track(dim1, i*dim2, 0, i*dim2, width, 0))
+ self.add(kicad_pcb_track(0, i*dim2, 0, (i+1)*dim2, width, 0))
+
+
+
+
+
+
+
+module = kicad_board('RepRap Heating Plate')
+p = meander(100, 2, 0.8, 1000/102)
+module.add(p)
+
+print module.render()
+
diff --git a/trunk/users/metalab/kicad-scripts/python/kicad.py b/trunk/users/metalab/kicad-scripts/python/kicad.py
new file mode 100644
index 00000000..5494db24
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/kicad.py
@@ -0,0 +1,220 @@
+from kicad_templates import *
+from kicad_templates_brd import *
+
+class kicad_object(object):
+ def __init__(self):
+ pass
+
+ def rotateCCW(self, times):
+ for i in range(0, times):
+ self.rotate90CCW()
+
+class kicad_container(kicad_object):
+ def __init__(self):
+ kicad_object.__init__(self)
+ self.children = []
+
+ def add(self, child):
+ self.children.append(child)
+
+ def addall(self, children):
+ for child in children:
+ self.add(child)
+
+ def renderChildren(self):
+ s = ""
+ for child in self.children:
+ s += child.render()
+ return s
+
+ def render(self):
+ return self.renderChildren()
+
+################################
+### Component Classes
+################################
+
+# Component is the main class for Schematic Symbols
+# Dimesnions are always in 1/1000 inch - no conversion to mm is done
+class kicad_component(kicad_container):
+ def __init__(self, ComponentName):
+ kicad_container.__init__(self)
+ self.ComponentName = ComponentName
+
+ def render(self):
+ s = COMPONENT_TEMPLATE_BEGIN % { \
+ 'ComponentName' : self.ComponentName \
+ }
+ s += self.renderChildren()
+ s += COMPONENT_TEMPLATE_END
+ return s
+
+class kicad_pin(kicad_object):
+ def __init__(self, PinName, PinNum, PinPositionX, PinPositionY, PinOrientation, PinType, PinLength):
+ kicad_object.__init__(self)
+ self.PinName = PinName
+ self.PinNum = PinNum
+ self.PinPositionX = PinPositionX
+ self.PinPositionY = PinPositionY
+ self.PinOrientation = PinOrientation % 4
+ self.PinType = PinType
+ self.PinLength = PinLength
+
+ def rotate90CCW(self):
+ self.PinPositionX, self.PinPositionY = -self.PinPositionY, self.PinPositionX
+ self.PinOrientation = (self.PinOrientation+1)%4
+
+ def render(self):
+ return COMPONENT_PIN_TEMPLATE % { \
+ 'PinName' : self.PinName, \
+ 'PinNum' : self.PinNum, \
+ 'PinPositionX' : self.PinPositionX, \
+ 'PinPositionY' : self.PinPositionY, \
+ 'PinOrientation' : COMPONENT_PIN_ROTATIONS[self.PinOrientation], \
+ 'PinType' : self.PinType, \
+ 'PinLength' : self.PinLength, \
+ }
+
+class kicad_rectangle(kicad_object):
+ def __init__(self, X1, Y1, X2, Y2):
+ kicad_object.__init__(self)
+ self.X1 = X1
+ self.Y1 = Y1
+ self.X2 = X2
+ self.Y2 = Y2
+
+ def rotate90CCW(self):
+ self.X1, self.Y1 = self.Y1, -self.X1
+ self.X2, self.Y2 = self.Y2, -self.X2
+
+ def render(self):
+ return COMPONENT_RECTANGLE_TEMPLATE % { \
+ 'X1' : self.X1, \
+ 'Y1' : self.Y1, \
+ 'X2' : self.X2, \
+ 'Y2' : self.Y2, \
+ }
+
+#################################
+### Module Classes
+#################################
+
+# kicad_module is the main class for PCB Footprints
+# Dimesnions are always in mm and translated to 1/1000 inch at render time
+class kicad_module(kicad_container):
+ def __init__(self, ModuleName):
+ kicad_container.__init__(self)
+ self.ModuleName = ModuleName
+
+ def render(self):
+ s = MODULE_TEMPLATE_BEGIN % { \
+ 'ModuleName' : self.ModuleName \
+ }
+ s += self.renderChildren()
+ s += MODULE_TEMPLATE_END
+ return s
+
+class kicad_pad(kicad_object):
+ def __init__(self, num, sizeX, sizeY, posX, posY):
+ kicad_object.__init__(self)
+ self.PadNum = num
+ self.PadShape = "R"
+ self.PadSizeX = sizeX
+ self.PadSizeY = sizeY
+ self.PadPositionX = posX
+ self.PadPositionY = posY
+ self.Rotation = 0
+
+ def rotate90CCW(self):
+ self.PadPositionX, self.PadPositionY = self.PadPositionY, -self.PadPositionX
+ self.Rotation = (self.Rotation + 900) % 3600
+
+ def render(self):
+ return MODULE_PAD_TEMPLATE % { \
+ 'PadNum' : self.PadNum, \
+ 'PadShape' : self.PadShape, \
+ 'PadSizeX' : self.PadSizeX * MM2MILL, \
+ 'PadSizeY' : self.PadSizeY * MM2MILL, \
+ 'PadPositionX' : self.PadPositionX * MM2MILL, \
+ 'PadPositionY' : self.PadPositionY * MM2MILL, \
+ 'Rotation' : self.Rotation, \
+ }
+
+class kicad_drawing(kicad_object):
+ def __init__(self, Command, X1, Y1, X2, Y2, Width):
+ kicad_object.__init__(self)
+ self.Command = Command
+ self.X1 = X1
+ self.Y1 = Y1
+ self.X2 = X2
+ self.Y2 = Y2
+ self.Width = Width
+
+ def rotate90CCW(self):
+ self.X1, self.Y1 = self.Y1, -self.X1
+ self.X2, self.Y2 = self.Y2, -self.X2
+
+ def render(self):
+ return MODULE_DRAWING_TEMPLATE % { \
+ 'Command' : self.Command, \
+ 'X1' : self.X1 * MM2MILL, \
+ 'Y1' : self.Y1 * MM2MILL, \
+ 'X2' : self.X2 * MM2MILL, \
+ 'Y2' : self.Y2 * MM2MILL, \
+ 'Width' : self.Width * MM2MILL, \
+ }
+
+class kicad_line(kicad_drawing):
+ def __init__(self, X1, Y1, X2, Y2, Width):
+ kicad_drawing.__init__(self, 'DS', X1, Y1, X2, Y2, Width)
+
+class kicad_circle(kicad_drawing):
+ def __init__(self, X1, Y1, X2, Y2, Width):
+ kicad_drawing.__init__(self, 'DC', X1, Y1, X2, Y2, Width)
+
+
+##################################
+## PCB classes - for .brd files ##
+##################################
+
+class kicad_board(kicad_container):
+ def __init__(self, ModuleName):
+ kicad_container.__init__(self)
+ self.ModuleName = ModuleName
+
+ def render(self):
+ s = KICAD_BRD_TEMPLATE % { \
+ 'Content' : self.renderChildren() \
+ }
+ return s
+
+
+class kicad_pcb_track_via(kicad_drawing):
+ def __init__(self, X1, Y1, X2, Y2, Width, Layer, Type, ViaFlag):
+ kicad_drawing.__init__(self, 'Po', X1, Y1, X2, Y2, Width)
+ self.Layer = Layer
+ self.Command2 = "De"
+ self.Type = Type
+ self.ViaFlag = ViaFlag
+
+ def render(self):
+ return PCB_TRACK_TEMPLATE % { \
+ 'Command' : self.Command, \
+ 'Command2' : self.Command2, \
+ 'Layer' : self.Layer, \
+ 'Type' : self.Type, \
+ 'ViaFlag' : self.ViaFlag, \
+ 'X1' : self.X1 * MM2MILL, \
+ 'Y1' : self.Y1 * MM2MILL, \
+ 'X2' : self.X2 * MM2MILL, \
+ 'Y2' : self.Y2 * MM2MILL, \
+ 'Width' : self.Width * MM2MILL, \
+ }
+
+class kicad_pcb_track(kicad_pcb_track_via):
+ def __init__(self, X1, Y1, X2, Y2, Width, Layer):
+ kicad_pcb_track_via.__init__(self, X1, Y1, X2, Y2, Width, Layer, 0, 0)
+
+class kicad_pcb_via(kicad_pcb_track_via):
+ def __init__(self, X1, Y1, X2, Y2, Width, Layer):
+ kicad_pcb_track_via.__init__(self, X1, Y1, X2, Y2, Width, Layer, 3, 1)
diff --git a/trunk/users/metalab/kicad-scripts/python/kicad_templates.py b/trunk/users/metalab/kicad-scripts/python/kicad_templates.py
new file mode 100644
index 00000000..92f225f2
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/kicad_templates.py
@@ -0,0 +1,73 @@
+MM2MILL = 393.700787
+
+#################################
+### Templates for KiCAD symbols
+#################################
+
+COMPONENT_TEMPLATE_BEGIN = """EESchema-LIBRARY Version 2.3 Date: 8/2/2008-15:46:38
+#
+# %(ComponentName)s
+#
+DEF %(ComponentName)s U 0 40 Y Y 1 F N
+F0 "U" 0 -50 60 H V C C
+F1 "%(ComponentName)s" 0 50 60 H V C C
+DRAW
+"""
+#X D 4 100 0 300 D 50 50 1 1 I
+#X C 3 0 50 300 L 50 50 1 1 I
+#X A 1 -700 0 300 R 50 50 1 1 I
+#X B 2 0 -600 300 U 50 50 1 1 I
+
+COMPONENT_TEMPLATE_END = """ENDDRAW
+ENDDEF
+#
+#End Library
+"""
+
+COMPONENT_PIN_TEMPLATE = "X %(PinName)s %(PinNum)d %(PinPositionX)d %(PinPositionY)d %(PinLength)d %(PinOrientation)s 50 50 1 1 %(PinType)s\n"
+
+COMPONENT_PIN_ROTATIONS = ['L', 'D', 'R', 'U']
+
+COMPONENT_RECTANGLE_TEMPLATE = "S %(X1)d %(Y1)d %(X2)d %(Y2)d 0 1 0 N\n"
+
+###################################
+### Templates for PCB footprints
+###################################
+
+MODULE_TEMPLATE_BEGIN = """PCBNEW-LibModule-V1 24/1/2008-21:54:27
+$INDEX
+%(ModuleName)s
+$EndINDEX
+$MODULE %(ModuleName)s
+Po 0 0 0 15 47990905 00000000 ~~
+Li TestChip
+Sc 00000000
+Op 0 0 0
+T0 0 0 600 600 0 120 N V 21 "%(ModuleName)s"
+T1 0 0 600 600 0 120 N V 21 "VAL**"
+"""
+
+MODULE_TEMPLATE_END = """$EndMODULE TestChip
+$EndLIBRARY
+"""
+
+MODULE_PAD_TEMPLATE = """$PAD
+Sh "%(PadNum)d" %(PadShape)s %(PadSizeX)d %(PadSizeY)d 0 0 %(Rotation)d
+Dr 0 0 0)
+At SMD N 00888000
+Ne 0 ""
+Po %(PadPositionX)d %(PadPositionY)d
+$EndPAD
+"""
+
+MODULE_DRAWING_TEMPLATE = "%(Command)s %(X1)d %(Y1)d %(X2)d %(Y2)d %(Width)d 21\n"
+
+###################################
+### Templates for PCB .brd files
+###################################
+
+PCB_TRACK_TEMPLATE = """%(Command)s %(Type)d %(X1)d %(Y1)d %(X2)d %(Y2)d %(Width)d -1
+%(Command2)s %(Layer)d %(ViaFlag)d 0 0 0
+"""
+
+
diff --git a/trunk/users/metalab/kicad-scripts/python/kicad_templates_brd.py b/trunk/users/metalab/kicad-scripts/python/kicad_templates_brd.py
new file mode 100644
index 00000000..8e0f1b48
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/kicad_templates_brd.py
@@ -0,0 +1,63 @@
+KICAD_BRD_TEMPLATE = """PCBNEW-BOARD Version 1 date 9/11/2008-04:06:17
+
+$GENERAL
+LayerCount 2
+Ly 1FFF8001
+Links 0
+NoConn 0
+Di 37361 33774 41639 37726
+Ndraw 0
+Ntrack 7
+Nzone 0
+Nmodule 0
+Nnets 0
+$EndGENERAL
+
+$SHEETDESCR
+Sheet A4 11700 8267
+Title ""
+Date "9 nov 2008"
+Rev ""
+Comp ""
+Comment1 ""
+Comment2 ""
+Comment3 ""
+Comment4 ""
+$EndSHEETDESCR
+
+$SETUP
+InternalUnit 0.000100 INCH
+UserGridSize 0.010000 0.010000 mm
+ZoneGridSize 250
+Layers 2
+Layer[0] Loetseite signal
+Layer[15] BestSeite signal
+TrackWidth 276
+TrackWidthHistory 276
+TrackClearence 60
+ZoneClearence 150
+DrawSegmWidth 150
+EdgeSegmWidth 150
+ViaSize 450
+ViaDrill 250
+ViaSizeHistory 450
+MicroViaSize 200
+MicroViaDrill 80
+MicroViasAllowed 0
+TextPcbWidth 120
+TextPcbSize 600 800
+EdgeModWidth 150
+TextModSize 600 600
+TextModWidth 120
+PadSize 600 600
+PadDrill 320
+AuxiliaryAxisOrg 0 0
+$EndSETUP
+
+$TRACK
+%(Content)s
+$EndTRACK
+$ZONE
+$EndZONE
+$EndBOARD
+"""
diff --git a/trunk/users/metalab/kicad-scripts/python/module_generator.py b/trunk/users/metalab/kicad-scripts/python/module_generator.py
new file mode 100644
index 00000000..a7d723ec
--- /dev/null
+++ b/trunk/users/metalab/kicad-scripts/python/module_generator.py
@@ -0,0 +1,60 @@
+from kicad import *
+
+### ALL measurements are in mm
+
+E = 6.0 # Width of IC
+NE = 10 # Pins on E sides
+D = 6.0 # Height of IC
+ND = 10 # Pins on D sides
+
+E2 = 4.1 # Width of the exposed Pad
+D2 = 4.1 # Height of the exposed Pad
+
+e = 0.5 # spacing between center of pins
+N = (NE+ND)*2 # number of pins
+
+b = 0.25 # width of one pin
+L = 0.4 # height of one pin
+
+drawingWidth = 0.05
+dotSize = 0.1
+
+
+# according to ST Technical note TN0019 the adds should be 0.1mm
+# according to OnSemi AND8086/D the adds should be 0mm
+
+L_add = 0.1 # gets added to the PCB land of the Pin
+b_add = 0.1
+E2_add = 0.1 # gets added to the width of the exposed pad
+D2_add = E2_add
+
+module = kicad_module('TFQN40')
+
+for side in range(0, 2):
+ # Pins on the bottom and top side
+ for pinNr in range(0, NE):
+ p = kicad_pad(1+pinNr+side*(NE+ND), b+b_add, L+L_add, pinNr*e - (NE-1)*e/2, D/2-L/2)
+ p.rotateCCW(side*2)
+ module.add(p)
+ # Package outline bottom and top side
+ l = kicad_line(-E/2, D/2, E/2, D/2, drawingWidth)
+ l.rotateCCW(side*2)
+ module.add(l)
+
+ for pinNr in range(0, ND):
+ p = kicad_pad(1+pinNr+NE+side*(NE+ND), b+b_add, L+L_add, pinNr*e - (ND-1)*e/2, E/2-L/2)
+ p.rotateCCW(side*2+1)
+ module.add(p)
+
+ l = kicad_line(-D/2, E/2, D/2, E/2, drawingWidth)
+ l.rotateCCW(side*2+1)
+ module.add(l)
+
+p = kicad_pad(N+1, E2+E2_add, D2+D2_add, 0, 0)
+module.add(p)
+
+
+pin1Circle = kicad_circle(-(NE-1)*e/2, D/2-L/2, dotSize-(NE-1)*e/2, D/2-L/2, drawingWidth)
+module.add(pin1Circle)
+
+print module.render()