Marthee commited on
Commit
56b36c1
·
verified ·
1 Parent(s): 3d2cd4d

Upload 4 files

Browse files
Files changed (4) hide show
  1. Code_2_7.py +14 -2
  2. app.py +9 -7
  3. deploying_3_3.py +83 -14
  4. dxf__omar3_2.py +192 -60
Code_2_7.py CHANGED
@@ -1244,9 +1244,15 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray,Coll
1244
  # colored_fix = [tuple(map(int, c)) for c in coloredarray]
1245
 
1246
  if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
 
 
 
 
1247
  hatched_areas.append([vertices, area1, length, rgb_color])
1248
  elif (len(coloredarray) == 0):
1249
  hatched_areas.append([vertices, area1, length, rgb_color])
 
 
1250
 
1251
 
1252
  elif str(path.type) == 'BoundaryPathType.EDGE' or path.type == 2:
@@ -1300,6 +1306,10 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray,Coll
1300
 
1301
  rgbconverted = tuple(rgb_color)
1302
  if ( len(CollectedColors) > 0 and (rgb_color in CollectedColors)):
 
 
 
 
1303
  hatched_areas.append([vert, area1, length, rgb_color])
1304
  elif (len(CollectedColors) == 0):
1305
  hatched_areas.append([vert, area1, length, rgb_color])
@@ -1334,6 +1344,8 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray,Coll
1334
  unique_shapes.append((normalized_vertices, area1))
1335
  rgbconverted = tuple(rgb_color)
1336
  if ( len(CollectedColors) > 0 and (rgb_color in CollectedColors)):
 
 
1337
  hatched_areas.append([vertices, area1, perimeter, rgb_color])
1338
  elif (len(CollectedColors) == 0):
1339
  hatched_areas.append([vertices, area1, perimeter, rgb_color])
@@ -1679,7 +1691,7 @@ def adjustannotations(OutputPdfStage1,text_with_positions,CollectedColors):
1679
  matched_text2, matched_nbs2 = matched_entry2
1680
  print("2nd entry nbs: " ,type(matched_nbs2))
1681
  print("2nd entry text: ",type(matched_text2))
1682
- combined = ""
1683
  if matched_text2 and matched_nbs2:
1684
  combined2 = f"{matched_text2} - {matched_nbs2}"
1685
  elif matched_text:
@@ -1978,7 +1990,7 @@ def draw_bb_onPDF(doc,bbox):
1978
 
1979
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,Thickness,pdfpath=0,pdfname=0,pdf_content=0):
1980
  print("points_Of_drawing_Canvas in 2.7 = ",points_Of_drawing_Canvas)
1981
- print("CollectedColors = ",CollectedColors)
1982
  OutputPdfStage1='BB Trial.pdf'
1983
  if pdf_content:
1984
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
 
1244
  # colored_fix = [tuple(map(int, c)) for c in coloredarray]
1245
 
1246
  if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1247
+
1248
+ print("rgbcolor in 2.7 hatch 1 type = ",type(rgb_color))
1249
+ print("coloredarray in 2.7 hatch 1 type = ",type(coloredarray))
1250
+
1251
  hatched_areas.append([vertices, area1, length, rgb_color])
1252
  elif (len(coloredarray) == 0):
1253
  hatched_areas.append([vertices, area1, length, rgb_color])
1254
+ print("rgbcolor = ",rgb_color)
1255
+ print("coloredarray =",coloredarray)
1256
 
1257
 
1258
  elif str(path.type) == 'BoundaryPathType.EDGE' or path.type == 2:
 
1306
 
1307
  rgbconverted = tuple(rgb_color)
1308
  if ( len(CollectedColors) > 0 and (rgb_color in CollectedColors)):
1309
+
1310
+ print("rgbcolor in 2.7 hatch 2 type = ",type(rgb_color))
1311
+ print("CollectedColors in 2.7 hatch 2 type = ",type(CollectedColors))
1312
+
1313
  hatched_areas.append([vert, area1, length, rgb_color])
1314
  elif (len(CollectedColors) == 0):
1315
  hatched_areas.append([vert, area1, length, rgb_color])
 
1344
  unique_shapes.append((normalized_vertices, area1))
1345
  rgbconverted = tuple(rgb_color)
1346
  if ( len(CollectedColors) > 0 and (rgb_color in CollectedColors)):
1347
+ print("rgbcolor in 2.7 solid type = ",type(rgb_color))
1348
+ print("CollectedColors in 2.7 solid type = ",type(CollectedColors))
1349
  hatched_areas.append([vertices, area1, perimeter, rgb_color])
1350
  elif (len(CollectedColors) == 0):
1351
  hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
1691
  matched_text2, matched_nbs2 = matched_entry2
1692
  print("2nd entry nbs: " ,type(matched_nbs2))
1693
  print("2nd entry text: ",type(matched_text2))
1694
+ combined2 = ""
1695
  if matched_text2 and matched_nbs2:
1696
  combined2 = f"{matched_text2} - {matched_nbs2}"
1697
  elif matched_text:
 
1990
 
1991
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,Thickness,pdfpath=0,pdfname=0,pdf_content=0):
1992
  print("points_Of_drawing_Canvas in 2.7 = ",points_Of_drawing_Canvas)
1993
+ print("CollectedColors in 2.7 = ",CollectedColors)
1994
  OutputPdfStage1='BB Trial.pdf'
1995
  if pdf_content:
1996
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
app.py CHANGED
@@ -1002,28 +1002,30 @@ def measureproject(result,dataDoc=0,img=0,dxffile=0,pdf_content=0,csvfile=0):
1002
  temp_file.write(dxfpath)
1003
  temp_filename = temp_file.name
1004
  print(temp_filename)
1005
- CorrectionRatio=result[8]
1006
  print("result = ",result)
1007
  SearchArray=result[6]
1008
  parsed_url = urlparse(result[0])
 
 
1009
  filename = parsed_url.path.split('/')[-1]
1010
  print(filename)
1011
  nameofpdf=filename
1012
  points_Of_drawing_Canvas=False
1013
 
1014
- if len(result[7])>0:
1015
- points_Of_drawing_Canvas=drawonpdf(nameofpdf,result[7]) ########################## points of manual drawing hereee
1016
 
1017
  if pdf_content:
1018
  if section.startswith('3.3'):
1019
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
1020
  else:
1021
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
1022
  else:
1023
  if section.startswith('3.3'):
1024
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0])
1025
  else:
1026
- doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath,result[0])
1027
  global colorsused
1028
  colorsused=list(SimilarAreaDictionary['Color'])
1029
  dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
 
1002
  temp_file.write(dxfpath)
1003
  temp_filename = temp_file.name
1004
  print(temp_filename)
1005
+ CorrectionRatio=result[9]
1006
  print("result = ",result)
1007
  SearchArray=result[6]
1008
  parsed_url = urlparse(result[0])
1009
+ CollectedColors=result[7]
1010
+ print("CollectedColors = ",CollectedColors)
1011
  filename = parsed_url.path.split('/')[-1]
1012
  print(filename)
1013
  nameofpdf=filename
1014
  points_Of_drawing_Canvas=False
1015
 
1016
+ if len(result[8])>0:
1017
+ points_Of_drawing_Canvas=drawonpdf(nameofpdf,result[8]) ########################## points of manual drawing hereee
1018
 
1019
  if pdf_content:
1020
  if section.startswith('3.3'):
1021
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
1022
  else:
1023
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath,result[0],pdf_content)
1024
  else:
1025
  if section.startswith('3.3'):
1026
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas , bax_pretty_xml, column_xml=deploying_3_3.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath,result[0])
1027
  else:
1028
+ doc,outputimg, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas, bax_pretty_xml, column_xml=dxf__omar3_2.mainFunctionDrawImgPdf(dataDoc,temp_filename,result[4] ,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath,result[0])
1029
  global colorsused
1030
  colorsused=list(SimilarAreaDictionary['Color'])
1031
  dbPath='/TSA JOBS/ADR Test'+pdfpath+'Measured Plan/'
deploying_3_3.py CHANGED
@@ -963,7 +963,9 @@ def color_close_enough(c1, c2, threshold=10):
963
 
964
 
965
  """### Hatched areas"""
966
- def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
 
 
967
 
968
  doc = ezdxf.readfile(filename)
969
  doc.header['$MEASUREMENT'] = 1
@@ -1241,7 +1243,12 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1241
  if not duplicate_found:
1242
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
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)
@@ -1312,7 +1319,11 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
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)]
@@ -1337,7 +1348,11 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1337
  if not duplicate_found:
1338
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1339
  unique_shapes.append((normalized_vertices, area1))
1340
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
1341
 
1342
 
1343
 
@@ -1379,7 +1394,11 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1379
  if not duplicate_found:
1380
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1381
  unique_shapes.append((normalized_vertices, area1))
1382
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
1383
 
1384
 
1385
 
@@ -1416,7 +1435,12 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1416
  if not duplicate_found:
1417
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1418
  unique_shapes.append((normalized_vertices, area1))
1419
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
1420
 
1421
 
1422
  elif entity.dxftype() == 'SPLINE':
@@ -1449,7 +1473,12 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1449
  if not duplicate_found:
1450
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1451
  unique_shapes.append((normalized_vertices, area1))
1452
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
1453
 
1454
  sorted_data = sorted(hatched_areas, key=lambda x: x[1])
1455
  return sorted_data,Legendarray
@@ -1570,10 +1599,21 @@ def extract_measurement(obj):
1570
  m = re.search(r"([0-9]*\.?[0-9]+)", text)
1571
  return float(m.group(1)) if m else None
1572
 
1573
- def adjustannotations(OutputPdfStage1,text_with_positions):
1574
  input_pdf_path = OutputPdfStage1
1575
  output_pdf_path = "Final-WallsAdjusted.pdf"
1576
  annotations_data = []
 
 
 
 
 
 
 
 
 
 
 
1577
  Trim=0
1578
 
1579
  # Load the input PDF
@@ -1636,6 +1676,24 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1636
  if combined:
1637
  obj.update({NameObject("/T"): TextStringObject(combined)})
1638
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1639
  # Update annotation dictionaries for measurement type
1640
  if subtype == "/PolyLine" and obj.get("/Subj", "") == "Perimeter Measurement":
1641
 
@@ -1674,15 +1732,26 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1674
  })
1675
 
1676
  # Append metadata
1677
- annotations_data.append([
 
1678
  vertices,
1679
  area,
1680
  perimeter,
1681
  annot_color,
1682
- matched_text,
1683
  matched_nbs,
1684
  Trim,
1685
- ])
 
 
 
 
 
 
 
 
 
 
1686
 
1687
 
1688
 
@@ -2199,7 +2268,7 @@ def draw_bb_onPDF(doc,bbox):
2199
  doc.save('kk.pdf') #ffor visualization only
2200
  return pdf_bbox
2201
 
2202
- def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath=0,pdfname=0,pdf_content=0):
2203
  OutputPdfStage1='BB Trial.pdf'
2204
  if pdf_content:
2205
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
@@ -2265,7 +2334,7 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio
2265
  rotationangle = 270
2266
 
2267
 
2268
- hatched_areas,Legendarray = get_hatched_areas(datadoc,dxfpath,FinalRatio,rotationangle,SearchArray)
2269
 
2270
  nearby,grouped_shapes_final=find_nearby_vertices(hatched_areas)
2271
  processed_intersections = process_polygons(nearby, img, dxfratio, rotationangle, derotationMatrix,rotationOld,width,height)
@@ -2545,7 +2614,7 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio
2545
  # doc.save(OutputPdfStage1)
2546
  modified_pdf_data = doc.tobytes()
2547
  # OutputPdfStage2=adjustannotations(modified_pdf_data)
2548
- OutputPdfStage2,annotations_data=adjustannotations(modified_pdf_data,Legendarray)
2549
 
2550
  allvertices = annotations_data
2551
  hatchcolorR= '0'
 
963
 
964
 
965
  """### Hatched areas"""
966
+ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray,CollectedColors):
967
+
968
+ coloredarray = [tuple(x) for x in CollectedColors]
969
 
970
  doc = ezdxf.readfile(filename)
971
  doc.header['$MEASUREMENT'] = 1
 
1243
  if not duplicate_found:
1244
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
1245
  unique_shapes.append((normalized_vertices, area1))
1246
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1247
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1248
+ elif ( len(coloredarray) == 0 ):
1249
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1250
+
1251
+
1252
 
1253
  elif path.type == 2:
1254
  # convert any spline edges to line edges (approximates splines)
 
1319
 
1320
  if not duplicate_found:
1321
  unique_shapes.append((normalized_vertices, area1))
1322
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1323
+ hatched_areas.append([vert, area1, perimeter, rgb_color])
1324
+ elif ( len(coloredarray) == 0):
1325
+ hatched_areas.append([vert, area1, perimeter, rgb_color])
1326
+
1327
 
1328
  elif entity.dxftype() == 'SOLID':
1329
  vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
 
1348
  if not duplicate_found:
1349
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1350
  unique_shapes.append((normalized_vertices, area1))
1351
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1352
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1353
+ elif( len(coloredarray) == 0):
1354
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1355
+
1356
 
1357
 
1358
 
 
1394
  if not duplicate_found:
1395
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1396
  unique_shapes.append((normalized_vertices, area1))
1397
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1398
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1399
+ elif ( len(coloredarray) == 0 ):
1400
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1401
+
1402
 
1403
 
1404
 
 
1435
  if not duplicate_found:
1436
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1437
  unique_shapes.append((normalized_vertices, area1))
1438
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1439
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1440
+ elif ( len(coloredarray) == 0 ):
1441
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1442
+
1443
+
1444
 
1445
 
1446
  elif entity.dxftype() == 'SPLINE':
 
1473
  if not duplicate_found:
1474
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1475
  unique_shapes.append((normalized_vertices, area1))
1476
+ if ( len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1477
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1478
+ elif ( len(coloredarray) == 0):
1479
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1480
+
1481
+
1482
 
1483
  sorted_data = sorted(hatched_areas, key=lambda x: x[1])
1484
  return sorted_data,Legendarray
 
1599
  m = re.search(r"([0-9]*\.?[0-9]+)", text)
1600
  return float(m.group(1)) if m else None
1601
 
1602
+ def adjustannotations(OutputPdfStage1,text_with_positions,CollectedColors):
1603
  input_pdf_path = OutputPdfStage1
1604
  output_pdf_path = "Final-WallsAdjusted.pdf"
1605
  annotations_data = []
1606
+
1607
+ paired_colors = []
1608
+ for i in range(0, len(CollectedColors), 2):
1609
+ name = CollectedColors[i] # take the current name
1610
+ color_list = CollectedColors[i+1] # take the next item as color list
1611
+ # convert each component to int
1612
+ color = tuple(int(c) for c in color_list)
1613
+ paired_colors.append([name, color])
1614
+
1615
+ CollectedColors = paired_colors
1616
+
1617
  Trim=0
1618
 
1619
  # Load the input PDF
 
1676
  if combined:
1677
  obj.update({NameObject("/T"): TextStringObject(combined)})
1678
 
1679
+ if subtype in ["/Line", "/PolyLine", "/Polygon"] and raw_color:
1680
+ matched_entry2 = next(
1681
+ ((name,color) for name,color in CollectedColors if color_close_enough(annot_color,color)),
1682
+ (None, None)
1683
+ )
1684
+ matched_text2, matched_nbs2 = matched_entry2
1685
+ print("2nd entry nbs: " ,type(matched_nbs2))
1686
+ print("2nd entry text: ",type(matched_text2))
1687
+ combined2 = ""
1688
+ if matched_text2 and matched_nbs2:
1689
+ combined2 = f"{matched_text2} - {matched_nbs2}"
1690
+ elif matched_text:
1691
+ combined2 = str(matched_text2)
1692
+ elif matched_nbs:
1693
+ combined2 = str(matched_nbs2)
1694
+ if combined2:
1695
+ obj.update({NameObject("/T"): TextStringObject(combined2)})
1696
+
1697
  # Update annotation dictionaries for measurement type
1698
  if subtype == "/PolyLine" and obj.get("/Subj", "") == "Perimeter Measurement":
1699
 
 
1732
  })
1733
 
1734
  # Append metadata
1735
+ if matched_text2 or matched_nbs2:
1736
+ annotations_data.append([
1737
  vertices,
1738
  area,
1739
  perimeter,
1740
  annot_color,
1741
+ matched_text2,
1742
  matched_nbs,
1743
  Trim,
1744
+ ])
1745
+ else:
1746
+ annotations_data.append([
1747
+ vertices,
1748
+ area,
1749
+ perimeter,
1750
+ annot_color,
1751
+ matched_text,
1752
+ matched_nbs,
1753
+ Trim,
1754
+ ])
1755
 
1756
 
1757
 
 
2268
  doc.save('kk.pdf') #ffor visualization only
2269
  return pdf_bbox
2270
 
2271
+ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath=0,pdfname=0,pdf_content=0):
2272
  OutputPdfStage1='BB Trial.pdf'
2273
  if pdf_content:
2274
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
 
2334
  rotationangle = 270
2335
 
2336
 
2337
+ hatched_areas,Legendarray = get_hatched_areas(datadoc,dxfpath,FinalRatio,rotationangle,SearchArray,CollectedColors)
2338
 
2339
  nearby,grouped_shapes_final=find_nearby_vertices(hatched_areas)
2340
  processed_intersections = process_polygons(nearby, img, dxfratio, rotationangle, derotationMatrix,rotationOld,width,height)
 
2614
  # doc.save(OutputPdfStage1)
2615
  modified_pdf_data = doc.tobytes()
2616
  # OutputPdfStage2=adjustannotations(modified_pdf_data)
2617
+ OutputPdfStage2,annotations_data=adjustannotations(modified_pdf_data,Legendarray,CollectedColors)
2618
 
2619
  allvertices = annotations_data
2620
  hatchcolorR= '0'
dxf__omar3_2.py CHANGED
@@ -909,54 +909,94 @@ def int_to_rgb(color_int):
909
 
910
 
911
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
912
  def get_hatch_color(entity):
913
- # Check if the entity has a "true color" set
 
 
 
 
 
914
  if entity.dxf.hasattr('true_color'):
915
- true_color = entity.dxf.true_color
916
- rgb_color = int_to_rgb(true_color) # Convert integer to (R, G, B)
917
- print(f"True color detected (RGB): {rgb_color}")
918
- return rgb_color
919
 
 
920
  color_index = entity.dxf.color
921
- print("color_index = ", color_index)
 
 
 
 
922
 
923
- # Check if the color is set to ByLayer or ByBlock
924
- if color_index == 0: # ByLayer color
925
- print("Color is ByLayer, checking layer color...")
926
  layer_name = entity.dxf.layer
927
  layer = entity.doc.layers.get(layer_name)
928
-
929
- if layer: # Ensure layer exists
930
  layer_color_index = layer.dxf.color
931
- print(f"Layer '{layer_name}' Color Index = {layer_color_index}")
932
- return aci_to_rgb(layer_color_index) # Use custom aci_to_rgb function
 
 
933
  else:
934
- print(f"Layer '{layer_name}' not found, defaulting to white.")
935
- return (255, 255, 255) # Default to white if layer not found
936
-
937
- elif color_index == 256: # ByBlock color
938
- print("Color is ByBlock, checking block color or defaulting to white.")
939
- block_color = (255, 255, 255) # White as default
940
-
941
- # Check if the entity is inside a block reference and inherit its color
942
- if hasattr(entity, 'block'): # Check if the entity belongs to a block
943
- block_ref = entity.block
944
- if block_ref.dxf.hasattr('color'):
945
- block_color = aci_to_rgb(block_ref.dxf.color)
946
- print(f"Block reference color found: {block_color}")
947
- else:
948
- print("Block has no color attribute, using default (white).")
949
- return block_color
950
-
951
- # Otherwise, convert the ACI color to RGB
952
- print(f"Entity Color Index = {color_index}")
953
- if 1 <= color_index <= 255:
954
- rgb_color = aci_to_rgb(color_index) # Use custom aci_to_rgb function
955
- print(f"Converted RGB = {rgb_color}")
956
- return rgb_color
957
 
958
- # Default to white if color index is out of bounds or invalid
959
- print("Invalid or unhandled color index, defaulting to white.")
960
  return (255, 255, 255)
961
 
962
  def calculate_distance(p1, p2):
@@ -971,7 +1011,10 @@ def color_close_enough(c1, c2, threshold=10):
971
 
972
 
973
  """### Hatched areas"""
974
- def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
 
 
 
975
 
976
  doc = ezdxf.readfile(filename)
977
  doc.header['$MEASUREMENT'] = 1
@@ -1249,7 +1292,19 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1249
  if not duplicate_found:
1250
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
1251
  unique_shapes.append((normalized_vertices, area1))
1252
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
 
 
 
 
 
 
 
1253
 
1254
  elif path.type == 2:
1255
  # convert any spline edges to line edges (approximates splines)
@@ -1320,7 +1375,20 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1320
 
1321
  if not duplicate_found:
1322
  unique_shapes.append((normalized_vertices, area1))
1323
- hatched_areas.append([vert, area1, perimeter, rgb_color])
 
 
 
 
 
 
 
 
 
 
 
 
 
1324
 
1325
  elif entity.dxftype() == 'SOLID':
1326
  vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
@@ -1345,7 +1413,16 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1345
  if not duplicate_found:
1346
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1347
  unique_shapes.append((normalized_vertices, area1))
1348
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
 
 
 
 
1349
 
1350
 
1351
 
@@ -1387,7 +1464,12 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1387
  if not duplicate_found:
1388
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1389
  unique_shapes.append((normalized_vertices, area1))
1390
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
1391
 
1392
 
1393
 
@@ -1424,7 +1506,13 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
1424
  if not duplicate_found:
1425
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1426
  unique_shapes.append((normalized_vertices, area1))
1427
- hatched_areas.append([vertices, area1, perimeter, rgb_color])
 
 
 
 
 
 
1428
 
1429
 
1430
  # elif entity.dxftype() == 'SPLINE':
@@ -1556,8 +1644,8 @@ def Create_DF(dxfpath,datadoc,hatched_areas,pdf_content=0):
1556
  # print(SimilarAreaDictionary)
1557
  return SimilarAreaDictionary
1558
  """### Draw on Image and PDF"""
1559
- def color_close_enough(c1, c2, threshold=10):
1560
- return all(abs(a - b) <= threshold for a, b in zip(c1, c2))
1561
 
1562
 
1563
 
@@ -1578,10 +1666,21 @@ def extract_measurement(obj):
1578
  m = re.search(r"([0-9]*\.?[0-9]+)", text)
1579
  return float(m.group(1)) if m else None
1580
 
1581
- def adjustannotations(OutputPdfStage1,text_with_positions):
1582
  input_pdf_path = OutputPdfStage1
1583
  output_pdf_path = "Final-WallsAdjusted.pdf"
1584
  annotations_data = []
 
 
 
 
 
 
 
 
 
 
 
1585
  Trim=0
1586
 
1587
  # Load the input PDF
@@ -1644,6 +1743,26 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1644
  if combined:
1645
  obj.update({NameObject("/T"): TextStringObject(combined)})
1646
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1647
  # Update annotation dictionaries for measurement type
1648
  if subtype == "/PolyLine" and obj.get("/Subj", "") == "Perimeter Measurement":
1649
 
@@ -1682,16 +1801,27 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1682
  })
1683
 
1684
  # Append metadata
1685
- annotations_data.append([
1686
- vertices,
1687
- area,
1688
- perimeter,
1689
- annot_color,
1690
- matched_text,
1691
- matched_nbs,
1692
- Trim,
1693
- ])
1694
-
 
 
 
 
 
 
 
 
 
 
 
1695
 
1696
 
1697
  output_pdf_io = BytesIO()
@@ -2273,7 +2403,9 @@ def remove_intersecting_vertices(
2273
  return pts, removed, segments
2274
 
2275
 
2276
- def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,points_Of_drawing_Canvas,pdfpath=0,pdfname=0,pdf_content=0):
 
 
2277
  OutputPdfStage1='BB Trial.pdf'
2278
  if pdf_content:
2279
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
@@ -2338,7 +2470,7 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio
2338
  rotationangle = 270
2339
 
2340
 
2341
- hatched_areas,Legendarray = get_hatched_areas(datadoc,dxfpath,FinalRatio,rotationangle,SearchArray)
2342
 
2343
  nearby,grouped_shapes_final=find_nearby_vertices(hatched_areas)
2344
  processed_intersections = process_polygons(nearby, img, dxfratio, rotationangle, derotationMatrix,rotationOld,width,height)
@@ -2728,7 +2860,7 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio
2728
  # doc.save(OutputPdfStage1)
2729
  modified_pdf_data = doc.tobytes()
2730
  # OutputPdfStage2=adjustannotations(modified_pdf_data)
2731
- OutputPdfStage2,annotations_data=adjustannotations(modified_pdf_data,Legendarray)
2732
 
2733
  allvertices = annotations_data
2734
  hatchcolorR= '0'
 
909
 
910
 
911
 
912
+ # def get_hatch_color(entity):
913
+ # # Check if the entity has a "true color" set
914
+ # if entity.dxf.hasattr('true_color'):
915
+ # true_color = entity.dxf.true_color
916
+ # rgb_color = int_to_rgb(true_color) # Convert integer to (R, G, B)
917
+ # print(f"True color detected (RGB): {rgb_color}")
918
+ # return rgb_color
919
+
920
+ # color_index = entity.dxf.color
921
+ # print("color_index = ", color_index)
922
+
923
+ # # Check if the color is set to ByLayer or ByBlock
924
+ # if color_index == 0: # ByLayer color
925
+ # print("Color is ByLayer, checking layer color...")
926
+ # layer_name = entity.dxf.layer
927
+ # layer = entity.doc.layers.get(layer_name)
928
+
929
+ # if layer: # Ensure layer exists
930
+ # layer_color_index = layer.dxf.color
931
+ # print(f"Layer '{layer_name}' Color Index = {layer_color_index}")
932
+ # return aci_to_rgb(layer_color_index) # Use custom aci_to_rgb function
933
+ # else:
934
+ # print(f"Layer '{layer_name}' not found, defaulting to white.")
935
+ # return (255, 255, 255) # Default to white if layer not found
936
+
937
+ # elif color_index == 256: # ByBlock color
938
+ # print("Color is ByBlock, checking block color or defaulting to white.")
939
+ # block_color = (255, 255, 255) # White as default
940
+
941
+ # # Check if the entity is inside a block reference and inherit its color
942
+ # if hasattr(entity, 'block'): # Check if the entity belongs to a block
943
+ # block_ref = entity.block
944
+ # if block_ref.dxf.hasattr('color'):
945
+ # block_color = aci_to_rgb(block_ref.dxf.color)
946
+ # print(f"Block reference color found: {block_color}")
947
+ # else:
948
+ # print("Block has no color attribute, using default (white).")
949
+ # return block_color
950
+
951
+ # # Otherwise, convert the ACI color to RGB
952
+ # print(f"Entity Color Index = {color_index}")
953
+ # if 1 <= color_index <= 255:
954
+ # rgb_color = aci_to_rgb(color_index) # Use custom aci_to_rgb function
955
+ # print(f"Converted RGB = {rgb_color}")
956
+ # return rgb_color
957
+
958
+ # # Default to white if color index is out of bounds or invalid
959
+ # print("Invalid or unhandled color index, defaulting to white.")
960
+ # return (255, 255, 255)
961
+
962
  def get_hatch_color(entity):
963
+ """Extract hatch color with detailed debugging."""
964
+ if not entity:
965
+ # print("No entity provided for color extraction.")
966
+ return (255, 255, 255)
967
+
968
+ # Check for true color
969
  if entity.dxf.hasattr('true_color'):
970
+ true_color = entity.dxf.true_color
971
+ rgb_color = int_to_rgb(true_color) # Convert integer to (R, G, B)
972
+ # print(f"True color detected (RGB): {rgb_color}")
973
+ return rgb_color
974
 
975
+ # Check for color index
976
  color_index = entity.dxf.color
977
+ # print(f"Entity color index: {color_index}")
978
+ if 1 <= color_index <= 255:
979
+ rgb_color = aci_to_rgb(color_index) # Convert ACI to RGB
980
+ # print(f"Converted ACI to RGB: {rgb_color}")
981
+ return rgb_color
982
 
983
+ # Handle ByLayer or ByBlock
984
+ if color_index == 0: # ByLayer
 
985
  layer_name = entity.dxf.layer
986
  layer = entity.doc.layers.get(layer_name)
987
+ # print(f"ByLayer detected for layer '{layer_name}'.")
988
+ if layer:
989
  layer_color_index = layer.dxf.color
990
+ # print(layer_color_index)
991
+ rgb_color = aci_to_rgb(layer_color_index)
992
+ # print(f"Layer '{layer_name}' color index {layer_color_index} converted to RGB: {rgb_color}")
993
+ return rgb_color
994
  else:
995
+ # print(f"Layer '{layer_name}' not found. Defaulting to white.")
996
+ return (255, 255, 255)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
997
 
998
+ # Default
999
+ # print("Unhandled color case. Defaulting to white.")
1000
  return (255, 255, 255)
1001
 
1002
  def calculate_distance(p1, p2):
 
1011
 
1012
 
1013
  """### Hatched areas"""
1014
+ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray,CollectedColors):
1015
+
1016
+ coloredarray = [tuple(x) for x in CollectedColors]
1017
+
1018
 
1019
  doc = ezdxf.readfile(filename)
1020
  doc.header['$MEASUREMENT'] = 1
 
1292
  if not duplicate_found:
1293
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
1294
  unique_shapes.append((normalized_vertices, area1))
1295
+
1296
+ print("rgbcolor in 3.2 hatch 1 type = ",type(rgb_color))
1297
+ print("coloredarray in 3.2 hatch 1 type = ",type(coloredarray))
1298
+ print("CollectedColors in 3.2 hatch 1 type = ",type(CollectedColors))
1299
+
1300
+ if (len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1301
+ print("inside if 1 hatch type")
1302
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1303
+ elif (len(coloredarray) == 0):
1304
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1305
+ # print('rgb_color',rgb_color)
1306
+ # print('coloredarray',coloredarray)
1307
+
1308
 
1309
  elif path.type == 2:
1310
  # convert any spline edges to line edges (approximates splines)
 
1375
 
1376
  if not duplicate_found:
1377
  unique_shapes.append((normalized_vertices, area1))
1378
+
1379
+ rgbconverted = tuple(rgb_color)
1380
+ if ( len(CollectedColors) > 0 and (rgb_color in CollectedColors)):
1381
+ hatched_areas.append([vert, area1, perimeter, rgb_color])
1382
+ elif (len(CollectedColors) == 0):
1383
+ hatched_areas.append([vert, area1, perimeter, rgb_color])
1384
+
1385
+
1386
+ # if (len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1387
+ # hatched_areas.append([vert, area1, perimeter, rgb_color])
1388
+ # elif (len(coloredarray) == 0):
1389
+ # hatched_areas.append([vert, area1, perimeter, rgb_color])
1390
+
1391
+
1392
 
1393
  elif entity.dxftype() == 'SOLID':
1394
  vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
 
1413
  if not duplicate_found:
1414
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1415
  unique_shapes.append((normalized_vertices, area1))
1416
+
1417
+ print("rgbcolor in 3.2 solid type = ",type(rgb_color))
1418
+ print("coloredarray in 3.2 solid type = ",type(coloredarray))
1419
+ print("CollectedColors in 3.2 solid type = ",type(CollectedColors))
1420
+
1421
+ if (len(CollectedColors) > 0 and ( rgb_color in CollectedColors)):
1422
+ print("inside solid if")
1423
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1424
+ elif (len(CollectedColors) == 0):
1425
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1426
 
1427
 
1428
 
 
1464
  if not duplicate_found:
1465
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1466
  unique_shapes.append((normalized_vertices, area1))
1467
+
1468
+ if (len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1469
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1470
+ elif (len(coloredarray) == 0):
1471
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1472
+
1473
 
1474
 
1475
 
 
1506
  if not duplicate_found:
1507
  rgb_color = get_hatch_color(entity) # Assuming this function exists
1508
  unique_shapes.append((normalized_vertices, area1))
1509
+ if (len(coloredarray) > 0 and ( rgb_color in coloredarray)):
1510
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1511
+ elif (len(coloredarray) == 0 ):
1512
+ hatched_areas.append([vertices, area1, perimeter, rgb_color])
1513
+
1514
+
1515
+
1516
 
1517
 
1518
  # elif entity.dxftype() == 'SPLINE':
 
1644
  # print(SimilarAreaDictionary)
1645
  return SimilarAreaDictionary
1646
  """### Draw on Image and PDF"""
1647
+ # def color_close_enough(c1, c2, threshold=10):
1648
+ # return all(abs(a - b) <= threshold for a, b in zip(c1, c2))
1649
 
1650
 
1651
 
 
1666
  m = re.search(r"([0-9]*\.?[0-9]+)", text)
1667
  return float(m.group(1)) if m else None
1668
 
1669
+ def adjustannotations(OutputPdfStage1,text_with_positions,CollectedColors):
1670
  input_pdf_path = OutputPdfStage1
1671
  output_pdf_path = "Final-WallsAdjusted.pdf"
1672
  annotations_data = []
1673
+
1674
+ paired_colors = []
1675
+ for i in range(0, len(CollectedColors), 2):
1676
+ name = CollectedColors[i] # take the current name
1677
+ color_list = CollectedColors[i+1] # take the next item as color list
1678
+ # convert each component to int
1679
+ color = tuple(int(c) for c in color_list)
1680
+ paired_colors.append([name, color])
1681
+
1682
+ CollectedColors = paired_colors
1683
+
1684
  Trim=0
1685
 
1686
  # Load the input PDF
 
1743
  if combined:
1744
  obj.update({NameObject("/T"): TextStringObject(combined)})
1745
 
1746
+ matched_text2 = None
1747
+ matched_nbs2 = None
1748
+ if subtype in ["/Line", "/PolyLine", "/Polygon"] and raw_color:
1749
+ matched_entry2 = next(
1750
+ ((name,color) for name,color in CollectedColors if color_close_enough(annot_color,color)),
1751
+ (None, None)
1752
+ )
1753
+ matched_text2, matched_nbs2 = matched_entry2
1754
+ print("2nd entry nbs: " ,type(matched_nbs2))
1755
+ print("2nd entry text: ",type(matched_text2))
1756
+ combined2 = ""
1757
+ if matched_text2 and matched_nbs2:
1758
+ combined2 = f"{matched_text2} - {matched_nbs2}"
1759
+ elif matched_text2:
1760
+ combined2 = str(matched_text2)
1761
+ elif matched_nbs2:
1762
+ combined2 = str(matched_nbs2)
1763
+ if combined2:
1764
+ obj.update({NameObject("/T"): TextStringObject(combined2)})
1765
+
1766
  # Update annotation dictionaries for measurement type
1767
  if subtype == "/PolyLine" and obj.get("/Subj", "") == "Perimeter Measurement":
1768
 
 
1801
  })
1802
 
1803
  # Append metadata
1804
+ if matched_text2 or matched_nbs2:
1805
+ annotations_data.append([
1806
+ vertices,
1807
+ area,
1808
+ perimeter,
1809
+ annot_color,
1810
+ matched_text2,
1811
+ matched_nbs,
1812
+ Trim,
1813
+ ])
1814
+ elif matched_text or matched_nbs:
1815
+ annotations_data.append([
1816
+ vertices,
1817
+ area,
1818
+ perimeter,
1819
+ annot_color,
1820
+ matched_text,
1821
+ matched_nbs,
1822
+ Trim,
1823
+ ])
1824
+ print('annot_color',annot_color)
1825
 
1826
 
1827
  output_pdf_io = BytesIO()
 
2403
  return pts, removed, segments
2404
 
2405
 
2406
+ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,CorrectionRatio,CollectedColors,points_Of_drawing_Canvas,pdfpath=0,pdfname=0,pdf_content=0):
2407
+ print("CollectedColors in 3.2 File = ",CollectedColors)
2408
+
2409
  OutputPdfStage1='BB Trial.pdf'
2410
  if pdf_content:
2411
  FinalRatio,width_dxf= RetriveRatio(datadoc,dxfpath,pdf_content)
 
2470
  rotationangle = 270
2471
 
2472
 
2473
+ hatched_areas,Legendarray = get_hatched_areas(datadoc,dxfpath,FinalRatio,rotationangle,SearchArray,CollectedColors)
2474
 
2475
  nearby,grouped_shapes_final=find_nearby_vertices(hatched_areas)
2476
  processed_intersections = process_polygons(nearby, img, dxfratio, rotationangle, derotationMatrix,rotationOld,width,height)
 
2860
  # doc.save(OutputPdfStage1)
2861
  modified_pdf_data = doc.tobytes()
2862
  # OutputPdfStage2=adjustannotations(modified_pdf_data)
2863
+ OutputPdfStage2,annotations_data=adjustannotations(modified_pdf_data,Legendarray,CollectedColors)
2864
 
2865
  allvertices = annotations_data
2866
  hatchcolorR= '0'