Update dxf__omar3_2.py
Browse files- dxf__omar3_2.py +49 -0
dxf__omar3_2.py
CHANGED
|
@@ -102,6 +102,40 @@ from xml.etree.ElementTree import Element, SubElement, tostring
|
|
| 102 |
import re
|
| 103 |
|
| 104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
"""## Notes"""
|
| 107 |
|
|
@@ -117,6 +151,8 @@ This portion is used to convert vertices read from dxf to pixels in order to acc
|
|
| 117 |
|
| 118 |
|
| 119 |
'''
|
|
|
|
|
|
|
| 120 |
AllhatchesCodes= {
|
| 121 |
'Brick':'<</Length 172/Type/Pattern/PatternType 1/PaintType 1/TilingType 1/Resources<<>>/Matrix[1 0 0 1 0 0]/BBox[0 0 18 18]/XStep 18/YStep 18>>\nstream\n{fillcolor} rg 0 0 18 18 re f {strokecolor} RG 1 w -1 18 m 19.00001 18 l 9 18 m 9 9 l -1 9 m 19.00001 9 l 0 9 m 0 0 l -1 0 m 19.00001 0 l 18 0 m 18 9 l S \nendstream'
|
| 122 |
,
|
|
@@ -2094,6 +2130,19 @@ def mainFunctionDrawImgPdf(datadoc,dxfpath, dxfratio,SearchArray,pdfpath=0,pdfna
|
|
| 2094 |
width=abs(page2.mediabox[2])+abs(page2.mediabox[0])
|
| 2095 |
height=abs(page2.mediabox[3])+abs(page2.mediabox[1])
|
| 2096 |
print('mediabox', width , height)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2097 |
if page2.rotation!=0:
|
| 2098 |
|
| 2099 |
rotationangle = page2.rotation
|
|
|
|
| 102 |
import re
|
| 103 |
|
| 104 |
|
| 105 |
+
import ezdxf
|
| 106 |
+
from ezdxf.bbox import extents
|
| 107 |
+
|
| 108 |
+
def detect_scale_from_page(dxf_path, page_pixel_width, m_per_pixel_from_pdf):
|
| 109 |
+
"""
|
| 110 |
+
Detects mm/px scale factor using the bounding box of the entire DXF content.
|
| 111 |
+
"""
|
| 112 |
+
doc = ezdxf.readfile(dxf_path)
|
| 113 |
+
|
| 114 |
+
#getting the bounding box from modelspace
|
| 115 |
+
msp = doc.modelspace()
|
| 116 |
+
bbox_msp = extents(msp, fast=True)
|
| 117 |
+
|
| 118 |
+
if bbox_msp.has_data: #not empty
|
| 119 |
+
min_x, min_y, max_x, max_y = bbox_msp.extmin.x, bbox_msp.extmin.y, bbox_msp.extmax.x, bbox_msp.extmax.y
|
| 120 |
+
else:
|
| 121 |
+
# Try paperspace as a fallback
|
| 122 |
+
psp = doc.layout("Layout1")
|
| 123 |
+
bbox_psp = extents(psp, fast=True)
|
| 124 |
+
if not bbox_psp.has_data:
|
| 125 |
+
raise ValueError("No bounding box data found in modelspace or paperspace.")
|
| 126 |
+
min_x, min_y, max_x, max_y = bbox_psp.extmin.x, bbox_psp.extmin.y, bbox_psp.extmax.x, bbox_psp.extmax.y
|
| 127 |
+
|
| 128 |
+
# DXF width
|
| 129 |
+
dxf_width = max_x - min_x
|
| 130 |
+
|
| 131 |
+
# PDF width in m
|
| 132 |
+
pdf_metric_width = page_pixel_width * m_per_pixel_from_pdf
|
| 133 |
+
|
| 134 |
+
# Correction factor
|
| 135 |
+
correction_factor = dxf_width / pdf_metric_width
|
| 136 |
+
# final_scale = mm_per_pixel_from_pdf * correction_factor
|
| 137 |
+
|
| 138 |
+
return correction_factor
|
| 139 |
|
| 140 |
"""## Notes"""
|
| 141 |
|
|
|
|
| 151 |
|
| 152 |
|
| 153 |
'''
|
| 154 |
+
|
| 155 |
+
|
| 156 |
AllhatchesCodes= {
|
| 157 |
'Brick':'<</Length 172/Type/Pattern/PatternType 1/PaintType 1/TilingType 1/Resources<<>>/Matrix[1 0 0 1 0 0]/BBox[0 0 18 18]/XStep 18/YStep 18>>\nstream\n{fillcolor} rg 0 0 18 18 re f {strokecolor} RG 1 w -1 18 m 19.00001 18 l 9 18 m 9 9 l -1 9 m 19.00001 9 l 0 9 m 0 0 l -1 0 m 19.00001 0 l 18 0 m 18 9 l S \nendstream'
|
| 158 |
,
|
|
|
|
| 2130 |
width=abs(page2.mediabox[2])+abs(page2.mediabox[0])
|
| 2131 |
height=abs(page2.mediabox[3])+abs(page2.mediabox[1])
|
| 2132 |
print('mediabox', width , height)
|
| 2133 |
+
|
| 2134 |
+
correction_factor= detect_scale_from_page(dxfpath,width,dxfratio/1000)
|
| 2135 |
+
|
| 2136 |
+
factor=1
|
| 2137 |
+
print('corr_factor',correction_factor)
|
| 2138 |
+
if correction_factor <0.26: #if less than 0.25 then the dxf ratio is correeect, if greater then *2
|
| 2139 |
+
factor=1
|
| 2140 |
+
print('Ratio working: keep as it is')
|
| 2141 |
+
else:
|
| 2142 |
+
factor =2
|
| 2143 |
+
print('Ratio was adjusted to be ur input ratio x2')
|
| 2144 |
+
|
| 2145 |
+
dxfratio=dxfratio*factor
|
| 2146 |
if page2.rotation!=0:
|
| 2147 |
|
| 2148 |
rotationangle = page2.rotation
|