def Parse_Polys(self, tag): # 0 gr_poly # 1 # 0 pts # 1 # 0 xy # 1 147.6375 # 2 120.9675 # 2 # 0 xy # 1 147.6375 # 2 120.9675 # 3 # ... # 2 # 0 layer # 1 B.Cu # 3 # 0 width # 1 0.1 data = [tag['type']] style = tag['style'] styletag = style[style.find('stroke-width:') + 13:] width = styletag[0:styletag.find('mm')] if tag.has_attr('layer'): layer = ['layer', tag['layer']] elif tag.parent.has_attr('inkscape:label'): #XML metadata trashed, try to recover from parent tag layer = ['layer', tag.parent['inkscape:label']] else: assert False, "Poly not in layer" path = parse_path(tag['d']) # print(tag) # print(tag['d']) # print(path) pts = ['pts'] for point in path: xy = ['xy'] xy.append(str(point.start.real / pxToMM)) xy.append(str(point.start.imag / pxToMM)) pts.append(xy) data.append(pts) data.append(layer) data.append(['width', width]) return data
def Parse_Arcs(self, tag, segments): # 0 gr_arc # 1 # 0 start # 1 66.66 # 2 99.99 # 2 # 0 end # 1 66.66 # 2 99.99 # 3 # 0 angle # 1 -90 # 4 # 0 layer # 1 Edge.Cuts # 5 # 0 width # 1 0.05 # 6 # 0 tstamp # 1 5E451B20 path = parse_path(tag['d']) # print(tag) # print(tag['d']) # print(path) radius = path[0].radius.real / pxToMM # angle = '90' sweep = path[0].sweep large_arc = path[0].large_arc # print(segments) if bool(large_arc) == True: print("Handle~!") #KiCad 'start' is actually centre, 'end' is actually svg start #SVG end is actual end, we need to calculate centre instead # print('path', path[0].start, path[0].end) end = [ str(path[0].start.real / pxToMM), str(path[0].start.imag / pxToMM) ] end_complex = (path[0].start.real / pxToMM) + 1j * (path[0].start.imag / pxToMM) start_complex = (path[0].end.real / pxToMM) + 1j * (path[0].end.imag / pxToMM) q = math.sqrt((end_complex.real - start_complex.real)**2 + (end_complex.real - start_complex.real)**2) x3 = (start_complex.real + end_complex.real) / 2 y3 = (start_complex.imag + end_complex.imag) / 2 if bool(large_arc) == True: #hackhack / fix / whatever: #figure out why this is larger than radius sometimes print("hackhack: generating janky arc") print(radius, q) q = q / 2 if bool(sweep) == False: # angle = -angle angle = 1 x = x3 + math.sqrt(radius**2 - (q / 2)**2) * (start_complex.imag - end_complex.imag) / q y = y3 - math.sqrt(radius**2 - (q / 2)**2) * (start_complex.real - end_complex.real) / q else: # angle = '90' angle = -1 x = x3 - math.sqrt(radius**2 - (q / 2)**2) * (start_complex.imag - end_complex.imag) / q y = y3 + math.sqrt(radius**2 - (q / 2)**2) * (start_complex.real - end_complex.real) / q start_list = ['start', str(x), str(y)] end_list = ['end', end[0], end[1]] start_angle = self.Get_Angle( [x, y], [path[0].start.real / pxToMM, path[0].start.imag / pxToMM]) end_angle = self.Get_Angle( [x, y], [path[0].end.real / pxToMM, path[0].end.imag / pxToMM]) angle = angle * (end_angle - start_angle) if bool(sweep) == True: angle = 360 - angle angle = "{:.6f}".format(round(angle, 6)) segments[0] = start_list segments[1] = end_list segments.insert(2, (['angle', angle])) segments.insert(0, 'gr_arc') return segments
def Parse_Zone(self, tag): # 0 zone # 1 # 0 net # 1 16 # 2 # 0 net_name # 1 GND # 3 # 0 layer # 1 B.Cu # 4 # 0 tstamp # 1 5EACCA92 # 5 # 0 hatch # 1 edge # 2 0.508 # 6 # 0 connect_pads # 1 # 0 clearance # 1 0.1524 # 7 # 0 min_thickness # 1 0.1524 # 8 # 0 fill # 1 yes # 2 # 0 arc_segments # 1 32 # 3 # 0 thermal_gap # 1 0.1524 # 4 # 0 thermal_bridge_width # 1 0.1525 # 9 # 0 polygon # 1 # 0 pts # 1 # 0 xy # 1 147.6375 # 2 120.9675 # 2 # 0 xy # 1 147.6375 # 2 120.9675 # 3 # ... # 10 # 0 filled_polygon # 1 # 0 pts # 1 # 0 xy # 1 147.6375 # 2 120.9675 # 2 # 0 xy # 1 147.6375 # 2 120.9675 # 3 # ... style = tag['style'] width = style[style.find('stroke-width:') + 13:] hatch = ['hatch', 'edge', width[0:width.find('mm')]] if tag.has_attr('layer'): layer = ['layer', tag['layer']] elif tag.parent.has_attr('inkscape:label'): #XML metadata trashed, try to recover from parent tag layer = ['layer', tag.parent['inkscape:label']] else: assert False, "Zone not in layer" path = parse_path(tag['d']) # print(tag) # print(tag['d']) # print(path) pts = ['pts'] for point in path: xy = ['xy'] xy.append(str(point.start.real / pxToMM)) xy.append(str(point.start.imag / pxToMM)) pts.append(xy) polygon = ['polygon', pts] content = tag.contents[0][0:-1] content = '[' + content + ' ]' # self.Save(content) data = json.loads(content) data.insert(3, layer) data.insert(5, hatch) data.insert(9, polygon) return data
def Parse_Segment(self, tag): # print(tag['id']) style = tag['style'] width = style[style.find('stroke-width:') + 13:] width = ['width', width[0:width.find('mm')]] if tag.has_attr('layer'): name = ['layer', tag['layer']] elif tag.parent.has_attr('inkscape:label'): #XML metadata trashed, try to recover from parent tag name = ['layer', tag.parent['inkscape:label']] else: assert False, "Path not in layer" paths = parse_path(tag['d']) segments = [] gr_lines = [] gr_arcs = [] gr_curves = [] gr_polys = [] for path in paths: segment = [] start = [ 'start', str(path.start.real / pxToMM), str(path.start.imag / pxToMM) ] end = [ 'end', str(path.end.real / pxToMM), str(path.end.imag / pxToMM) ] segment = [start, end, width, name] if tag.has_attr('net'): segment.append(['net', tag['net']]) if tag.has_attr('status'): segment.append(['status', tag['status']]) if tag.has_attr('tstamp'): segment.append(['tstamp', tag['tstamp']]) if tag.has_attr('type') == False or tag['type'] == 'gr_line': segment = ['gr_line'] + segment gr_lines.append(segment) elif tag['type'] == 'segment': segment = ['segment'] + segment segments.append(segment) elif tag['type'] == 'gr_arc': gr_arcs.append(self.Parse_Arcs(tag, segment)) elif tag['type'] == 'gr_curve': gr_curves.append(self.Parse_Curves(tag, segment)) else: print(tag) assert False, "Gr_line / segments: Nobody knows!" segments.reverse() gr_lines.reverse() gr_arcs.reverse() gr_curves.reverse() return segments, gr_lines, gr_arcs, gr_curves