# -*- coding: utf-8 -*- """pdfbt Form3 列定义重建为标准设计器格式(57 字段+new+属性块+AddRange),设计器可见中文列头。 列属性数据从 cadbt Form3.ui.cs 索引段提取(权威参照);行内容定位插入(缩进无关)。""" import io import re CADBT = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\cadbt\cadbt\Plugin\Form3.ui.cs" PDFBT = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\pdfbt\Plugin\Form3.Designer.cs" TABLES = ["advancedDataGridView6", "advancedDataGridView9", "advancedDataGridView4", "advancedDataGridView5"] cs = io.open(CADBT, encoding="utf-8-sig").read() ps = io.open(PDFBT, encoding="utf-8-sig").read() # ---------- 1) 从 cadbt 解析每表每列属性 ---------- pat_idx = re.compile(r"this\.(\w+)\.Columns\[(\d+)\]\.(\w+) = (.*?);") cols = {t: {} for t in TABLES} pat_add = re.compile(r"this\.(\w+)\.Columns\.Add\(new System\.Windows\.Forms\.DataGridViewTextBoxColumn\(\)\);") for l in cs.split("\n"): t = l.strip() m = pat_add.match(t) if m and m.group(1) in TABLES: continue m = pat_idx.match(t) if m and m.group(1) in cols: tbl, idx, prop, val = m.group(1), int(m.group(2)), m.group(3), m.group(4) cols[tbl].setdefault(idx, {})[prop] = val for t in TABLES: n = len(cols[t]) assert n > 0 and cols[t].get(0, {}).get("HeaderText"), t + " 解析不完整" print(t, "解析列数", n) # ---------- 2) 生成标准设计器代码 ---------- gnum = 0 fields, news, blocks = [], [], [] addRanges = {} for tbl in TABLES: ids = [] for idx in sorted(cols[tbl].keys()): gnum += 1 fname = "dataGridViewTextBoxColumn%d" % gnum ids.append(fname) fields.append("\t\tprivate System.Windows.Forms.DataGridViewTextBoxColumn %s;" % fname) news.append("\t\t\tthis.%s = new System.Windows.Forms.DataGridViewTextBoxColumn();" % fname) props = dict(cols[tbl][idx]) defaults = {"Width": "100", "Visible": "true", "ReadOnly": "true", "Frozen": "false", "AutoSizeMode": "System.Windows.Forms.DataGridViewAutoSizeColumnMode.NotSet", "SortMode": "System.Windows.Forms.DataGridViewColumnSortMode.NotSortable"} for k, v in defaults.items(): props.setdefault(k, v) blocks.append("\t\t\t// ") blocks.append("\t\t\t// %s" % fname) blocks.append("\t\t\t// ") blocks.append('\t\t\tthis.%s.Name = "%s";' % (fname, fname)) for k in ["HeaderText", "Width", "Visible", "ReadOnly", "Frozen", "AutoSizeMode", "DataPropertyName", "SortMode"]: if k in props: blocks.append("\t\t\tthis.%s.%s = %s;" % (fname, k, props[k])) inner = ",\n".join("\t\t\t\tthis." + i for i in ids) addRanges[tbl] = ("\t\t\tthis.%s.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {\n" % tbl + inner + "\n\t\t\t});") # ---------- 3) 重写 pdfbt ---------- def lines_of(text): return text.split("\n") def to_text(lines): return "\n".join(lines) # 3a) 删索引式段 for tbl in TABLES: pat_a = re.compile(r"this\." + tbl + r"\.Columns\.Add\(") pat_i = re.compile(r"this\." + tbl + r"\.Columns\[\d+\]\.") out, in_seg, seen = [], False, False for l in lines_of(ps): t = l.strip() if pat_a.search(t): in_seg, seen = True, True continue if in_seg: if pat_i.search(t) or t == "": continue in_seg = False out.append(l) assert seen, tbl + " 索引段未找到" ps = to_text(out) print(tbl, "索引段已删") # 3b) 每表 ColumnHeadersDefaultCellStyle 行后插 AddRange for tbl in TABLES: ls = lines_of(ps) key = "this.%s.ColumnHeadersDefaultCellStyle" % tbl done = False for i, l in enumerate(ls): if key in l: ls.insert(i + 1, addRanges[tbl]) done = True break assert done, tbl + " 样式行未找到" ps = to_text(ls) # 3c) new 行插在 InitializeComponent 里第一个 cellStyle 声明行前 ls = lines_of(ps) done = False for i, l in enumerate(ls): if "System.Windows.Forms.DataGridViewCellStyle" in l and "= new" in l: ls[i:i] = news done = True break assert done, "cellStyle 首行未找到" ps = to_text(ls) # 3d) 字段声明插在 components = null 行后 ls = lines_of(ps) done = False for i, l in enumerate(ls): if "private System.ComponentModel.IContainer components = null;" in l: ls[i + 1:i + 1] = fields done = True break assert done, "components 声明未找到" ps = to_text(ls) io.open(PDFBT, "w", encoding="utf-8-sig").write(ps) print("重建完成: 字段%d new%d HeaderText%d" % (len(fields), len(news), len(re.findall("HeaderText", ps))))