From f9c1778b2d6baa68635f32676a77b9e53c4ca7fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Mr=C3=A1zek?= Date: Thu, 23 Feb 2023 09:49:08 +0100 Subject: [PATCH] Adapt to v7 release changes --- .github/workflows/test-pcbdraw.yml | 2 +- pcbdraw/pcbnew_common.py | 21 +++++++++------------ pcbdraw/renderer.py | 19 ++++++++++++------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test-pcbdraw.yml b/.github/workflows/test-pcbdraw.yml index 1f986b8..76002bd 100644 --- a/.github/workflows/test-pcbdraw.yml +++ b/.github/workflows/test-pcbdraw.yml @@ -7,7 +7,7 @@ on: jobs: test: name: "Run Pcbdraw tests" - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v2 with: diff --git a/pcbdraw/pcbnew_common.py b/pcbdraw/pcbnew_common.py index 7edfa4d..4bc0d1b 100644 --- a/pcbdraw/pcbnew_common.py +++ b/pcbdraw/pcbnew_common.py @@ -1,18 +1,18 @@ from pcbnewTransition import pcbnew, isV6 # type: ignore -from pcbnew import wxRect # type: ignore +from pcbnewTransition.pcbnew import BOX2I # type: ignore from itertools import chain from typing import Optional, List import wx # type: ignore import os -def getBBoxWithoutContours(edge: pcbnew.EDA_SHAPE) -> pcbnew.wxRect: +def getBBoxWithoutContours(edge: pcbnew.EDA_SHAPE) -> pcbnew.BOX2I: width = edge.GetWidth() edge.SetWidth(0) bBox = edge.GetBoundingBox() edge.SetWidth(width) return bBox -def findBoundingBox(edges: List[pcbnew.EDA_SHAPE]) -> pcbnew.wxRect: +def findBoundingBox(edges: List[pcbnew.EDA_SHAPE]) -> pcbnew.BOX2I: """ Return a bounding box of all drawings in edges """ @@ -23,10 +23,10 @@ def findBoundingBox(edges: List[pcbnew.EDA_SHAPE]) -> pcbnew.wxRect: boundingBox = combineBoundingBoxes(boundingBox, getBBoxWithoutContours(edge)) return boundingBox -def findBoardBoundingBox(board: pcbnew.BOARD) -> wxRect: +def findBoardBoundingBox(board: pcbnew.BOARD) -> BOX2I: """ - Returns a bounding box (wxRect) of all Edge.Cuts items either in - specified source area (wxRect) or in the whole board + Returns a bounding box (BOX2I) of all Edge.Cuts items either in + specified source area (BOX2I) or in the whole board """ edges = collectEdges(board, "Edge.Cuts") return findBoundingBox(edges) @@ -43,16 +43,13 @@ def collectEdges(board: pcbnew.BOARD, layerName: str) -> List[pcbnew.EDA_SHAPE]: edges.append(edge) return edges -def combineBoundingBoxes(a: pcbnew.wxRect, b: pcbnew.wxRect) -> pcbnew.wxRect: - """ Retrun wxRect as a combination of source bounding boxes """ +def combineBoundingBoxes(a: pcbnew.BOX2I, b: pcbnew.BOX2I) -> pcbnew.BOX2I: + """ Retrun BOX2I as a combination of source bounding boxes """ x1 = min(a.GetX(), b.GetX()) y1 = min(a.GetY(), b.GetY()) x2 = max(a.GetX() + a.GetWidth(), b.GetX() + b.GetWidth()) y2 = max(a.GetY() + a.GetHeight(), b.GetY() + b.GetHeight()) - # Beware that we cannot use the following code! It will add 1 to width and - # height. See https://github.com/wxWidgets/wxWidgets/blob/e43895e5317a1e82e295788264553d9839190337/src/common/gdicmn.cpp#L94-L114 - # return wxRect(topLeft, bottomRight) - return wxRect(x1, y1, x2 - x1, y2 - y1) + return BOX2I(pcbnew.VECTOR2I(x1, y1), pcbnew.VECTOR2I(x2 - x1, y2 - y1)) def fakeKiCADGui() -> Optional[wx.App]: """ diff --git a/pcbdraw/renderer.py b/pcbdraw/renderer.py index 930ed30..bf600c2 100644 --- a/pcbdraw/renderer.py +++ b/pcbdraw/renderer.py @@ -25,7 +25,7 @@ from pyvirtualdisplay.smartdisplay import SmartDisplay from .pcbnew_common import findBoardBoundingBox -from pcbnewTransition import pcbnew # type: ignore +from pcbnewTransition import pcbnew, getVersion # type: ignore PKG_BASE = os.path.dirname(__file__) DEBUG_PATH = None @@ -83,7 +83,7 @@ def listWindows(self) -> Dict[str, int]: """ List currently active windows, return mapping "Title -> id" """ - windows = self._xdotool(["search", "--pid", self._process.pid]) + windows = self._xdotool(["search", "--pid", self._process.pid, ".*"]) res = {} for winId in windows: name = self._xdotool(["getwindowname", winId]) @@ -225,6 +225,9 @@ def waitForResponsiveness(self) -> None: # We have to wait for board processing, we try to open preferences # and then close it. The safest seems to open the menu as it is # not locale dependent + + # KiCAD 7 doesn't like when keystrokes are send too quickly + time.sleep(1) self._sendKeys(["alt+p", "Down", "Return"]) prefId = self._parent.waitForWindow("Preferences") self._parent._xdotool(["key", "--window", prefId, "Escape"]) @@ -234,20 +237,20 @@ def waitForResponsiveness(self) -> None: def showFront(self) -> None: self._sendKeys(["z"]) time.sleep(0.5) - self._parent.waitForImmovable(threshold=20) + self._parent.waitForImmovable(threshold=3) def showBack(self) -> None: self._sendKeys(["Shift+z"]) time.sleep(0.5) - self._parent.waitForImmovable(threshold=20) + self._parent.waitForImmovable(threshold=3) def toggleAxisIndicator(self) -> None: self._sendKeys(["alt+p"] + 6 * ["Down"] + ["Return"]) - self._parent.waitForImmovable(threshold=20) + self._parent.waitForImmovable(threshold=3) def toggleComponents(self) -> None: self._sendKeys(["T", "S", "V"]) - self._parent.waitForImmovable(threshold=20) + self._parent.waitForImmovable(threshold=3) def captureRaytraced(self, withComponents: bool=True) -> Image.Image: if not withComponents: @@ -266,6 +269,7 @@ def captureRaytraced(self, withComponents: bool=True) -> Image.Image: def toggleOrthographic(self) -> None: # There is no shortcut... self._click((676, 44)) + self._parent.waitForImmovable(threshold=3) def _rotate(self, angle: int, plusButton: Tuple[int, int], minusButton: Tuple[int, int]) -> None: @@ -329,7 +333,8 @@ def startPcbnewSession(resolution: Tuple[int, int]=(3000, 3000), # to produce consistent results, we provide a new KiCAD config # that is a copy of the original and the crucial files are # replaced with pristine ones. - kicadConfigDir = os.path.join(tempdir, "6.0") + kicadVersion = getVersion() + kicadConfigDir = os.path.join(tempdir, f"{kicadVersion[0]}.{kicadVersion[1]}") duplicateKiCadSettings(kicadConfigDir) shutil.copy(os.path.join(PKG_BASE, "resources", "defaultKiCadSettings", "3d_viewer.json"), os.path.join(kicadConfigDir, "3d_viewer.json"))