Update dxf__omar3_2.py
Browse files- dxf__omar3_2.py +71 -44
dxf__omar3_2.py
CHANGED
|
@@ -1208,7 +1208,7 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
|
|
| 1208 |
for path in entity.paths:
|
| 1209 |
vertices = [] # Reset vertices for each path
|
| 1210 |
|
| 1211 |
-
if
|
| 1212 |
# Handle POLYLINE type HATCH
|
| 1213 |
vertices = [(vertex[0] * FinalRatio, vertex[1] * FinalRatio) for vertex in path.vertices]
|
| 1214 |
|
|
@@ -1245,49 +1245,76 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
|
|
| 1245 |
unique_shapes.append((normalized_vertices, area1))
|
| 1246 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 1247 |
|
| 1248 |
-
elif
|
| 1249 |
-
#
|
| 1250 |
-
|
| 1251 |
-
|
| 1252 |
-
|
| 1253 |
-
|
| 1254 |
-
|
| 1255 |
-
|
| 1256 |
-
|
| 1257 |
-
|
| 1258 |
-
|
| 1259 |
-
|
| 1260 |
-
|
| 1261 |
-
|
| 1262 |
-
|
| 1263 |
-
|
| 1264 |
-
|
| 1265 |
-
|
| 1266 |
-
|
| 1267 |
-
|
| 1268 |
-
|
| 1269 |
-
|
| 1270 |
-
|
| 1271 |
-
|
| 1272 |
-
|
| 1273 |
-
|
| 1274 |
-
|
| 1275 |
-
|
| 1276 |
-
|
| 1277 |
-
|
| 1278 |
-
|
| 1279 |
-
|
| 1280 |
-
|
| 1281 |
-
|
| 1282 |
-
|
| 1283 |
-
|
| 1284 |
-
|
| 1285 |
-
|
| 1286 |
-
|
| 1287 |
-
|
| 1288 |
-
|
| 1289 |
-
|
| 1290 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1291 |
|
| 1292 |
elif entity.dxftype() == 'SOLID':
|
| 1293 |
vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
|
|
|
|
| 1208 |
for path in entity.paths:
|
| 1209 |
vertices = [] # Reset vertices for each path
|
| 1210 |
|
| 1211 |
+
if path.type == 1:
|
| 1212 |
# Handle POLYLINE type HATCH
|
| 1213 |
vertices = [(vertex[0] * FinalRatio, vertex[1] * FinalRatio) for vertex in path.vertices]
|
| 1214 |
|
|
|
|
| 1245 |
unique_shapes.append((normalized_vertices, area1))
|
| 1246 |
hatched_areas.append([vertices, area1, perimeter, rgb_color])
|
| 1247 |
|
| 1248 |
+
elif path.type == 2:
|
| 1249 |
+
# convert any spline edges to line edges (approximates splines)
|
| 1250 |
+
# factor controls approximation density: control_points_count * factor
|
| 1251 |
+
try:
|
| 1252 |
+
path.spline_edges_to_line_edges(factor=8) # increase factor for finer approx
|
| 1253 |
+
except Exception:
|
| 1254 |
+
# defensive: some versions / unexpected objects might raise — ignore and continue
|
| 1255 |
+
pass
|
| 1256 |
+
|
| 1257 |
+
vert = []
|
| 1258 |
+
# After conversion most edges will be LineEdge with .start and .end
|
| 1259 |
+
for edge in path.edges:
|
| 1260 |
+
# If edge has start/end attributes (LineEdge), use them
|
| 1261 |
+
if hasattr(edge, "start") and hasattr(edge, "end"):
|
| 1262 |
+
sx, sy = edge.start
|
| 1263 |
+
ex, ey = edge.end
|
| 1264 |
+
# append only start point; we'll append the end of the last edge too to close path
|
| 1265 |
+
vert.append((sx * FinalRatio, sy * FinalRatio))
|
| 1266 |
+
else:
|
| 1267 |
+
# fallback: try control_points or fit_points if present (rare after conversion)
|
| 1268 |
+
if hasattr(edge, "control_points") and edge.control_points:
|
| 1269 |
+
for cp in edge.control_points:
|
| 1270 |
+
vert.append((cp[0] * FinalRatio, cp[1] * FinalRatio))
|
| 1271 |
+
elif hasattr(edge, "fit_points") and edge.fit_points:
|
| 1272 |
+
for fp in edge.fit_points:
|
| 1273 |
+
vert.append((fp[0] * FinalRatio, fp[1] * FinalRatio))
|
| 1274 |
+
else:
|
| 1275 |
+
# last resort: try to string-represent and skip if nothing usable
|
| 1276 |
+
continue
|
| 1277 |
+
|
| 1278 |
+
# After loop, ensure polygon is closed: append last edge.end if available
|
| 1279 |
+
if path.edges and hasattr(path.edges[-1], "end"):
|
| 1280 |
+
ex, ey = path.edges[-1].end
|
| 1281 |
+
vert.append((ex * FinalRatio, ey * FinalRatio))
|
| 1282 |
+
|
| 1283 |
+
# remove possible duplicate consecutive points (optional)
|
| 1284 |
+
cleaned = []
|
| 1285 |
+
for p in vert:
|
| 1286 |
+
if not cleaned or (abs(cleaned[-1][0] - p[0]) > 1e-9 or abs(cleaned[-1][1] - p[1]) > 1e-9):
|
| 1287 |
+
cleaned.append(p)
|
| 1288 |
+
vert = cleaned
|
| 1289 |
+
|
| 1290 |
+
# validate and use Shapely as before
|
| 1291 |
+
if len(vert) >= 3:
|
| 1292 |
+
poly = ShapelyPolygon(vert)
|
| 1293 |
+
minx, miny, maxx, maxy = poly.bounds
|
| 1294 |
+
width = maxx - minx
|
| 1295 |
+
height = maxy - miny
|
| 1296 |
+
|
| 1297 |
+
if (poly.area > 0.1 and (height > 0.1 and width > 0.1)):
|
| 1298 |
+
area1 = round(poly.area, 3)
|
| 1299 |
+
perimeter = round(poly.length, 3)
|
| 1300 |
+
normalized_vertices = normalize_vertices(vert)
|
| 1301 |
+
rgb_color = get_hatch_color(entity)
|
| 1302 |
+
if rgb_color == (255, 255, 255) and len(text_with_positions) > 0:
|
| 1303 |
+
for text, position, color in text_with_positions:
|
| 1304 |
+
text_position = Point(position[0], position[1])
|
| 1305 |
+
if poly.contains(text_position):
|
| 1306 |
+
rgb_color = color
|
| 1307 |
+
break
|
| 1308 |
+
|
| 1309 |
+
duplicate_found = False
|
| 1310 |
+
for existing_vertices, existing_area in unique_shapes:
|
| 1311 |
+
if normalized_vertices == existing_vertices and areas_are_similar(area1, existing_area):
|
| 1312 |
+
duplicate_found = True
|
| 1313 |
+
break
|
| 1314 |
+
|
| 1315 |
+
if not duplicate_found:
|
| 1316 |
+
unique_shapes.append((normalized_vertices, area1))
|
| 1317 |
+
hatched_areas.append([vert, area1, perimeter, rgb_color])
|
| 1318 |
|
| 1319 |
elif entity.dxftype() == 'SOLID':
|
| 1320 |
vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
|