58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""恢复 pdfbt Form3 四表列定义:照 cadbt Form3.ui.cs 的索引式列(Columns.Add+Columns[N]属性)
|
|
替换 pdfbt 的空壳字段+AddRange,并清理 57 个无用列字段。幂等(重复跑前检查)。"""
|
|
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"
|
|
|
|
cs = io.open(CADBT, encoding="utf-8-sig").read()
|
|
ps = io.open(PDFBT, encoding="utf-8-sig").read()
|
|
|
|
TABLES = ["advancedDataGridView6", "advancedDataGridView9", "advancedDataGridView4", "advancedDataGridView5"]
|
|
|
|
def 提取cadbt段(tbl):
|
|
"""从 cadbt 提取该表的 Columns.Add...Columns[N] 连续段(含首尾)。"""
|
|
i = cs.find("this." + tbl + ".Columns.Add(")
|
|
assert i > 0, tbl + " Add 未找到"
|
|
lines = cs[i:].split("\n")
|
|
out = []
|
|
for l in lines:
|
|
t = l.strip()
|
|
if ("Columns.Add(" in t) or re.match(re.escape(tbl) + r"\.Columns\[\d+\]\.", t):
|
|
out.append(l)
|
|
elif out and t == "":
|
|
# 允许段内不换段(索引段中间无空行),遇其他语句即止
|
|
continue
|
|
elif out:
|
|
break
|
|
assert out, tbl + " 段为空"
|
|
return "\n".join(out)
|
|
|
|
def 替换pdfbt段(tbl, new_seg):
|
|
"""把 pdfbt 该表的 Columns.AddRange(...) 块替换为 cadbt 索引段。"""
|
|
global ps
|
|
m = re.search(
|
|
r"this\." + tbl + r"\.Columns\.AddRange\(new System\.Windows\.Forms\.DataGridViewColumn\[\] \{.*?\}\);",
|
|
ps, re.S)
|
|
if not m:
|
|
# 已经替换过(幂等)
|
|
if "this." + tbl + ".Columns.Add(" in ps:
|
|
return False
|
|
raise AssertionError(tbl + " AddRange 未找到")
|
|
ps = ps[:m.start()] + new_seg + ps[m.end():]
|
|
return True
|
|
|
|
for tbl in TABLES:
|
|
seg = 提取cadbt段(tbl)
|
|
n = len([l for l in seg.split("\n") if "Columns.Add(" in l])
|
|
changed = 替换pdfbt段(tbl, seg)
|
|
print(tbl, "列数", n, "替换" if changed else "跳过(已改)")
|
|
|
|
# 删 57 个字段声明 + new 行(带 \t 或空格缩进两种都匹配)
|
|
ps = re.sub(r"\n\s*private System\.Windows\.Forms\.DataGridViewTextBoxColumn dataGridViewTextBoxColumn\d+;", "", ps)
|
|
ps = re.sub(r"\n\s*this\.dataGridViewTextBoxColumn\d+ = new System\.Windows\.Forms\.DataGridViewTextBoxColumn\(\);", "", ps)
|
|
io.open(PDFBT, "w", encoding="utf-8-sig").write(ps)
|
|
print("字段清理完成; HeaderText 数:", len(re.findall(r"HeaderText", ps)))
|