How to create polygons with arcs in shapely (or a better library)

6.3k views Asked by At

I am trying to use shapely to identify the area used by a shape and the area used by the tools that will cut it on a CNC router. The shape is imported from a dxf drawing using ezdxf.

The tool paths can be either rectangles (if they are cut by a saw disk that follows a straight line) or a set of segments (if they are routed by a milling bit). In both cases I can use a LineString.buffer() to automatically create the offsets and find the area used by the tool.

I am using shapely because I think it is the best tool available to find out if shapes overlap each other (using union() to merge all the tools into one shape and overlaps() to find the interference). Please let me know if there is a better tool for this purpose.

buffer() does a good job at creating segments to represent arcs on the corners.

Is there a way to create the segments to represent arcs on the shape itself?

For example, how do I create the arc on the left of this shape? Do I need to create my own (slow) python function? Or is there an optimized shapely way?

Green is the part, yellow are the saw disk cuts, magenta are the milling bit cuts

2

There are 2 answers

6
Joe Kington On BEST ANSWER

Creating your own way of making the arc in python isn't necessarily slow. Numpy is excellent for operations along these lines, and shapely is deliberately intended to interoperate well with numpy.

For example,

import numpy as np
import shapely.geometry as geom

# Define the arc (presumably ezdxf uses a similar convention)
centerx, centery = 3, 4
radius = 2
start_angle, end_angle = 30, 56 # In degrees
numsegments = 1000

# The coordinates of the arc
theta = np.radians(np.linspace(start_angle, end_angle, numsegments))
x = centerx + radius * np.cos(theta)
y = centery + radius * np.sin(theta)

arc = geom.LineString(np.column_stack([x, y]))

Approximating the arc with 1000 points between the start and end angles takes ~3 milliseconds on my machine (that's including converting it to a shapely LineString).

0
Alex Ivanov On

I've never used shapely but I know some vector graphics principles. Overlays are usually extracted with the "difference". If you take the difference of the polygons out of the union the remaineder will be your arc. https://gis.stackexchange.com/questions/11987/polygon-overlay-with-shapely