- overwritten the Beta_8.995 branch with the Gerber_Editor_Upgrade branch
This commit is contained in:
@@ -1,20 +1,24 @@
|
||||
|
||||
from PyQt6 import QtWidgets
|
||||
from camlib import Geometry, arc, arc_angle, ApertureMacro, grace, flatten_shapely_geometry
|
||||
|
||||
from appParsers.ParseDXF import getdxfgeo
|
||||
from appParsers.ParseSVG import svgparselength, getsvggeo, svgparse_viewbox
|
||||
|
||||
import numpy as np
|
||||
import traceback
|
||||
from copy import deepcopy
|
||||
|
||||
from shapely.ops import unary_union, linemerge
|
||||
import shapely.affinity as affinity
|
||||
from shapely.geometry import box as shply_box
|
||||
from shapely.geometry import LinearRing, MultiLineString
|
||||
from shapely import box as shply_box
|
||||
from shapely import LinearRing, MultiLineString, LineString, Polygon, MultiPolygon, Point, prepare, is_prepared
|
||||
|
||||
from lxml import etree as ET
|
||||
import ezdxf
|
||||
|
||||
from appParsers.ParseDXF import *
|
||||
from appParsers.ParseSVG import svgparselength, getsvggeo, svgparse_viewbox
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
|
||||
import gettext
|
||||
import builtins
|
||||
@@ -505,6 +509,7 @@ class Gerber(Geometry):
|
||||
|
||||
geo_dict = {}
|
||||
geo_f = LineString(path)
|
||||
prepare(geo_f)
|
||||
if not geo_f.is_empty:
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
@@ -513,6 +518,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
if not geo_s.is_empty and geo_s.is_valid:
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -675,7 +681,7 @@ class Gerber(Geometry):
|
||||
# ################################################################
|
||||
if current_macro is None: # No macro started yet
|
||||
match = self.am1_re.search(gline)
|
||||
# Start macro if match, else not an AM, carry on.
|
||||
# Start macro if there is a match, else not an AM, carry on.
|
||||
if match:
|
||||
self.app.log.debug("Starting macro. Line %d: %s" % (line_num, gline))
|
||||
current_macro = match.group(1)
|
||||
@@ -733,8 +739,9 @@ class Gerber(Geometry):
|
||||
if not flash.is_empty:
|
||||
if self.app.options['gerber_simplification']:
|
||||
flash = flash.simplify(s_tol)
|
||||
poly_buffer.append(flash)
|
||||
|
||||
prepare(flash)
|
||||
poly_buffer.append(flash)
|
||||
if self.is_lpc is True:
|
||||
geo_dict['clear'] = flash
|
||||
else:
|
||||
@@ -784,6 +791,7 @@ class Gerber(Geometry):
|
||||
else:
|
||||
geo_dict = {}
|
||||
geo_f = LineString(path)
|
||||
prepare(geo_f)
|
||||
if not geo_f.is_empty:
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
@@ -793,6 +801,7 @@ class Gerber(Geometry):
|
||||
geo_s = LineString(path).buffer(width / 1.999, int(self.steps_per_circle))
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
prepare(geo_s)
|
||||
if not geo_s.is_empty:
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
@@ -851,6 +860,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
pol = pol.simplify(s_tol)
|
||||
|
||||
prepare(pol)
|
||||
poly_buffer.append(pol)
|
||||
if self.is_lpc is True:
|
||||
geo_dict['clear'] = pol
|
||||
@@ -865,6 +875,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
if self.is_lpc is True:
|
||||
geo_dict['clear'] = geo_s
|
||||
@@ -907,6 +918,7 @@ class Gerber(Geometry):
|
||||
geo_dict = {}
|
||||
if geo_f:
|
||||
if not geo_f.is_empty:
|
||||
prepare(geo_f)
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
if geo_s:
|
||||
@@ -916,6 +928,7 @@ class Gerber(Geometry):
|
||||
|
||||
if not geo_s.is_valid:
|
||||
print("Not valid: ", line_num)
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
if self.is_lpc is True:
|
||||
geo_dict['clear'] = geo_s
|
||||
@@ -970,7 +983,9 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
pol = pol.simplify(s_tol)
|
||||
|
||||
prepare(pol)
|
||||
pol_f = pol.exterior
|
||||
prepare(pol_f)
|
||||
if not pol_f.is_empty:
|
||||
follow_buffer.append(pol_f)
|
||||
geo_dict['follow'] = pol
|
||||
@@ -991,9 +1006,11 @@ class Gerber(Geometry):
|
||||
|
||||
region_f = region_s.exterior
|
||||
if not region_f.is_empty:
|
||||
prepare(region_f)
|
||||
follow_buffer.append(region_f)
|
||||
geo_dict['follow'] = region_f
|
||||
|
||||
prepare(region_s)
|
||||
poly_buffer.append(region_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1010,9 +1027,11 @@ class Gerber(Geometry):
|
||||
|
||||
region_f = region_s.exterior
|
||||
if not region_f.is_empty:
|
||||
prepare(region_f)
|
||||
follow_buffer.append(region_f)
|
||||
geo_dict['follow'] = region_f
|
||||
|
||||
prepare(region_s)
|
||||
poly_buffer.append(region_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1107,6 +1126,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
flash = flash.simplify(s_tol)
|
||||
|
||||
prepare(flash)
|
||||
poly_buffer.append(flash)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1136,6 +1156,7 @@ class Gerber(Geometry):
|
||||
|
||||
geo_dict = {}
|
||||
geo_f = Point([current_x, current_y])
|
||||
prepare(geo_f)
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
|
||||
@@ -1143,6 +1164,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1240,6 +1262,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1251,6 +1274,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1308,6 +1332,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1318,6 +1343,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1339,6 +1365,7 @@ class Gerber(Geometry):
|
||||
# this treats the case when we are storing geometry as paths
|
||||
geo_dict = {}
|
||||
geo_flash = Point([linear_x, linear_y])
|
||||
prepare(geo_flash)
|
||||
follow_buffer.append(geo_flash)
|
||||
geo_dict['follow'] = geo_flash
|
||||
|
||||
@@ -1353,6 +1380,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
flash = flash.simplify(s_tol)
|
||||
|
||||
prepare(flash)
|
||||
poly_buffer.append(flash)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1422,7 +1450,8 @@ class Gerber(Geometry):
|
||||
j = 0
|
||||
|
||||
if quadrant_mode is None:
|
||||
self.app.log.error("Found arc without preceding quadrant specification G74 or G75. (%d)" % line_num)
|
||||
self.app.log.error(
|
||||
"Found arc without preceding quadrant specification G74 or G75. (%d)" % line_num)
|
||||
self.app.log.error(gline)
|
||||
continue
|
||||
|
||||
@@ -1457,6 +1486,7 @@ class Gerber(Geometry):
|
||||
# this treats the case when we are storing geometry as paths
|
||||
geo_f = LineString(path)
|
||||
if not geo_f.is_empty:
|
||||
prepare(geo_f)
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
|
||||
@@ -1466,6 +1496,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
buffered = buffered.simplify(s_tol)
|
||||
|
||||
prepare(buffered)
|
||||
poly_buffer.append(buffered)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1609,6 +1640,7 @@ class Gerber(Geometry):
|
||||
# this treats the case when we are storing geometry as paths
|
||||
geo_f = LineString(path)
|
||||
if not geo_f.is_empty:
|
||||
prepare(geo_f)
|
||||
follow_buffer.append(geo_f)
|
||||
geo_dict['follow'] = geo_f
|
||||
|
||||
@@ -1619,6 +1651,7 @@ class Gerber(Geometry):
|
||||
if self.app.options['gerber_simplification']:
|
||||
geo_s = geo_s.simplify(s_tol)
|
||||
|
||||
prepare(geo_s)
|
||||
poly_buffer.append(geo_s)
|
||||
|
||||
if self.is_lpc is True:
|
||||
@@ -1678,6 +1711,10 @@ class Gerber(Geometry):
|
||||
new_poly = new_poly.buffer(0, int(self.steps_per_circle))
|
||||
self.app.log.warning("Union done.")
|
||||
|
||||
# #########################################################################################################
|
||||
prepare(new_poly)
|
||||
# #########################################################################################################
|
||||
|
||||
if current_polarity == 'D':
|
||||
self.app.inform.emit('%s' % _("Gerber processing. Applying Gerber polarity."))
|
||||
if new_poly.is_valid:
|
||||
@@ -1722,6 +1759,13 @@ class Gerber(Geometry):
|
||||
# flatten the solid geometry
|
||||
self.solid_geometry = flatten_shapely_geometry(self.solid_geometry)
|
||||
|
||||
# import time
|
||||
# start = time.time()
|
||||
# #########################################################################################################
|
||||
prepare(self.solid_geometry)
|
||||
# #########################################################################################################
|
||||
# print(f"Time elapsed: {time.time() - start}; Is prepared? {is_prepared(self.solid_geometry)}")
|
||||
|
||||
if self.app.options['gerber_clean_apertures']:
|
||||
# clean the Gerber file of apertures with no geometry
|
||||
for apid, apvalue in list(self.tools.items()):
|
||||
@@ -1762,7 +1806,7 @@ class Gerber(Geometry):
|
||||
maxx = loc[0] + width / 2
|
||||
miny = loc[1] - height / 2
|
||||
maxy = loc[1] + height / 2
|
||||
return shply_box(minx, miny, maxx, maxy)
|
||||
return shply_box(minx, miny, maxx, maxy).buffer(0.0000001)
|
||||
|
||||
if aperture['type'] == 'O': # Obround
|
||||
loc = location.coords[0]
|
||||
@@ -1959,7 +2003,7 @@ class Gerber(Geometry):
|
||||
self.app.log.debug("appParsers.ParseGerber.Gerber.import_svg(). Finished parsing the SVG geometry.")
|
||||
|
||||
if flip:
|
||||
geos = [translate(scale(g, 1.0, -1.0, origin=(0, 0)), yoff=h) for g in geos]
|
||||
geos = [affinity.translate(affinity.scale(g, 1.0, -1.0, origin=(0, 0)), yoff=h) for g in geos]
|
||||
self.app.log.debug("appParsers.ParseGerber.Gerber.import_svg(). SVG geometry was flipped.")
|
||||
|
||||
# Add to object
|
||||
@@ -2008,6 +2052,7 @@ class Gerber(Geometry):
|
||||
}
|
||||
|
||||
for pol in self.solid_geometry:
|
||||
prepare(pol)
|
||||
new_el = {'solid': pol, 'follow': LineString(pol.exterior.coords)}
|
||||
self.tools[0]['geometry'].append(new_el)
|
||||
|
||||
@@ -2065,6 +2110,7 @@ class Gerber(Geometry):
|
||||
flat_geo = list(self.flatten_list(self.solid_geometry))
|
||||
if flat_geo:
|
||||
self.solid_geometry = unary_union(flat_geo)
|
||||
prepare(self.solid_geometry)
|
||||
self.follow_geometry = self.solid_geometry
|
||||
else:
|
||||
return "fail"
|
||||
@@ -2529,7 +2575,7 @@ class Gerber(Geometry):
|
||||
try:
|
||||
if isinstance(self.solid_geometry, (MultiPolygon, MultiLineString)):
|
||||
self.geo_len = len(self.solid_geometry.geoms)
|
||||
else:
|
||||
if isinstance(self.solid_geometry, list):
|
||||
self.geo_len = len(self.solid_geometry)
|
||||
except (TypeError, ValueError, RuntimeError):
|
||||
self.geo_len = 1
|
||||
@@ -2634,8 +2680,8 @@ class Gerber(Geometry):
|
||||
geo_p = shply_box(minx, miny, maxx, maxy)
|
||||
new_geo_el['solid'] = geo_p
|
||||
else:
|
||||
self.app.log.debug("appParsers.ParseGerber.Gerber.buffer() --> "
|
||||
"ap type not supported")
|
||||
self.app.log.debug(
|
||||
"appParsers.ParseGerber.Gerber.buffer() --> ap type not supported")
|
||||
else:
|
||||
new_geo_el['solid'] = geo_el['follow'].buffer(
|
||||
size/1.9999,
|
||||
|
||||
Reference in New Issue
Block a user