- fixed the Tcl commands AddCircle, AddPolygon, AddPolyline and AddRectangle to have stored bounds therefore making them movable/selectable on canvas

This commit is contained in:
Marius Stanciu
2020-08-27 02:25:07 +03:00
parent 66fcec49cc
commit 19b5e100d5
6 changed files with 115 additions and 49 deletions

View File

@@ -7,6 +7,10 @@ CHANGELOG for FlatCAM beta
================================================= =================================================
27.08.2020
- fixed the Tcl commands AddCircle, AddPolygon, AddPolyline and AddRectangle to have stored bounds therefore making them movable/selectable on canvas
26.08.2020 26.08.2020
- fix for issue nr 2 in case of Drilling Tool. Need to check Isolation Tool, Paint Tool, NCC Tool - fix for issue nr 2 in case of Drilling Tool. Need to check Isolation Tool, Paint Tool, NCC Tool

121
camlib.py
View File

@@ -520,69 +520,130 @@ class Geometry(object):
for i, g in enumerate(self.flat_geometry): for i, g in enumerate(self.flat_geometry):
self.index.insert(i, g) self.index.insert(i, g)
def add_circle(self, origin, radius): def add_circle(self, origin, radius, tool=None):
""" """
Adds a circle to the object. Adds a circle to the object.
:param origin: Center of the circle. :param origin: Center of the circle.
:param radius: Radius of the circle. :param radius: Radius of the circle.
:param tool: A tool in the Tools dictionary attribute of the object
:return: None :return: None
""" """
if self.solid_geometry is None: if self.solid_geometry is None:
self.solid_geometry = [] self.solid_geometry = []
if type(self.solid_geometry) is list: new_circle = Point(origin).buffer(radius, int(self.geo_steps_per_circle))
self.solid_geometry.append(Point(origin).buffer(radius, int(self.geo_steps_per_circle))) if not new_circle.is_valid:
return return "fail"
# add to the solid_geometry
try: try:
self.solid_geometry = self.solid_geometry.union( self.solid_geometry.append(new_circle)
Point(origin).buffer(radius, int(self.geo_steps_per_circle)) except TypeError:
) try:
except Exception as e: self.solid_geometry = self.solid_geometry.union(new_circle)
log.error("Failed to run union on polygons. %s" % str(e)) except Exception as e:
return log.error("Failed to run union on polygons. %s" % str(e))
return "fail"
def add_polygon(self, points): # add in tools solid_geometry
if tool is None or tool not in self.tools:
tool = 1
self.tools[tool]['solid_geometry'].append(new_circle)
# calculate bounds
try:
xmin, ymin, xmax, ymax = self.bounds()
self.options['xmin'] = xmin
self.options['ymin'] = ymin
self.options['xmax'] = xmax
self.options['ymax'] = ymax
except Exception as e:
log.error("Failed. The object has no bounds properties. %s" % str(e))
def add_polygon(self, points, tool=None):
""" """
Adds a polygon to the object (by union) Adds a polygon to the object (by union)
:param points: The vertices of the polygon. :param points: The vertices of the polygon.
:return: None :param tool: A tool in the Tools dictionary attribute of the object
:return: None
""" """
if self.solid_geometry is None: if self.solid_geometry is None:
self.solid_geometry = [] self.solid_geometry = []
new_poly = Polygon(points)
if not new_poly.is_valid:
return "fail"
# add to the solid_geometry
if type(self.solid_geometry) is list: if type(self.solid_geometry) is list:
self.solid_geometry.append(Polygon(points)) self.solid_geometry.append(new_poly)
return else:
try:
self.solid_geometry = self.solid_geometry.union(Polygon(points))
except Exception as e:
log.error("Failed to run union on polygons. %s" % str(e))
return "fail"
# add in tools solid_geometry
if tool is None or tool not in self.tools:
tool = 1
self.tools[tool]['solid_geometry'].append(new_poly)
# calculate bounds
try: try:
self.solid_geometry = self.solid_geometry.union(Polygon(points)) xmin, ymin, xmax, ymax = self.bounds()
except Exception as e:
log.error("Failed to run union on polygons. %s" % str(e))
return
def add_polyline(self, points): self.options['xmin'] = xmin
self.options['ymin'] = ymin
self.options['xmax'] = xmax
self.options['ymax'] = ymax
except Exception as e:
log.error("Failed. The object has no bounds properties. %s" % str(e))
def add_polyline(self, points, tool=None):
""" """
Adds a polyline to the object (by union) Adds a polyline to the object (by union)
:param points: The vertices of the polyline. :param points: The vertices of the polyline.
:return: None :param tool: A tool in the Tools dictionary attribute of the object
:return: None
""" """
if self.solid_geometry is None: if self.solid_geometry is None:
self.solid_geometry = [] self.solid_geometry = []
if type(self.solid_geometry) is list: new_line = LineString(points)
self.solid_geometry.append(LineString(points)) if not new_line.is_valid:
return return "fail"
# add to the solid_geometry
if type(self.solid_geometry) is list:
self.solid_geometry.append(new_line)
else:
try:
self.solid_geometry = self.solid_geometry.union(new_line)
except Exception as e:
log.error("Failed to run union on polylines. %s" % str(e))
return "fail"
# add in tools solid_geometry
if tool is None or tool not in self.tools:
tool = 1
self.tools[tool]['solid_geometry'].append(new_line)
# calculate bounds
try: try:
self.solid_geometry = self.solid_geometry.union(LineString(points)) xmin, ymin, xmax, ymax = self.bounds()
self.options['xmin'] = xmin
self.options['ymin'] = ymin
self.options['xmax'] = xmax
self.options['ymax'] = ymax
except Exception as e: except Exception as e:
log.error("Failed to run union on polylines. %s" % str(e)) log.error("Failed. The object has no bounds properties. %s" % str(e))
return
def is_empty(self): def is_empty(self):
if isinstance(self.solid_geometry, BaseGeometry) or isinstance(self.solid_geometry, Polygon) or \ if isinstance(self.solid_geometry, BaseGeometry) or isinstance(self.solid_geometry, Polygon) or \

View File

@@ -51,17 +51,18 @@ class TclCommandAddCircle(TclCommand):
:return: :return:
""" """
obj_name = args['name'] name = args['name']
center_x = args['center_x'] center_x = args['center_x']
center_y = args['center_y'] center_y = args['center_y']
radius = args['radius'] radius = args['radius']
try: try:
obj = self.app.collection.get_by_name(str(obj_name)) obj = self.app.collection.get_by_name(str(name))
except Exception: except Exception:
return "Could not retrieve object: %s" % obj_name return "Could not retrieve object: %s" % name
if obj is None: if obj is None:
return "Object not found: %s" % obj_name return "Object not found: %s" % name
if obj.kind != 'geometry':
self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
obj.add_circle([float(center_x), float(center_y)], float(radius)) obj.add_circle([float(center_x), float(center_y)], float(radius))

View File

@@ -1,4 +1,3 @@
from camlib import Geometry
from tclCommands.TclCommand import * from tclCommands.TclCommand import *
@@ -51,7 +50,7 @@ class TclCommandAddPolygon(TclCommandSignaled):
if obj is None: if obj is None:
self.raise_tcl_error("Object not found: %s" % name) self.raise_tcl_error("Object not found: %s" % name)
if not isinstance(obj, Geometry): if obj.kind != 'geometry':
self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj))) self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
if len(unnamed_args) % 2 != 0: if len(unnamed_args) % 2 != 0:
@@ -61,4 +60,3 @@ class TclCommandAddPolygon(TclCommandSignaled):
points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(nr_points)] points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(nr_points)]
obj.add_polygon(points) obj.add_polygon(points)
obj.plot()

View File

@@ -1,6 +1,4 @@
from camlib import Geometry from tclCommands.TclCommand import *
import collections
from tclCommands.TclCommand import TclCommandSignaled
class TclCommandAddPolyline(TclCommandSignaled): class TclCommandAddPolyline(TclCommandSignaled):
@@ -52,7 +50,7 @@ class TclCommandAddPolyline(TclCommandSignaled):
if obj is None: if obj is None:
self.raise_tcl_error("Object not found: %s" % name) self.raise_tcl_error("Object not found: %s" % name)
if not isinstance(obj, Geometry): if obj.kind != 'geometry':
self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj))) self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
if len(unnamed_args) % 2 != 0: if len(unnamed_args) % 2 != 0:
@@ -62,4 +60,3 @@ class TclCommandAddPolyline(TclCommandSignaled):
points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(nr_points)] points = [[float(unnamed_args[2*i]), float(unnamed_args[2*i+1])] for i in range(nr_points)]
obj.add_polyline(points) obj.add_polyline(points)
obj.plot()

View File

@@ -1,5 +1,4 @@
import collections from tclCommands.TclCommand import *
from tclCommands.TclCommand import TclCommandSignaled
class TclCommandAddRectangle(TclCommandSignaled): class TclCommandAddRectangle(TclCommandSignaled):
@@ -52,17 +51,23 @@ class TclCommandAddRectangle(TclCommandSignaled):
:return: None or exception :return: None or exception
""" """
obj_name = args['name'] name = args['name']
x0 = args['x0'] x0 = args['x0']
y0 = args['y0'] y0 = args['y0']
x1 = args['x1'] x1 = args['x1']
y1 = args['y1'] y1 = args['y1']
if unnamed_args:
self.raise_tcl_error(
"Too many arguments. Correct format: %s" % '["add_rectangle geo_name xmin ymin xmax ymax"]')
try: try:
obj = self.app.collection.get_by_name(str(obj_name)) obj = self.app.collection.get_by_name(str(name))
except Exception: except Exception:
return "Could not retrieve object: %s" % obj_name return "Could not retrieve object: %s" % name
if obj is None: if obj is None:
return "Object not found: %s" % obj_name return "Object not found: %s" % name
if obj.kind != 'geometry':
self.raise_tcl_error('Expected Geometry, got %s %s.' % (name, type(obj)))
obj.add_polygon([(x0, y0), (x1, y0), (x1, y1), (x0, y1)]) obj.add_polygon([(x0, y0), (x1, y0), (x1, y1), (x0, y1)])