Files
Pier_v1/pierzainer_ground/investigate_registration.py
T

43 lines
2.7 KiB
Python

"""Inspect source drawing control annotations and rendering, without inventing controls."""
import argparse
from collections import Counter
import json
import os
from pathlib import Path
import re
from build_sample_grounds import OUTPUT, save
from ezdxf import recover
def run(number):
job=next(j for j in json.loads((OUTPUT/'inputs.json').read_text(encoding='utf-8')) if j['sample']==number)
out=Path(job['output']); source=next((out/'cache').glob('*.dxf'))
doc,_=recover.readfile(str(source))
annotations=[]; layers=Counter(); points=[]; inserts=[]
for e in doc.modelspace():
kind=e.dxftype();layers[e.dxf.layer]+=1
if kind in ('TEXT','MTEXT'):
text=e.plain_text() if kind=='MTEXT' else e.dxf.text
annotations.append({'handle':e.dxf.handle,'text':text,'xyz':list(e.dxf.insert),'layer':e.dxf.layer,'rotation':e.dxf.get('rotation',0),'height':e.dxf.get('height',1) if kind=='TEXT' else e.dxf.get('char_height',1)})
elif kind=='INSERT':
inserts.append({'handle':e.dxf.handle,'name':e.dxf.name,'layer':e.dxf.layer,'xyz':list(e.dxf.insert),'rotation':e.dxf.rotation,'scale':[e.dxf.get(k,1) for k in ('xscale','yscale','zscale')]})
key=[a for a in annotations if re.search(r'\d{5,}|[XY]\s*[:=]|[XY]좌표|좌표|기준점|GRID|IP[123]|P[123456789]|북|동|위치|SCALE',a['text'],re.I)]
headers={k:list(doc.header[k]) if hasattr(doc.header[k],'x') else doc.header[k] for k in ('$UCSORG','$UCSXDIR','$UCSYDIR','$INSBASE','$INSUNITS','$EXTMIN','$EXTMAX') if k in doc.header}
save(out/'registration_inspection.json',{'headers':headers,'layers':dict(layers),'texts':annotations,'inserts':inserts,'key_annotations':key})
print(json.dumps({'sample':number,'headers':headers,'key_annotations':key[:70]},ensure_ascii=False),flush=True)
os.environ['MPLCONFIGDIR']=str(OUTPUT/'cache/matplotlib')
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from ezdxf.addons.drawing import RenderContext,Frontend,layout
from ezdxf.addons.drawing.config import Configuration,BackgroundPolicy,ColorPolicy
from ezdxf.addons.drawing.matplotlib import MatplotlibBackend
plt.rcParams['font.family']='Malgun Gothic'
fig=plt.figure(figsize=(18,12));ax=fig.add_axes([0,0,1,1])
config=Configuration(background_policy=BackgroundPolicy.WHITE,color_policy=ColorPolicy.BLACK)
Frontend(RenderContext(doc),MatplotlibBackend(ax),config=config).draw_layout(doc.modelspace(),finalize=True)
ax.set_axis_off();fig.savefig(out/'drawing_overview.png',dpi=140);plt.close(fig)
if __name__=='__main__':
p=argparse.ArgumentParser();p.add_argument('samples',nargs='+',type=int);a=p.parse_args()
for n in a.samples:run(n)