-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_replace_part.tcl
More file actions
306 lines (257 loc) · 11.2 KB
/
bulk_replace_part.tcl
File metadata and controls
306 lines (257 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#####################################################################
# bulk_replace_part.tcl - 批量物料替换工具
# 作者: Jerry Silicon
# 日期: 2026-03-06
# 描述: 一键将原理图中所有相同物料(Value 或 Part Number)
# 替换为新的物料,保留 REFDES 和位置
#####################################################################
# 加载公共工具
set scriptDir [file dirname [info script]]
source [file join $scriptDir utils.tcl]
namespace eval ::OrcadTools::BulkReplace {
# 替换日志
variable replaceLog {}
#---------------------------------------------------------------
# 扫描全设计,查找匹配的元件
# matchProp: 匹配的属性名 (Value / Part Number)
# matchValue: 要匹配的值
# 返回: {refdes page propValue} 的列表
#---------------------------------------------------------------
proc scanDesign {matchProp matchValue} {
set lDesign [::OrcadTools::getActiveDesign]
if {$lDesign == "NULL"} { return {} }
set results {}
set lStatus [DboState]
# 遍历所有页面
set lSchematicIter [$lDesign GetSchematicsIter $lStatus]
while {$lSchematicIter != "NULL"} {
set lSchematic $lSchematicIter
set lPageIter [$lSchematic GetPagesIter $lStatus]
while {$lPageIter != "NULL"} {
set lPage $lPageIter
set lPageName [::OrcadTools::getCString [$lPage GetName]]
# 遍历元件
set lPartInst [$lPage GetPartInstsIter $lStatus]
while {$lPartInst != "NULL"} {
set propVal [::OrcadTools::getPartPropValue $lPartInst $matchProp]
if {$propVal eq $matchValue} {
set refdes [::OrcadTools::getRefDes $lPartInst]
lappend results [list $refdes $lPageName $propVal $lPartInst]
}
set lPartInst [$lPage GetPartInstsIterNext $lStatus]
}
set lPageIter [$lSchematic GetPagesIterNext $lStatus]
}
set lSchematicIter [$lDesign GetSchematicsIterNext $lStatus]
}
return $results
}
#---------------------------------------------------------------
# 执行批量替换
# matchProp: 匹配的属性名
# oldValue: 旧值
# newValue: 新值
# replaceProp: 要替换的属性名(可以与 matchProp 不同)
#---------------------------------------------------------------
proc executeReplace {matchProp oldValue newValue {replaceProp ""}} {
variable replaceLog
if {$replaceProp eq ""} {
set replaceProp $matchProp
}
set results [scanDesign $matchProp $oldValue]
if {[llength $results] == 0} {
::OrcadTools::msgBoxWarn "未找到匹配" "未找到 $matchProp = \"$oldValue\" 的元件"
return 0
}
set replaceLog {}
set successCount 0
set failCount 0
foreach item $results {
set refdes [lindex $item 0]
set pageName [lindex $item 1]
set partInst [lindex $item 3]
set result [::OrcadTools::setPartPropValue $partInst $replaceProp $newValue]
if {$result == 0} {
incr successCount
lappend replaceLog [list $refdes $pageName $oldValue $newValue "成功"]
::OrcadTools::log "$refdes ($pageName): $replaceProp \"$oldValue\" -> \"$newValue\" [成功]"
} else {
incr failCount
lappend replaceLog [list $refdes $pageName $oldValue $newValue "失败"]
::OrcadTools::logError "$refdes ($pageName): 替换失败 (错误码: $result)"
}
}
return $successCount
}
#---------------------------------------------------------------
# 导出替换日志
#---------------------------------------------------------------
proc exportLog {filename} {
variable replaceLog
if {[llength $replaceLog] == 0} {
::OrcadTools::msgBoxWarn "提示" "没有可导出的替换记录"
return
}
set f [open $filename w]
puts $f "REFDES,页面,旧值,新值,状态"
foreach entry $replaceLog {
puts $f [join $entry ","]
}
close $f
::OrcadTools::log "替换日志已导出: $filename"
::OrcadTools::msgBox "导出成功" "替换日志已保存至:\n$filename"
}
#---------------------------------------------------------------
# GUI 界面
#---------------------------------------------------------------
proc showGUI {} {
set w .bulkReplaceGUI
if {[winfo exists $w]} {
wm deiconify $w
raise $w
return
}
toplevel $w
wm title $w "批量物料替换"
wm geometry $w 500x480
wm resizable $w 0 0
# === 匹配模式 ===
labelframe $w.mode -text "匹配模式" -padx 10 -pady 5
pack $w.mode -fill x -padx 10 -pady 5
set ::OrcadTools::BulkReplace::matchMode "Value"
radiobutton $w.mode.r1 -text "按 Value 匹配 (如: 100nF/25V)" \
-variable ::OrcadTools::BulkReplace::matchMode -value "Value"
radiobutton $w.mode.r2 -text "按 Part Number 匹配 (如: CL05A104KP5NNNC)" \
-variable ::OrcadTools::BulkReplace::matchMode -value "Part Number"
pack $w.mode.r1 -anchor w
pack $w.mode.r2 -anchor w
# === 输入区 ===
labelframe $w.input -text "替换参数" -padx 10 -pady 5
pack $w.input -fill x -padx 10 -pady 5
frame $w.input.old
pack $w.input.old -fill x -pady 3
label $w.input.old.lbl -text "旧物料值:" -width 12 -anchor e
entry $w.input.old.entry -textvariable ::OrcadTools::BulkReplace::oldVal -width 35
pack $w.input.old.lbl -side left
pack $w.input.old.entry -side left -padx 5 -fill x -expand 1
frame $w.input.new
pack $w.input.new -fill x -pady 3
label $w.input.new.lbl -text "新物料值:" -width 12 -anchor e
entry $w.input.new.entry -textvariable ::OrcadTools::BulkReplace::newVal -width 35
pack $w.input.new.lbl -side left
pack $w.input.new.entry -side left -padx 5 -fill x -expand 1
# === 操作按钮 ===
frame $w.buttons
pack $w.buttons -fill x -padx 10 -pady 5
button $w.buttons.scan -text "🔍 扫描匹配" -width 15 -command {
::OrcadTools::BulkReplace::doScan
}
button $w.buttons.replace -text "✏️ 执行替换" -width 15 -command {
::OrcadTools::BulkReplace::doReplace
}
button $w.buttons.export -text "📄 导出日志" -width 15 -command {
::OrcadTools::BulkReplace::doExport
}
pack $w.buttons.scan -side left -padx 5
pack $w.buttons.replace -side left -padx 5
pack $w.buttons.export -side left -padx 5
# === 结果列表 ===
labelframe $w.results -text "匹配结果" -padx 10 -pady 5
pack $w.results -fill both -expand 1 -padx 10 -pady 5
# 创建 Treeview 风格的列表
frame $w.results.list
pack $w.results.list -fill both -expand 1
text $w.results.list.text -width 55 -height 10 \
-yscrollcommand "$w.results.list.sb set" -wrap none -state disabled \
-font {Courier 9}
scrollbar $w.results.list.sb -command "$w.results.list.text yview"
pack $w.results.list.sb -side right -fill y
pack $w.results.list.text -side left -fill both -expand 1
# 状态栏
label $w.status -text "就绪" -relief sunken -anchor w
pack $w.status -fill x -padx 10 -pady 5
# 关闭按钮
button $w.close -text "关闭" -command "destroy $w" -width 10
pack $w.close -pady 5
}
#---------------------------------------------------------------
# GUI 操作: 扫描
#---------------------------------------------------------------
proc doScan {} {
variable ::OrcadTools::BulkReplace::matchMode
variable ::OrcadTools::BulkReplace::oldVal
if {$oldVal eq ""} {
::OrcadTools::msgBoxWarn "提示" "请输入旧物料值!"
return
}
set results [scanDesign $matchMode $oldVal]
set count [llength $results]
# 更新结果列表
set w .bulkReplaceGUI
set txt $w.results.list.text
$txt configure -state normal
$txt delete 1.0 end
if {$count == 0} {
$txt insert end "未找到匹配的元件\n"
} else {
$txt insert end [format "%-12s %-20s %-25s\n" "REFDES" "页面" "$matchMode"]
$txt insert end [string repeat "-" 60]
$txt insert end "\n"
foreach item $results {
set refdes [lindex $item 0]
set pageName [lindex $item 1]
set propVal [lindex $item 2]
$txt insert end [format "%-12s %-20s %-25s\n" $refdes $pageName $propVal]
}
}
$txt configure -state disabled
$w.status configure -text "扫描完成:找到 $count 个匹配元件"
}
#---------------------------------------------------------------
# GUI 操作: 替换
#---------------------------------------------------------------
proc doReplace {} {
variable ::OrcadTools::BulkReplace::matchMode
variable ::OrcadTools::BulkReplace::oldVal
variable ::OrcadTools::BulkReplace::newVal
if {$oldVal eq ""} {
::OrcadTools::msgBoxWarn "提示" "请输入旧物料值!"
return
}
if {$newVal eq ""} {
::OrcadTools::msgBoxWarn "提示" "请输入新物料值!"
return
}
# 先扫描确认
set results [scanDesign $matchMode $oldVal]
set count [llength $results]
if {$count == 0} {
::OrcadTools::msgBoxWarn "未找到" "未找到匹配的元件"
return
}
set confirm [::OrcadTools::msgBoxConfirm "确认替换" \
"将替换 $count 个元件的 $matchMode:\n\"$oldVal\" → \"$newVal\"\n\n是否继续?"]
if {$confirm ne "yes"} { return }
set successCount [executeReplace $matchMode $oldVal $newVal]
set w .bulkReplaceGUI
$w.status configure -text "替换完成:成功 $successCount / $count 个"
::OrcadTools::msgBox "替换完成" \
"替换完成!\n成功: $successCount\n失败: [expr {$count - $successCount}]"
# 刷新扫描结果
doScan
}
#---------------------------------------------------------------
# GUI 操作: 导出日志
#---------------------------------------------------------------
proc doExport {} {
set filename [tk_getSaveFile -title "保存替换日志" \
-defaultextension ".csv" \
-filetypes {{"CSV 文件" ".csv"} {"所有文件" "*"}}]
if {$filename ne ""} {
exportLog $filename
}
}
}
# 提供快捷命令
proc BulkReplace {} { ::OrcadTools::BulkReplace::showGUI }
::OrcadTools::log "批量物料替换工具 (bulk_replace_part.tcl) 已加载 - 输入 BulkReplace 打开界面"