Skip to content

Commit ccdc254

Browse files
committed
Add svg path parser and arc/polygon bbox
Issue #95
1 parent 8fb55d7 commit ccdc254

File tree

3 files changed

+543
-18
lines changed

3 files changed

+543
-18
lines changed

InteractiveHtmlBom/ecad/common.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import math
22

3+
from .svgpath import parse_path
4+
35

46
class EcadParser(object):
57

@@ -110,9 +112,12 @@ def add_circle(self, x, y, r):
110112
self.add_point(x, y + r)
111113
return self
112114

113-
def add_arc(self):
114-
# TODO
115-
pass
115+
def add_svgpath(self, svgpath, width, logger):
116+
w = width / 2
117+
for segment in parse_path(svgpath, logger):
118+
x0, x1, y0, y1 = segment.bbox()
119+
self.add_point(x0 - w, y0 - w)
120+
self.add_point(x1 + w, y1 + w)
116121

117122
def pad(self, amount):
118123
"""Add small padding to the box."""

InteractiveHtmlBom/ecad/easyeda.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33

44
from .common import EcadParser, Component, BoundingBox
5+
from .svgpath import parse_path
56

67

78
PY3 = sys.version_info[0] == 3
@@ -202,7 +203,7 @@ def parse_pad(self, shape):
202203
else:
203204
points = []
204205
angle = int(shape[10])
205-
hole_length = self.normalize(shape[12])
206+
hole_length = self.normalize(shape[12]) if shape[12] else 0
206207

207208
pad_layers = {
208209
self.TOP_COPPER_LAYER: ['F'],
@@ -273,8 +274,7 @@ def add_custom():
273274
'custom': add_custom,
274275
}.get(pad['shape'])()
275276

276-
@staticmethod
277-
def add_drawing_bounding_box(drawing, bbox):
277+
def add_drawing_bounding_box(self, drawing, bbox):
278278
# type: (dict, BoundingBox) -> None
279279

280280
def add_segment():
@@ -286,9 +286,13 @@ def add_circle():
286286
bbox.add_circle(drawing['start'][0], drawing['start'][1],
287287
drawing['radius'] + drawing['width'] / 2)
288288

289+
def add_svgpath():
290+
width = drawing.get('width', 0)
291+
bbox.add_svgpath(drawing['svgpath'], width, self.logger)
292+
289293
def add_polygon():
290294
if 'polygons' not in drawing:
291-
# TODO: svgpath polygons
295+
add_svgpath()
292296
return
293297
polygon = drawing['polygons'][0]
294298
for point in polygon:
@@ -297,9 +301,9 @@ def add_polygon():
297301
{
298302
'segment': add_segment,
299303
'circle': add_circle,
300-
'arc': lambda: None, # TODO
304+
'arc': add_svgpath,
301305
'polygon': add_polygon,
302-
'text': lambda: None, # TODO
306+
'text': lambda: None, # text is not really needed for bounding box
303307
}.get(drawing['type'])()
304308

305309
def parse_lib(self, shape):
@@ -353,7 +357,7 @@ def parse_lib(self, shape):
353357
for pad in pads:
354358
self.add_pad_bounding_box(pad, bbox)
355359
for drawing in copper_drawings:
356-
self.add_drawing_bounding_box(drawing, bbox)
360+
self.add_drawing_bounding_box(drawing['drawing'], bbox)
357361
for _, drawing in extra_drawings:
358362
self.add_drawing_bounding_box(drawing, bbox)
359363
bbox.pad(0.5) # pad by 5 mil
@@ -430,14 +434,21 @@ def parse(self):
430434

431435
drawings, modules, components = self.parse_shapes(pcb['shape'])
432436

433-
bbox = {
434-
"minx": self.normalize(pcb['BBox']['x']),
435-
"miny": self.normalize(pcb['BBox']['y']),
436-
"maxx": self.normalize(pcb['BBox']['x']) + self.normalize(
437-
pcb['BBox']['width']),
438-
"maxy": self.normalize(pcb['BBox']['y']) + self.normalize(
439-
pcb['BBox']['height'])
440-
}
437+
board_outline_bbox = BoundingBox()
438+
for drawing in drawings.get(self.BOARD_OUTLINE_LAYER, []):
439+
self.add_drawing_bounding_box(drawing, board_outline_bbox)
440+
if board_outline_bbox.initialized():
441+
bbox = board_outline_bbox.to_dict()
442+
else:
443+
# if nothing is drawn on outline layer then rely on EasyEDA bbox
444+
x = self.normalize(pcb['BBox']['x'])
445+
y = self.normalize(pcb['BBox']['y'])
446+
bbox = {
447+
"minx": x,
448+
"miny": y,
449+
"maxx": x + self.normalize(pcb['BBox']['width']),
450+
"maxy": y + self.normalize(pcb['BBox']['height'])
451+
}
441452

442453
pcbdata = {
443454
"edges_bbox": bbox,

0 commit comments

Comments
 (0)