Root/emqbit-utilities/kicad/kccomp.py

1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3"""
4Copyright (C) 2010 Andres Calderon, andres.calderon@emqbit.com
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20"""
21
22from datetime import datetime
23from math import ceil
24
25
26class KcLibrary:
27  def __init__(self, name):
28    self.name = name
29    self.parts = []
30  
31  def add_part(self, unit):
32    self.parts.append(unit)
33
34  def write(self):
35    of = open(self.name,'w')
36    
37    of.writelines('EESchema-LIBRARY Version 2.3 Date: ' + datetime.now().strftime("%d/%m/%Y-%H:%M:%S") + '\n\n')
38    
39    for p in self.parts:
40      p.write(of)
41
42class KcComponent:
43  def __init__(self, part):
44    self.part = part
45    self.units = []
46        
47  def add_unit(self, unit):
48    self.units.append(unit)
49    
50  def write(self, of):
51      of.writelines('DEF ' + self.part + " U 0 40 Y Y " + str(len(self.units)) + " F N\n")
52      of.writelines('F0 "U" 0 100 70 H V C C\n')
53      of.writelines('F1 "' + self.part + '" 0 -100 70 H V C C\n')
54      of.writelines('DRAW\n')
55
56      for u in self.units:
57        u.write(of,self.part)
58
59      of.writelines('ENDDRAW\n')
60      of.writelines('ENDDEF\n')
61
62class KcUnit:
63  def __init__(self, id_unit):
64    self.id_unit = id_unit
65    self.top_pins = []
66    self.bot_pins = []
67    self.left_pins = []
68    self.right_pins = []
69
70  def write(self, of, part):
71    ll = max(self.left_pins, key=lambda KcPin: len(KcPin.name))
72    lr = max(self.right_pins, key=lambda KcPin: len(KcPin.name))
73    sl = (len( ll.name + " " + part + " " + lr.name))/2
74       
75    H = max(len(self.left_pins),len(self.right_pins),10)/2*100
76    W = max(len(self.top_pins)*60+len(ll.name)*40+40,len(self.bot_pins)*60+len(lr.name)*40+40,sl/2*100)
77    W = int(ceil(W/100.0)*100)
78
79    of.writelines('S ' + str(-W) + ' ' + str(-H-100) + ' ' + str(W) + ' ' + str(H+100) + ' ' + str(self.id_unit) +' 1 0 f\n')
80  
81    self.write_pin(of,self.left_pins, -W-300, (len(self.right_pins)/2*100),0,-100,'R')
82    self.write_pin(of,self.right_pins, W+300,-(len(self.right_pins)/2*100),0, 100,'L')
83
84    self.write_pin(of,self.top_pins,-(len(self.top_pins)/2*100), H+400,100,0,'D')
85    self.write_pin(of,self.bot_pins,-(len(self.bot_pins)/2*100),-H-400,100,0,'U')
86            
87  def write_pin(self, of, pins, x, y, dx, dy, d):
88    for signal in pins:
89      of.writelines('X ' + signal.name + ' ' + signal.pin + ' ' + str(x) + ' ' + str(y) +' 300 ' + d + ' 60 60 ' + str(self.id_unit) + ' 1 ' + signal.electrical_type + ' ' + signal.pin_type + '\n')
90      x = x + dx
91      y = y + dy
92
93class KcPin:
94  def __init__(self, name, pin , electrical_type='P', pin_type=''):
95    self.pin = pin
96    self.name = name
97    self.pin_type = pin_type
98    self.electrical_type = electrical_type

Archive Download this file

Branches:
master



interactive