fix: harden supervised replacement and missing uid handling

This commit is contained in:
阿白叔 committed 2026-10-30 16:33:10 +08:00
1 parent 656f43e2ff
commit 131b6dbbb3
30 files changed
+1655 -92

No files matched your search

@@ -281,7 +281,7 @@ Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token,
"If QP_NR_Code<>0 Then QP_NR_Text=""ERROR"" & vbTab & " & QPNativeExpression(Token) & _
" & vbTab & CStr(QP_NR_Code) & vbTab & QPEncode(QP_NR_Source) & vbTab & QPEncode(QP_NR_Description) & vbCrLf" & vbCrLf & _
"QPWriteUtf8 " & QPNativeExpression(ResultPath) & ",QP_NR_Text" & vbCrLf & "On Error GoTo 0" & vbCrLf
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Names = Split("QPNativeCollectMode QPNativeRequireContext QPNativeReadInvalid QPSnapshotComponent QPUntouchedComponent QPUntouchedFullState QPUntouchedIdentity QPComponentPins QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPEncode QPDecode QPProjectPath QPSheetIdentity QPCanonicalPath QPEnsureProjectAccess QPReadUtf8 QPWriteUtf8", " ")
Pattern.Global = False
For Each Name In Names
Pattern.Pattern = "^(Function|Sub) " & Name & "\([^\n]*\n[\s\S]*?^End \1[ \t]*"
+168 -35
View File
@@ -25,6 +25,7 @@ Dim QP_RestoreTransactionOpen
Dim QP_PinIndex
Dim QP_PinContext
Dim QP_ComponentUidIndex, QP_ComponentRefdesIndex, QP_ComponentLocationIndex
Dim QP_FallbackIdentityUsed
Dim QP_ComponentIndexContext
Dim QP_NativeCallActive
Dim QP_BatchMode, QP_OwnedProject, QP_BatchState, QP_BatchCode, QP_BatchMessage
@@ -69,6 +70,7 @@ Set QP_ComponentUidIndex = Nothing
Set QP_ComponentRefdesIndex = Nothing
Set QP_ComponentLocationIndex = Nothing
QP_ComponentIndexContext = ""
QP_FallbackIdentityUsed = False
QP_CurrentJobId = ""
QP_ExitCode = 0
QP_Stage = "session_startup"
@@ -714,8 +716,8 @@ End Sub
Sub QPDiscoverInventory(ByRef Documents)
Dim Requested, Schematics, Schematic, Sheets, Sheet, Document, View
Dim Components, Comp, PartAttr, ValueAttr, PartNumber, PartKey, HitKey, Seen, Text, Count
Dim ComponentText, ValueText, ComponentMode
Dim Components, Comp, PartAttr, ValueAttr, PartNumber, PartKey, HitKey, Seen, Text, Count, Uid
Dim ComponentText, ValueText, ComponentMode, SheetRefdes, RefdesKey
Set Requested = QPReadInventoryRequest()
Set Seen = CreateObject("Scripting.Dictionary")
Text = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & "old_part" & vbCrLf
@@ -739,22 +741,31 @@ Sub QPDiscoverInventory(ByRef Documents)
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "inventory sheet identity changed"
QP_Stage = "inventory_query " & CStr(Schematic) & ":" & CStr(Sheet)
Set Components = View.Query(VDM_COMP, VD_ALL)
Set SheetRefdes = CreateObject("Scripting.Dictionary")
SheetRefdes.CompareMode = vbTextCompare
For Each Comp In Components
QP_Stage = "inventory_read_component " & CStr(Schematic) & ":" & CStr(Sheet)
Uid = QPComponentIdentityKey(Comp)
If ComponentMode Then
RefdesKey = Trim(CStr(Comp.Refdes))
If Len(RefdesKey) > 0 Then
If SheetRefdes.Exists(RefdesKey) Then Err.Raise vbObjectError + 872, _
"QuickPartReplace.Discovery", "duplicate component Refdes: " & RefdesKey
SheetRefdes.Add RefdesKey, True
End If
End If
PartNumber = ""
Set PartAttr = QPFindCurrentAttribute(Comp, "Part Number")
If Not PartAttr Is Nothing Then PartNumber = QPEffectiveAttributeValue(PartAttr)
PartKey = LCase(CStr(PartNumber))
If Requested.Exists(PartKey) Then
If ComponentMode Then
ValueText = ""
Set ValueAttr = QPFindCurrentAttribute(Comp, "Value")
If Not ValueAttr Is Nothing Then ValueText = QPEffectiveAttributeValue(ValueAttr)
If Len(CStr(Comp.UID)) = 0 Or Len(CStr(Comp.Refdes)) = 0 Then _
ValueText = QPEffectiveComponentAttribute(Comp, "Value")
If Len(Trim(CStr(Uid))) = 0 Or Len(Trim(CStr(Comp.Refdes))) = 0 Then _
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component identity is incomplete"
ComponentText = ComponentText & QPEncode(QPProjectPath()) & vbTab & _
QPEncode(CStr(Schematic)) & vbTab & QPEncode(CStr(Sheet)) & vbTab & _
QPEncode(CStr(Comp.UID)) & vbTab & QPEncode(CStr(Comp.Refdes)) & vbTab & _
QPEncode(Uid) & vbTab & QPEncode(CStr(Comp.Refdes)) & vbTab & _
QPEncode(CStr(PartNumber)) & vbTab & QPEncode(CStr(ValueText)) & vbCrLf
End If
HitKey = QPSheetKey(Schematic, Sheet) & Chr(30) & PartKey
@@ -776,6 +787,49 @@ Sub QPDiscoverInventory(ByRef Documents)
If ComponentMode Then QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "components.tsv"), ComponentText
End Sub
Function QPComponentIdentityKey(ByRef Comp)
Dim Uid, Refdes, Number, Description
On Error Resume Next
Err.Clear
Uid = CStr(Comp.UID)
Number = Err.Number
Description = Err.Description
Err.Clear
On Error GoTo 0
If Number <> 0 Then Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", _
"component UID could not be read: " & Description
On Error Resume Next
Err.Clear
Refdes = CStr(Comp.Refdes)
Number = Err.Number
Description = Err.Description
Err.Clear
On Error GoTo 0
If Number <> 0 Then Err.Raise vbObjectError + 872, _
"QuickPartReplace.Discovery", "component Refdes is unreadable; operation stopped before any replacement"
If Len(Trim(CStr(Uid))) > 0 Then
QPComponentIdentityKey = CStr(Uid)
Else
QP_FallbackIdentityUsed = True
If Len(Trim(CStr(Refdes))) > 0 Then
QPComponentIdentityKey = "@REFDES|" & Trim(CStr(Refdes))
QPLog QP_Root, "WARN", "component_uid_missing_using_refdes_fallback refdes=" & CStr(Refdes)
Else
Dim Point
On Error Resume Next
Set Point = Comp.GetLocation()
If Err.Number <> 0 Or Point Is Nothing Then
Err.Clear
On Error GoTo 0
Err.Raise vbObjectError + 872, "QuickPartReplace.Discovery", "component UID and Refdes are empty"
End If
QPComponentIdentityKey = "@ANON|" & CStr(Point.X) & "|" & CStr(Point.Y)
Err.Clear
On Error GoTo 0
End If
End If
End Function
Function QPReadInventoryRequest()
Dim Path, Lines, Header, Result, Index, Fields, OldPart, Key
Path = QP_Fso.BuildPath(QP_Root, "inventory-request.tsv")
@@ -1185,13 +1239,13 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
' when restoration needs them, from fresh proxies after native mutation.
Set Selectors = QPCommandTargets(Command)
For Each Comp In Components
Uid = CStr(Comp.UID)
Uid = QPComponentIdentityKey(Comp)
If Selectors Is Nothing Then
PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number")
PartNumber = QPReadableAttributeValue(Comp, "Part Number")
If StrComp(PartNumber, CStr(Command("old_part_number")), _
vbTextCompare) = 0 Then Matches.Add CStr(Matches.Count), Comp
ElseIf Selectors.Exists(Uid) Then
PartNumber = QPEffectiveComponentAttribute(Comp, "Part Number")
PartNumber = QPRequiredAttributeValue(Comp, "Part Number")
Selector = Selectors.Item(Uid)
ValueText = ""
Set ValueAttr = QPFindCurrentAttribute(Comp, "Value")
@@ -1235,10 +1289,10 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Set MatchedUids = CreateObject("Scripting.Dictionary")
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
MatchedUids.Add CStr(Comp.UID), True
MatchedUids.Add QPComponentIdentityKey(Comp), True
Next
Set NativeRead = Nothing
If QPUseNativeSnapshot() Then
If QPUseNativeSnapshot() And Not QP_FallbackIdentityUsed Then
QPRequireProject Command
Set NativeRead = QPNativeCapture(MatchedUids)
If Not NativeRead Is Nothing Then
@@ -1248,7 +1302,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
End If
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
Uid = CStr(Comp.UID)
Uid = QPComponentIdentityKey(Comp)
QPLog QP_Root, "INFO", "stage=snapshot_component uid=" & QPLogField(Uid)
If NativeRead Is Nothing Then
Snapshot = Snapshot & QPSnapshotComponent(Comp)
@@ -1318,6 +1372,24 @@ Function QPUseNativeSnapshot()
QPUseNativeSnapshot = True
End Function
Function QPReadableAttributeValue(ByRef Comp, ByVal Name)
Dim Attr, ErrorNumber, ErrorSource, ErrorDescription
On Error Resume Next
Err.Clear
Set Attr = Comp.FindAttribute(CStr(Name))
ErrorNumber = Err.Number
ErrorSource = Err.Source
ErrorDescription = Err.Description
Err.Clear
On Error GoTo 0
If ErrorNumber <> 0 Then Err.Raise ErrorNumber, ErrorSource, ErrorDescription
If Attr Is Nothing Then
QPReadableAttributeValue = ""
Else
QPReadableAttributeValue = QPEffectiveAttributeValue(Attr)
End If
End Function
Function QPUntouchedComponent(ByRef Comp)
Dim State, Identity, Fields
State = QPUntouchedFullState(Comp)
@@ -1360,8 +1432,10 @@ Function QPUntouchedIdentity(ByVal State)
End Function
Function QPUntouchedFullState(ByRef Comp)
Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid
Dim Point, Pins, Connections, Attr, AttributeText, Result, Uid, Identity
Uid = CStr(Comp.UID)
Identity = Uid
If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes))
Set Point = Comp.GetLocation()
Set Pins = QPComponentPins(Uid)
If Pins.Count = 0 Then
@@ -1371,22 +1445,54 @@ Function QPUntouchedFullState(ByRef Comp)
Else
Connections = QPConnectionSignatureFromPins(Uid, Pins)
End If
Result = "C" & vbTab & QPEncode(Uid) & vbTab & QPEncode(CStr(Comp.Refdes)) & _
Result = "C" & vbTab & QPEncode(Identity) & vbTab & QPEncode(CStr(Comp.Refdes)) & _
vbTab & QPEncode(CStr(Point.X)) & vbTab & QPEncode(CStr(Point.Y)) & _
vbTab & QPEncode(CStr(Comp.Orientation)) & vbTab & QPEncode(CStr(Comp.Scale)) & _
vbTab & QPEncode(Connections) & vbCrLf
AttributeText = ""
For Each Attr In Comp.Attributes
AttributeText = AttributeText & QPSnapshotAttribute(Uid, Attr)
AttributeText = AttributeText & QPSnapshotAttribute(Identity, Attr)
Next
QPUntouchedFullState = Result & QPSortSnapshotLines(AttributeText)
End Function
Function QPEffectiveComponentAttribute(ByRef Comp, ByVal Name)
Dim Attr
Dim Attr, ErrorNumber, ErrorDescription
QPEffectiveComponentAttribute = ""
On Error Resume Next
Err.Clear
Set Attr = QPFindCurrentAttribute(Comp, Name)
If Not Attr Is Nothing Then QPEffectiveComponentAttribute = QPEffectiveAttributeValue(Attr)
ErrorNumber = Err.Number
ErrorDescription = Err.Description
Err.Clear
If ErrorNumber = 0 And Not Attr Is Nothing Then
QPEffectiveComponentAttribute = QPReadOptionalAttributeValue(Attr)
ElseIf ErrorNumber <> 0 Then
QPLog QP_Root, "WARN", "optional_attribute_unavailable name=" & CStr(Name) & _
" description=" & CStr(ErrorDescription)
End If
On Error GoTo 0
End Function
Function QPReadOptionalAttributeValue(ByRef Attr)
Dim Instance, Value, Number
QPReadOptionalAttributeValue = ""
On Error Resume Next
Err.Clear
Instance = CStr(Attr.InstanceValue)
Number = Err.Number
Err.Clear
If Number = 0 And Len(CStr(Instance)) > 0 Then
QPReadOptionalAttributeValue = Instance
On Error GoTo 0
Exit Function
End If
Err.Clear
Value = CStr(Attr.Value)
Number = Err.Number
Err.Clear
If Number = 0 Then QPReadOptionalAttributeValue = CStr(Value)
On Error GoTo 0
End Function
Sub QPVerifyUntouched(ByRef Records)
@@ -1409,7 +1515,7 @@ Sub QPVerifyUntouched(ByRef Records)
Set Seen = CreateObject("Scripting.Dictionary")
For Each Comp In Components
Total = Total + 1
Uid = CStr(Comp.UID)
Uid = QPComponentIdentityKey(Comp)
If Seen.Exists(Uid) Then Err.Raise vbObjectError + 874, _
"QuickPartReplace.Target", "duplicate component UID"
Seen.Add Uid, True
@@ -1471,13 +1577,15 @@ Function QPSnapshotComponent(ByRef Comp)
Dim ErrorNumber
Dim ErrorSource
Dim ErrorDescription
Dim Uid, Pins
Dim Uid, Identity, Pins
Uid = CStr(Comp.UID)
Identity = Uid
If Len(Trim(Uid)) = 0 Then Identity = "@REFDES|" & Trim(CStr(Comp.Refdes))
Set Point = Comp.GetLocation()
Set Pins = QPComponentPins(Uid)
Connections = QPConnectionSignatureFromPins(Uid, Pins)
Result = "C" & vbTab & QPEncode(Uid) & vbTab & _
Result = "C" & vbTab & QPEncode(Identity) & vbTab & _
QPEncode(CStr(Comp.Refdes)) & vbTab & QPEncode(CStr(Point.X)) & _
vbTab & QPEncode(CStr(Point.Y)) & vbTab & _
QPEncode(CStr(Comp.Orientation)) & vbTab & QPEncode(CStr(Comp.Scale)) & _
@@ -1488,7 +1596,7 @@ Function QPSnapshotComponent(ByRef Comp)
AttributeText = ""
AttributeCount = 0
For Each Attr In Comp.Attributes
AttributeText = AttributeText & QPSnapshotAttribute(Uid, Attr)
AttributeText = AttributeText & QPSnapshotAttribute(Identity, Attr)
If Err.Number <> 0 Then Exit For
AttributeCount = AttributeCount + 1
QPProgress "snapshot_attributes", AttributeCount, 0
@@ -1530,7 +1638,7 @@ Function QPSortSnapshotLines(ByVal Text)
End Function
Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr)
Dim Instance, Point, ErrorNumber, ErrorDescription
Dim Instance, Value, Point, ErrorNumber, ErrorDescription
Dim LocationX
Dim LocationY
Dim Color
@@ -1539,24 +1647,40 @@ Function QPSnapshotAttribute(ByVal ComponentUid, ByRef Attr)
Dim Orientation
Dim Visible
Instance = Attr.InstanceValue
Instance = ""
Value = ""
On Error Resume Next
Err.Clear
Instance = CStr(Attr.InstanceValue)
If Err.Number <> 0 Then
Instance = ""
QPLog QP_Root, "WARN", "optional_attribute_unavailable field=instance_value"
End If
Err.Clear
Value = CStr(Attr.Value)
If Err.Number <> 0 Then
Value = ""
QPLog QP_Root, "WARN", "optional_attribute_unavailable field=value"
End If
Err.Clear
Set Point = Attr.GetLocation()
LocationX = CStr(Point.X)
LocationY = CStr(Point.Y)
ErrorNumber = Err.Number
ErrorDescription = Err.Description
If Err.Number = 0 And Not Point Is Nothing Then
LocationX = CStr(Point.X)
LocationY = CStr(Point.Y)
Else
LocationX = "0"
LocationY = "0"
QPLog QP_Root, "WARN", "optional_display_field_unavailable field=location"
End If
Err.Clear
On Error GoTo 0
If ErrorNumber <> 0 Then Err.Raise ErrorNumber, "QuickPartReplace.Snapshot", _
"Cannot capture " & CStr(Attr.Name) & ".location: " & ErrorDescription
Color = QPOptionalAttributeField(Attr, "color")
Font = QPOptionalAttributeField(Attr, "font")
Size = QPOptionalAttributeField(Attr, "size")
Orientation = QPOptionalAttributeField(Attr, "orientation")
Visible = QPOptionalAttributeField(Attr, "visible")
QPSnapshotAttribute = "A" & vbTab & QPEncode(ComponentUid) & vbTab & _
QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(CStr(Attr.Value)) & _
QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(Value) & _
vbTab & QPEncode(CStr(Instance)) & vbTab & QPEncode(LocationX) & _
vbTab & QPEncode(LocationY) & vbTab & QPEncode(Color) & _
vbTab & QPEncode(Font) & vbTab & QPEncode(Size) & vbTab & _
@@ -1598,8 +1722,9 @@ Function QPOptionalAttributeField(ByRef Attr, ByVal FieldName)
Err.Clear
On Error GoTo 0
If ErrorNumber <> 0 Then
Err.Raise ErrorNumber, "QuickPartReplace.Snapshot", _
"Cannot capture " & CStr(Attr.Name) & "." & FieldName & ": " & ErrorDescription
QPOptionalAttributeField = "0"
QPLog QP_Root, "WARN", "optional_display_field_unavailable field=" & CStr(FieldName) & _
" description=" & CStr(ErrorDescription)
Else
QPOptionalAttributeField = Value
End If
@@ -2597,8 +2722,16 @@ Function QPFindCurrentAttribute(ByRef Comp, ByVal Name)
End Function
Function QPRequiredAttributeValue(ByRef Comp, ByVal Name)
Dim Attr
Set Attr = QPFindCurrentAttribute(Comp, Name)
Dim Attr, ErrorNumber, ErrorSource, ErrorDescription
On Error Resume Next
Err.Clear
Set Attr = Comp.FindAttribute(CStr(Name))
ErrorNumber = Err.Number
ErrorSource = Err.Source
ErrorDescription = Err.Description
Err.Clear
On Error GoTo 0
If ErrorNumber <> 0 Then Err.Raise ErrorNumber, ErrorSource, ErrorDescription
If Attr Is Nothing Then
Err.Raise vbObjectError + 806, "QuickPartReplace", _
"required attribute missing: " & CStr(Name)