Files
ctbjrj/tools/rewrite_proj_fak.py
T

87 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
"""MenuProject_AddToFak_Click 重写为:复制项目库文件为方案(一个方案一个库文件)。幂等(按方法名定位替换)。"""
import io
P = r"C:\Users\099978\Documents\SharpDevelop Projects\成套报价软件\成套报价软件\主窗口.菜单与库.cs"
s = io.open(P, encoding="utf-8-sig").read()
si = None
for i, l in enumerate(s.split("\n")):
if l.startswith(" private void MenuProject_AddToFak_Click"):
si = i
break
assert si is not None, "MenuProject 方法未找到"
lines = s.split("\n")
ei = None
for i in range(si + 1, len(lines)):
if lines[i] == " }":
ei = i
break
assert ei is not None, "方法结束未找到"
NEW = r''' private void MenuProject_AddToFak_Click(object sender, EventArgs e)
{
var row = GetFirstSelectedRow(this.advancedDataGridView6);
if (row == null)
{
MessageBox.Show(this, "请先在项目列表中选择要存为方案的项目。",
"存为方案", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string projectId = GetBoundValueAsString(row, "项目编号");
string projName = GetBoundValueAsString(row, "项目名称");
if (string.IsNullOrWhiteSpace(projectId))
{
MessageBox.Show(this, "选中行没有项目编号,无法存为方案。",
"存为方案", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
string srcDbPath = GetQuoteDbPath(projectId);
if (!System.IO.File.Exists(srcDbPath))
{
MessageBox.Show(this, "项目报价库不存在:\n" + srcDbPath,
"存为方案", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
// 方案名默认 = 项目名称(空则项目编号);重名自动加序号
string 方案名 = !string.IsNullOrWhiteSpace(projName) ? projName : projectId;
string dstDir = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sjk", "方案库");
System.IO.Directory.CreateDirectory(dstDir);
string dstPath = System.IO.Path.Combine(dstDir, 方案名 + ".db");
int seq = 1;
while (System.IO.File.Exists(dstPath))
{
seq++;
dstPath = System.IO.Path.Combine(dstDir, 方案名 + "(" + seq + ").db");
}
var sw = System.Diagnostics.Stopwatch.StartNew();
Cursor = Cursors.WaitCursor;
try
{
// ★ 一个方案 = 一份独立项目库副本(含全部图号/箱号/元件/项目信息)
BackupSQLiteDb(srcDbPath, dstPath);
sw.Stop();
Log("[存为方案] 项目=" + projectId + " → " + dstPath + " 耗时 " + sw.ElapsedMilliseconds + "ms");
MessageBox.Show(this, "已把项目 [" + projectId + "] 存为方案。\n方案文件: " + dstPath +
"\n耗时 " + sw.ElapsedMilliseconds + "ms。", "存为方案",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception exSave)
{
MessageBox.Show(this, "存为方案失败:\n" + exSave.Message, "存为方案",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor = Cursors.Default;
}
}'''
lines[si:ei + 1] = NEW.split("\n")
io.open(P, "w", encoding="utf-8-sig").write("\n".join(lines))
print("MenuProject 重写完成")