aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ee/kicad/read_schematic.py227
1 files changed, 114 insertions, 113 deletions
diff --git a/src/ee/kicad/read_schematic.py b/src/ee/kicad/read_schematic.py
index 6fa9686..275f536 100644
--- a/src/ee/kicad/read_schematic.py
+++ b/src/ee/kicad/read_schematic.py
@@ -1,129 +1,130 @@
import shlex
from ee.kicad.model import *
-def read_schematic(path):
- schematic = Schematic()
- sheet = None
- def descr_section(lines):
- print("descr_section: len={}".format(len(lines)))
- pass
+def read_schematic(path):
+ schematic = Schematic()
+ sheet = None
- def comp_section(lines):
- print("comp_section: len={}".format(len(lines)))
- timestamp = None
- position = None
- library = None
- name = None
- unit = None
- fields = []
+ def descr_section(lines):
+ print("descr_section: len={}".format(len(lines)))
+ pass
- extra_lines = 0
- for line in lines:
- if line.startswith((' ', '\t')):
- parts = line.strip().split(" ")
- if extra_lines == 0:
- pass
- else:
- # rotation/mirroring, x1, y1, x2, y2
- pass
- extra_lines += 1
- else:
- parts = shlex.split(line)
- if len(parts) < 3:
- raise EeException("Bad component line: {}".format(line))
+ def comp_section(lines):
+ print("comp_section: len={}".format(len(lines)))
+ timestamp = None
+ position = None
+ library = None
+ name = None
+ unit = None
+ fields = []
- if parts[0] == "L" and len(parts) == 3:
- name = parts[1]
- ref = parts[2]
- elif parts[0] == "U" and len(parts) == 4:
- unit = int(parts[1])
- # parts[2] == body style
- timestamp = parts[3]
- elif parts[0] == "P" and len(parts) == 3:
- position = Position(int(parts[1]), int(parts[2]))
- elif parts[0] == "F" and len(parts) >= 10:
- oritentation = parts[3]
- pos = Position(int(parts[4]), int(parts[5]))
- value = parts[2]
- size = parts[6]
- attributes = parts[7]
- justify = parts[8] # One of L, R, C
- textAttrs = parts[9] # tree characters,
- # 0: T=top justify, B=bottom justify, C=center
- # 1: I=italics, N=normal
- # 2: B=bold, N=normal
- field_name = parts[10] if len(parts) > 10 else None
- idx = int(parts[1])
- if len(fields) != idx:
- raise EeException("Bad index: {}, expected={}. component={}, line={}".format(idx, len(fields), ref, line))
- fields.append(ComponentField(idx, field_name, value, pos))
- else:
- raise EeException("Bad component field: '{}'".format(line))
+ extra_lines = 0
+ for line in lines:
+ if line.startswith((' ', '\t')):
+ parts = line.strip().split(" ")
+ if extra_lines == 0:
+ pass
+ else:
+ # rotation/mirroring, x1, y1, x2, y2
+ pass
+ extra_lines += 1
+ else:
+ parts = shlex.split(line)
+ if len(parts) < 3:
+ raise EeException("Bad component line: {}".format(line))
- sheet.add_component(Component(position, timestamp, library, name, unit, ref, fields))
+ if parts[0] == "L" and len(parts) == 3:
+ name = parts[1]
+ ref = parts[2]
+ elif parts[0] == "U" and len(parts) == 4:
+ unit = int(parts[1])
+ # parts[2] == body style
+ timestamp = parts[3]
+ elif parts[0] == "P" and len(parts) == 3:
+ position = Position(int(parts[1]), int(parts[2]))
+ elif parts[0] == "F" and len(parts) >= 10:
+ oritentation = parts[3]
+ pos = Position(int(parts[4]), int(parts[5]))
+ value = parts[2]
+ size = parts[6]
+ attributes = parts[7]
+ justify = parts[8] # One of L, R, C
+ textAttrs = parts[9] # tree characters,
+ # 0: T=top justify, B=bottom justify, C=center
+ # 1: I=italics, N=normal
+ # 2: B=bold, N=normal
+ field_name = parts[10] if len(parts) > 10 else None
+ idx = int(parts[1])
+ if len(fields) != idx:
+ raise EeException(
+ "Bad index: {}, expected={}. component={}, line={}".format(idx, len(fields), ref, line))
+ fields.append(ComponentField(idx, field_name, value, pos))
+ else:
+ raise EeException("Bad component field: '{}'".format(line))
- with open(path) as f:
- header = f.readline()
- if not "EESchema Schematic File Version" in header:
- raise EeException("Not a KiCAD schematic file.")
+ sheet.add_component(Component(position, timestamp, library, name, unit, ref, fields))
- sheet = schematic.first_sheet
+ with open(path) as f:
+ header = f.readline()
+ if not "EESchema Schematic File Version" in header:
+ raise EeException("Not a KiCAD schematic file.")
- section_name = None
- section = []
+ sheet = schematic.first_sheet
- seen_end = False
- while not seen_end:
- line = f.readline()
- if not line:
- break
+ section_name = None
+ section = []
- line = line.rstrip()
+ seen_end = False
+ while not seen_end:
+ line = f.readline()
+ if not line:
+ break
- if section_name:
- if line.startswith("$End"):
- print("END SECTION: {}".format(section_name))
- if section_name == "$Comp":
- comp_section(section)
- elif section_name == "$Descr":
- descr_section(section)
- else:
- if line == "$EndSCHEMATIC":
- seen_end = True
- break
- section_name = None
- else:
- section.append(line)
- else:
- parts = line.split(" ")
- if line.startswith("EELAYER "):
- pass # Legacy, ignored
- elif line.startswith("LIBS:"):
- schematic.add_library(line[5:])
- elif line.startswith("$"):
- section_name = parts[0]
- print("SECTION: {}".format(section_name))
- section = []
- elif line == "Entry Wire Line":
- f.readline() # ignore the next line for now
- elif line.startswith("Text Label "):
- f.readline() # ignore the next line for now
- elif line.startswith("Text Notes "):
- f.readline() # ignore the next line for now
- elif line.startswith("NoConn "):
- pass
- elif parts[0:3] == ["Wire", "Notes", "Line"]:
- f.readline() # ignore the next line for now
- elif parts[0:3] == ["Wire", "Wire", "Line"]:
- f.readline() # ignore the next line for now
- elif parts[0:3] == ["Wire", "Bus", "Line"]:
- f.readline() # ignore the next line for now
- elif parts[0:2] == ["Connection", "~"]:
- pass
- else:
- print("line={}, len={}, wat={}".format(line, len(line), parts[0:3]))
- raise EeException("Bad line: {}".format(line))
+ line = line.rstrip()
- return schematic
+ if section_name:
+ if line.startswith("$End"):
+ print("END SECTION: {}".format(section_name))
+ if section_name == "$Comp":
+ comp_section(section)
+ elif section_name == "$Descr":
+ descr_section(section)
+ else:
+ if line == "$EndSCHEMATIC":
+ seen_end = True
+ break
+ section_name = None
+ else:
+ section.append(line)
+ else:
+ parts = line.split(" ")
+ if line.startswith("EELAYER "):
+ pass # Legacy, ignored
+ elif line.startswith("LIBS:"):
+ schematic.add_library(line[5:])
+ elif line.startswith("$"):
+ section_name = parts[0]
+ print("SECTION: {}".format(section_name))
+ section = []
+ elif line == "Entry Wire Line":
+ f.readline() # ignore the next line for now
+ elif line.startswith("Text Label "):
+ f.readline() # ignore the next line for now
+ elif line.startswith("Text Notes "):
+ f.readline() # ignore the next line for now
+ elif line.startswith("NoConn "):
+ pass
+ elif parts[0:3] == ["Wire", "Notes", "Line"]:
+ f.readline() # ignore the next line for now
+ elif parts[0:3] == ["Wire", "Wire", "Line"]:
+ f.readline() # ignore the next line for now
+ elif parts[0:3] == ["Wire", "Bus", "Line"]:
+ f.readline() # ignore the next line for now
+ elif parts[0:2] == ["Connection", "~"]:
+ pass
+ else:
+ print("line={}, len={}, wat={}".format(line, len(line), parts[0:3]))
+ raise EeException("Bad line: {}".format(line))
+ return schematic