Marthee commited on
Commit
3beabf7
·
verified ·
1 Parent(s): e9ed019

Update dxf__omar3_2.py

Browse files
Files changed (1) hide show
  1. 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 str(path.type) == 'BoundaryPathType.POLYLINE':
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 str(path.type) == 'BoundaryPathType.EDGE':
1249
- # Handle EDGE type HATCH
1250
- vert = []
1251
- for edge in path.edges:
1252
- x, y = edge.start
1253
- x1, y1 = edge.end
1254
- vert.append((x * FinalRatio, y * FinalRatio))
1255
- vert.append((x1 * FinalRatio, y1 * FinalRatio))
1256
-
1257
- poly = ShapelyPolygon(vert)
1258
- minx, miny, maxx, maxy = poly.bounds
1259
- width = maxx - minx
1260
- height = maxy - miny
1261
-
1262
- if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
1263
- area1 = round(poly.area, 3)
1264
- perimeter = round(poly.length, 3)
1265
-
1266
- normalized_vertices = normalize_vertices(vert)
1267
-
1268
- rgb_color = get_hatch_color(entity)
1269
- if(rgb_color == (255, 255, 255)):
1270
- if(len(text_with_positions)>0):
1271
- for text, position, color in text_with_positions:
1272
- text_position = Point(position[0], position[1])
1273
-
1274
- if poly.contains(text_position):
1275
- rgb_color = color
1276
- break
1277
-
1278
- duplicate_found = False
1279
- for existing_vertices, existing_area in unique_shapes:
1280
- if normalized_vertices == existing_vertices and areas_are_similar(area1, existing_area):
1281
- duplicate_found = True
1282
- break
1283
-
1284
- if not duplicate_found:
1285
- # rgb_color = get_hatch_color(entity) # Assuming this function exists
1286
- unique_shapes.append((normalized_vertices, area1))
1287
- hatched_areas.append([vert, area1, perimeter, rgb_color])
1288
-
1289
- else:
1290
- print(f"Unhandled path type: {path.type}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)]