PadArray.py 6.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#  PadArray.py
#
#  Copyright 2014 john <john@johndev>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#

from __future__ import division

import math
25 26 27 28 29 30 31 32 33 34
import pcbnew

class PadMaker:
    """
    Useful construction functions for common types of pads
    """

    def __init__(self, module):
        self.module = module

35
    def THPad(self, w, l, drill, shape=pcbnew.PAD_OVAL):
36 37 38 39 40 41 42
        pad = pcbnew.D_PAD(self.module)

        pad.SetSize(pcbnew.wxSize(l, w))

        pad.SetShape(shape)

        pad.SetAttribute(pcbnew.PAD_STANDARD)
43
        pad.SetLayerSet(pad.StandardMask())
44 45 46 47
        pad.SetDrillSize(pcbnew.wxSize(drill, drill))

        return pad

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    def THRoundPad(self, size, drill):
        pad = self.THPad(size, size, drill, shape=pcbnew.PAD_CIRCLE)
        return pad

    def NPTHRoundPad(self, drill):
        pad = pcbnew.D_PAD(self.module)

        pad.SetSize(pcbnew.wxSize(drill, drill))

        pad.SetShape(pcbnew.PAD_CIRCLE)

        pad.SetAttribute(pcbnew.PAD_HOLE_NOT_PLATED)
        pad.SetLayerSet(pad.UnplatedHoleMask())
        pad.SetDrillSize(pcbnew.wxSize(drill, drill))
        return pad

    def SMDPad(self, w, l, shape=pcbnew.PAD_RECT):
65 66 67 68 69 70
        pad = pcbnew.D_PAD(self.module)
        pad.SetSize(pcbnew.wxSize(l, w))

        pad.SetShape(shape)

        pad.SetAttribute(pcbnew.PAD_SMD)
71
        pad.SetLayerSet(pad.SMDMask())
72 73 74 75

        return pad

    def SMTRoundPad(self, size):
76
        pad = self.SMDPad(size, size, shape=pcbnew.PAD_CIRCLE)
77 78
        return pad

79

80 81 82
class PadArray:

    def __init__(self):
83 84 85 86 87 88 89 90 91 92 93 94
        self.firstPadNum = 1
        self.pinNames = None
        self.firstPad = None

    def SetPinNames(self, pinNames):
        """
        Set a name for all the pins
        """
        self.pinNames = pinNames

    def SetFirstPadType(self, firstPad):
        self.firstPad = firstPad
95 96

    def SetFirstPadInArray(self, fpNum):
97
        self.firstPadNum = fpNum
98 99 100 101

    def AddPad(self, pad):
        self.pad.GetParent().Add(pad)

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    def GetPad(self, is_first_pad, pos):

        if (self.firstPad and is_first_pad):
            pad = self.firstPad
        else:
            pad = self.pad

        # create a new pad with same characteristics
        pad = pad.Duplicate()

        pad.SetPos0(pos)
        pad.SetPosition(pos)

        return pad

    def GetName(self, *args, **kwargs):

        if self.pinNames is None:
            return self.NamingFunction(*args, **kwargs)

        return self.pinNames

    def NamingFunction(self, *args, **kwargs):
        """
        Implement this as needed for each array type
        """
        raise NotImplementedError;


131 132
class PadGridArray(PadArray):

133 134
    def __init__(self, pad, nx, ny, px, py, centre=pcbnew.wxPoint(0, 0)):
        PadArray.__init__(self)
135 136 137 138 139 140 141
        # this pad is more of a "context", we will use it as a source of
        # pad data, but not actually add it
        self.pad = pad
        self.nx = int(nx)
        self.ny = int(ny)
        self.px = px
        self.py = py
142
        self.centre = centre
143 144 145

    # handy utility function 1 - A, 2 - B, 26 - AA, etc
    # aIndex = 0 for 0 - A
146 147 148 149
    # alphabet = set of allowable chars if not A-Z,
    #            eg ABCDEFGHJKLMNPRTUVWY for BGA
    def AlphaNameFromNumber(self, n, aIndex=1,
                            alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
150

151 152
        div, mod = divmod(n - aIndex, len(alphabet))
        alpha = alphabet[mod]
153 154

        if div > 0:
155
            return self.AlphaNameFromNumber(div, aIndex, alphabet) + alpha
156

157
        return alpha
158 159 160

    # right to left, top to bottom
    def NamingFunction(self, x, y):
161
        return self.firstPadNum + (self.nx * y + x)
162 163

    #relocate the pad and add it as many times as we need
164 165 166 167
    def AddPadsToModule(self, dc):

        pin1posX = self.centre.x - self.px * (self.nx - 1) / 2
        pin1posY = self.centre.y - self.py * (self.ny - 1) / 2
168 169 170

        for x in range(0, self.nx):

171
            posX = pin1posX + (x * self.px)
172

173 174 175 176
            for y in range(self.ny):
                posY = pin1posY + (self.py * y)

                pos = dc.TransformPoint(posX, posY)
177

178
                pad = self.GetPad(x == 0 and y == 0, pos)
179

180
                pad.SetPadName(self.GetName(x,y))
181 182 183

                self.AddPad(pad)

184

185 186
class PadLineArray(PadGridArray):

187 188
    def __init__(self, pad, n, pitch, isVertical,
                 centre=pcbnew.wxPoint(0, 0)):
189 190

        if isVertical:
191
            PadGridArray.__init__(self, pad, 1, n, 0, pitch, centre)
192
        else:
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
            PadGridArray.__init__(self, pad, n, 1, pitch, 0, centre)

class PadCircleArray(PadArray):

    def __init__(self, pad, n, r, angle_offset=0, centre=pcbnew.wxPoint(0, 0),
                 clockwise=True):
        PadArray.__init__(self)
        # this pad is more of a "context", we will use it as a source of
        # pad data, but not actually add it
        self.pad = pad
        self.n = int(n)
        self.r = r
        self.angle_offset = angle_offset
        self.centre = centre
        self.clockwise = clockwise

    # around the circle, CW or CCW according to the flag
    def NamingFunction(self, n):
        return str(self.firstPadNum + n)

    #relocate the pad and add it as many times as we need
    def AddPadsToModule(self, dc):

        for pin in range(0, self.n):

            angle = self.angle_offset + (360 / self.n) * pin

            if not self.clockwise:
                angle = -angle

            pos_x = math.sin(angle * math.pi / 180) * self.r
            pos_y = -math.cos(angle  * math.pi / 180) * self.r

            pos = dc.TransformPoint(pos_x, pos_y)

            pad = self.GetPad(pin == 0, pos)

            pad.SetPadName(self.GetName(pin))

            self.AddPad(pad)

class PadCustomArray(PadArray):
    """
    Layout pads according to a custom array of [x,y] data
    """

    def __init__(self, pad, array):
        PadArray.__init__(self)
        self.pad = pad

        self.array = array

    def NamingFunction(self, n):
        return str(self.firstPadNum + n)

    #relocate the pad and add it as many times as we need
    def AddPadsToModule(self, dc):

        for i in range(len(self.array)):

            pos = dc.TransformPoint(self.array[i][0], self.array[i][1])

            pad = self.GetPad(i == 0, pos)

            pad.SetPadName(self.GetName(i))

            self.AddPad(pad)