Marthee commited on
Commit
775b8f6
·
verified ·
1 Parent(s): cdb1676

Update deploying_3_3.py

Browse files
Files changed (1) hide show
  1. deploying_3_3.py +431 -53
deploying_3_3.py CHANGED
@@ -238,8 +238,335 @@ def flip(img):
238
  flipped_horizontal = cv2.flip(rotated_image, 1)
239
  return flipped_horizontal
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
  """### Hatched areas"""
242
- def get_hatched_areas(filename,FinalRatio):
243
  doc = ezdxf.readfile(filename)
244
  doc.header['$MEASUREMENT'] = 1
245
  msp = doc.modelspace()
@@ -262,14 +589,15 @@ def get_hatched_areas(filename,FinalRatio):
262
  width = maxx - minx
263
  height = maxy - miny
264
 
265
- if poly.area > 0 and (height > 0.3 or width > 0.3):
266
  area1 = round(poly.area, 3)
267
  perimeter = round(poly.length, 3)
268
 
269
  # Append POLYLINE if vertices don't already exist
270
  if not vertices_exist(hatched_areas, vertices, threshold):
 
271
  # print(f"Appending POLYLINE with area: {area1} and perimeter: {perimeter}")
272
- hatched_areas.append([vertices, area1, perimeter])
273
 
274
  elif str(path.type) == 'BoundaryPathType.EDGE':
275
  # Handle EDGE type HATCH
@@ -285,14 +613,15 @@ def get_hatched_areas(filename,FinalRatio):
285
  width = maxx - minx
286
  height = maxy - miny
287
 
288
- if poly.area > 0 and (height > 0.3 or width > 0.3):
289
  area1 = round(poly.area, 3)
290
  perimeter = round(poly.length, 3)
291
 
292
  # Append EDGE if vertices don't already exist
293
  if not vertices_exist(hatched_areas, vert, threshold):
 
294
  # print(f"Appending EDGE with area: {area1} and perimeter: {perimeter}")
295
- hatched_areas.append([vert, area1, perimeter])
296
 
297
  else:
298
  print(f"Unhandled path type: {path.type}")
@@ -306,9 +635,10 @@ def get_hatched_areas(filename,FinalRatio):
306
  width = maxx - minx
307
  height = maxy - miny
308
 
309
- if (poly.area > 0 and (height > 0.3 or width > 0.3)):
310
  if not vertices_exist(hatched_areas, vertices, threshold):#if not vertices_exist(hatched_areas, vertices):
311
- hatched_areas.append([vertices,poly.area,poly.length])
 
312
 
313
 
314
 
@@ -334,7 +664,7 @@ def get_hatched_areas(filename,FinalRatio):
334
  height = maxy - miny
335
 
336
  # Check area and size constraints
337
- if poly.area > 0 and (height > 0.3 or width > 0.3):
338
  area1 = round(poly.area, 3)
339
  perimeter = round(poly.length, 3)
340
 
@@ -343,7 +673,8 @@ def get_hatched_areas(filename,FinalRatio):
343
  # flag = 1
344
  # if flag == 0:
345
  if not vertices_exist(hatched_areas, vertices, threshold):#if not vertices_exist(hatched_areas, vertices):
346
- hatched_areas.append([vertices, area1, perimeter])
 
347
 
348
 
349
 
@@ -366,14 +697,13 @@ def get_hatched_areas(filename,FinalRatio):
366
  width = maxx - minx
367
  height = maxy - miny
368
 
369
- if (poly.area > 0 and (height > 0.3 or width > 0.3)):
370
  area1 = round(poly.area,3)
371
  perimeter = round (poly.length,3)
372
- for i in range(len(hatched_areas)):
373
- if(area1 == hatched_areas[i][1]):
374
- flag=1
375
- if(flag==0):
376
- hatched_areas.append([vertices,area1,perimeter])
377
 
378
  elif entity.dxftype() == 'SPLINE':
379
  spline_entity = entity
@@ -391,11 +721,12 @@ def get_hatched_areas(filename,FinalRatio):
391
  height = maxy - miny
392
 
393
 
394
- if (poly.area > 0 and (height > 0.3 or width > 0.3)):
395
  area1 = round(poly.area,3)
396
  perimeter = round (poly.length,3)
397
- if not vertices_exist(hatched_areas, vertices):
398
- hatched_areas.append([vertices,area1,perimeter])
 
399
 
400
  sorted_data = sorted(hatched_areas, key=lambda x: x[1])
401
  return sorted_data
@@ -443,50 +774,36 @@ def rotate_polygon(polygon, angle, pdfrotation,width,height,center_point=(0, 0))
443
  #SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
444
  #loop 3la hatched areas and count the occurences of each shape w create a table bl hagat di
445
 
446
- def generate_color_array(length):
447
- colorRanges = []
448
- while len(colorRanges) < length:
449
- # Generate random RGB values
450
- r = random.randint(0, 255)
451
- g = random.randint(0, 255)
452
- b = random.randint(0, 255)
453
- # Ensure no duplicate colors
454
- if (r, g, b) not in colorRanges:
455
- colorRanges.append((r, g, b))
456
- return colorRanges
457
-
458
-
459
-
460
-
461
 
462
- def Create_DF(dxfpath,datadoc):
463
 
464
  FinalRatio= RetriveRatio(datadoc,dxfpath)
465
 
466
- hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
467
  # print('hatched_areas',hatched_areas)
468
  # hatched_areas=remove_duplicate_shapes(new_hatched_areas)
469
 
470
  # SimilarAreaDictionary= pd.DataFrame(columns=['Area', 'Total Area', 'Perimeter', 'Total Perimeter', 'Occurences', 'Color'])
471
  SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','Texts','Comments'])
472
 
473
- colorRanges2=generate_color_array(30000)
474
- colorRanges = [[255, 0, 0], [0, 0, 255], [0, 255, 255], [0, 64, 0], [255, 204, 0], [255, 128, 64], [255, 0, 128], [255, 128, 192], [128, 128, 255], [128, 64, 0],[0, 255, 0],[0, 200, 0],[255, 128, 255], [128, 0, 255], [0, 128, 192], [128, 0, 128],[128, 0, 0], [0, 128, 255], [149, 1, 70], [255, 182, 128], [222, 48, 71], [240, 0, 112], [255, 0, 255], [192, 46, 65], [0, 0, 128],[0, 128, 64],[255, 255, 0], [128, 0, 80], [255, 255, 128], [90, 255, 140],[255, 200, 20],[91, 16, 51], [90, 105, 138], [114, 10, 138], [36, 82, 78], [225, 105, 190], [108, 150, 170], [11, 35, 75], [42, 176, 170], [255, 176, 170], [209, 151, 15],[81, 27, 85], [226, 106, 122], [67, 119, 149], [159, 179, 140], [159, 179, 30],[255, 85, 198], [255, 27, 85], [188, 158, 8],[140, 188, 120], [59, 61, 52], [65, 81, 21], [212, 255, 174], [15, 164, 90],[41, 217, 245], [213, 23, 182], [11, 85, 169], [78, 153, 239], [0, 66, 141],[64, 98, 232], [140, 112, 255], [57, 33, 154], [194, 117, 252], [116, 92, 135], [74, 43, 98], [188, 13, 123], [129, 58, 91], [255, 128, 100], [171, 122, 145], [255, 98, 98], [222, 48, 77]]
475
- colorUsed=[]
 
476
  TotalArea=0
477
  TotalPerimeter=0
478
- for i in range(len(hatched_areas)):
479
- area = hatched_areas[i][1] # area
480
- perimeter = hatched_areas[i][2] # perimeter
481
- if(i < len(colorRanges)):
482
- color = colorRanges[i]
483
- colorUsed.append(color)
484
- else:
485
- color = colorRanges2[i]
486
- colorUsed.append(color)
487
  TotalArea = area
488
  TotalPerimeter = perimeter
489
- tol=2
490
  condition1 = (SimilarAreaDictionary['Area'] >= area - tol) & (SimilarAreaDictionary['Area'] <= area +tol)
491
  condition2 = (SimilarAreaDictionary['Perimeter'] >= perimeter -tol) & (SimilarAreaDictionary['Perimeter'] <= perimeter +tol)
492
  combined_condition = condition1 & condition2
@@ -499,7 +816,7 @@ def Create_DF(dxfpath,datadoc):
499
  else:
500
  TotalArea=area
501
  TotalPerimeter=perimeter
502
- new_data = {'Area': area, 'Total Area': TotalArea ,'Perimeter': perimeter, 'Total Perimeter': TotalPerimeter, 'Occurences': 1, 'Color':color,'Comments':''} #add color here and read color to insert in
503
  SimilarAreaDictionary = pd.concat([SimilarAreaDictionary, pd.DataFrame([new_data])], ignore_index=True)
504
 
505
  # print(SimilarAreaDictionary)
@@ -509,7 +826,7 @@ def Create_DF(dxfpath,datadoc):
509
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
510
  FinalRatio= RetriveRatio(datadoc,dxfpath)
511
 
512
- hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
513
  # hatched_areas=remove_duplicate_shapes(new_hatched_areas)
514
 
515
  img=pdftoimg(datadoc)
@@ -534,17 +851,25 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
534
  ratio = pix.width/ img.shape[1]
535
  rotationangle = 270
536
 
 
 
537
  allshapes=[]
538
  # Iterate through each polygon in metric units
539
  NewColors = []
540
- SimilarAreaDictionary=Create_DF(dxfpath,datadoc)
541
  i=0
 
542
 
543
 
544
  for polygon in hatched_areas:
545
  cntPoints = []
546
  cntPoints1 = []
547
  shapee = []
 
 
 
 
 
548
  # Convert each vertex from metric to pixel coordinates
549
  for vertex in polygon[0]:
550
  x = (vertex[0]) *dxfratio
@@ -555,6 +880,9 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
555
  cntPoints.append([int(x), int(y)])
556
  cntPoints1.append([x, y])
557
 
 
 
 
558
  for poi in np.array(cntPoints1):
559
  x1, y1 = poi
560
  p1 = fitz.Point(x1,y1)
@@ -564,19 +892,69 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
564
 
565
  shapee=np.flip(shapee,1)
566
  shapee=rotate_polygon(shapee,rotationangle,rotationOld,width,height)
567
- tol=2
568
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
569
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
570
  combined_condition = condition1 & condition2
571
 
572
  if any(combined_condition):
573
-
574
  index = np.where(combined_condition)[0][0]
575
  # print(SimilarAreaDictionary.at[index, 'Color'])
576
  NewColors=SimilarAreaDictionary.at[index, 'Color']
577
  else:
 
578
  NewColors=SimilarAreaDictionary.at[i, 'Color']
579
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580
  # cv2.drawContours(imgg, [np.array(cntPoints)], -1, (NewColors), thickness=2)
581
  cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
582
  annot11 = page2.add_polygon_annot( points=shapee) # 'Polygon'
 
238
  flipped_horizontal = cv2.flip(rotated_image, 1)
239
  return flipped_horizontal
240
 
241
+
242
+
243
+ def aci_to_rgb(aci):
244
+ aci_rgb_map = {
245
+ 0: (0, 0, 0),
246
+ 1: (255, 0, 0),
247
+ 2: (255, 255, 0),
248
+ 3: (0, 255, 0),
249
+ 4: (0, 255, 255),
250
+ 5: (0, 0, 255),
251
+ 6: (255, 0, 255),
252
+ 7: (255, 255, 255),
253
+ 8: (65, 65, 65),
254
+ 9: (128, 128, 128),
255
+ 10: (255, 0, 0),
256
+ 11: (255, 170, 170),
257
+ 12: (189, 0, 0),
258
+ 13: (189, 126, 126),
259
+ 14: (129, 0, 0),
260
+ 15: (129, 86, 86),
261
+ 16: (104, 0, 0),
262
+ 17: (104, 69, 69),
263
+ 18: (79, 0, 0),
264
+ 19: (79, 53, 53),
265
+ 20: (255, 63, 0),
266
+ 21: (255, 191, 170),
267
+ 22: (189, 46, 0),
268
+ 23: (189, 141, 126),
269
+ 24: (129, 31, 0),
270
+ 25: (129, 96, 86),
271
+ 26: (104, 25, 0),
272
+ 27: (104, 78, 69),
273
+ 28: (79, 19, 0),
274
+ 29: (79, 59, 53),
275
+ 30: (255, 127, 0),
276
+ 31: (255, 212, 170),
277
+ 32: (189, 94, 0),
278
+ 33: (189, 157, 126),
279
+ 34: (129, 64, 0),
280
+ 35: (129, 107, 86),
281
+ 36: (104, 52, 0),
282
+ 37: (104, 86, 69),
283
+ 38: (79, 39, 0),
284
+ 39: (79, 66, 53),
285
+ 40: (255, 191, 0),
286
+ 41: (255, 234, 170),
287
+ 42: (189, 141, 0),
288
+ 43: (189, 173, 126),
289
+ 44: (129, 96, 0),
290
+ 45: (129, 118, 86),
291
+ 46: (104, 78, 0),
292
+ 47: (104, 95, 69),
293
+ 48: (79, 59, 0),
294
+ 49: (79, 73, 53),
295
+ 50: (255, 255, 0),
296
+ 51: (255, 255, 170),
297
+ 52: (189, 189, 0),
298
+ 53: (189, 189, 126),
299
+ 54: (129, 129, 0),
300
+ 55: (129, 129, 86),
301
+ 56: (104, 104, 0),
302
+ 57: (104, 104, 69),
303
+ 58: (79, 79, 0),
304
+ 59: (79, 79, 53),
305
+ 60: (191, 255, 0),
306
+ 61: (234, 255, 170),
307
+ 62: (141, 189, 0),
308
+ 63: (173, 189, 126),
309
+ 64: (96, 129, 0),
310
+ 65: (118, 129, 86),
311
+ 66: (78, 104, 0),
312
+ 67: (95, 104, 69),
313
+ 68: (59, 79, 0),
314
+ 69: (73, 79, 53),
315
+ 70: (127, 255, 0),
316
+ 71: (212, 255, 170),
317
+ 72: (94, 189, 0),
318
+ 73: (157, 189, 126),
319
+ 74: (64, 129, 0),
320
+ 75: (107, 129, 86),
321
+ 76: (52, 104, 0),
322
+ 77: (86, 104, 69),
323
+ 78: (39, 79, 0),
324
+ 79: (66, 79, 53),
325
+ 80: (63, 255, 0),
326
+ 81: (191, 255, 170),
327
+ 82: (46, 189, 0),
328
+ 83: (141, 189, 126),
329
+ 84: (31, 129, 0),
330
+ 85: (96, 129, 86),
331
+ 86: (25, 104, 0),
332
+ 87: (78, 104, 69),
333
+ 88: (19, 79, 0),
334
+ 89: (59, 79, 53),
335
+ 90: (0, 255, 0),
336
+ 91: (170, 255, 170),
337
+ 92: (0, 189, 0),
338
+ 93: (126, 189, 126),
339
+ 94: (0, 129, 0),
340
+ 95: (86, 129, 86),
341
+ 96: (0, 104, 0),
342
+ 97: (69, 104, 69),
343
+ 98: (0, 79, 0),
344
+ 99: (53, 79, 53),
345
+ 100: (0, 255, 63),
346
+ 101: (170, 255, 191),
347
+ 102: (0, 189, 46),
348
+ 103: (126, 189, 141),
349
+ 104: (0, 129, 31),
350
+ 105: (86, 129, 96),
351
+ 106: (0, 104, 25),
352
+ 107: (69, 104, 78),
353
+ 108: (0, 79, 19),
354
+ 109: (53, 79, 59),
355
+ 110: (0, 255, 127),
356
+ 111: (170, 255, 212),
357
+ 112: (0, 189, 94),
358
+ 113: (126, 189, 157),
359
+ 114: (0, 129, 64),
360
+ 115: (86, 129, 107),
361
+ 116: (0, 104, 52),
362
+ 117: (69, 104, 86),
363
+ 118: (0, 79, 39),
364
+ 119: (53, 79, 66),
365
+ 120: (0, 255, 191),
366
+ 121: (170, 255, 234),
367
+ 122: (0, 189, 141),
368
+ 123: (126, 189, 173),
369
+ 124: (0, 129, 96),
370
+ 125: (86, 129, 118),
371
+ 126: (0, 104, 78),
372
+ 127: (69, 104, 95),
373
+ 128: (0, 79, 59),
374
+ 129: (53, 79, 73),
375
+ 130: (0, 255, 255),
376
+ 131: (170, 255, 255),
377
+ 132: (0, 189, 189),
378
+ 133: (126, 189, 189),
379
+ 134: (0, 129, 129),
380
+ 135: (86, 129, 129),
381
+ 136: (0, 104, 104),
382
+ 137: (69, 104, 104),
383
+ 138: (0, 79, 79),
384
+ 139: (53, 79, 79),
385
+ 140: (0, 191, 255),
386
+ 141: (170, 234, 255),
387
+ 142: (0, 141, 189),
388
+ 143: (126, 173, 189),
389
+ 144: (0, 96, 129),
390
+ 145: (86, 118, 129),
391
+ 146: (0, 78, 104),
392
+ 147: (69, 95, 104),
393
+ 148: (0, 59, 79),
394
+ 149: (53, 73, 79),
395
+ 150: (0, 127, 255),
396
+ 151: (170, 212, 255),
397
+ 152: (0, 94, 189),
398
+ 153: (126, 157, 189),
399
+ 154: (0, 64, 129),
400
+ 155: (86, 107, 129),
401
+ 156: (0, 52, 104),
402
+ 157: (69, 86, 104),
403
+ 158: (0, 39, 79),
404
+ 159: (53, 66, 79),
405
+ 160: (0, 63, 255),
406
+ 161: (170, 191, 255),
407
+ 162: (0, 46, 189),
408
+ 163: (126, 141, 189),
409
+ 164: (0, 31, 129),
410
+ 165: (86, 96, 129),
411
+ 166: (0, 25, 104),
412
+ 167: (69, 78, 104),
413
+ 168: (0, 19, 79),
414
+ 169: (53, 59, 79),
415
+ 170: (0, 0, 255),
416
+ 171: (170, 170, 255),
417
+ 172: (0, 0, 189),
418
+ 173: (126, 126, 189),
419
+ 174: (0, 0, 129),
420
+ 175: (86, 86, 129),
421
+ 176: (0, 0, 104),
422
+ 177: (69, 69, 104),
423
+ 178: (0, 0, 79),
424
+ 179: (53, 53, 79),
425
+ 180: (63, 0, 255),
426
+ 181: (191, 170, 255),
427
+ 182: (46, 0, 189),
428
+ 183: (141, 126, 189),
429
+ 184: (31, 0, 129),
430
+ 185: (96, 86, 129),
431
+ 186: (25, 0, 104),
432
+ 187: (78, 69, 104),
433
+ 188: (19, 0, 79),
434
+ 189: (59, 53, 79),
435
+ 190: (127, 0, 255),
436
+ 191: (212, 170, 255),
437
+ 192: (94, 0, 189),
438
+ 193: (157, 126, 189),
439
+ 194: (64, 0, 129),
440
+ 195: (107, 86, 129),
441
+ 196: (52, 0, 104),
442
+ 197: (86, 69, 104),
443
+ 198: (39, 0, 79),
444
+ 199: (66, 53, 79),
445
+ 200: (191, 0, 255),
446
+ 201: (234, 170, 255),
447
+ 202: (141, 0, 189),
448
+ 203: (173, 126, 189),
449
+ 204: (96, 0, 129),
450
+ 205: (118, 86, 129),
451
+ 206: (78, 0, 104),
452
+ 207: (95, 69, 104),
453
+ 208: (59, 0, 79),
454
+ 209: (73, 53, 79),
455
+ 210: (255, 0, 255),
456
+ 211: (255, 170, 255),
457
+ 212: (189, 0, 189),
458
+ 213: (189, 126, 189),
459
+ 214: (129, 0, 129),
460
+ 215: (129, 86, 129),
461
+ 216: (104, 0, 104),
462
+ 217: (104, 69, 104),
463
+ 218: (79, 0, 79),
464
+ 219: (79, 53, 79),
465
+ 220: (255, 0, 191),
466
+ 221: (255, 170, 234),
467
+ 222: (189, 0, 141),
468
+ 223: (189, 126, 173),
469
+ 224: (129, 0, 96),
470
+ 225: (129, 86, 118),
471
+ 226: (104, 0, 78),
472
+ 227: (104, 69, 95),
473
+ 228: (79, 0, 59),
474
+ 229: (79, 53, 73),
475
+ 230: (255, 0, 127),
476
+ 231: (255, 170, 212),
477
+ 232: (189, 0, 94),
478
+ 233: (189, 126, 157),
479
+ 234: (129, 0, 64),
480
+ 235: (129, 86, 107),
481
+ 236: (104, 0, 52),
482
+ 237: (104, 69, 86),
483
+ 238: (79, 0, 39),
484
+ 239: (79, 53, 66),
485
+ 240: (255, 0, 63),
486
+ 241: (255, 170, 191),
487
+ 242: (189, 0, 46),
488
+ 243: (189, 126, 141),
489
+ 244: (129, 0, 31),
490
+ 245: (129, 86, 96),
491
+ 246: (104, 0, 25),
492
+ 247: (104, 69, 78),
493
+ 248: (79, 0, 19),
494
+ 249: (79, 53, 59),
495
+ 250: (51, 51, 51),
496
+ 251: (80, 80, 80),
497
+ 252: (105, 105, 105),
498
+ 253: (130, 130, 130),
499
+ 254: (190, 190, 190),
500
+ 255: (255, 255, 255)
501
+ }
502
+
503
+ # Default to white if index is invalid or not found
504
+ return aci_rgb_map.get(aci, (255, 255, 255))
505
+
506
+
507
+
508
+ def int_to_rgb(color_int):
509
+ """Convert an integer to an (R, G, B) tuple."""
510
+ r = (color_int >> 16) & 255
511
+ g = (color_int >> 8) & 255
512
+ b = color_int & 255
513
+ return (r, g, b)
514
+
515
+
516
+
517
+ def get_hatch_color(entity):
518
+ # Check if the entity has a "true color" set
519
+ if entity.dxf.hasattr('true_color'):
520
+ true_color = entity.dxf.true_color
521
+ rgb_color = int_to_rgb(true_color) # Convert integer to (R, G, B)
522
+ print(f"True color detected (RGB): {rgb_color}")
523
+ return rgb_color
524
+
525
+ color_index = entity.dxf.color
526
+ print("color_index = ", color_index)
527
+
528
+ # Check if the color is set to ByLayer or ByBlock
529
+ if color_index == 0: # ByLayer color
530
+ print("Color is ByLayer, checking layer color...")
531
+ layer_name = entity.dxf.layer
532
+ layer = entity.doc.layers.get(layer_name)
533
+
534
+ if layer: # Ensure layer exists
535
+ layer_color_index = layer.dxf.color
536
+ print(f"Layer '{layer_name}' Color Index = {layer_color_index}")
537
+ return aci_to_rgb(layer_color_index) # Use custom aci_to_rgb function
538
+ else:
539
+ print(f"Layer '{layer_name}' not found, defaulting to white.")
540
+ return (255, 255, 255) # Default to white if layer not found
541
+
542
+ elif color_index == 256: # ByBlock color
543
+ print("Color is ByBlock, checking block color or defaulting to white.")
544
+ block_color = (255, 255, 255) # White as default
545
+
546
+ # Check if the entity is inside a block reference and inherit its color
547
+ if hasattr(entity, 'block'): # Check if the entity belongs to a block
548
+ block_ref = entity.block
549
+ if block_ref.dxf.hasattr('color'):
550
+ block_color = aci_to_rgb(block_ref.dxf.color)
551
+ print(f"Block reference color found: {block_color}")
552
+ else:
553
+ print("Block has no color attribute, using default (white).")
554
+ return block_color
555
+
556
+ # Otherwise, convert the ACI color to RGB
557
+ print(f"Entity Color Index = {color_index}")
558
+ if 1 <= color_index <= 255:
559
+ rgb_color = aci_to_rgb(color_index) # Use custom aci_to_rgb function
560
+ print(f"Converted RGB = {rgb_color}")
561
+ return rgb_color
562
+
563
+ # Default to white if color index is out of bounds or invalid
564
+ print("Invalid or unhandled color index, defaulting to white.")
565
+ return (255, 255, 255)
566
+
567
+
568
  """### Hatched areas"""
569
+ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle):
570
  doc = ezdxf.readfile(filename)
571
  doc.header['$MEASUREMENT'] = 1
572
  msp = doc.modelspace()
 
589
  width = maxx - minx
590
  height = maxy - miny
591
 
592
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
593
  area1 = round(poly.area, 3)
594
  perimeter = round(poly.length, 3)
595
 
596
  # Append POLYLINE if vertices don't already exist
597
  if not vertices_exist(hatched_areas, vertices, threshold):
598
+ rgb_color = get_hatch_color(entity)
599
  # print(f"Appending POLYLINE with area: {area1} and perimeter: {perimeter}")
600
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
601
 
602
  elif str(path.type) == 'BoundaryPathType.EDGE':
603
  # Handle EDGE type HATCH
 
613
  width = maxx - minx
614
  height = maxy - miny
615
 
616
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
617
  area1 = round(poly.area, 3)
618
  perimeter = round(poly.length, 3)
619
 
620
  # Append EDGE if vertices don't already exist
621
  if not vertices_exist(hatched_areas, vert, threshold):
622
+ rgb_color = get_hatch_color(entity)
623
  # print(f"Appending EDGE with area: {area1} and perimeter: {perimeter}")
624
+ hatched_areas.append([vert, area1, perimeter, rgb_color])
625
 
626
  else:
627
  print(f"Unhandled path type: {path.type}")
 
635
  width = maxx - minx
636
  height = maxy - miny
637
 
638
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
639
  if not vertices_exist(hatched_areas, vertices, threshold):#if not vertices_exist(hatched_areas, vertices):
640
+ rgb_color = get_hatch_color(entity)
641
+ hatched_areas.append([vertices,poly.area,poly.length,rgb_color])
642
 
643
 
644
 
 
664
  height = maxy - miny
665
 
666
  # Check area and size constraints
667
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
668
  area1 = round(poly.area, 3)
669
  perimeter = round(poly.length, 3)
670
 
 
673
  # flag = 1
674
  # if flag == 0:
675
  if not vertices_exist(hatched_areas, vertices, threshold):#if not vertices_exist(hatched_areas, vertices):
676
+ rgb_color = get_hatch_color(entity)
677
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
678
 
679
 
680
 
 
697
  width = maxx - minx
698
  height = maxy - miny
699
 
700
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
701
  area1 = round(poly.area,3)
702
  perimeter = round (poly.length,3)
703
+ if not vertices_exist(hatched_areas, vertices, threshold):#if not vertices_exist(hatched_areas, vertices):
704
+ rgb_color = get_hatch_color(entity)
705
+ hatched_areas.append([vertices, area1, perimeter,rgb_color])
706
+
 
707
 
708
  elif entity.dxftype() == 'SPLINE':
709
  spline_entity = entity
 
721
  height = maxy - miny
722
 
723
 
724
+ if (poly.area > 0.9 and (height > 0.7 and width > 0.7)):
725
  area1 = round(poly.area,3)
726
  perimeter = round (poly.length,3)
727
+ if not vertices_exist(hatched_areas, vertices, threshold):
728
+ rgb_color = get_hatch_color(entity)
729
+ hatched_areas.append([vertices,area1,perimeter,rgb_color])
730
 
731
  sorted_data = sorted(hatched_areas, key=lambda x: x[1])
732
  return sorted_data
 
774
  #SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','R','G','B'])
775
  #loop 3la hatched areas and count the occurences of each shape w create a table bl hagat di
776
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
777
 
778
+ def Create_DF(dxfpath,datadoc,hatched_areas):
779
 
780
  FinalRatio= RetriveRatio(datadoc,dxfpath)
781
 
782
+ # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
783
  # print('hatched_areas',hatched_areas)
784
  # hatched_areas=remove_duplicate_shapes(new_hatched_areas)
785
 
786
  # SimilarAreaDictionary= pd.DataFrame(columns=['Area', 'Total Area', 'Perimeter', 'Total Perimeter', 'Occurences', 'Color'])
787
  SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','Texts','Comments'])
788
 
789
+ # colorRanges2=generate_color_array(30000)
790
+ # colorRanges = [[255, 0, 0], [0, 0, 255], [0, 255, 255], [0, 64, 0], [255, 204, 0], [255, 128, 64], [255, 0, 128], [255, 128, 192], [128, 128, 255], [128, 64, 0],[0, 255, 0],[0, 200, 0],[255, 128, 255], [128, 0, 255], [0, 128, 192], [128, 0, 128],[128, 0, 0], [0, 128, 255], [149, 1, 70], [255, 182, 128], [222, 48, 71], [240, 0, 112], [255, 0, 255], [192, 46, 65], [0, 0, 128],[0, 128, 64],[255, 255, 0], [128, 0, 80], [255, 255, 128], [90, 255, 140],[255, 200, 20],[91, 16, 51], [90, 105, 138], [114, 10, 138], [36, 82, 78], [225, 105, 190], [108, 150, 170], [11, 35, 75], [42, 176, 170], [255, 176, 170], [209, 151, 15],[81, 27, 85], [226, 106, 122], [67, 119, 149], [159, 179, 140], [159, 179, 30],[255, 85, 198], [255, 27, 85], [188, 158, 8],[140, 188, 120], [59, 61, 52], [65, 81, 21], [212, 255, 174], [15, 164, 90],[41, 217, 245], [213, 23, 182], [11, 85, 169], [78, 153, 239], [0, 66, 141],[64, 98, 232], [140, 112, 255], [57, 33, 154], [194, 117, 252], [116, 92, 135], [74, 43, 98], [188, 13, 123], [129, 58, 91], [255, 128, 100], [171, 122, 145], [255, 98, 98], [222, 48, 77]]
791
+ # colorUsed=[]
792
+
793
  TotalArea=0
794
  TotalPerimeter=0
795
+ for shape in hatched_areas:
796
+ area = shape[1] # area
797
+ perimeter = shape[1] # perimeter
798
+ # if(i < len(colorRanges)):
799
+ # color = colorRanges[i]
800
+ # colorUsed.append(color)
801
+ # else:
802
+ # color = colorRanges2[i]
803
+ # colorUsed.append(color)
804
  TotalArea = area
805
  TotalPerimeter = perimeter
806
+ tol=0
807
  condition1 = (SimilarAreaDictionary['Area'] >= area - tol) & (SimilarAreaDictionary['Area'] <= area +tol)
808
  condition2 = (SimilarAreaDictionary['Perimeter'] >= perimeter -tol) & (SimilarAreaDictionary['Perimeter'] <= perimeter +tol)
809
  combined_condition = condition1 & condition2
 
816
  else:
817
  TotalArea=area
818
  TotalPerimeter=perimeter
819
+ new_data = {'Area': area, 'Total Area': TotalArea ,'Perimeter': perimeter, 'Total Perimeter': TotalPerimeter, 'Occurences': 1, 'Color':shape[3],'Comments':''} #add color here and read color to insert in
820
  SimilarAreaDictionary = pd.concat([SimilarAreaDictionary, pd.DataFrame([new_data])], ignore_index=True)
821
 
822
  # print(SimilarAreaDictionary)
 
826
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath=0,pdfname=0):
827
  FinalRatio= RetriveRatio(datadoc,dxfpath)
828
 
829
+ # hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
830
  # hatched_areas=remove_duplicate_shapes(new_hatched_areas)
831
 
832
  img=pdftoimg(datadoc)
 
851
  ratio = pix.width/ img.shape[1]
852
  rotationangle = 270
853
 
854
+
855
+ hatched_areas = get_hatched_areas(datadoc,dxfpath,FinalRatio,rotationangle)
856
  allshapes=[]
857
  # Iterate through each polygon in metric units
858
  NewColors = []
859
+ SimilarAreaDictionary=Create_DF(dxfpath,datadoc,hatched_areas)
860
  i=0
861
+ flagcolor = 0
862
 
863
 
864
  for polygon in hatched_areas:
865
  cntPoints = []
866
  cntPoints1 = []
867
  shapee = []
868
+
869
+ blackImgShapes = np.zeros(imgg.shape[:2], dtype="uint8")
870
+ blackImgShapes= cv2.cvtColor(blackImgShapes, cv2.COLOR_GRAY2BGR)
871
+
872
+
873
  # Convert each vertex from metric to pixel coordinates
874
  for vertex in polygon[0]:
875
  x = (vertex[0]) *dxfratio
 
880
  cntPoints.append([int(x), int(y)])
881
  cntPoints1.append([x, y])
882
 
883
+ cv2.drawContours(blackImgShapes, [np.array(cntPoints)], -1, ([255,255,255]), thickness=-1)
884
+ x, y, w, h = cv2.boundingRect(np.array(cntPoints))
885
+
886
  for poi in np.array(cntPoints1):
887
  x1, y1 = poi
888
  p1 = fitz.Point(x1,y1)
 
892
 
893
  shapee=np.flip(shapee,1)
894
  shapee=rotate_polygon(shapee,rotationangle,rotationOld,width,height)
895
+ tol=0
896
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
897
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
898
  combined_condition = condition1 & condition2
899
 
900
  if any(combined_condition):
901
+ flagcolor = 1
902
  index = np.where(combined_condition)[0][0]
903
  # print(SimilarAreaDictionary.at[index, 'Color'])
904
  NewColors=SimilarAreaDictionary.at[index, 'Color']
905
  else:
906
+ flagcolor = 2
907
  NewColors=SimilarAreaDictionary.at[i, 'Color']
908
 
909
+ if(int(NewColors[0])==255 and int(NewColors[1])==255 and int(NewColors[2])==255):
910
+ WhiteImgFinal = cv2.bitwise_and(blackImgShapes,imgg)
911
+ flipped=flip(WhiteImgFinal)
912
+
913
+
914
+ imgslice = WhiteImgFinal[y:y+h, x:x+w]
915
+ flippedSlice=flip(imgslice)
916
+
917
+
918
+ # Convert flippedSlice to PIL for color extraction
919
+ flippedSlice_pil = Image.fromarray(flippedSlice)
920
+
921
+ # Define patch size for color sampling (e.g., 10x10 pixels)
922
+ patch_size = 60
923
+ patch_colors = []
924
+
925
+ # Loop through patches in the image
926
+ for i in range(0, flippedSlice_pil.width, patch_size):
927
+ for j in range(0, flippedSlice_pil.height, patch_size):
928
+ # Crop a patch from the original image
929
+ patch = flippedSlice_pil.crop((i, j, i + patch_size, j + patch_size))
930
+ patch_colors += patch.getcolors(patch_size * patch_size)
931
+
932
+ # Calculate the dominant color from all patches
933
+ max_count = 0
934
+ dominant_color = None
935
+ tolerance = 5
936
+ black_threshold = 30 # Max RGB value for a color to be considered "black"
937
+ white_threshold = 225 # Min RGB value for a color to be considered "white"
938
+
939
+ for count, color in patch_colors:
940
+ # Exclude colors within the black and white ranges
941
+ if not (all(c <= black_threshold for c in color) or all(c >= white_threshold for c in color)):
942
+ # Update if the current color has a higher count than previous max
943
+ if count > max_count:
944
+ max_count = count
945
+ dominant_color = color
946
+
947
+
948
+
949
+ # Append dominant color to ColorCheck and update NewColors
950
+ if (dominant_color != None):
951
+ ColorCheck.append(dominant_color)
952
+ NewColors = (dominant_color[2], dominant_color[1], dominant_color[0])
953
+ if(flagcolor == 1):
954
+ SimilarAreaDictionary.at[index, 'Color'] = NewColors
955
+ elif(flagcolor == 2):
956
+ SimilarAreaDictionary.at[i, 'Color'] = NewColors
957
+
958
  # cv2.drawContours(imgg, [np.array(cntPoints)], -1, (NewColors), thickness=2)
959
  cv2.drawContours(imgg, [np.array(cntPoints)], -1, ([NewColors[2],NewColors[1],NewColors[0]]), thickness=-1)
960
  annot11 = page2.add_polygon_annot( points=shapee) # 'Polygon'