개정판 bbc263db
cad auto scale, offset test
Change-Id: I2c9d96123bb32a2b34c91cbb86cb9e341d2aa97a
DTI_PID/DTI_PID/ImportTextFromCADDialog.py | ||
---|---|---|
276 | 276 |
|
277 | 277 |
self.text_scale = None |
278 | 278 |
self.id2_bbox = None |
279 |
self.scales = None |
|
280 |
self.offsets = None |
|
279 | 281 |
|
280 | 282 |
self.isAccepted = False |
281 | 283 |
|
... | ... | |
780 | 782 |
from AppDocData import AppDocData |
781 | 783 |
import cv2 |
782 | 784 |
|
783 |
min_x, min_y, max_x, max_y, width, height = 0, 0, None, None, None, None
|
|
785 |
min_x, min_y, max_x, max_y, width, height = None, None, None, None, None, None
|
|
784 | 786 |
|
785 | 787 |
img = AppDocData.my_imread(file_path=img_file_path) |
786 |
max_x = img.shape[1] |
|
787 |
max_y = img.shape[0] |
|
788 | 788 |
width = img.shape[1] |
789 | 789 |
height = img.shape[0] |
790 | 790 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
... | ... | |
866 | 866 |
id2_xml_root = id2_xml.getroot() |
867 | 867 |
|
868 | 868 |
#id2_bbox = [0, 0, 9600, 6781] |
869 |
self.id2_bbox = get_contents_size_from_image(id2_image_file)
|
|
869 |
id2_bbox = get_contents_size_from_image(id2_image_file) |
|
870 | 870 |
|
871 | 871 |
if 'Symbol' in will_be_converted_items: |
872 | 872 |
symbols_node = id2_xml_root.find(xg.SYMBOL_LIST_NODE_NAME) |
... | ... | |
912 | 912 |
autocad_bbox = [min_values[0], min_values[1], |
913 | 913 |
max_values[0] - min_values[0], max_values[1] - min_values[1]] |
914 | 914 |
|
915 |
scale_x = id2_bbox[2] / autocad_bbox[2] \ |
|
916 |
if self.ui.doubleSpinBox.value() < 1 else self.ui.doubleSpinBox.value() |
|
917 |
scale_y = id2_bbox[3] / autocad_bbox[3] \ |
|
918 |
if self.ui.doubleSpinBox_2.value() < 1 else self.ui.doubleSpinBox_2.value() |
|
919 |
self.scales = [scale_x, scale_y] |
|
920 |
offsets = [id2_bbox[0] - autocad_bbox[0] * scale_x, id2_bbox[5] + id2_bbox[1]] |
|
921 |
self.offsets = offsets + [autocad_bbox[1]] |
|
922 |
|
|
915 | 923 |
if 'Text' in will_be_converted_items: |
916 | 924 |
for record in blk_tbl_record.iter('AcDbText'): |
917 | 925 |
if record.attrib['Layer'] not in exclude_layers: |
918 |
node = self.text_to_xml(record, self.id2_bbox, autocad_bbox)
|
|
926 |
node = self.text_to_xml(record) |
|
919 | 927 |
if node: |
920 | 928 |
textInfo.append(node) |
921 | 929 |
|
922 | 930 |
for blk_ref in blk_tbl_record.iter('AcDbBlockReference'): |
923 | 931 |
if 'Symbol' in will_be_converted_items: |
924 | 932 |
if blk_ref.attrib['Layer'] not in exclude_layers: |
925 |
node = self.symbol_to_xml(blk_ref, self.id2_bbox, autocad_bbox, mapping_configs)
|
|
933 |
node = self.symbol_to_xml(blk_ref, mapping_configs) |
|
926 | 934 |
if node: |
927 | 935 |
symbols_node.append(node) |
928 | 936 |
|
929 | 937 |
if 'Text' in will_be_converted_items: |
930 | 938 |
angle = round(float(blk_ref.attrib['Angle']), 2) |
931 | 939 |
for record in blk_ref.iter('AcDbAttribute'): |
932 |
node = self.text_to_xml(record, self.id2_bbox, autocad_bbox)
|
|
940 |
node = self.text_to_xml(record) |
|
933 | 941 |
if node: |
934 | 942 |
textInfo.append(node) |
935 | 943 |
|
936 | 944 |
if 'Line' in will_be_converted_items: |
937 | 945 |
for record in blk_tbl_record.iter('AcDbLine'): |
938 | 946 |
if record.attrib['Layer'] not in exclude_layers: |
939 |
nodes = self.lines_to_xml(layers, line_types, record, self.id2_bbox, autocad_bbox)
|
|
947 |
nodes = self.lines_to_xml(layers, line_types, record) |
|
940 | 948 |
if nodes: |
941 | 949 |
for node in nodes: |
942 | 950 |
line_infos.append(node) |
943 | 951 |
|
944 | 952 |
for record in blk_tbl_record.iter('AcDbPolyline'): |
945 | 953 |
if record.attrib['Layer'] not in exclude_layers: |
946 |
nodes = self.lines_to_xml(layers, line_types, record, self.id2_bbox, autocad_bbox)
|
|
954 |
nodes = self.lines_to_xml(layers, line_types, record) |
|
947 | 955 |
if nodes: |
948 | 956 |
for node in nodes: |
949 | 957 |
line_infos.append(node) |
... | ... | |
968 | 976 |
QMessageBox.information(self, self.tr('Information'), self.tr('Importing finished!'), |
969 | 977 |
QMessageBox.Close) |
970 | 978 |
|
971 |
def symbol_to_xml(self, blk_ref_node, id2_bbox, autocad_bbox, mapping_configs: list) -> str: |
|
979 |
def convert_to_image_coords(self, pt): |
|
980 |
"""convert autocad coordinates to image coordinates""" |
|
981 |
return [pt[0] * self.scales[0] + self.offsets[0], (self.offsets[2] - pt[1]) * self.scales[1] + self.offsets[1]] |
|
982 |
|
|
983 |
def symbol_to_xml(self, blk_ref_node, mapping_configs: list) -> str: |
|
972 | 984 |
"""try to convert block reference element to id2 xml""" |
973 | 985 |
import uuid |
974 | 986 |
from SymbolSvgItem import SymbolSvgItem |
975 | 987 |
|
976 |
def convert_to_image_coords(pt, scale: list, offset: list): |
|
977 |
"""convert autocad coordinates to image coordinates""" |
|
978 |
return [pt[0] * scale[0] + offset[0], -pt[1] * scale[1] + offset[1]] |
|
979 |
|
|
980 | 988 |
try: |
981 |
scale_x = max([id2_bbox[2] / autocad_bbox[2], id2_bbox[3] / autocad_bbox[3]]) if self.ui.doubleSpinBox.value() < 1 else self.ui.doubleSpinBox.value() |
|
982 |
scale_y = max([id2_bbox[2] / autocad_bbox[2], id2_bbox[3] / autocad_bbox[3]]) if self.ui.doubleSpinBox_2.value() < 1 else self.ui.doubleSpinBox_2.value() |
|
983 |
offsets = [id2_bbox[0] + self.ui.spinBoxX.value() - autocad_bbox[0] * scale_x, (id2_bbox[1] + id2_bbox[3]) + self.ui.spinBoxY.value() - autocad_bbox[1] * scale_y] |
|
984 |
#offsets = [id2_bbox[0] - autocad_bbox[0] * scale_x, (id2_bbox[1] + id2_bbox[3]) - autocad_bbox[1] * scale_y] |
|
985 |
#offsets = [self.ui.spinBoxTextX.value(), self.drawing_height + self.ui.spinBoxTextY.value()] |
|
986 |
|
|
987 | 989 |
name = blk_ref_node.attrib['Name'] |
988 |
origin = convert_to_image_coords((float(blk_ref_node.attrib['X']), float(blk_ref_node.attrib['Y'])), |
|
989 |
[scale_x, scale_y], offsets) |
|
990 |
origin = self.convert_to_image_coords([float(blk_ref_node.attrib['X']), float(blk_ref_node.attrib['Y'])]) |
|
990 | 991 |
flip = float(blk_ref_node.attrib['ScaleFactors'].replace('(', '').replace(')', '').split(',')[0]) |
991 | 992 |
flip = 1 if flip < 0 else 0 |
992 | 993 |
if flip == 0: |
... | ... | |
1004 | 1005 |
min_extents, max_extents = None, None |
1005 | 1006 |
tokens = blk_ref_node.attrib['MaxExtents'].strip('()').split(',') |
1006 | 1007 |
if 3 == len(tokens): |
1007 |
max_extents = convert_to_image_coords((float(tokens[0]), float(tokens[1])), [scale_x, scale_y], offsets)
|
|
1008 |
max_extents = self.convert_to_image_coords([float(tokens[0]), float(tokens[1])])
|
|
1008 | 1009 |
|
1009 | 1010 |
tokens = blk_ref_node.attrib['MinExtents'].strip('()').split(',') |
1010 | 1011 |
if 3 == len(tokens): |
1011 |
min_extents = convert_to_image_coords((float(tokens[0]), float(tokens[1])), [scale_x, scale_y], offsets)
|
|
1012 |
min_extents = self.convert_to_image_coords([float(tokens[0]), float(tokens[1])])
|
|
1012 | 1013 |
|
1013 | 1014 |
_height = abs(max_extents[1] - min_extents[1]) |
1014 | 1015 |
_width = abs(max_extents[0] - min_extents[0]) |
... | ... | |
1058 | 1059 |
|
1059 | 1060 |
return None |
1060 | 1061 |
|
1061 |
def text_to_xml(self, text_node, id2_bbox, autocad_bbox):
|
|
1062 |
def text_to_xml(self, text_node): |
|
1062 | 1063 |
""" |
1063 | 1064 |
try to convert text element to id2 xml |
1064 | 1065 |
@param text_node: |
... | ... | |
1070 | 1071 |
from EngineeringTextItem import QEngineeringTextItem |
1071 | 1072 |
|
1072 | 1073 |
try: |
1073 |
scale_x = id2_bbox[2] / autocad_bbox[2] \ |
|
1074 |
if self.ui.doubleSpinBox.value() < 1 else self.ui.doubleSpinBox.value() |
|
1075 |
scale_y = id2_bbox[3] / autocad_bbox[3] \ |
|
1076 |
if self.ui.doubleSpinBox_2.value() < 1 else self.ui.doubleSpinBox_2.value() |
|
1077 |
offsets = [id2_bbox[0] - autocad_bbox[0] * scale_x, (id2_bbox[5] + id2_bbox[1]) + autocad_bbox[1] * scale_y] |
|
1078 |
|
|
1079 |
#offsets = [self.ui.spinBoxTextX.value(), (id2_bbox[1] + id2_bbox[3]) + self.ui.spinBoxTextY.value()] |
|
1080 |
|
|
1081 |
loc = [float(text_node.attrib['X']) * scale_x + offsets[0], |
|
1082 |
-float(text_node.attrib['Y']) * scale_y + offsets[1]] |
|
1074 |
loc = self.convert_to_image_coords([float(text_node.attrib['X']), float(text_node.attrib['Y'])]) |
|
1083 | 1075 |
|
1084 | 1076 |
text = text_node.text |
1085 | 1077 |
angle = round(float(text_node.attrib['Angle']), 2) |
1086 | 1078 |
if 'IsMirroredInX' in text_node.attrib and text_node.attrib['IsMirroredInX'] == 'True': |
1087 |
if abs(math.pi*0.5 - angle) < 0.01 or abs(math.pi*1.5 - angle) < 0.01:
|
|
1079 |
if abs(math.pi * 0.5 - angle) < 0.01 or abs(math.pi * 1.5 - angle) < 0.01:
|
|
1088 | 1080 |
angle += math.pi |
1089 |
angle = angle - math.pi*2 if angle > math.pi*2 else angle
|
|
1081 |
angle = angle - math.pi * 2 if angle > math.pi * 2 else angle
|
|
1090 | 1082 |
|
1091 | 1083 |
if 'IsMirroredInY' in text_node.attrib and text_node.attrib['IsMirroredInY'] == 'True': |
1092 | 1084 |
if abs(angle) < 0.01 or abs(math.pi - angle) < 0.01: |
1093 | 1085 |
angle += math.pi |
1094 |
angle = angle - math.pi*2 if angle > math.pi*2 else angle
|
|
1086 |
angle = angle - math.pi * 2 if angle > math.pi * 2 else angle
|
|
1095 | 1087 |
|
1096 |
_height = round(float(text_node.attrib['Height']) * scale_y)
|
|
1088 |
_height = round(float(text_node.attrib['Height']) * self.scales[1])
|
|
1097 | 1089 |
#loc[1] -= _height |
1098 | 1090 |
_width = round(_height * len(text) * self.text_scale) |
1099 | 1091 |
#_width = round(float(text_node.attrib['Width'])) |
... | ... | |
1130 | 1122 |
|
1131 | 1123 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
1132 | 1124 |
|
1133 |
def lines_to_xml(self, layers: list, line_types: list, line_node, id2_bbox, autocad_bbox) -> list:
|
|
1125 |
def lines_to_xml(self, layers: list, line_types: list, line_node) -> list: |
|
1134 | 1126 |
"""try to convert line element to id2 xml""" |
1135 | 1127 |
from LineTypeConditions import LineTypeConditions |
1136 | 1128 |
from EngineeringLineItem import QEngineeringLineItem |
... | ... | |
1154 | 1146 |
return res |
1155 | 1147 |
|
1156 | 1148 |
try: |
1157 |
scale_x = max([id2_bbox[2] / autocad_bbox[2], id2_bbox[3] / autocad_bbox[3]]) if self.ui.doubleSpinBox.value() < 1 else self.ui.doubleSpinBox.value() |
|
1158 |
scale_y = max([id2_bbox[2] / autocad_bbox[2], id2_bbox[3] / autocad_bbox[3]]) if self.ui.doubleSpinBox_2.value() < 1 else self.ui.doubleSpinBox_2.value() |
|
1159 |
offsets = [id2_bbox[0] + self.ui.spinBoxX.value() - autocad_bbox[0] * scale_x, (id2_bbox[1] + id2_bbox[3]) + self.ui.spinBoxY.value() - autocad_bbox[1] * scale_y] |
|
1160 |
#offsets = [self.ui.spinBoxX.value(), self.drawing_height + self.ui.spinBoxX.value()] |
|
1161 |
|
|
1162 | 1149 |
pts = [] |
1163 | 1150 |
for vertex in line_node.iter('Vertex'): |
1164 |
pts.append((float(vertex.attrib['X']) * scale_x + offsets[0], |
|
1165 |
-float(vertex.attrib['Y']) * scale_y + offsets[1])) |
|
1151 |
pts.append(self.convert_to_image_coords([float(vertex.attrib['X']), float(vertex.attrib['Y'])])) |
|
1166 | 1152 |
|
1167 | 1153 |
# append first point if 'Closed' attribute is True |
1168 | 1154 |
if 'Closed' in line_node.attrib and line_node.attrib['Closed'].upper() == 'True'.upper(): |
내보내기 Unified diff