Marthee commited on
Commit
baec955
·
verified ·
1 Parent(s): b1dd219

Update dxf__omar3_2.py

Browse files
Files changed (1) hide show
  1. dxf__omar3_2.py +89 -78
dxf__omar3_2.py CHANGED
@@ -87,100 +87,88 @@ def flip(img):
87
  """### Hatched areas"""
88
 
89
  def get_hatched_areas(filename):
90
- doc = ezdxf.readfile(filename)
91
- doc.header['$MEASUREMENT'] = 1
92
- msp = doc.modelspace()
93
- trial=0
94
- hatched_areas = []
95
-
96
- for entity in msp:
97
- # print(entity.dxftype())
98
  if entity.dxftype() == 'HATCH':
99
  flag=0
100
  trial=0
101
- # print("Gowa el hatch")
 
 
102
  for path in entity.paths:
103
- # print("gowa el for")
104
- try:
105
  vertices = [(vertex[0], vertex[1])for vertex in path.vertices]
106
  if(len(vertices)>3):
107
-
108
  poly = ShapelyPolygon(vertices)
109
 
110
- if poly.area > 0.5:
 
 
 
 
 
 
 
 
111
  area1 = round(poly.area,3)
112
  perimeter = round (poly.length,3)
113
-
114
  if trial==0:
115
  hatched_areas.append([vertices,area1,perimeter])
116
  trial=1
117
- # print("hatched_areas = ", hatched_areas)
118
-
119
  else:
120
-
121
  for i in range(len(hatched_areas)):
122
-
123
  if(area1 == hatched_areas[i][1]):
124
  flag=1
125
-
126
- # if(flag==0):
127
- # hatched_areas.append([vertices,area1])
128
-
129
- # hatched_areas.append([vertices,area1])
130
- # print("Gowa el hatch2")
131
-
132
-
133
-
134
- # print("Gowa el if ely b3d el hatch")
135
-
136
- # if entity.area > 0.5:
137
- # hatched_areas.append([entity.area])
138
-
139
- except:
140
- # print("in except")
141
  vert=[]
142
  flag=0
143
  flag2=0
144
  for edge in path.edges:
145
  x,y=edge.start
146
  x1,y1=edge.end
147
- # print(edge.start)
148
- # print(edge.end)
149
  if(flag==0):
150
  vert=[(x,y),(x1,y1)]
151
  else:
152
  vert.append([x1,y1])
153
  flag=1
154
- # print(vert)
155
  poly = ShapelyPolygon(vert)
156
- # print("New Hatch Area = ",poly.area)
 
 
 
 
157
 
158
- if (poly.area > 0.5):
159
  area1= round(poly.area,3)
160
  perimeter = round (poly.length,3)
161
-
162
  for i in range(len(hatched_areas)):
163
-
164
  if(area1 == hatched_areas[i][1]):
165
  flag2=1
166
-
167
  if(flag2==0):
168
  hatched_areas.append([vert,area1,perimeter])
169
- # hatched_areas.append([vert,area1])
170
- # # hatched_areas.append(vertices)
171
- # continue
172
 
173
  elif entity.dxftype() == 'SOLID':
174
-
175
  vertices = [entity.dxf.vtx0, entity.dxf.vtx1, entity.dxf.vtx2, entity.dxf.vtx3]
176
  poly = ShapelyPolygon(vertices)
177
- # print("Gowa el solid")
178
 
 
 
 
179
 
180
- if poly.area > 0.5:
181
  hatched_areas.append([vertices,poly.area,poly.length])
182
- # print("Gowa el if ely b3d el solid")
183
-
184
 
185
  elif entity.dxftype() == 'LWPOLYLINE':
186
 
@@ -196,51 +184,71 @@ def get_hatched_areas(filename):
196
  if(vertices[0][0] == vertices[len(vertices)-1][0] or vertices[0][1] == vertices[len(vertices)-1][1]):
197
 
198
  poly=ShapelyPolygon(vertices)
199
- # print("vertices==",vertices)
200
 
201
- if poly.area > 0.5:
 
 
202
 
 
203
  area1 = round(poly.area,3)
204
  perimeter = round (poly.length,3)
205
-
206
  for i in range(len(hatched_areas)):
207
-
208
  if(area1 == hatched_areas[i][1]):
209
  flag=1
210
-
211
  if(flag==0):
212
  hatched_areas.append([vertices,area1,perimeter])
213
 
214
 
 
215
 
216
- elif entity.dxftype() == 'SPLINE':
 
 
217
 
218
- spline_entity = entity
219
 
220
- vertices = []
221
 
222
- control_points = spline_entity.control_points
 
223
 
 
 
 
224
 
225
- if(len(control_points)>3):
 
 
 
 
 
 
 
226
 
 
 
 
 
 
227
  for i in range(len(control_points)):
228
  vertices.append([control_points[i][0],control_points[i][1]])
229
-
230
-
231
-
232
  poly=ShapelyPolygon(vertices)
233
 
 
234
 
 
 
 
235
 
236
 
237
- if poly.area > 0.5:
238
-
239
  area1 = round(poly.area,3)
240
  perimeter = round (poly.length,3)
241
-
242
  hatched_areas.append([vertices,area1,perimeter])
243
- return hatched_areas
 
 
244
 
245
  """### Rotate polygon"""
246
 
@@ -301,15 +309,22 @@ def Create_DF(dxfpath):
301
 
302
  hatched_areas = get_hatched_areas(dxfpath)
303
  # SimilarAreaDictionary= pd.DataFrame(columns=['Area', 'Total Area', 'Perimeter', 'Total Perimeter', 'Occurences', 'Color'])
304
- SimilarAreaDictionary= pd.DataFrame(columns=['Guess','Color','Occurences','Area','Total Area','Perimeter','Total Perimeter','Length','Total Length','Texts'])
305
 
306
- colorRanges=generate_color_array(300)
 
 
307
  TotalArea=0
308
  TotalPerimeter=0
309
  for i in range(len(hatched_areas)):
310
  area = hatched_areas[i][1] # area
311
  perimeter = hatched_areas[i][2] # perimeter
312
- color = colorRanges[i]
 
 
 
 
 
313
  TotalArea = area
314
  TotalPerimeter = perimeter
315
  tol=1
@@ -325,7 +340,7 @@ def Create_DF(dxfpath):
325
  else:
326
  TotalArea=area
327
  TotalPerimeter=perimeter
328
- new_data = {'Area': area, 'Total Area': TotalArea ,'Perimeter': perimeter, 'Total Perimeter': TotalPerimeter, 'Occurences': 1, 'Color':color} #add color here and read color to insert in
329
  SimilarAreaDictionary = pd.concat([SimilarAreaDictionary, pd.DataFrame([new_data])], ignore_index=True)
330
 
331
  # print(SimilarAreaDictionary)
@@ -333,13 +348,12 @@ def Create_DF(dxfpath):
333
  """### Draw on Image and PDF"""
334
 
335
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
336
-
337
  hatched_areas = get_hatched_areas(dxfpath)
338
  img=pdftoimg(datadoc)
339
  flipped_horizontal=flip(img)
340
  allcnts = []
341
  imgg = flipped_horizontal
342
- imgtransparent1=imgg.copy()
343
  doc = fitz.open('pdf',datadoc)
344
  page2 = doc[0]
345
  rotationOld=page2.rotation
@@ -375,19 +389,16 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
375
  cntPoints.append([int(x), int(y)])
376
  cntPoints1.append([x, y])
377
 
378
-
379
-
380
  for poi in np.array(cntPoints1):
381
  x1, y1 = poi
382
  p1 = fitz.Point(x1,y1)
383
  # p1 = fitz.Point(x1,y1)
384
- p1=p1*derotationMatrix
385
  shapee.append([p1[0],p1[1]])
386
 
387
  shapee=np.flip(shapee,1)
388
  shapee=rotate_polygon(shapee,rotationangle,rotationOld,page2.mediabox)
389
 
390
-
391
  tol=1
392
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
393
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
@@ -438,5 +449,5 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
438
  for page in doc:
439
  for annot in page.annots():
440
  list1.loc[len(list1)] =annot.info
441
- return doc,image_new1, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1
442
 
 
87
  """### Hatched areas"""
88
 
89
  def get_hatched_areas(filename):
90
+ doc = ezdxf.readfile(filename)
91
+ doc.header['$MEASUREMENT'] = 1
92
+ msp = doc.modelspace()
93
+ trial=0
94
+ hatched_areas = []
95
+
96
+ for entity in msp:
97
+
98
  if entity.dxftype() == 'HATCH':
99
  flag=0
100
  trial=0
101
+
102
+ print(entity.dxftype())
103
+
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
 
111
+ minx, miny, maxx, maxy = poly.bounds
112
+
113
+ # Calculate the width and height of the bounding box
114
+ width = maxx - minx
115
+ height = maxy - miny
116
+
117
+
118
+
119
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
120
  area1 = round(poly.area,3)
121
  perimeter = round (poly.length,3)
 
122
  if trial==0:
123
  hatched_areas.append([vertices,area1,perimeter])
124
  trial=1
 
 
125
  else:
 
126
  for i in range(len(hatched_areas)):
 
127
  if(area1 == hatched_areas[i][1]):
128
  flag=1
129
+ elif str(path.type) == 'BoundaryPathType.EDGE':
130
+ print('Second type of Hatch')
131
+
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  vert=[]
133
  flag=0
134
  flag2=0
135
  for edge in path.edges:
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
145
+
146
+ # Calculate the width and height of the bounding box
147
+ width = maxx - minx
148
+ height = maxy - miny
149
 
150
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
151
  area1= round(poly.area,3)
152
  perimeter = round (poly.length,3)
 
153
  for i in range(len(hatched_areas)):
 
154
  if(area1 == hatched_areas[i][1]):
155
  flag2=1
 
156
  if(flag2==0):
157
  hatched_areas.append([vert,area1,perimeter])
158
+ else:
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
 
166
+ # Calculate the width and height of the bounding box
167
+ width = maxx - minx
168
+ height = maxy - miny
169
 
170
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
171
  hatched_areas.append([vertices,poly.area,poly.length])
 
 
172
 
173
  elif entity.dxftype() == 'LWPOLYLINE':
174
 
 
184
  if(vertices[0][0] == vertices[len(vertices)-1][0] or vertices[0][1] == vertices[len(vertices)-1][1]):
185
 
186
  poly=ShapelyPolygon(vertices)
187
+ minx, miny, maxx, maxy = poly.bounds
188
 
189
+ # Calculate the width and height of the bounding box
190
+ width = maxx - minx
191
+ height = maxy - miny
192
 
193
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
194
  area1 = round(poly.area,3)
195
  perimeter = round (poly.length,3)
 
196
  for i in range(len(hatched_areas)):
 
197
  if(area1 == hatched_areas[i][1]):
198
  flag=1
 
199
  if(flag==0):
200
  hatched_areas.append([vertices,area1,perimeter])
201
 
202
 
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):
210
 
211
+ if(vertices[0][0] == vertices[len(vertices)-1][0] or vertices[0][1] == vertices[len(vertices)-1][1]):
212
 
213
+ poly=ShapelyPolygon(vertices)
214
+ minx, miny, maxx, maxy = poly.bounds
215
 
216
+ # Calculate the width and height of the bounding box
217
+ width = maxx - minx
218
+ height = maxy - miny
219
 
220
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
221
+ area1 = round(poly.area,3)
222
+ perimeter = round (poly.length,3)
223
+ for i in range(len(hatched_areas)):
224
+ if(area1 == hatched_areas[i][1]):
225
+ flag=1
226
+ if(flag==0):
227
+ hatched_areas.append([vertices,area1,perimeter])
228
 
229
+ elif entity.dxftype() == 'SPLINE':
230
+ spline_entity = entity
231
+ vertices = []
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
239
 
240
+ # Calculate the width and height of the bounding box
241
+ width = maxx - minx
242
+ height = maxy - miny
243
 
244
 
245
+ if (poly.area > 1.5 and (height > 0.7 and width > 0.7)):
 
246
  area1 = round(poly.area,3)
247
  perimeter = round (poly.length,3)
 
248
  hatched_areas.append([vertices,area1,perimeter])
249
+
250
+ sorted_data = sorted(hatched_areas, key=lambda x: x[1])
251
+ return sorted_data
252
 
253
  """### Rotate polygon"""
254
 
 
309
 
310
  hatched_areas = get_hatched_areas(dxfpath)
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
 
314
+ colorRanges2=generate_color_array(300)
315
+ 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],[179,106,179],[115,52,179],[0,128,192],[128,0,128],[128,0,0],[0,128,255],[255,182,128],[255,0,255],[0,0,128],[0,128,64],[255,255,0],[128,0,64],[203,203,106],[128,255,166],[255,128,0],[255,98,98],[90,105,138],[114,10,72],[36,82,78],[225,105,29],[108,62,40],[11,35,75],[42,176,203],[255,153,153],[129,74,138],[99,123,137],[159,179,30]]
316
+ colorUsed=[]
317
  TotalArea=0
318
  TotalPerimeter=0
319
  for i in range(len(hatched_areas)):
320
  area = hatched_areas[i][1] # area
321
  perimeter = hatched_areas[i][2] # perimeter
322
+ if(i < len(colorRanges)):
323
+ color = colorRanges[i]
324
+ colorUsed.append(color)
325
+ else:
326
+ color = colorRanges2[i]
327
+ colorUsed.append(color)
328
  TotalArea = area
329
  TotalPerimeter = perimeter
330
  tol=1
 
340
  else:
341
  TotalArea=area
342
  TotalPerimeter=perimeter
343
+ 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
344
  SimilarAreaDictionary = pd.concat([SimilarAreaDictionary, pd.DataFrame([new_data])], ignore_index=True)
345
 
346
  # print(SimilarAreaDictionary)
 
348
  """### Draw on Image and PDF"""
349
 
350
  def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,pdfpath,pdfname):
 
351
  hatched_areas = get_hatched_areas(dxfpath)
352
  img=pdftoimg(datadoc)
353
  flipped_horizontal=flip(img)
354
  allcnts = []
355
  imgg = flipped_horizontal
356
+ # imgtransparent1=imgg.copy()
357
  doc = fitz.open('pdf',datadoc)
358
  page2 = doc[0]
359
  rotationOld=page2.rotation
 
389
  cntPoints.append([int(x), int(y)])
390
  cntPoints1.append([x, y])
391
 
 
 
392
  for poi in np.array(cntPoints1):
393
  x1, y1 = poi
394
  p1 = fitz.Point(x1,y1)
395
  # p1 = fitz.Point(x1,y1)
396
+ p1=p1*page2.derotation_matrix
397
  shapee.append([p1[0],p1[1]])
398
 
399
  shapee=np.flip(shapee,1)
400
  shapee=rotate_polygon(shapee,rotationangle,rotationOld,page2.mediabox)
401
 
 
402
  tol=1
403
  condition1 = (SimilarAreaDictionary['Area'] >= polygon[1] - tol) & (SimilarAreaDictionary['Area'] <= polygon[1] +tol)
404
  condition2 = (SimilarAreaDictionary['Perimeter'] >= polygon[2] -tol) & (SimilarAreaDictionary['Perimeter'] <= polygon[2] +tol)
 
449
  for page in doc:
450
  for annot in page.annots():
451
  list1.loc[len(list1)] =annot.info
452
+ return doc,image_new1, SimilarAreaDictionary ,spreadsheetId, spreadsheet_url , namepathArr , list1,hatched_areas
453