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

Update deploying_3_3.py

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