7474# Constants for layout calculations
7575FONT_SIZE = 1.27 # Default font size for pin names and numbers
7676GRID_SPACING = 1.27 # Grid spacing for aligning pins and symbols
77- PIN_LENGTH = 4 * GRID_SPACING # Standard pin length in KiCad
77+ MIN_PIN_LENGTH = 4 * GRID_SPACING # Minimum pin length
7878PIN_HEIGHT = 2 * GRID_SPACING # Standard pin height in KiCad
7979PIN_SPACING = 2 * GRID_SPACING # Standard pin spacing in KiCad
8080PIN_NAME_OFFSET = 0.85 # Offset from the end of the pin to the pin name
@@ -142,13 +142,18 @@ def text_width(text, alt_pin_delim=None, font_size=FONT_SIZE):
142142 Returns:
143143 float: width of the text in mm.
144144 """
145- # Approximate character width as 60 % of font size for monospaced fonts
145+ # Approximate character width as 90 % of font size for monospaced fonts
146146 char_width = font_size * 0.9
147147
148+ if not alt_pin_delim :
149+ # No splitting into alternate pin names, so use the full text
150+ alternates = [text ]
151+ else :
152+ # Split into alternate pin names using the specified delimiter
153+ alternates = text .split (alt_pin_delim )
154+
148155 # If using alternate pin names, then the bounding box width
149156 # is the length of the longest alternate name.
150- # Otherwise, the bounding box width is the length of the text.
151- alternates = text .split (alt_pin_delim )
152157 if not alternates :
153158 text_len = 0
154159 else :
@@ -1056,6 +1061,15 @@ def rows_to_symbol(
10561061 raise ValueError (
10571062 f"No valid pins defined for part { part_name } (all pins are placeholders)"
10581063 )
1064+
1065+ # Determine the symbol's pin length based on the longest pin number.
1066+ pin_length = max (
1067+ # Strip off any leading spacer stars from the pin number and add spaces for padding.
1068+ text_width (pin ["number" ].lstrip ("*" ) + " " )
1069+ for pin in pins
1070+ )
1071+ pin_length = max (pin_length , MIN_PIN_LENGTH )
1072+ pin_length = gridify (pin_length , policy = "up" )
10591073
10601074 # Group pins by the unit and side of the unit they're in.
10611075 units = {}
@@ -1226,15 +1240,15 @@ def rows_to_symbol(
12261240 # Set parameters for placing pins on the left side
12271241 if side == "left" :
12281242 ctr_offset = gridify (push * (lr_height - pin_cnt * PIN_HEIGHT ))
1229- x = x0 - PIN_LENGTH
1243+ x = x0 - pin_length
12301244 y = y0 + tb_height + lr_height - ctr_offset - PIN_HEIGHT / 2
12311245 orientation = 0
12321246 dx , dy = 0 , - PIN_SPACING
12331247
12341248 # Set parameters for placing pins on the right side
12351249 elif side == "right" :
12361250 ctr_offset = gridify (push * (lr_height - pin_cnt * PIN_HEIGHT ))
1237- x = x1 + PIN_LENGTH
1251+ x = x1 + pin_length
12381252 if ccw :
12391253 # Start from bottom, go upward when ccw is True
12401254 y = y0 + tb_height + ctr_offset + PIN_HEIGHT / 2
@@ -1265,7 +1279,7 @@ def rows_to_symbol(
12651279 ctr_offset = gridify (push * (tb_width - pin_cnt * PIN_HEIGHT ))
12661280 x = x0 + lr_width + ctr_offset + PIN_HEIGHT / 2
12671281 dx , dy = PIN_SPACING , 0
1268- y = y1 + PIN_LENGTH
1282+ y = y1 + pin_length
12691283 orientation = 270
12701284
12711285 # Set parameters for placing pins on the bottom side
@@ -1276,7 +1290,7 @@ def rows_to_symbol(
12761290 else :
12771291 ctr_offset = gridify (push * (tb_width - pin_cnt * PIN_HEIGHT ))
12781292 x = x0 + lr_width + ctr_offset + PIN_HEIGHT / 2
1279- y = - y1 - PIN_LENGTH
1293+ y = - y1 - pin_length
12801294 orientation = 90
12811295 dx , dy = PIN_SPACING , 0
12821296
@@ -1290,14 +1304,14 @@ def rows_to_symbol(
12901304 x ,
12911305 y ,
12921306 orientation ,
1293- PIN_LENGTH ,
1307+ pin_length ,
12941308 alt_pin_delim = alt_pin_delim ,
12951309 )
12961310 )
12971311 if debug :
12981312 unit_sexp .append (
12991313 create_pin_name_outline (
1300- pin , x , y , orientation , PIN_LENGTH
1314+ pin , x , y , orientation , pin_length
13011315 )
13021316 )
13031317 x += dx
0 commit comments