- added a new option for the Gerber objects: on the project context menu now can be chosen a color for the selected Gerber object

This commit is contained in:
Marius Stanciu
2019-12-22 06:52:45 +02:00
committed by Marius
parent 73d41816d6
commit 612666d01a
14 changed files with 251 additions and 32 deletions

View File

@@ -17,10 +17,9 @@ from flatcamGUI.VisPyTesselators import GLUTess
class FlatCAMLineVisual(LineVisual):
def __init__(self, pos=None, color=(0.5, 0.5, 0.5, 1), width=1, connect='strip',
method='gl', antialias=False):
LineVisual.__init__(self, pos=None, color=(0.5, 0.5, 0.5, 1), width=1, connect='strip',
method='gl', antialias=True)
def __init__(self, pos=None, color=(0.5, 0.5, 0.5, 1), width=1, connect='strip', method='gl', antialias=False):
LineVisual.__init__(self, pos=pos, color=color, width=width, connect=connect,
method=method, antialias=True)
def clear_data(self):
self._bounds = None
@@ -158,11 +157,14 @@ class ShapeGroup(object):
if update:
self._collection.redraw([]) # Skip waiting results
def redraw(self):
def redraw(self, update_colors=None):
"""
Redraws shape collection
"""
self._collection.redraw(self._indexes)
if update_colors:
self._collection.redraw(self._indexes, update_colors=update_colors)
else:
self._collection.redraw(self._indexes)
@property
def visible(self):
@@ -228,9 +230,9 @@ class ShapeCollectionVisual(CompoundVisual):
pass
m.set_gl_state(polygon_offset_fill=True, polygon_offset=(1, 1), cull_face=False)
for l in self._lines:
for lne in self._lines:
pass
l.set_gl_state(blend=True)
lne.set_gl_state(blend=True)
self.freeze()
@@ -245,6 +247,8 @@ class ShapeCollectionVisual(CompoundVisual):
Line/edge color
:param face_color: str, tuple
Polygon face color
:param alpha: str
Polygon transparency
:param visible: bool
Shape visibility
:param update: bool
@@ -271,11 +275,11 @@ class ShapeCollectionVisual(CompoundVisual):
# Add data to process pool if pool exists
try:
self.results[key] = self.pool.map_async(_update_shape_buffers, [self.data[key]])
except Exception as e:
except Exception:
self.data[key] = _update_shape_buffers(self.data[key])
if update:
self.redraw() # redraw() waits for pool process end
self.redraw() # redraw() waits for pool process end
return key
@@ -309,6 +313,108 @@ class ShapeCollectionVisual(CompoundVisual):
if update:
self.__update()
def update_color(self, new_mesh_color=None, new_line_color=None, indexes=None):
if (new_mesh_color is None or new_mesh_color == '') and (new_line_color is None or new_line_color == ''):
return
if not self.data:
return
mesh_colors = [[] for _ in range(0, len(self._meshes))] # Face colors
line_colors = [[] for _ in range(0, len(self._meshes))] # Line colors
line_pts = [[] for _ in range(0, len(self._lines))] # Vertices for line
# Lock sub-visuals updates
self.update_lock.acquire(True)
# Merge shapes buffers
if indexes is None:
for data in self.data.values():
if data['visible'] and 'line_pts' in data:
if new_mesh_color and new_mesh_color != '':
dim_mesh_tris = (len(data['mesh_tris']) // 3)
if dim_mesh_tris != 0:
try:
mesh_colors[data['layer']] += [Color(new_mesh_color).rgba] * dim_mesh_tris
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create mesh colors --> Data error. %s" % str(e))
if new_line_color and new_line_color != '':
dim_line_pts = (len(data['line_pts']))
if dim_line_pts != 0:
try:
line_pts[data['layer']] += data['line_pts']
line_colors[data['layer']] += [Color(new_line_color).rgba] * dim_line_pts
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create line colors --> Data error. %s" % str(e))
else:
for k, data in self.data.items():
if data['visible'] and 'line_pts' in data:
dim_mesh_tris = (len(data['mesh_tris']) // 3)
dim_line_pts = (len(data['line_pts']))
if k in indexes:
if new_mesh_color and new_mesh_color != '':
if dim_mesh_tris != 0:
try:
mesh_colors[data['layer']] += [Color(new_mesh_color).rgba] * dim_mesh_tris
self.data[k]['face_color'] = new_mesh_color
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create mesh colors --> Data error. %s" % str(e))
if new_line_color and new_line_color != '':
if dim_line_pts != 0:
try:
line_pts[data['layer']] += data['line_pts']
line_colors[data['layer']] += [Color(new_line_color).rgba] * dim_line_pts
self.data[k]['color'] = new_line_color
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create line colors --> Data error. %s" % str(e))
else:
if dim_mesh_tris != 0:
try:
mesh_colors[data['layer']] += [Color(data['face_color']).rgba] * dim_mesh_tris
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create mesh colors --> Data error. %s" % str(e))
if dim_line_pts != 0:
try:
line_pts[data['layer']] += data['line_pts']
line_colors[data['layer']] += [Color(data['color']).rgba] * dim_line_pts
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Create line colors --> Data error. %s" % str(e))
# Updating meshes
if new_mesh_color and new_mesh_color != '':
for i, mesh in enumerate(self._meshes):
if mesh_colors[i]:
try:
mesh._meshdata.set_face_colors(colors=np.asarray(mesh_colors[i]))
mesh.mesh_data_changed()
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Apply mesh colors --> Data error. %s" % str(e))
# Updating lines
if new_line_color and new_line_color != '':
for i, line in enumerate(self._lines):
if len(line_pts[i]) > 0:
try:
line._color = np.asarray(line_colors[i])
line._changed['color'] = True
line.update()
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.update_color(). "
"Apply line colors --> Data error. %s" % str(e))
else:
line.clear_data()
self.update_lock.release()
def __update(self):
"""
Merges internal buffers, sets data to visuals, redraws collection on scene
@@ -328,20 +434,23 @@ class ShapeCollectionVisual(CompoundVisual):
try:
line_pts[data['layer']] += data['line_pts']
line_colors[data['layer']] += data['line_colors']
mesh_tris[data['layer']] += [x + len(mesh_vertices[data['layer']])
for x in data['mesh_tris']]
mesh_tris[data['layer']] += [x + len(mesh_vertices[data['layer']]) for x in data['mesh_tris']]
mesh_vertices[data['layer']] += data['mesh_vertices']
mesh_colors[data['layer']] += data['mesh_colors']
except Exception as e:
print("Data error", e)
print("VisPyVisuals.ShapeCollectionVisual._update() --> Data error. %s" % str(e))
# Updating meshes
for i, mesh in enumerate(self._meshes):
if len(mesh_vertices[i]) > 0:
set_state(polygon_offset_fill=False)
mesh.set_data(np.asarray(mesh_vertices[i]), np.asarray(mesh_tris[i], dtype=np.uint32)
.reshape((-1, 3)), face_colors=np.asarray(mesh_colors[i]))
faces_array = np.asarray(mesh_tris[i], dtype=np.uint32)
mesh.set_data(
vertices=np.asarray(mesh_vertices[i]),
faces=faces_array.reshape((-1, 3)),
face_colors=np.asarray(mesh_colors[i])
)
else:
mesh.set_data()
@@ -350,17 +459,20 @@ class ShapeCollectionVisual(CompoundVisual):
# Updating lines
for i, line in enumerate(self._lines):
if len(line_pts[i]) > 0:
line.set_data(np.asarray(line_pts[i]), np.asarray(line_colors[i]), self._line_width, 'segments')
line.set_data(
pos=np.asarray(line_pts[i]),
color=np.asarray(line_colors[i]),
width=self._line_width,
connect='segments')
else:
line.clear_data()
line._bounds_changed()
self._bounds_changed()
self.update_lock.release()
def redraw(self, indexes=None):
def redraw(self, indexes=None, update_colors=None):
"""
Redraws collection
:param indexes: list
@@ -377,11 +489,22 @@ class ShapeCollectionVisual(CompoundVisual):
self.data[i] = self.results[i].get()[0] # Store translated data
del self.results[i]
except Exception as e:
print(e, indexes)
print("VisPyVisuals.ShapeCollectionVisual.redraw() --> Data error = %s. Indexes = %s" %
(str(e), str(indexes)))
self.results_lock.release()
self.__update()
if update_colors is None:
self.__update()
else:
try:
self.update_color(
new_mesh_color=update_colors[0],
new_line_color=update_colors[1],
indexes=indexes
)
except Exception as e:
print("VisPyVisuals.ShapeCollectionVisual.redraw() --> Update colors error = %s." % str(e))
def lock_updates(self):
self.update_lock.acquire(True)
@@ -489,7 +612,7 @@ class TextCollectionVisual(TextVisual):
self.lock.release()
# Prepare data for translation
self.data[key] = {'text': text, 'pos': pos, 'visible': visible,'font_size': font_size, 'color': color}
self.data[key] = {'text': text, 'pos': pos, 'visible': visible, 'font_size': font_size, 'color': color}
if update:
self.redraw()
@@ -537,7 +660,7 @@ class TextCollectionVisual(TextVisual):
font_s = data['font_size']
color = data['color']
except Exception as e:
print("Data error", e)
print("VisPyVisuals.TextCollectionVisual._update() --> Data error. %s" % str(e))
# Updating text
if len(labels) > 0: