22
22
23
23
from kikit import substrate
24
24
from kikit import units
25
+ from kikit .kicadUtil import getPageDimensionsFromAst
25
26
from kikit .substrate import Substrate , linestringToKicad , extractRings
26
- from kikit .defs import STROKE_T , Layer , EDA_TEXT_HJUSTIFY_T , EDA_TEXT_VJUSTIFY_T , PAPER_SIZES
27
+ from kikit .defs import PAPER_DIMENSIONS , STROKE_T , Layer , EDA_TEXT_HJUSTIFY_T , EDA_TEXT_VJUSTIFY_T , PAPER_SIZES
27
28
from kikit .common import *
28
- from kikit .sexpr import parseSexprF , SExpr , Atom
29
+ from kikit .sexpr import parseSexprF , SExpr , Atom , findNode
29
30
from kikit .annotations import AnnotationReader , TabAnnotation
30
31
from kikit .drc import DrcExclusion , readBoardDrcExclusions , serializeExclusion
31
32
@@ -734,6 +735,14 @@ def inheritPageSize(self, board: Union[pcbnew.BOARD, str]) -> None:
734
735
if not isinstance (board , pcbnew .BOARD ):
735
736
board = pcbnew .LoadBoard (board )
736
737
self .board .SetPageSettings (board .GetPageSettings ())
738
+ self .pageSize = None
739
+
740
+ # What follows is a hack as KiCAD has no API for page access. Therefore,
741
+ # we have to read out the page size from the source board and save it so
742
+ # we can recover it.
743
+ with open (board .GetFileName (), "r" ) as f :
744
+ tree = parseSexprF (f , limit = 10 ) # Introduce limit to speed up parsing
745
+ self ._inheritedPageDimensions = getPageDimensionsFromAst (tree )
737
746
738
747
def setPageSize (self , size : Union [str , Tuple [int , int ]] ) -> None :
739
748
"""
@@ -744,6 +753,23 @@ def setPageSize(self, size: Union[str, Tuple[int, int]] ) -> None:
744
753
raise RuntimeError (f"Unknown paper size: { size } " )
745
754
self .pageSize = size
746
755
756
+ def getPageDimensions (self ) -> Tuple [KiLength , KiLength ]:
757
+ """
758
+ Get page size in KiCAD units for the current panel
759
+ """
760
+ if self .pageSize is None :
761
+ return self ._inheritedPageDimensions
762
+ if isinstance (self .pageSize , tuple ):
763
+ return self .pageSize
764
+ if isinstance (self .pageSize , str ):
765
+ if self .pageSize .endswith ("-portrait" ):
766
+ # Portrait
767
+ pageSize = PAPER_DIMENSIONS [pageSize .split ("-" )[0 ]]
768
+ return pageSize [1 ], pageSize [0 ]
769
+ else :
770
+ return PAPER_DIMENSIONS [pageSize ]
771
+ raise RuntimeError ("Unknown page dimension - this is probably a bug and you should report it." )
772
+
747
773
def setProperties (self , properties ):
748
774
"""
749
775
Set text properties cached in the board
0 commit comments