summaryrefslogtreecommitdiff
path: root/trunk/users/metalab/kicad-scripts/python/component_generator.py
blob: 212cebb098cbabee097a8699bd54513cb5136881 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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()