diff --git a/README.md b/README.md index 04931cc..3021b1d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# This branch is the plugin for development version of KiCad +# This branch is the plugin for KiCad 10.0 and development version of KiCad Please select the right branch according your KiCad version: - - master for development version of KiCad + - master for KiCad version 10.0.* and development version of KiCad - master for KiCad V6 (until python API changes in dev version of KiCad) - v5.1 for KiCad version 5.1.* - v6.0 for KiCad version 6.0.* @@ -27,7 +27,8 @@ Please select the right branch according your KiCad version: Thoses plugins must be copied inside KiCad's python plugins directory (~/.kicad_plugins or /usr/share/kicad/scripting/plugins/ for -Linux). +Linux). For KiCad 10 the user plugin directory on Linux is +~/.local/share/kicad/10.0/scripting/plugins/. Most of them use python plugin (Action plugins) in KiCad. This feature is enabled in daily builds of KiCad. See https://forum.kicad.info/t/howto-register-a-python-plugin-inside-pcbnew-tools-menu/5540 @@ -42,6 +43,13 @@ interface. ![The interface](images/via1.png) +The "Net name" list contains only nets that have a zone; GND is listed +first and preselected. The values entered in the dialog are remembered +after clicking OK and offered again on the next run. They are stored in +ViaStitching.json in the KiCad user settings directory +(~/.config/kicad/10.0/ on Linux); delete this file to get the defaults +back (via drill and clearance from the board design settings). + Then the result should be: ![The result](images/via2.png) diff --git a/ViaStitching/FillArea.py b/ViaStitching/FillArea.py index e2f7b34..937c383 100644 --- a/ViaStitching/FillArea.py +++ b/ViaStitching/FillArea.py @@ -38,6 +38,37 @@ def wxPrint(msg): wx.LogMessage(msg) +def KicadMajorVersion(): + """Return KiCad major version as int (string compare breaks for 10+).""" + try: + return int(str(Version()).lstrip("(").split(".")[0]) + except (ValueError, IndexError): + return 99 + + +def ViaWidth(via): + """Via copper width; KiCad 10+ padstacks need a layer argument.""" + try: + return via.GetWidth(F_Cu) + except TypeError: + return via.GetWidth() + + +def PadCollidesShape(pad, shape): + """Test pad copper polygon vs. shape; KiCad 10+ needs a layer argument.""" + try: + layers = list(pad.GetLayerSet().CuStack()) + if not layers: + return False + for layer in layers: + if pad.GetEffectivePolygon(layer).Collide(shape): + return True + return False + except TypeError: + # KiCad 9 and older + return pad.GetEffectivePolygon().Collide(shape) + + # if sys.version[0] == "2": # maui None @@ -297,7 +328,7 @@ STEP = '-' for i in range(self.pcb.GetAreaCount()): area = self.pcb.GetArea(i) # No more making a real refill since it's crashing KiCad - if Version() < "7": + if KicadMajorVersion() < 7: None else: area.SetNeedRefill(True) @@ -329,7 +360,7 @@ STEP = '-' point_to_test = VECTOR2I(int(via.PosX + dx), int(via.PosY + dy)) hit_test_area = False - if Version() < "7": + if KicadMajorVersion() < 7: # below 7.0.0 for layer_id in area.GetLayerSet().CuStack(): hit_test_area = hit_test_area or area.HitTestFilledArea(layer_id, point_to_test) # Collides with a filled area @@ -417,7 +448,7 @@ STEP = '-' def CheckViaDistance(self, p, via, outline): p2 = VECTOR2I(via.GetPosition()) - dist = self.clearance + self.size / 2 + via.GetWidth() / 2 + dist = self.clearance + self.size / 2 + ViaWidth(via) / 2 # If via in same outline, then apply bigger space if outline.Collide(p2): @@ -481,7 +512,7 @@ STEP = '-' # For KiCad >= 7, zone.Outline() may return an empty SHAPE_POLY_SET for inner-layer # zones. We therefore try to build the polygon from points directly. zone_poly = SHAPE_POLY_SET() - if Version() < "7": + if KicadMajorVersion() < 7: for layer_id in zone.GetLayerSet().CuStack(): z = zone.RawPolysList(layer_id) if z.OutlineCount() > 0: @@ -667,7 +698,12 @@ STEP = '-' # Get the board outline and size with board_edge = SHAPE_POLY_SET() - self.pcb.GetBoardPolygonOutlines(board_edge) + try: + # KiCad 10+: aInferOutlineIfNecessary is a required argument + self.pcb.GetBoardPolygonOutlines(board_edge, True) + except TypeError: + # KiCad 9 and older + self.pcb.GetBoardPolygonOutlines(board_edge) b_clearance = max(self.pcb.GetDesignSettings().m_CopperEdgeClearance, self.clearance) + self.size board_edge.Deflate(int(b_clearance), CORNER_STRATEGY_ROUND_ALL_CORNERS, FromMM(0.01)) @@ -706,7 +742,7 @@ STEP = '-' offset = 0 # Use an exact zone match point_to_test = VECTOR2I(int(current_x), int(current_y)) hit_test_area = False - if Version() < "7": + if KicadMajorVersion() < 7: # below 7.0.0 hit_test_area = area.HitTestFilledArea(area.GetLayer(), VECTOR2I(point_to_test), int(offset)) # Collides with a filled area else: @@ -774,7 +810,7 @@ STEP = '-' m.SetViaType(VIATYPE_THROUGH) m.SetDrill(int(self.drill)) m.SetWidth(int(self.size)) - if pad.GetEffectivePolygon(pad.GetLayer()).Collide(m.GetEffectiveShape()): + if PadCollidesShape(pad, m.GetEffectiveShape()): rectangle[x][y] = self.REASON_PAD except: diff --git a/ViaStitching/FillAreaAction.py b/ViaStitching/FillAreaAction.py index 32b9be3..e37a165 100644 --- a/ViaStitching/FillAreaAction.py +++ b/ViaStitching/FillAreaAction.py @@ -23,18 +23,87 @@ import wx from . import FillArea from . import FillAreaDialog import os +import json def PopulateNets(anet, dlg): - netnames = list(set([zone.GetNetname() for zone in pcbnew.GetBoard().Zones()])) - netnames.sort() + """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) - if anet is not None: - index = dlg.m_cbNet.FindString(anet) - dlg.m_cbNet.Select(index) + 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): @@ -44,7 +113,7 @@ class FillAreaDialogEx(FillAreaDialog.FillAreaDialog): class FillAreaAction(pcbnew.ActionPlugin): def defaults(self): - self.name = "Via Stitching Generator 1.1" + 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") @@ -53,7 +122,7 @@ class FillAreaAction(pcbnew.ActionPlugin): def Run(self): a = FillAreaDialogEx(None) # a.m_SizeMM.SetValue("0.8") - a.m_StepMM.SetValue("2.54") + a.m_StepMM.SetValue("2.5") # a.m_DrillMM.SetValue("0.3") # a.m_Netname.SetValue("GND") # a.m_ClearanceMM.SetValue("0.2") @@ -65,10 +134,14 @@ class FillAreaAction(pcbnew.ActionPlugin): a.m_ClearanceMM.SetValue(str(pcbnew.ToMM(self.boardDesignSettings.GetSmallestClearanceValue()))) a.SetMinSize(a.GetSize()) - PopulateNets("GND", a) + 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(',', '.'))) diff --git a/ViaStitching/metadata.json b/ViaStitching/metadata.json new file mode 100644 index 0000000..8bd7b7a --- /dev/null +++ b/ViaStitching/metadata.json @@ -0,0 +1,36 @@ +{ + "$schema": "https://go.kicad.org/pcm/schemas/v1", + "name": "Via Stitching", + "description": "Via Stitching for PCB Zone.", + "description_full": "This plugin fills a zone with vias (stitching). It is useful for creating a strong connection between copper layers, for example for grounding.", + "identifier": "com.github.adamvotava1.kicad-action-scripts.viastitching", + "type": "plugin", + "author": { + "name": "JS Reynaud", + "contact": { + "web": "https://github.com/jsreynaud/kicad-action-scripts" + } + }, + "maintainer": { + "name": "Adam", + "contact": { + "web": "https://github.com/adamvotava1/kicad-action-scripts" + } + }, + "license": "MIT", + "resources": { + "homepage": "https://github.com/adamvotava1/kicad-action-scripts" + }, + "tags": [ + "via", + "stitching", + "grounding" + ], + "versions": [ + { + "version": "1.2.0", + "status": "stable", + "kicad_version": "9.0" + } + ] +}