Last active
September 9, 2020 00:45
-
-
Save 1m38/345b409b14a0ab415936e86e7e01e592 to your computer and use it in GitHub Desktop.
KiCad Pcbnewでキースイッチを角度・相対位置指定で配置するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from __future__ import division | |
from __future__ import absolute_import | |
from __future__ import unicode_literals | |
import pcbnew | |
import math | |
pcb = pcbnew.GetBoard() | |
class MyPosition(object): | |
def __init__(self, x, y): | |
self.x = x | |
self.y = y | |
def __add__(self, other): | |
if isinstance(other, MyPosition): | |
return MyPosition(self.x + other.x, self.y + other.y) | |
if isinstance(other, tuple): | |
return self + MyPosition(*other) | |
raise RuntimeError("Unknown object type: " + str(type(other))) | |
def __radd__(self, other): | |
return self + other | |
def __sub__(self, other): | |
if isinstance(other, MyPosition): | |
return self + (-1 * other) | |
if isinstance(other, tuple): | |
return self + (-1 * MyPosition(*other)) | |
def __rsub__(self, other): | |
return self - other | |
def __mul__(self, other): | |
return MyPosition(self.x * other, self.y * other) | |
def __rmul__(self, other): | |
return self * other | |
def __str__(self): | |
return "({:.2f}, {:.2f})".format(self.x, self.y) | |
class SwitchPosition(MyPosition): | |
U_mm = 19.05 # 1U == 19.05mm | |
def __init__(self, x, y, angle_deg): | |
super(SwitchPosition, self).__init__(x, y) | |
self.set_angle(angle_deg) | |
def set_angle(self, angle_deg): | |
self.angle_deg = angle_deg | |
self.angle_rad = math.radians(angle_deg) | |
return self | |
def right(self, u): | |
return SwitchPosition( | |
self.x + u * math.cos(self.angle_rad), | |
self.y + u * math.sin(self.angle_rad), | |
self.angle_deg) | |
def up(self, u): | |
return SwitchPosition( | |
self.x + u * math.cos(math.pi / 2 - self.angle_rad), | |
self.y - u * math.sin(math.pi / 2 - self.angle_rad), | |
self.angle_deg | |
) | |
def __add__(self, other): | |
p = super(SwitchPosition, self).__add__(other) | |
return SwitchPosition(p.x, p.y, self.angle_deg) | |
def __radd__(self, other): | |
return self + other | |
def __sub__(self, other): | |
p = super(SwitchPosition, self).__sub__(other) | |
return SwitchPosition(p.x, p.y, self.angle_deg) | |
def __rsub__(self, other): | |
return self - other | |
def __mul__(self, other): | |
p = super(SwitchPosition, self).__mul__(other) | |
return SwitchPosition(p.x, p.y, self.angle_deg) | |
def __rmul__(self, other): | |
return self * other | |
def __str__(self): | |
return "({:.2f}, {:.2f} angle: {})".format(self.x, self.y, self.angle_deg) | |
def to_mm(self): | |
return self * self.U_mm | |
# main: arrange left hand keys | |
def arrange_left(): | |
P = MyPosition | |
SP = SwitchPosition | |
sw_pos_u = {} | |
# col1 | |
sw_pos_u.update({ | |
"SW1": SP(0, 0, 0), | |
"SW7": SP(0, 1, 0), | |
"SW14": SP(0, 2, 0) | |
}) | |
# col0 | |
sw_pos_u.update({ | |
"SW6": SP(-1, 0.75, 0), | |
"SW13": SP(-1, 1.75, 0) | |
}) | |
# col2 | |
sw_pos_u["SW15"] = sw_pos_u["SW14"].right(0.5).set_angle(6).right(0.5).up(0.5) | |
sw_pos_u["SW8"] = sw_pos_u["SW15"].up(1) | |
sw_pos_u["SW2"] = sw_pos_u["SW8"].up(1) | |
# col3 | |
sw_pos_u["SW16"] = sw_pos_u["SW15"].right(0.5).set_angle(10).right(0.5).up(0.5) | |
sw_pos_u["SW9"] = sw_pos_u["SW16"].up(1) | |
sw_pos_u["SW3"] = sw_pos_u["SW9"].up(1) | |
# col4 | |
sw_pos_u["SW4"] = sw_pos_u["SW3"].right(1).up(-0.25) | |
sw_pos_u["SW10"] = sw_pos_u["SW4"].up(-1) | |
sw_pos_u["SW17"] = sw_pos_u["SW10"].up(-1) | |
# col5 | |
sw_pos_u["SW5"] = sw_pos_u["SW4"].right(1).up(-0.125) | |
sw_pos_u["SW11"] = sw_pos_u["SW5"].up(-1) | |
sw_pos_u["SW18"] = sw_pos_u["SW11"].up(-1) | |
# col6 | |
sw_pos_u["SW12"] = sw_pos_u["SW11"].right(1).up(-0.125) | |
sw_pos_u["SW19"] = sw_pos_u["SW12"].up(-1) | |
# thumb | |
sw_pos_u["SW21"] = sw_pos_u["SW18"].right(-0.5).up(-0.5).set_angle(18).right(0.5).up(-0.5) | |
sw_pos_u["SW20"] = sw_pos_u["SW21"].right(-0.5).up(-0.5).set_angle(10).right(-0.5).up(0.5) | |
sw_pos_u["SW22"] = sw_pos_u["SW21"].right(0.5).up(-0.5).set_angle(26).right(0.5).up(0.5) | |
sw_pos_u["SW23"] = sw_pos_u["SW21"].up(-1) | |
# base_pos: SW14 | |
base_pos_mm = P(52.8, 51.575) | |
# U -> mm | |
sw_pos_mm = {r: v.to_mm() + base_pos_mm for r, v in sw_pos_u.items()} | |
# # dbg: print | |
# for r in sorted(sw_pos_mm.keys()): | |
# print("{}: {}".format(r, sw_pos_mm[r])) | |
# move | |
for r, pos in sw_pos_mm.items(): | |
m = pcb.FindModuleByReference(r) | |
m.SetPosition(pcbnew.wxPointMM(pos.x, pos.y)) | |
m.SetOrientationDegrees(-pos.angle_deg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MIT License
Copyright (c) 2020 1m38
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.