Files
kicad-action-scripts/CircularZone/CircularZone.py
T

133 lines
4.5 KiB
Python

from math import *
import pcbnew
from .CircularZoneDlg import CircularZoneDlg
import wx
import os
___version__ = "2.0"
class CircularZone(pcbnew.ActionPlugin):
def defaults(self):
self.name = "Circular Zone - Keepout Zone Generator\nversion " + ___version__
self.category = "Modify PCB"
self.description = "Create a Circular Zone or a Circular Keepout Zone\nversion " + ___version__
self.icon_file_name = os.path.join(os.path.dirname(__file__), "./round_keepout_area.png")
self.show_toolbar_button = True
def build(self, center_x, center_y, radius, keepout, edge_count, layer_id):
sp = pcbnew.SHAPE_POLY_SET()
sp.NewOutline()
cnt = int(edge_count)
for i in range(cnt):
x = int(center_x + radius * cos(i * 2 * pi / cnt))
y = int(center_y + radius * sin(i * 2 * pi / cnt))
sp.Append(x, y)
sp.thisown = 0
zone = pcbnew.ZONE(self.pcb)
zone.SetOutline(sp)
zone.SetLayer(layer_id)
zone.SetIsRuleArea(keepout)
if keepout:
zone.SetDoNotAllowFootprints(True)
zone.thisown = 0
self.pcb.Add(zone)
def Warn(self, message, caption='Warning!'):
dlg = wx.MessageDialog(
None, message, caption, wx.OK | wx.ICON_WARNING)
dlg.ShowModal()
dlg.Destroy()
def CheckInput(self, value, data):
val = None
try:
val = float(value)
if val == 0:
raise Exception("Invalid")
except:
self.Warn(
"Invalid parameter for %s: Must be a positive number" % data)
val = None
return val
def Run(self):
self.pcb = pcbnew.GetBoard()
a = CircularZoneDlg(None)
x = 0
y = 0
reference = None
for module in self.pcb.Footprints():
for pad in module.Pads():
if pad.IsSelected():
pad_pos = pad.GetPosition()
if pad_pos.x == 0 and pad_pos.y == 0:
fp_pos = module.GetPosition()
x = fp_pos.x
y = fp_pos.y
else:
x = pad_pos.x
y = pad_pos.y
pad_name = pad.GetPadName()
pad_num = pad.GetNumber()
if pad_name and pad_name != "":
reference = module.GetReference() + " pad " + str(pad_name)
else:
reference = module.GetReference() + " pad " + str(pad_num)
break
if reference:
break
for module in self.pcb.Footprints():
if module.IsSelected():
x = module.GetPosition().x
y = module.GetPosition().y
reference = module.GetReference()
break
if reference is None:
a.m_comment.SetLabel("No reference position: start at origin [0,0]")
font = a.m_comment.GetFont()
font.SetWeight(wx.FONTWEIGHT_BOLD)
a.m_comment.SetFont(font)
a.m_comment.SetForegroundColour(wx.RED)
else:
a.m_comment.SetLabel("Using %s as position reference" % reference)
font = a.m_comment.GetFont()
font.SetWeight(wx.FONTWEIGHT_BOLD)
a.m_comment.SetFont(font)
a.m_comment.SetForegroundColour(wx.BLACK)
a.m_radiusMM.SetValue("10")
for l in range(pcbnew.PCB_LAYER_ID_COUNT):
a.m_comboBoxLayer.Append(pcbnew.GetBoard().GetLayerName(l))
a.m_comboBoxLayer.Select(0)
modal_result = a.ShowModal()
segment = self.CheckInput(
a.m_textCtrl_seg.GetValue(), "segment number")
dimension_value = self.CheckInput(a.m_radiusMM.GetValue(), "dimension value")
is_diameter = a.m_radio_diameter.GetValue()
if segment is not None and dimension_value is not None:
if modal_result == wx.ID_OK:
keepout = a.m_radio_out.GetValue()
layer_name = a.m_comboBoxLayer.GetStringSelection()
layer_id = a.m_comboBoxLayer.FindString(layer_name)
if is_diameter:
radius = dimension_value / 2.0
else:
radius = dimension_value
self.build(x, y, int(radius * 1e6), keepout, segment, layer_id)
a.Destroy()
CircularZone().register()