Spaces:
Sleeping
Sleeping
Update dxf__omar3_2.py
Browse files- dxf__omar3_2.py +123 -14
dxf__omar3_2.py
CHANGED
|
@@ -38,6 +38,7 @@ import random
|
|
| 38 |
import pandas as pd
|
| 39 |
import google_sheet_Legend
|
| 40 |
import tsadropboxretrieval
|
|
|
|
| 41 |
|
| 42 |
"""## Notes"""
|
| 43 |
|
|
@@ -66,6 +67,111 @@ def pdftoimg(datadoc):
|
|
| 66 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 67 |
return img
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
"""Flips image
|
| 70 |
DXF origin is at the bottom left while img origin is top left
|
| 71 |
"""
|
|
@@ -86,7 +192,7 @@ def flip(img):
|
|
| 86 |
|
| 87 |
"""### Hatched areas"""
|
| 88 |
|
| 89 |
-
def get_hatched_areas(filename):
|
| 90 |
doc = ezdxf.readfile(filename)
|
| 91 |
doc.header['$MEASUREMENT'] = 1
|
| 92 |
msp = doc.modelspace()
|
|
@@ -104,7 +210,7 @@ def get_hatched_areas(filename):
|
|
| 104 |
for path in entity.paths:
|
| 105 |
if str(path.type)=='BoundaryPathType.POLYLINE':
|
| 106 |
print('First type of Hatch')
|
| 107 |
-
vertices = [(vertex[0], vertex[1])for vertex in path.vertices]
|
| 108 |
if(len(vertices)>3):
|
| 109 |
poly = ShapelyPolygon(vertices)
|
| 110 |
|
|
@@ -136,9 +242,9 @@ def get_hatched_areas(filename):
|
|
| 136 |
x,y=edge.start
|
| 137 |
x1,y1=edge.end
|
| 138 |
if(flag==0):
|
| 139 |
-
vert=[(x,y),(x1,y1)]
|
| 140 |
-
else:
|
| 141 |
-
vert.append([x1,y1])
|
| 142 |
flag=1
|
| 143 |
poly = ShapelyPolygon(vert)
|
| 144 |
minx, miny, maxx, maxy = poly.bounds
|
|
@@ -159,7 +265,7 @@ def get_hatched_areas(filename):
|
|
| 159 |
print(path.type)
|
| 160 |
|
| 161 |
elif entity.dxftype() == 'SOLID':
|
| 162 |
-
vertices = [entity.dxf.vtx0, entity.dxf.vtx1, entity.dxf.vtx2, entity.dxf.vtx3]
|
| 163 |
poly = ShapelyPolygon(vertices)
|
| 164 |
minx, miny, maxx, maxy = poly.bounds
|
| 165 |
|
|
@@ -178,7 +284,7 @@ def get_hatched_areas(filename):
|
|
| 178 |
flag=0
|
| 179 |
|
| 180 |
for i in range(len(points)):
|
| 181 |
-
vertices.append([points[i][0],points[i][1]])
|
| 182 |
if(len(vertices)>3):
|
| 183 |
|
| 184 |
if(vertices[0][0] == vertices[len(vertices)-1][0] or vertices[0][1] == vertices[len(vertices)-1][1]):
|
|
@@ -203,7 +309,7 @@ def get_hatched_areas(filename):
|
|
| 203 |
elif entity.dxftype() == 'POLYLINE':
|
| 204 |
|
| 205 |
flag=0
|
| 206 |
-
vertices = [(v.dxf.location.x, v.dxf.location.y) for v in entity.vertices]
|
| 207 |
print('Vertices:', vertices)
|
| 208 |
|
| 209 |
if(len(vertices)>3):
|
|
@@ -232,7 +338,7 @@ def get_hatched_areas(filename):
|
|
| 232 |
control_points = spline_entity.control_points
|
| 233 |
if(len(control_points)>3):
|
| 234 |
for i in range(len(control_points)):
|
| 235 |
-
vertices.append([control_points[i][0],control_points[i][1]])
|
| 236 |
poly=ShapelyPolygon(vertices)
|
| 237 |
|
| 238 |
minx, miny, maxx, maxy = poly.bounds
|
|
@@ -305,9 +411,11 @@ def generate_color_array(length):
|
|
| 305 |
colorRanges.append((r, g, b))
|
| 306 |
return colorRanges
|
| 307 |
|
| 308 |
-
def Create_DF(dxfpath):
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
| 311 |
# SimilarAreaDictionary= pd.DataFrame(columns=['Area', 'Total Area', 'Perimeter', 'Total Perimeter', 'Occurences', 'Color'])
|
| 312 |
SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','Texts','Comments'])
|
| 313 |
|
|
@@ -348,7 +456,8 @@ def Create_DF(dxfpath):
|
|
| 348 |
"""### Draw on Image and PDF"""
|
| 349 |
|
| 350 |
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
|
| 351 |
-
|
|
|
|
| 352 |
img=pdftoimg(datadoc)
|
| 353 |
flipped_horizontal=flip(img)
|
| 354 |
allcnts = []
|
|
@@ -371,7 +480,7 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
|
|
| 371 |
allshapes=[]
|
| 372 |
# Iterate through each polygon in metric units
|
| 373 |
NewColors = []
|
| 374 |
-
SimilarAreaDictionary=Create_DF(dxfpath)
|
| 375 |
i=0
|
| 376 |
|
| 377 |
|
|
|
|
| 38 |
import pandas as pd
|
| 39 |
import google_sheet_Legend
|
| 40 |
import tsadropboxretrieval
|
| 41 |
+
from ezdxf import bbox
|
| 42 |
|
| 43 |
"""## Notes"""
|
| 44 |
|
|
|
|
| 67 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 68 |
return img
|
| 69 |
|
| 70 |
+
|
| 71 |
+
# Standard ISO paper sizes in inches
|
| 72 |
+
ISO_SIZES_INCHES = {
|
| 73 |
+
"A0": (33.11, 46.81),
|
| 74 |
+
"A1": (23.39, 33.11),
|
| 75 |
+
"A2": (16.54, 23.39),
|
| 76 |
+
"A3": (11.69, 16.54),
|
| 77 |
+
"A4": (8.27, 11.69),
|
| 78 |
+
"A5": (5.83, 8.27),
|
| 79 |
+
"A6": (4.13, 5.83),
|
| 80 |
+
"A7": (2.91, 4.13),
|
| 81 |
+
"A8": (2.05, 2.91),
|
| 82 |
+
"A9": (1.46, 2.05),
|
| 83 |
+
"A10": (1.02, 1.46)
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
def get_paper_size_in_inches(width, height):
|
| 87 |
+
"""Find the closest matching paper size in inches."""
|
| 88 |
+
for size, (w, h) in ISO_SIZES_INCHES.items():
|
| 89 |
+
if (abs(w - width) < 0.1 and abs(h - height) < 0.1) or (abs(w - height) < 0.1 and abs(h - width) < 0.1):
|
| 90 |
+
return size
|
| 91 |
+
return "Unknown Size"
|
| 92 |
+
|
| 93 |
+
def analyze_pdf(file_path):
|
| 94 |
+
# Open the PDF file
|
| 95 |
+
pdf_document = fitz.open(file_path)
|
| 96 |
+
|
| 97 |
+
# Iterate through pages and print their sizes
|
| 98 |
+
for page_number in range(len(pdf_document)):
|
| 99 |
+
page = pdf_document[page_number]
|
| 100 |
+
rect = page.rect
|
| 101 |
+
width_points, height_points = rect.width, rect.height
|
| 102 |
+
|
| 103 |
+
# Convert points to inches
|
| 104 |
+
width_inches, height_inches = width_points / 72, height_points / 72
|
| 105 |
+
|
| 106 |
+
paper_size = get_paper_size_in_inches(width_inches, height_inches)
|
| 107 |
+
|
| 108 |
+
print(f"Page {page_number + 1}: {width_inches:.2f} x {height_inches:.2f} inches ({paper_size})")
|
| 109 |
+
|
| 110 |
+
pdf_document.close()
|
| 111 |
+
return width_inches , height_inches , paper_size
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def get_dxfSize(dxfpath):
|
| 115 |
+
|
| 116 |
+
doc = ezdxf.readfile(dxfpath)
|
| 117 |
+
msp = doc.modelspace()
|
| 118 |
+
# Create a cache for bounding box calculations
|
| 119 |
+
# Get the overall bounding box for all entities in the modelspace
|
| 120 |
+
cache = bbox.Cache()
|
| 121 |
+
overall_bbox = bbox.extents(msp, cache=cache)
|
| 122 |
+
print("Overall Bounding Box:", overall_bbox)
|
| 123 |
+
print(overall_bbox.extmin[0]+overall_bbox.extmax[0], overall_bbox.extmin[1]+overall_bbox.extmax[1])
|
| 124 |
+
|
| 125 |
+
return overall_bbox.extmin[0]+overall_bbox.extmax[0], overall_bbox.extmin[1]+overall_bbox.extmax[1]
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
def switch_case(argument):
|
| 130 |
+
switcher = {
|
| 131 |
+
"A0": 1.27,
|
| 132 |
+
"A1": 2.54,
|
| 133 |
+
"A2": 5.08,
|
| 134 |
+
"A3": 10.16,
|
| 135 |
+
"A4": 20.32,
|
| 136 |
+
"A5": 40.64,
|
| 137 |
+
"A6": 81.28,
|
| 138 |
+
"A7": 162.56,
|
| 139 |
+
"A8": 325.12,
|
| 140 |
+
"A9": 650.24,
|
| 141 |
+
"A10": 1300.48
|
| 142 |
+
}
|
| 143 |
+
# Get the value from the dictionary; if not found, return a default value
|
| 144 |
+
print("Final Ratio=",switcher.get(argument, 1))
|
| 145 |
+
return switcher.get(argument, 1)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def RetriveRatio(pdfpath,dxfpath):
|
| 151 |
+
|
| 152 |
+
width,height,paper_size = analyze_pdf (pdfpath)
|
| 153 |
+
|
| 154 |
+
if(width > height ):
|
| 155 |
+
bigger=width
|
| 156 |
+
else:
|
| 157 |
+
bigger=height
|
| 158 |
+
|
| 159 |
+
width_dxf,height_dxf = get_dxfSize(dxfpath)
|
| 160 |
+
|
| 161 |
+
if(width_dxf > height_dxf ):
|
| 162 |
+
bigger_dxf=width_dxf
|
| 163 |
+
else:
|
| 164 |
+
bigger_dxf=height_dxf
|
| 165 |
+
|
| 166 |
+
if(0.2 < bigger_dxf/bigger < 1.2):
|
| 167 |
+
print("bigger_dxf/bigger",bigger/bigger_dxf)
|
| 168 |
+
argument = paper_size
|
| 169 |
+
FinalRatio=switch_case(argument)
|
| 170 |
+
else:
|
| 171 |
+
FinalRatio=1
|
| 172 |
+
return FinalRatio
|
| 173 |
+
|
| 174 |
+
|
| 175 |
"""Flips image
|
| 176 |
DXF origin is at the bottom left while img origin is top left
|
| 177 |
"""
|
|
|
|
| 192 |
|
| 193 |
"""### Hatched areas"""
|
| 194 |
|
| 195 |
+
def get_hatched_areas(filename,FinalRatio):
|
| 196 |
doc = ezdxf.readfile(filename)
|
| 197 |
doc.header['$MEASUREMENT'] = 1
|
| 198 |
msp = doc.modelspace()
|
|
|
|
| 210 |
for path in entity.paths:
|
| 211 |
if str(path.type)=='BoundaryPathType.POLYLINE':
|
| 212 |
print('First type of Hatch')
|
| 213 |
+
vertices = [(vertex[0]* (FinalRatio), vertex[1]* (FinalRatio))for vertex in path.vertices]
|
| 214 |
if(len(vertices)>3):
|
| 215 |
poly = ShapelyPolygon(vertices)
|
| 216 |
|
|
|
|
| 242 |
x,y=edge.start
|
| 243 |
x1,y1=edge.end
|
| 244 |
if(flag==0):
|
| 245 |
+
vert=[(x* (FinalRatio),y* (FinalRatio)),(x1* (FinalRatio),y1* (FinalRatio))]
|
| 246 |
+
else:m
|
| 247 |
+
vert.append([x1* (FinalRatio),y1* (FinalRatio)])
|
| 248 |
flag=1
|
| 249 |
poly = ShapelyPolygon(vert)
|
| 250 |
minx, miny, maxx, maxy = poly.bounds
|
|
|
|
| 265 |
print(path.type)
|
| 266 |
|
| 267 |
elif entity.dxftype() == 'SOLID':
|
| 268 |
+
vertices = [entity.dxf.vtx0 * (FinalRatio), entity.dxf.vtx1* (FinalRatio), entity.dxf.vtx2* (FinalRatio), entity.dxf.vtx3* (FinalRatio)]
|
| 269 |
poly = ShapelyPolygon(vertices)
|
| 270 |
minx, miny, maxx, maxy = poly.bounds
|
| 271 |
|
|
|
|
| 284 |
flag=0
|
| 285 |
|
| 286 |
for i in range(len(points)):
|
| 287 |
+
vertices.append([points[i][0]* (FinalRatio),points[i][1]* (FinalRatio)])
|
| 288 |
if(len(vertices)>3):
|
| 289 |
|
| 290 |
if(vertices[0][0] == vertices[len(vertices)-1][0] or vertices[0][1] == vertices[len(vertices)-1][1]):
|
|
|
|
| 309 |
elif entity.dxftype() == 'POLYLINE':
|
| 310 |
|
| 311 |
flag=0
|
| 312 |
+
vertices = [(v.dxf.location.x * (FinalRatio), v.dxf.location.y * (FinalRatio)) for v in entity.vertices]
|
| 313 |
print('Vertices:', vertices)
|
| 314 |
|
| 315 |
if(len(vertices)>3):
|
|
|
|
| 338 |
control_points = spline_entity.control_points
|
| 339 |
if(len(control_points)>3):
|
| 340 |
for i in range(len(control_points)):
|
| 341 |
+
vertices.append([control_points[i][0]* (FinalRatio),control_points[i][1]* (FinalRatio)])
|
| 342 |
poly=ShapelyPolygon(vertices)
|
| 343 |
|
| 344 |
minx, miny, maxx, maxy = poly.bounds
|
|
|
|
| 411 |
colorRanges.append((r, g, b))
|
| 412 |
return colorRanges
|
| 413 |
|
| 414 |
+
def Create_DF(dxfpath,pdfpath):
|
| 415 |
+
|
| 416 |
+
FinalRatio= RetriveRatio(pdfpath,dxfpath)
|
| 417 |
+
|
| 418 |
+
hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 419 |
# SimilarAreaDictionary= pd.DataFrame(columns=['Area', 'Total Area', 'Perimeter', 'Total Perimeter', 'Occurences', 'Color'])
|
| 420 |
SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','Texts','Comments'])
|
| 421 |
|
|
|
|
| 456 |
"""### Draw on Image and PDF"""
|
| 457 |
|
| 458 |
def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
|
| 459 |
+
FinalRatio= RetriveRatio(pdfpath,dxfpath)
|
| 460 |
+
hatched_areas = get_hatched_areas(dxfpath,FinalRatio)
|
| 461 |
img=pdftoimg(datadoc)
|
| 462 |
flipped_horizontal=flip(img)
|
| 463 |
allcnts = []
|
|
|
|
| 480 |
allshapes=[]
|
| 481 |
# Iterate through each polygon in metric units
|
| 482 |
NewColors = []
|
| 483 |
+
SimilarAreaDictionary=Create_DF(dxfpath,pdfpath)
|
| 484 |
i=0
|
| 485 |
|
| 486 |
|