Skip to content

feat: Add find and replace functionality for signal names (#163)#169

Open
shanemmattner wants to merge 1 commit intomainfrom
fix/issue-163-find-and-replace
Open

feat: Add find and replace functionality for signal names (#163)#169
shanemmattner wants to merge 1 commit intomainfrom
fix/issue-163-find-and-replace

Conversation

@shanemmattner
Copy link
Contributor

Summary

Implements comprehensive find and replace functionality for renaming signals across KiCAD schematics. Users can now refactor signal naming patterns programmatically without manual editing.

Key Features

  • Literal string replacement - Simple string matching and replacement
  • Regex pattern support - Complex patterns with capture groups for advanced use cases
  • Scope-based targeting - Replace in specific element types:
    • labels - Wire labels and net names
    • components - Component references (e.g., R1, R2)
    • properties - Component property values
    • all - All element types simultaneously
  • Comprehensive validation - Prevents invalid component references and duplicates
  • Dry-run mode - Preview changes before committing
  • Detailed results - Returns information about each replacement

Use Case

import kicad_sch_api as ksa

sch = ksa.Schematic.load('circuit.kicad_sch')

# Simple string replacement
sch.find_and_replace(
    find='MOTOR_',
    replace='DC_MOTOR_',
    scope='labels'
)

# Regex with capture groups
sch.find_and_replace(
    find=r'MOTOR_(\d)',
    replace=r'DC_MOTOR_\1',
    scope='labels',
    regex=True
)

sch.save('circuit_renamed.kicad_sch')

Implementation Details

  • Added find_and_replace() method to Schematic class
  • Input validation for find string, scope, and regex patterns
  • Component reference validation (must start with letter, no duplicates)
  • Case-sensitive matching by default
  • Returns detailed replacement information

Testing

  • 18 comprehensive unit tests covering all functionality
  • Tests for literal string replacement
  • Tests for regex pattern replacement
  • Tests for scope isolation
  • Tests for validation and error handling
  • Tests for dry-run mode
  • All tests passing

Acceptance Criteria

  • Find and replace supports literal strings
  • Find and replace supports regex patterns
  • Can scope replacements to specific element types
  • Validation prevents invalid component references
  • Comprehensive test coverage (18 tests)
  • Clear error messages for validation failures

Fixes #163

Implement comprehensive find and replace API for renaming signals
across schematics. Supports literal strings and regex patterns with
scope-based targeting.

Features:
- Literal string replacement in labels, components, properties, or all
- Regex pattern replacement with capture groups
- Validation preventing invalid component references
- Dry-run mode for previewing changes
- Detailed replacement results with element tracking

This enables users to refactor signal naming patterns programmatically
without manual KiCAD editing.

Fixes #163

- Add find_and_replace() method to Schematic class
- Comprehensive input validation and error handling
- 18 unit tests covering all functionality
- Support for case-sensitive matching
- Duplicate reference detection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Find and Replace for Signal Names

2 participants