Skip to content

Commit 943bee3

Browse files
committed
use shapely.ops.unary_union
1 parent 12ab431 commit 943bee3

9 files changed

+627
-226
lines changed

.vscode/launch.json

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78
{
89
"name": "OPENGL DEMO1",
910
"type": "python",
@@ -64,6 +65,15 @@
6465
"cwd": "C:\\Users\\xavie\\Documents\\GITHUB\\pycut",
6566
"args": ["-job", "C:\\Users\\xavie\\Documents\\GITHUB\\pycut\\jobs\\cnc_three_rects_with_circle.json"]
6667
},
68+
{
69+
"name": "PYCUT JOB ALL LETTERS",
70+
"type": "python",
71+
"request": "launch",
72+
"program": "pycut.py",
73+
"console": "integratedTerminal",
74+
"cwd": "C:\\Users\\xavie\\Documents\\GITHUB\\pycut",
75+
"args": ["-job", "C:\\Users\\xavie\\Documents\\GITHUB\\pycut\\jobs\\cnc_all_letters.json"]
76+
},
6777
{
6878
"name": "PYCUT GEAR 40",
6979
"type": "python",

cam.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
class CamPath:
3434
'''
3535
CamPath has this format: {
36-
path: Shapely path
36+
path: Shapely LineString
3737
safeToClose: Is it safe to close the path without retracting?
3838
}
3939
'''
@@ -67,31 +67,31 @@ def pocket(cls, geometry: shapely.geometry.MultiPolygon, cutterDia: float, overl
6767
cutterDia is in "UserUnit" units.
6868
overlap is in the range [0, 1).
6969
'''
70-
# use polygons exteriors lines - offset them and and diff with the interiors if any
70+
#new_algo = False
71+
#if new_algo:
72+
# for poly in geometry.geoms:
73+
# pc = PocketCalculator(poly, cutterDia, overlap, climb)
74+
# pc.calculate()
75+
# return pc.camPath
76+
77+
# use polygons exteriors lines - offset them and and diff with the offseted interiors if any
7178
geometry = ShapelyUtils.orientMultiPolygon(geometry)
7279

7380
MatplotLibUtils.MatplotlibDisplay("geom pocket init", geometry)
7481

7582
# the exterior
7683
multi_offset, current = ShapelyUtils.offsetMultiPolygon(geometry, cutterDia / 2, 'left', ginterior=True)
77-
78-
current = ShapelyUtils.simplifyMultiPoly(current, 0.001)
79-
current = ShapelyUtils.orientMultiPolygon(current)
8084

8185
for geom in current.geoms:
8286
MatplotLibUtils.MatplotlibDisplay("geom pocket init after simplify", geom)
83-
84-
if not current:
85-
return []
8687

8788
if len(current.geoms) == 0:
8889
# cannot offset ! maybe geometry too narrow for the cutter
8990
return []
9091

9192
# bound must be the exterior enveloppe + the interiors polygons
92-
#bounds = geometry
93-
9493
# no! the bounds are from the first offset with width cutterDia / 2
94+
#bounds = geometry
9595
bounds = shapely.geometry.MultiPolygon(current)
9696

9797
allPaths : List[shapely.geometry.LineString] = []
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import svgpathtools
2+
import math
3+
4+
pt1 = [335.4321, 69.71516199999999]
5+
pt2 = [335.27971, 69.86756199999999]
6+
7+
ctrl = [335.33051, 69.816762]
8+
9+
def test1():
10+
st = (335.43211+69.71516199999999j)
11+
end = (335.27971+69.86756199999999j)
12+
ct = (335.33051+69.816762j)
13+
zz = svgpathtools.QuadraticBezier(st, ct, end)
14+
15+
try:
16+
d1 = zz.length()
17+
print("test1:", d1)
18+
except :
19+
print("test1 exception")
20+
21+
def test2():
22+
st = (335.43211+69.715162j)
23+
ct = (335.33051+69.816762j)
24+
end = (335.27971+69.867562j)
25+
26+
zz = svgpathtools.QuadraticBezier(st, ct, end)
27+
28+
try:
29+
d1 = zz.length()
30+
print("test2:", d1)
31+
except :
32+
print("test2 exception")
33+
34+
35+
36+
if __name__ == '__main__':
37+
38+
dx = pt1[0]-pt2[0]
39+
dy = pt1[1]-pt2[1]
40+
41+
print("dist(pt1-pt2) = ", math.sqrt(dx*dx+dy*dy))
42+
43+
test1()
44+
test2()
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
3+
import matplotlib.pyplot as plt
4+
5+
import shapely
6+
from shapely.geometry import LineString, Polygon, LinearRing, MultiPolygon
7+
from shapely.ops import unary_union
8+
'''
9+
10+
11+
'''
12+
13+
print("")
14+
print("")
15+
print("")
16+
print("")
17+
print("")
18+
print("")
19+
20+
# -------- SETUP INITIAL LINESTRING/LINEARRINF AND POLYGONS ---------------
21+
22+
# we give two list of coords with "opposite directions"
23+
24+
coords1 = [(0,0), (2,0), (2,2), (0,2)]
25+
coords2 = [(4,0), (6,0), (6,2), (4,2)]
26+
coords3 = [(5,0), (7,0), (7,2), (6,2)]
27+
coords4 = [(8,0), (9,0), (9,2), (8,2)]
28+
29+
p1 = Polygon(coords1)
30+
p2 = Polygon(coords2)
31+
p3 = Polygon(coords3)
32+
p4 = Polygon(coords4)
33+
34+
35+
res = unary_union([p1,p2,p3, p4])
36+
print(res)
37+
38+
39+
plt.title("unary_union")
40+
for poly in res.geoms:
41+
if poly.geom_type == 'Polygon':
42+
xx, yy = poly.exterior.coords.xy
43+
plt.plot(xx,yy, 'bo-', label="poly")
44+
plt.legend()
45+
plt.show()
46+
47+
# ---------------------------------------------------------
48+
49+
# we give two list of coords with "opposite directions"
50+
51+
coords1 = [(0,0), (2,0), (2,2), (0,2)]
52+
coords2 = [(4,0), (6,0), (6,2), (4,2)]
53+
coords3 = [(1,1), (5,1), (5,3), (1,3)]
54+
55+
p1 = Polygon(coords1)
56+
p2 = Polygon(coords2)
57+
p3 = Polygon(coords3)
58+
59+
p1p2 = MultiPolygon([p1, p2])
60+
p1p3 = MultiPolygon([p1, p3])
61+
p2p3 = MultiPolygon([p2, p3])
62+
63+
print("p1p2 valid ?", p1p2.is_valid)
64+
print("p1p3 valid ?", p1p3.is_valid)
65+
print("p2p3 valid ?", p2p3.is_valid)
66+
67+
p2up3 = p2.union(p3)
68+
69+
print("p2up3 valid ?", p2up3.is_valid)
70+
71+
res = unary_union([p1,p2,p3])
72+
print(res)
73+
74+
if res.geom_type == 'Polygon':
75+
plt.title("unary_union")
76+
xx, yy = res.exterior.coords.xy
77+
plt.plot(xx,yy, 'bo-', label="poly")
78+
plt.legend()
79+
plt.show()

0 commit comments

Comments
 (0)