- numeric KiCad version check (string compare broke on 10.x) - GetBoardPolygonOutlines, PCB_VIA.GetWidth, PAD.GetEffectivePolygon: KiCad 10 API - default grid 2.5 mm - dialog values saved to ViaStitching.json in KiCad user settings dir - net list: only nets with a zone, GND first and preselected - add metadata.json - README: KiCad 10
182 lines
7.0 KiB
Python
182 lines
7.0 KiB
Python
#
|
|
# FillAreaAction.py
|
|
#
|
|
# Copyright 2017 JS Reynaud <js.reynaud@gmail.com>
|
|
#
|
|
# 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 print_function
|
|
import pcbnew
|
|
import wx
|
|
from . import FillArea
|
|
from . import FillAreaDialog
|
|
import os
|
|
import json
|
|
|
|
|
|
def PopulateNets(anet, dlg):
|
|
"""Fill the net list with nets that have a zone (GND first).
|
|
|
|
Preselects GND; if the board has no GND zone, falls back to anet
|
|
(last used net), then to the first net in the list.
|
|
"""
|
|
netnames = sorted(set(zone.GetNetname() for zone in pcbnew.GetBoard().Zones()) - {""})
|
|
if "GND" in netnames:
|
|
netnames.remove("GND")
|
|
netnames.insert(0, "GND")
|
|
dlg.m_cbNet.Set(netnames)
|
|
for name in ("GND", anet):
|
|
if name:
|
|
index = dlg.m_cbNet.FindString(name)
|
|
if index != wx.NOT_FOUND:
|
|
dlg.m_cbNet.SetSelection(index)
|
|
return
|
|
if netnames:
|
|
dlg.m_cbNet.SetSelection(0)
|
|
#
|
|
|
|
|
|
def SettingsFile():
|
|
"""Path to the JSON file with the last used dialog values."""
|
|
try:
|
|
base = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath()
|
|
except Exception:
|
|
base = os.path.dirname(os.path.realpath(__file__))
|
|
return os.path.join(base, "ViaStitching.json")
|
|
|
|
|
|
def LoadSettings():
|
|
try:
|
|
with open(SettingsFile(), "r") as f:
|
|
data = json.load(f)
|
|
return data if isinstance(data, dict) else {}
|
|
except Exception:
|
|
return {}
|
|
|
|
|
|
def SaveSettings(dlg):
|
|
data = {
|
|
"size": dlg.m_SizeMM.GetValue(),
|
|
"drill": dlg.m_DrillMM.GetValue(),
|
|
"clearance": dlg.m_ClearanceMM.GetValue(),
|
|
"step": dlg.m_StepMM.GetValue(),
|
|
"net": dlg.m_cbNet.GetStringSelection(),
|
|
"fill_type": dlg.m_cbFillType.GetStringSelection(),
|
|
"grid_type": dlg.m_cbGridType.GetStringSelection(),
|
|
"random": dlg.m_Random.IsChecked(),
|
|
"only_selected": dlg.m_only_selected.IsChecked(),
|
|
"via_through_areas": dlg.m_viaThroughAreas.IsChecked(),
|
|
"same_net_tracks": dlg.m_sameNetTracks.IsChecked(),
|
|
}
|
|
try:
|
|
with open(SettingsFile(), "w") as f:
|
|
json.dump(data, f, indent=2)
|
|
except Exception as e:
|
|
wx.LogMessage("ViaStitching: cannot save settings: {}".format(e))
|
|
|
|
|
|
def ApplySettings(dlg, data):
|
|
for key, ctrl in (("size", dlg.m_SizeMM), ("drill", dlg.m_DrillMM),
|
|
("clearance", dlg.m_ClearanceMM), ("step", dlg.m_StepMM)):
|
|
if isinstance(data.get(key), str) and data[key].strip():
|
|
ctrl.SetValue(data[key])
|
|
for key, ctrl in (("random", dlg.m_Random), ("only_selected", dlg.m_only_selected),
|
|
("via_through_areas", dlg.m_viaThroughAreas),
|
|
("same_net_tracks", dlg.m_sameNetTracks)):
|
|
if isinstance(data.get(key), bool):
|
|
ctrl.SetValue(data[key])
|
|
for key, ctrl in (("fill_type", dlg.m_cbFillType), ("grid_type", dlg.m_cbGridType)):
|
|
if isinstance(data.get(key), str):
|
|
index = ctrl.FindString(data[key])
|
|
if index != wx.NOT_FOUND:
|
|
ctrl.SetSelection(index)
|
|
|
|
|
|
class FillAreaDialogEx(FillAreaDialog.FillAreaDialog):
|
|
|
|
def onDeleteClick(self, event):
|
|
return self.EndModal(wx.ID_DELETE)
|
|
|
|
|
|
class FillAreaAction(pcbnew.ActionPlugin):
|
|
|
|
def defaults(self):
|
|
self.name = "Via Stitching Generator 1.2"
|
|
self.category = "Modify PCB"
|
|
self.description = "Via Stitching for PCB Zone"
|
|
self.icon_file_name = os.path.join(os.path.dirname(__file__), "./stitching-vias.png")
|
|
self.show_toolbar_button = True
|
|
|
|
def Run(self):
|
|
a = FillAreaDialogEx(None)
|
|
# a.m_SizeMM.SetValue("0.8")
|
|
a.m_StepMM.SetValue("2.5")
|
|
# a.m_DrillMM.SetValue("0.3")
|
|
# a.m_Netname.SetValue("GND")
|
|
# a.m_ClearanceMM.SetValue("0.2")
|
|
a.m_bitmapStitching.SetBitmap(wx.Bitmap(os.path.join(os.path.dirname(os.path.realpath(__file__)), "stitching-vias-help.png")))
|
|
self.board = pcbnew.GetBoard()
|
|
self.boardDesignSettings = self.board.GetDesignSettings()
|
|
a.m_SizeMM.SetValue(str(pcbnew.ToMM(self.boardDesignSettings.GetCurrentViaSize())))
|
|
a.m_DrillMM.SetValue(str(pcbnew.ToMM(self.boardDesignSettings.GetCurrentViaDrill())))
|
|
a.m_ClearanceMM.SetValue(str(pcbnew.ToMM(self.boardDesignSettings.GetSmallestClearanceValue())))
|
|
a.SetMinSize(a.GetSize())
|
|
|
|
settings = LoadSettings()
|
|
ApplySettings(a, settings)
|
|
last_net = settings.get("net")
|
|
PopulateNets(last_net if isinstance(last_net, str) else None, a)
|
|
modal_result = a.ShowModal()
|
|
if modal_result == wx.ID_OK:
|
|
wx.LogMessage('Via Stitching')
|
|
SaveSettings(a)
|
|
if 1: # try:
|
|
fill = FillArea.FillArea()
|
|
fill.SetStepMM(float(a.m_StepMM.GetValue().replace(',', '.')))
|
|
fill.SetSizeMM(float(a.m_SizeMM.GetValue().replace(',', '.')))
|
|
fill.SetDrillMM(float(a.m_DrillMM.GetValue().replace(',', '.')))
|
|
fill.SetClearanceMM(float(a.m_ClearanceMM.GetValue().replace(',', '.')))
|
|
# fill.SetNetname(a.m_Netname.GetValue())
|
|
netname = a.m_cbNet.GetStringSelection()
|
|
fill.SetNetname(netname)
|
|
if a.m_Debug.IsChecked():
|
|
fill.SetDebug()
|
|
fill.SetRandom(a.m_Random.IsChecked())
|
|
fill.SetViaThroughAreas(a.m_viaThroughAreas.IsChecked())
|
|
fill.SetGridType(a.m_cbGridType.GetStringSelection())
|
|
fill.SetFillType(a.m_cbFillType.GetStringSelection())
|
|
fill.SetSameNetTracks(a.m_sameNetTracks.IsChecked())
|
|
if a.m_only_selected.IsChecked():
|
|
fill.OnlyOnSelectedArea()
|
|
fill.Run()
|
|
else: # except Exception:
|
|
wx.MessageDialog(None, "Invalid parameter")
|
|
elif modal_result == wx.ID_DELETE:
|
|
if 1: # try:
|
|
fill = FillArea.FillArea()
|
|
fill.SetNetname(a.m_cbNet.GetStringSelection()) # a.m_Netname.GetValue())
|
|
if a.m_Debug.IsChecked():
|
|
fill.SetDebug()
|
|
fill.DeleteVias()
|
|
fill.Run()
|
|
else: # except Exception:
|
|
wx.MessageDialog(None, "Invalid parameter for delete")
|
|
else:
|
|
print("Cancel")
|
|
a.Destroy()
|
|
|
|
|
|
FillAreaAction().register()
|