Marthee commited on
Commit
a81188c
·
verified ·
1 Parent(s): 8710f5e

Update Code_2_7.py

Browse files
Files changed (1) hide show
  1. Code_2_7.py +81 -29
Code_2_7.py CHANGED
@@ -846,7 +846,9 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
846
  if not duplicate_found:
847
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
848
  unique_shapes.append((normalized_vertices, area1))
849
- hatched_areas.append([vertices, area1, length, rgb_color])
 
 
850
 
851
  elif str(path.type) == 'BoundaryPathType.EDGE' or path.type == 2:
852
  # elif path.type == 2: # Edge path
@@ -894,7 +896,9 @@ def get_hatched_areas(datadoc,filename,FinalRatio,rotationangle,SearchArray):
894
  if not duplicate_found:
895
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
896
  unique_shapes.append((normalized_vertices, area1))
897
- hatched_areas.append([vert, area1, length, rgb_color])
 
 
898
 
899
  else:
900
  print(f"Encountered path type: {path.type}")
@@ -1193,19 +1197,6 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1193
  metadata = reader.metadata
1194
  writer.add_metadata(metadata)
1195
 
1196
- # x=input_pdf_path
1197
-
1198
- # # Load the input PDF
1199
- # reader = PdfReader(input_pdf_path)
1200
- # writer = PdfWriter()
1201
-
1202
- # # Append all pages to the writer
1203
- # writer.append_pages_from_reader(reader)
1204
-
1205
- # # Add metadata (optional)
1206
- # metadata = reader.metadata
1207
- # writer.add_metadata(metadata)
1208
-
1209
  for page_index, page in enumerate(writer.pages):
1210
  if "/Annots" in page:
1211
  annotations = page["/Annots"]
@@ -1265,12 +1256,74 @@ def adjustannotations(OutputPdfStage1,text_with_positions):
1265
 
1266
  print(f"Annotations updated and saved to {output_pdf_path}")
1267
  return output_pdf_io.read()
1268
- # # Save the modified PDF
1269
- # with open(output_pdf_path, "wb") as fp:
1270
- # writer.write(fp)
1271
 
1272
- # print(f"Annotations updated and saved to {output_pdf_path}")
1273
- # return output_pdf_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1274
 
1275
 
1276
 
@@ -1281,7 +1334,7 @@ def calculate_distance(p1, p2):
1281
 
1282
 
1283
 
1284
- def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,pdfpath=0,pdfname=0):
1285
  OutputPdfStage1='BB Trial.pdf'
1286
  FinalRatio= RetriveRatio(datadoc,dxfpath)
1287
 
@@ -1586,15 +1639,14 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,pdfpath=0,pdfna
1586
  # OutputPdfStage2=adjustannotations(OutputPdfStage1,text_with_positions)
1587
  modified_pdf_data = doc.tobytes()
1588
  OutputPdfStage2=adjustannotations(modified_pdf_data,text_with_positions)
1589
- latestimg,pix=pdftoimg(OutputPdfStage2)
1590
- doc2 =fitz.open('pdf',OutputPdfStage2)
1591
-
 
 
 
 
1592
  gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(grouped_df , pdfname,pdfpath)
1593
- # dbxTeam=tsadropboxretrieval.ADR_Access_DropboxTeam('user')
1594
- # md, res =dbxTeam.files_download(path= pdfpath+pdfname)
1595
- # data = res.content
1596
- # doc=fitz.open("pdf", data)
1597
- # list1=pd.DataFrame(columns=['content', 'creationDate', 'id', 'modDate', 'name', 'subject', 'title'])
1598
  list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
1599
 
1600
  # for page in doc:
 
846
  if not duplicate_found:
847
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
848
  unique_shapes.append((normalized_vertices, area1))
849
+
850
+ if length > 0.6:
851
+ hatched_areas.append([vertices, area1, length, rgb_color])
852
 
853
  elif str(path.type) == 'BoundaryPathType.EDGE' or path.type == 2:
854
  # elif path.type == 2: # Edge path
 
896
  if not duplicate_found:
897
  # rgb_color = get_hatch_color(entity) # Assuming this function exists
898
  unique_shapes.append((normalized_vertices, area1))
899
+
900
+ if length > 0.6:
901
+ hatched_areas.append([vert, area1, length, rgb_color])
902
 
903
  else:
904
  print(f"Encountered path type: {path.type}")
 
1197
  metadata = reader.metadata
1198
  writer.add_metadata(metadata)
1199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1200
  for page_index, page in enumerate(writer.pages):
1201
  if "/Annots" in page:
1202
  annotations = page["/Annots"]
 
1256
 
1257
  print(f"Annotations updated and saved to {output_pdf_path}")
1258
  return output_pdf_io.read()
 
 
 
1259
 
1260
+ def distance(rect1, rect2):
1261
+ """Calculate the Euclidean distance between two annotation centers."""
1262
+ x1, y1 = (float(rect1[0]) + float(rect1[2])) / 2, (float(rect1[1]) + float(rect1[3])) / 2
1263
+ x2, y2 = (float(rect2[0]) + float(rect2[2])) / 2, (float(rect2[1]) + float(rect2[3])) / 2
1264
+ return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
1265
+
1266
+ def remove_duplicate_annotations(pdf_path, threshold):
1267
+ """Remove one of the duplicate annotations if they are close and have the same color."""
1268
+
1269
+ input_pdf_path = pdf_path
1270
+ output_pdf_path = "Filtered-Walls.pdf"
1271
+
1272
+ # Load the input PDF
1273
+ pdf_bytes_io = BytesIO(pdf_path)
1274
+
1275
+ reader = PdfReader(pdf_bytes_io)
1276
+ writer = PdfWriter()
1277
+
1278
+ # Append all pages to the writer
1279
+ # writer.append_pages_from_reader(reader)
1280
+
1281
+ # Add metadata (optional)
1282
+ metadata = reader.metadata
1283
+ writer.add_metadata(metadata)
1284
+
1285
+ for page_index in range(len(reader.pages)):
1286
+ page = reader.pages[page_index]
1287
+
1288
+ if "/Annots" in page:
1289
+ annotations = page["/Annots"]
1290
+ annots_data = []
1291
+ to_delete = set()
1292
+
1293
+ # Extract annotation positions and colors
1294
+ for annot_index, annot_ref in enumerate(annotations):
1295
+ annot = annot_ref.get_object()
1296
+
1297
+ if "/Rect" in annot and "/C" in annot:
1298
+ rect = annot["/Rect"]
1299
+ if isinstance(rect, ArrayObject): # Ensure rect is a list
1300
+ rect = list(rect)
1301
+
1302
+ color = normalize_color(annot["/C"])
1303
+ annots_data.append((annot_index, rect, color))
1304
+
1305
+ # Compare distances and mark duplicates
1306
+ for i, (idx1, rect1, color1) in enumerate(annots_data):
1307
+ if idx1 in to_delete:
1308
+ continue
1309
+ for j, (idx2, rect2, color2) in enumerate(annots_data[i+1:], start=i+1):
1310
+ if idx2 in to_delete:
1311
+ continue
1312
+ if color1 == color2 and distance(rect1, rect2) < threshold:
1313
+ to_delete.add(idx2) # Mark second annotation for deletion
1314
+
1315
+ # Remove duplicates
1316
+ new_annotations = [annotations[i] for i in range(len(annotations)) if i not in to_delete]
1317
+ page[NameObject("/Annots")] = ArrayObject(new_annotations)
1318
+
1319
+ writer.add_page(page)
1320
+
1321
+ output_pdf_io = BytesIO()
1322
+ writer.write(output_pdf_io)
1323
+ output_pdf_io.seek(0)
1324
+
1325
+ return output_pdf_io.read()
1326
+
1327
 
1328
 
1329
 
 
1334
 
1335
 
1336
 
1337
+ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,Thickness,pdfpath=0,pdfname=0):
1338
  OutputPdfStage1='BB Trial.pdf'
1339
  FinalRatio= RetriveRatio(datadoc,dxfpath)
1340
 
 
1639
  # OutputPdfStage2=adjustannotations(OutputPdfStage1,text_with_positions)
1640
  modified_pdf_data = doc.tobytes()
1641
  OutputPdfStage2=adjustannotations(modified_pdf_data,text_with_positions)
1642
+
1643
+ threshold = math.ceil(float(Thickness) * float(dxfratio) )
1644
+ print(threshold)
1645
+ OutputPdfStage3 = remove_duplicate_annotations(OutputPdfStage2,threshold)
1646
+
1647
+ latestimg,pix=pdftoimg(OutputPdfStage3)
1648
+ doc2 =fitz.open('pdf',OutputPdfStage3)
1649
  gc,spreadsheet_service,spreadsheetId, spreadsheet_url , namepathArr=google_sheet_Legend.legendGoogleSheets(grouped_df , pdfname,pdfpath)
 
 
 
 
 
1650
  list1=pd.DataFrame(columns=['content', 'id', 'subject','color'])
1651
 
1652
  # for page in doc: