fix: isolate component failures and recover owned CAD resources

This commit is contained in:
阿白叔 committed 2026-11-06 14:56:53 +08:00
1 parent a8da30aad2
commit 4db5f05531
87 files changed
+6688 -301

No files matched your search

@@ -108,7 +108,10 @@ Sub QPValidateNativeSelectedState(ByVal State, ByVal Uid)
If UBound(Fields) <> 11 Then QPNativeReadInvalid "invalid selected attribute fields"
If Fields(0) <> "A" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected attribute owner mismatch"
Name = QPDecode(Fields(2))
If Len(Name) = 0 Or Names.Exists(Name) Then QPNativeReadInvalid "duplicate or empty selected attribute name"
' Raw A rows remain in the native frame even when values are identical.
' Value/InstanceValue do not prove Block/Symbol hierarchy ownership.
Name = LCase(Name)
If Len(Name) = 0 Or Names.Exists(Name) Then QPNativeReadInvalid "ambiguous selected attribute ownership; raw occurrences retained; no layer writes"
Names.Add Name, True
Next
End Sub
+117 -5
View File
@@ -726,7 +726,8 @@ Sub QPDiscoverInventory(ByRef Documents)
Text = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & "old_part" & vbCrLf
ComponentMode = QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root, "component-inventory.request"))
ComponentText = "project_path" & vbTab & "schematic_name" & vbTab & "sheet_number" & vbTab & _
"uid" & vbTab & "refdes" & vbTab & "old_part" & vbTab & "old_value" & vbCrLf
"uid" & vbTab & "refdes" & vbTab & "old_part" & vbTab & "old_value" & vbTab & _
"actual_part_label" & vbTab & "actual_cell" & vbCrLf
Count = 0
Set Schematics = Documents.GetAvailableSchematics()
For Each Schematic In Schematics
@@ -763,13 +764,16 @@ Sub QPDiscoverInventory(ByRef Documents)
PartKey = LCase(CStr(PartNumber))
If Requested.Exists(PartKey) Then
If ComponentMode Then
QPRequireUniqueAttributeNames Comp
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(Uid) & vbTab & QPEncode(CStr(Comp.Refdes)) & vbTab & _
QPEncode(CStr(PartNumber)) & vbTab & QPEncode(CStr(ValueText)) & vbCrLf
QPEncode(CStr(PartNumber)) & vbTab & QPEncode(CStr(ValueText)) & vbTab & _
QPEncode(QPEffectiveComponentAttribute(Comp, "Part Label")) & vbTab & _
QPEncode(QPEffectiveComponentAttribute(Comp, "Cell Name")) & vbCrLf
End If
HitKey = QPSheetKey(Schematic, Sheet) & Chr(30) & PartKey
If Not Seen.Exists(HitKey) Then
@@ -979,7 +983,7 @@ Sub QPDiscardFailedBatchProject(ByRef ErrorDescription)
End Sub
Sub QPFinishBatchItem()
Dim Command, SnapshotPath, Records, Plans, ValueStates, Index, Record, Candidate, TargetPath, FinalState
Dim Command, SnapshotPath, Records, Plans, ValueStates, Index, Record, Candidate, TargetPath, FinalState, ValueReportState
If QP_BatchState <> "restored_ok" And QP_BatchState <> "no_match" Then _
Err.Raise vbObjectError + 871, "QuickPartReplace.Batch", "worker has no verified terminal result"
FinalState = QP_BatchState
@@ -1025,6 +1029,9 @@ Sub QPFinishBatchItem()
Next
QPVerifyRestoredPlans Command, Records, Plans, ValueStates
QPWriteVerifiedState Command
ValueReportState = "saved_ok"
If QP_Fso.FileExists(QP_Fso.BuildPath(QP_Root, "recovery.request")) Then ValueReportState = "restored_ok"
QPWriteValueReadback Command, Records, Plans, ValueReportState
If Not QP_SessionMode Then QPCloseBatchProject TargetPath, False
FinalState = "saved_ok"
QPLog QP_Root, "INFO", "saved_readback_verified job=" & QP_CurrentJobId
@@ -1295,7 +1302,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Dim PartNumber
Dim Snapshot
Dim Index
Dim Selectors, Selector, Uid, ValueAttr, ValueText, MatchedUids, UntouchedCount, NativeRead
Dim Selectors, Selector, Uid, ValueAttr, ValueText, MatchedUids, UntouchedCount, NativeRead, AlreadyTargets
QP_FallbackIdentityUsed = False
Set QP_FallbackIdentityWarnings = CreateObject("Scripting.Dictionary")
@@ -1305,6 +1312,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
QPInvalidateComponentIndex
QPProgress "query_components", 0, 0
Set Matches = CreateObject("Scripting.Dictionary")
Set AlreadyTargets = CreateObject("Scripting.Dictionary")
QP_Stage = "query_components"
QPLog QP_Root, "INFO", "stage=query_components"
Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL)
@@ -1318,22 +1326,35 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
If StrComp(PartNumber, CStr(Command("old_part_number")), _
vbTextCompare) = 0 Then Matches.Add CStr(Matches.Count), Comp
ElseIf Selectors.Exists(Uid) Then
QPRequireUniqueAttributeNames Comp
PartNumber = QPRequiredAttributeValue(Comp, "Part Number")
Selector = Selectors.Item(Uid)
ValueText = ""
Set ValueAttr = QPFindCurrentAttribute(Comp, "Value")
If Not ValueAttr Is Nothing Then ValueText = QPEffectiveAttributeValue(ValueAttr)
If StrComp(PartNumber, CStr(Command("new_part_number")), vbTextCompare) = 0 Then
QPRequireAlreadyTargetPolicy Comp, Command, Selector
AlreadyTargets.Add Uid, True
Else
If CStr(Comp.Refdes) <> CStr(Selector(0)) Or _
StrComp(PartNumber, CStr(Command("old_part_number")), vbTextCompare) <> 0 Or _
ValueText <> CStr(Selector(1)) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component changed since inventory"
Matches.Add CStr(Matches.Count), Comp
End If
End If
QPProgress "find_components", Matches.Count, 0
Next
If Not Selectors Is Nothing Then
If Matches.Count <> Selectors.Count Then _
If Matches.Count + AlreadyTargets.Count <> Selectors.Count Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "selected component is missing"
If AlreadyTargets.Count > 0 Then
If Matches.Count > 0 Then Err.Raise vbObjectError + 880, "QuickPartReplace.Target", _
"legacy group mixes source and already-target components; explicit per-component repair required"
QPWriteStatus QP_Root, CStr(Command("job_id")), "no_match", "already_target", _
"Selected target UID already has requested Part Number and verified Value/Cell policy; skipped without writes."
Exit Sub
End If
End If
If Matches.Count = 0 Then
If Not Selectors Is Nothing Then _
@@ -1362,6 +1383,7 @@ Sub QPCaptureAndSelect(ByRef Command, ByVal SnapshotPath)
Set MatchedUids = CreateObject("Scripting.Dictionary")
For Index = 0 To Matches.Count - 1
Set Comp = Matches.Item(CStr(Index))
QPRequireUniqueAttributeNames Comp
MatchedUids.Add QPComponentIdentityKey(Comp), True
Next
Set NativeRead = Nothing
@@ -1901,6 +1923,7 @@ Sub QPRestoreAfterOperatorReplace(ByRef Command, ByVal SnapshotPath)
" Target Cell verified: " & QPTargetCell(Command) & "."
If Len(QP_NcSyncSummary) > 0 Then SuccessMessage = SuccessMessage & _
" NC synced: " & QP_NcSyncSummary
If Not QP_BatchMode Then QPWriteValueReadback Command, Records, Plans, "restored_ok"
QPWriteStatus QP_Root, CStr(Command("job_id")), "restored_ok", "", SuccessMessage
QPLog QP_Root, "INFO", "verified replacement count=" & Plans.Count & " value_mode=" & QPValueMode(Command)
End Sub
@@ -2065,11 +2088,13 @@ Sub QPRecoverSavedJob(ByRef Command, ByVal SnapshotPath)
' Closing without saving avoids a second save/reopen cycle for no writes.
QPRequireCommandSnapshot Command, SnapshotPath
QPWriteVerifiedState Command
QPWriteValueReadback Command, Records, Plans, "restored_ok"
QPCloseBatchProject CStr(Command("project_path")), False
QP_BatchMode = False
QPWriteStatus QP_Root, QP_CurrentJobId, "restored_ok", "recovery_saved_verified", _
"Saved replacement verified from disk and owned project closed without additional saving."
Else
QPWriteValueReadback Command, Records, Plans, "restored_ok"
QPWriteStatus QP_Root, QP_CurrentJobId, "restored_ok", "recovery_verified", "Saved replacement already passes all restoration checks; no CAD writes."
End If
Else
@@ -2122,6 +2147,7 @@ Sub QPRestorePreflight(ByRef Comp, ByRef ComponentRecord, _
Dim CurrentConnections
Dim SavedConnections
QPRequireUniqueAttributeNames Comp
If QPComponentIdentityKey(Comp) <> CStr(ComponentRecord("uid")) Or _
CStr(Comp.Refdes) <> CStr(ComponentRecord("refdes")) Then _
Err.Raise vbObjectError + 831, "QuickPartReplace", "replacement component UID or Refdes differs from snapshot; no restoration writes allowed"
@@ -2814,6 +2840,92 @@ Function QPFindCurrentAttribute(ByRef Comp, ByVal Name)
Set QPFindCurrentAttribute = Attr
End Function
Function QPAttributeObservations(ByRef Comp)
Dim Attr, Ordinal, Text, Uid
Uid = QPComponentIdentityKey(Comp)
Text = ""
Ordinal = 0
' API properties are observations, not proven Block/Symbol ownership.
For Each Attr In Comp.Attributes
Text = Text & "E" & vbTab & QPEncode(Uid) & vbTab & CStr(Ordinal) & vbTab & _
QPEncode(CStr(Attr.Name)) & vbTab & QPEncode(CStr(Attr.Value)) & vbTab & _
QPEncode(CStr(Attr.InstanceValue)) & vbTab & "unknown" & vbCrLf
Ordinal = Ordinal + 1
Next
QPAttributeObservations = Text
End Function
Sub QPRequireUniqueAttributeNames(ByRef Comp)
Dim Attr, Names, Name, Evidence, Ambiguous
Evidence = QPAttributeObservations(Comp)
Evidence = "O" & vbTab & QPEncode(CStr(QP_CurrentJobId)) & vbTab & _
QPEncode(QPComponentIdentityKey(Comp)) & vbTab & QPEncode(CStr(QP_Stage)) & vbTab & CStr(Timer) & vbCrLf & Evidence
QPAppendAscii QP_Fso.BuildPath(QP_Root, "attribute-observations-" & CStr(QP_CurrentJobId) & ".tsv"), Evidence
Set Names = CreateObject("Scripting.Dictionary")
Ambiguous = False
For Each Attr In Comp.Attributes
Name = LCase(CStr(Attr.Name))
If Names.Exists(Name) Then
Ambiguous = True
Else
Names.Add Name, True
End If
Next
If Ambiguous Then Err.Raise vbObjectError + 880, "QuickPartReplace.AttributeSource", _
"attribute ownership ambiguous; every occurrence retained with unknown Block/Symbol layer; no speculative writes allowed"
End Sub
Sub QPWriteValueReadback(ByRef Command, ByRef Records, ByRef Plans, ByVal State)
Dim Text, Index, Comp, Attr, Present, Value, Selectors, Schematic, Sheet, SnapshotPath
Selectors = "": Schematic = "": Sheet = ""
If Command.Exists("target_selectors") Then Selectors = QPDecode(CStr(Command("target_selectors")))
If Command.Exists("schematic_name") Then Schematic = CStr(Command("schematic_name"))
If Command.Exists("sheet_number") Then Sheet = CStr(Command("sheet_number"))
SnapshotPath = QPJobSnapshotPath(CStr(Command("job_id")))
Text = "V" & vbTab & "2" & vbTab & QPEncode(CStr(Command("task_id"))) & vbTab & _
QPEncode(CStr(Command("job_id"))) & vbTab & QPEncode(State) & vbTab & _
QPEncode(CStr(Command("project_path"))) & vbTab & QPEncode(Schematic) & vbTab & QPEncode(Sheet) & vbTab & _
QPEncode(CStr(Command("old_part_number"))) & vbTab & QPEncode(CStr(Command("new_part_number"))) & vbTab & _
QPEncode(QPValueMode(Command)) & vbTab & QPEncode(QPCustomValue(Command)) & vbTab & _
QPBooleanText(QPSyncNc(Command)) & vbTab & QPEncode(QPTargetCell(Command)) & vbTab & _
QPEncode(Selectors) & vbTab & QPEncode(QPReadUtf8(SnapshotPath)) & vbCrLf
For Index = 0 To Plans.Count - 1
Set Comp = Plans.Item(CStr(Index))
Set Attr = QPFindCurrentAttribute(Comp, "Value")
Present = Not (Attr Is Nothing)
Value = ""
If Present Then Value = QPEffectiveAttributeValue(Attr)
Text = Text & "C" & vbTab & QPEncode(QPComponentIdentityKey(Comp)) & vbTab & _
QPEncode(CStr(Comp.Refdes)) & vbTab & QPEncode(QPEffectiveComponentAttribute(Comp, "Part Number")) & vbTab & _
QPEncode(Value) & vbTab & QPBooleanText(Present) & vbTab & _
QPEncode(QPEffectiveComponentAttribute(Comp, "Cell Name")) & vbCrLf
Text = Text & QPAttributeObservations(Comp)
Next
QPWriteUtf8 QP_Fso.BuildPath(QP_Root, "value-readback-" & CStr(Command("job_id")) & ".tsv"), Text
End Sub
Sub QPRequireAlreadyTargetPolicy(ByRef Comp, ByRef Command, ByRef Selector)
Dim ExpectedValue, ActualValue, Mode, Cell
If CStr(Comp.Refdes) <> CStr(Selector(0)) Then _
Err.Raise vbObjectError + 873, "QuickPartReplace.Target", "already-target Refdes differs from frozen selector"
Mode = QPValueMode(Command)
Select Case Mode
Case "preserve": ExpectedValue = CStr(Selector(1))
Case "custom": ExpectedValue = QPCustomValue(Command)
Case "part_number": ExpectedValue = QPEffectiveComponentAttribute(Comp, "Part Number")
Case "part_label", "legacy_part_label"
ExpectedValue = QPEffectiveComponentAttribute(Comp, "Part Label")
If Len(ExpectedValue) = 0 Then Err.Raise vbObjectError + 880, "QuickPartReplace.Target", _
"already-target Part Label is unavailable; Value policy cannot be proven"
Case Else: Err.Raise vbObjectError + 880, "QuickPartReplace.Target", "already-target Value policy cannot be proven"
End Select
If QPSyncNc(Command) And QPValueHasNcMarker(CStr(Selector(1))) Then ExpectedValue = QPAppendNcMarker(ExpectedValue)
ActualValue = QPEffectiveComponentAttribute(Comp, "Value")
Cell = QPTargetCell(Command)
If ActualValue <> ExpectedValue Or (Len(Cell) > 0 And QPEffectiveComponentAttribute(Comp, "Cell Name") <> Cell) Then _
Err.Raise vbObjectError + 880, "QuickPartReplace.Target", "already-target Part Number has unresolved Value/Cell policy; no replacement or completion claimed"
End Sub
Function QPRequiredAttributeValue(ByRef Comp, ByVal Name)
Dim Attr, ErrorNumber, ErrorSource, ErrorDescription
On Error Resume Next