' Read-only native-host adapter. Appended to the external worker at preparation. Function QPNativeReadUntouched(ByRef SelectedUids) Set QPNativeReadUntouched = QPNativeRead(SelectedUids, False) End Function Function QPNativeCapture(ByRef SelectedUids) Set QPNativeCapture = QPNativeRead(SelectedUids, True) End Function Function QPNativeRead(ByRef SelectedUids, ByVal CaptureSelected) Dim Token, Base, ScriptPath, ResultPath, Stream, CommandPath, Text Dim Project, Sheet, Guid, Safe, Result Set QPNativeRead = Nothing Project = QPProjectPath() Sheet = QPSheetIdentity() Guid = CStr(QP_App.Guid()) Token = CStr(QP_AttemptToken) & "-" & CStr(QP_ProgressSequence) & "-" & QP_Fso.GetTempName() Base = QP_Fso.BuildPath(QP_Root, "native-read-" & Token) ScriptPath = Base & ".vbs" ResultPath = Base & ".tsv" Safe = Not CBool(QP_NativeMutationPossible) And Not CBool(QP_RestoreTransactionOpen) And Not CBool(QP_NativeCallActive) Text = QPBuildNativeReadScript(QPReadUtf8(WScript.ScriptFullName), SelectedUids, _ Token, Project, Sheet, Guid, ResultPath, CStr(QP_AttemptToken), Safe, CaptureSelected) ' Generated source is ASCII; Unicode values are escaped with QPEncode. Set Stream = QP_Fso.CreateTextFile(ScriptPath, False, False) Stream.Write Text Stream.Close CommandPath = QP_Fso.GetFile(ScriptPath).ShortPath If Not QPNativeCommandPathSupported(CommandPath) Then QPLog QP_Root, "INFO", "native_read_path_unavailable; using external full read" Exit Function End If QPProgress "native_snapshot_read", 0, 0 If Not QP_App.ExecuteCommand("run " & CommandPath) Then _ Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script command failed" If Not QP_Fso.FileExists(ResultPath) Then _ Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "native script returned no complete result" If CaptureSelected Then Set Result = QPParseNativeCapture(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids) Else Set Result = QPParseNativeSnapshot(QPReadUtf8(ResultPath), Token, Project, Sheet, SelectedUids) End If If CStr(QP_App.Guid()) <> Guid Or QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then _ Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", "context changed after native read" QPProgress "native_snapshot_read", Result("total"), Result("total") QPLog QP_Root, "INFO", "native_snapshot_verified components=" & CStr(Result("total")) & " token=" & Token Set QPNativeRead = Result End Function Function QPNativeCommandPathSupported(ByVal Path) Dim Index, Code QPNativeCommandPathSupported = False For Index = 1 To Len(Path) Code = AscW(Mid(Path, Index, 1)) If Code <= 32 Or Code > 126 Or Code = 34 Then Exit Function Next QPNativeCommandPathSupported = (Len(Path) > 0) End Function Function QPParseNativeCapture(ByVal Text, ByVal Token, ByVal Project, ByVal Sheet, ByRef SelectedUids) Dim Lines, Index, Fields, State, Uid, Targets, BaseText, SeenEnd, Result, Last If Left(Text, 6) = "ERROR" & vbTab Then Set QPParseNativeCapture = QPParseNativeSnapshot(Text, Token, Project, Sheet, SelectedUids) Exit Function End If Lines = Split(Text, vbCrLf) Fields = Split(Lines(0), vbTab) If Fields(0) <> "NR2" Then QPNativeReadInvalid "missing selected capture header" Lines(0) = "NR1" & Mid(Lines(0), 4) Set Targets = CreateObject("Scripting.Dictionary") BaseText = Lines(0) & vbCrLf SeenEnd = False Last = UBound(Lines) If Lines(Last) = "" Then Last = Last - 1 For Index = 1 To Last Fields = Split(Lines(Index), vbTab) If Fields(0) = "T" Then If SeenEnd Or UBound(Fields) <> 2 Then QPNativeReadInvalid "invalid selected capture row" Uid = QPDecode(Fields(1)) If Not SelectedUids.Exists(Uid) Or Targets.Exists(Uid) Then _ QPNativeReadInvalid "duplicate or foreign selected capture UID" State = QPDecode(Fields(2)) QPValidateNativeSelectedState State, Uid Targets.Add Uid, State Else If Fields(0) = "END" Then SeenEnd = True BaseText = BaseText & Lines(Index) & vbCrLf End If Next If Targets.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected capture missing target" Set Result = QPParseNativeSnapshot(BaseText, Token, Project, Sheet, SelectedUids) Result.Add "selected", Targets Set QPParseNativeCapture = Result End Function Sub QPValidateNativeSelectedState(ByVal State, ByVal Uid) Dim Lines, Fields, Index, Names, Name If Right(State, 2) <> vbCrLf Then QPNativeReadInvalid "truncated selected component state" Lines = Split(State, vbCrLf) If UBound(Lines) < 2 Then QPNativeReadInvalid "selected component has no attributes" Fields = Split(Lines(0), vbTab) If UBound(Fields) <> 7 Then QPNativeReadInvalid "invalid selected component fields" If Fields(0) <> "C" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "selected component owner mismatch" Set Names = CreateObject("Scripting.Dictionary") For Index = 1 To UBound(Lines) - 1 Fields = Split(Lines(Index), vbTab) 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)) ' 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 Function QPTryNativeVerifyUntouched(ByRef Records) Dim Selected, Record, Index, Actual QPTryNativeVerifyUntouched = False QPRequireRecordsContext Records Set Selected = CreateObject("Scripting.Dictionary") For Index = 0 To Records("components").Count - 1 Set Record = Records("components").Item(CStr(Index)) Selected.Add CStr(Record("uid")), True Next Set Actual = QPNativeReadUntouched(Selected) If Actual Is Nothing Then Exit Function QPRequireRecordsContext Records QPCompareNativeUntouched Records, Actual QPTryNativeVerifyUntouched = True End Function Sub QPCompareNativeUntouched(ByRef Records, ByRef Actual) Dim Baseline, FullBaseline, Rows, Uid, Expected Set Baseline = Records("untouched") Set FullBaseline = Records("untouched_full") Set Rows = Actual("rows") If Actual("total") <> Baseline.Count + Records("components").Count Or Rows.Count <> Baseline.Count Then _ QPNativeReadInvalid "component count changed outside selected targets" For Each Uid In Baseline.Keys If Not Rows.Exists(Uid) Or Not FullBaseline.Exists(Uid) Then QPNativeReadInvalid "unselected component missing" Expected = CStr(Baseline(Uid)) & vbCrLf & "G" & vbTab & QPEncode(Uid) & vbTab & _ QPEncode(CStr(FullBaseline(Uid))) & vbCrLf If Rows(Uid) <> Expected Then QPNativeReadInvalid "unselected component changed" Next End Sub Function QPParseNativeSnapshot(ByVal Text, ByVal Token, ByVal Project, ByVal Sheet, ByRef SelectedUids) Dim Lines, Fields, Last, Index, Uid, Identity, State, Pair, Rows, Result, Total Set Rows = CreateObject("Scripting.Dictionary") Lines = Split(Text, vbCrLf) Last = UBound(Lines) If Last > 0 Then If Lines(Last) = "" Then Last = Last - 1 End If Fields = Split(Lines(0), vbTab) If Fields(0) = "ERROR" Then If UBound(Fields) <> 4 Then QPNativeReadInvalid "malformed error frame" If Fields(1) <> Token Then QPNativeReadInvalid "stale error token" Err.Raise CLng(Fields(2)), QPDecode(Fields(3)), QPDecode(Fields(4)) End If If UBound(Fields) <> 3 Then QPNativeReadInvalid "missing native header" If Fields(0) <> "NR1" Or Fields(1) <> Token Or QPDecode(Fields(2)) <> Project Or _ QPDecode(Fields(3)) <> Sheet Then QPNativeReadInvalid "native header context mismatch" If Last < 1 Then QPNativeReadInvalid "truncated native result" Fields = Split(Lines(Last), vbTab) If UBound(Fields) <> 2 Then QPNativeReadInvalid "missing completion frame" If Fields(0) <> "END" Or Fields(1) <> Token Then QPNativeReadInvalid "missing completion token" If Len(Fields(2)) = 0 Then QPNativeReadInvalid "invalid component count" For Index = 1 To Len(Fields(2)) If InStr(1, "0123456789", Mid(Fields(2), Index, 1), vbBinaryCompare) = 0 Then _ QPNativeReadInvalid "invalid component count" Next Total = CLng(Fields(2)) Index = 1 Do While Index < Last If Index + 1 >= Last Then QPNativeReadInvalid "incomplete U/G pair" Fields = Split(Lines(Index), vbTab) If UBound(Fields) <> 6 Then QPNativeReadInvalid "malformed identity row" If Fields(0) <> "U" Then QPNativeReadInvalid "unexpected native row" Uid = QPDecode(Fields(1)) If Len(Uid) = 0 Then QPNativeReadInvalid "empty component UID" If SelectedUids.Exists(Uid) Or Rows.Exists(Uid) Then QPNativeReadInvalid "duplicate or selected UID in unselected result" Identity = Lines(Index) Fields = Split(Lines(Index + 1), vbTab) If UBound(Fields) <> 2 Then QPNativeReadInvalid "malformed full-state row" If Fields(0) <> "G" Or QPDecode(Fields(1)) <> Uid Then QPNativeReadInvalid "full-state owner mismatch" State = QPDecode(Fields(2)) If Len(State) = 0 Or QPUntouchedIdentity(State) <> Identity Then QPNativeReadInvalid "full-state identity mismatch" Pair = Identity & vbCrLf & Lines(Index + 1) & vbCrLf Rows.Add Uid, Pair Index = Index + 2 Loop If Rows.Count + SelectedUids.Count <> Total Then QPNativeReadInvalid "component total mismatch" Set Result = CreateObject("Scripting.Dictionary") Result.Add "total", Total Result.Add "rows", Rows Text = "" For Each Uid In Rows.Keys Text = Text & Rows(Uid) Next Result.Add "text", Text Set QPParseNativeSnapshot = Result End Function Sub QPNativeReadInvalid(ByVal Description) Err.Raise vbObjectError + 875, "QuickPartReplace.NativeRead", Description End Sub Function QPNativeCollect(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid) QPNativeCollect = QPNativeCollectMode(SelectedUids, Token, Project, Sheet, Guid, False) End Function Function QPNativeCollectMode(ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal CaptureSelected) Dim Components, Comp, Uid, Seen, Found, Text, Count QPNativeRequireContext Project, Sheet, Guid QPInvalidatePinIndex Set Components = QP_App.ActiveView.Query(VDM_COMP, VD_ALL) Set Seen = CreateObject("Scripting.Dictionary") Set Found = CreateObject("Scripting.Dictionary") Text = "NR1" & vbTab & Token & vbTab & QPEncode(Project) & vbTab & QPEncode(Sheet) & vbCrLf If CaptureSelected Then Text = "NR2" & Mid(Text, 4) Count = 0 QPProgress "native_snapshot_read", 0, Components.Count For Each Comp In Components Uid = QPComponentIdentityKey(Comp) If Len(Uid) = 0 Or Seen.Exists(Uid) Then QPNativeReadInvalid "duplicate or empty component UID" Seen.Add Uid, True If SelectedUids.Exists(Uid) Then Found.Add Uid, True If CaptureSelected Then Text = Text & "T" & vbTab & QPEncode(Uid) & vbTab & QPEncode(QPSnapshotComponent(Comp)) & vbCrLf Else Text = Text & QPUntouchedComponent(Comp) End If Count = Count + 1 QPProgress "native_snapshot_read", Count, Components.Count Next If Found.Count <> SelectedUids.Count Then QPNativeReadInvalid "selected component missing from native read" QPNativeRequireContext Project, Sheet, Guid QPNativeCollectMode = Text & "END" & vbTab & Token & vbTab & CStr(Count) & vbCrLf End Function Sub QPNativeRequireContext(ByVal Project, ByVal Sheet, ByVal Guid) If CStr(QP_App.Guid()) <> Guid Then QPNativeReadInvalid "foreign CAD GUID" If QPProjectPath() <> Project Or QPSheetIdentity() <> Sheet Then QPNativeReadInvalid "native CAD context mismatch" End Sub Function QPBuildNativeSnapshotScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe) QPBuildNativeSnapshotScript = QPBuildNativeReadScript(Source, SelectedUids, Token, Project, Sheet, Guid, ResultPath, StopToken, Safe, False) End Function Function QPBuildNativeReadScript(ByVal Source, ByRef SelectedUids, ByVal Token, ByVal Project, ByVal Sheet, ByVal Guid, ByVal ResultPath, ByVal StopToken, ByVal Safe, ByVal CaptureSelected) Dim Header, Body, Names, Name, Uid, Pattern, Matches, MonitorJob Header = Left(Source, InStr(Source, "Set QP_Fso =") - 1) Set Pattern = CreateObject("VBScript.RegExp") Pattern.Global = True Pattern.MultiLine = True Pattern.Pattern = "^Const (VDM_COMP|VDM_COMPPIN|VD_ALL) = [^\r\n]*\r?\n" Header = Pattern.Replace(Header, "") MonitorJob = QP_ExpectedJobId If QP_SessionMode Then MonitorJob = QP_MonitorJobId Body = Header & vbCrLf & "Dim QP_NR_Selected, QP_NR_Text, QP_NR_Code, QP_NR_Source, QP_NR_Description, QP_NR_Safe" & vbCrLf & _ "Dim QP_NR_MonitorJob, QP_NR_CurrentJob, QP_NR_ParentSequence, QP_NR_Sequence, QP_NR_LastProgress" & vbCrLf & _ "Set QP_Fso=CreateObject(""Scripting.FileSystemObject"")" & vbCrLf & _ "QP_Root=" & QPNativeExpression(QP_Root) & vbCrLf & _ "QP_AttemptToken=" & QPNativeExpression(StopToken) & vbCrLf & _ "QP_NR_Safe=" & CStr(CBool(Safe)) & vbCrLf & _ "QP_NR_MonitorJob=" & QPNativeExpression(MonitorJob) & vbCrLf & _ "QP_NR_CurrentJob=" & QPNativeExpression(QP_ExpectedJobId) & vbCrLf & _ "QP_NR_ParentSequence=" & CStr(CLng(QP_ProgressSequence) + 1) & vbCrLf & _ "QP_NR_Sequence=0:QP_NR_LastProgress=-1" & vbCrLf & _ "Set QP_NR_Selected=CreateObject(""Scripting.Dictionary"")" & vbCrLf For Each Uid In SelectedUids.Keys Body = Body & "QP_NR_Selected.Add " & QPNativeExpression(Uid) & ",True" & vbCrLf Next Body = Body & "On Error Resume Next" & vbCrLf & _ "Set QP_App=GetObject(,""Viewdraw.Application"")" & vbCrLf & _ "If Err.Number=0 Then QP_NR_Text=QPNativeCollectMode(QP_NR_Selected," & _ QPNativeExpression(Token) & "," & QPNativeExpression(Project) & "," & QPNativeExpression(Sheet) & "," & QPNativeExpression(Guid) & "," & CStr(CBool(CaptureSelected)) & ")" & vbCrLf & _ "QP_NR_Code=Err.Number: QP_NR_Source=Err.Source: QP_NR_Description=Err.Description" & vbCrLf & _ "Err.Clear" & vbCrLf & _ "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 QPComponentIdentityKey QPInvalidatePinIndex QPConnectionSignatureFromPins QPAddSortedSignature QPSnapshotAttribute QPReadOptionalAttributeValue QPOptionalAttributeField QPSortSnapshotLines QPLog QPAppendAscii 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]*" Set Matches = Pattern.Execute(Source) If Matches.Count <> 1 Then QPNativeReadInvalid "missing native read routine: " & Name Body = Body & vbCrLf & Matches(0).Value & vbCrLf Next Body = Body & vbCrLf & "Sub QPProgress(ByVal Stage,ByVal Done,ByVal Total)" & vbCrLf & _ " Dim StopPath,Elapsed,CurrentTime,ProgressPath,Stream,SafeFlag" & vbCrLf & _ " If QP_NR_Safe And Len(QP_AttemptToken)>0 Then" & vbCrLf & _ " StopPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".stop"")" & vbCrLf & _ " If QP_Fso.FileExists(StopPath) Then" & vbCrLf & _ " If Trim(QPReadUtf8(StopPath))=QP_AttemptToken Then Err.Raise vbObjectError+860,""QuickPartReplace.SafeStop"",""native read stopped at safe boundary""" & vbCrLf & _ " End If" & vbCrLf & " End If" & vbCrLf & _ " If Len(QP_AttemptToken)=0 Then Exit Sub" & vbCrLf & _ " CurrentTime=Timer:Elapsed=CurrentTime-QP_NR_LastProgress" & vbCrLf & _ " If Elapsed<0 Then Elapsed=Elapsed+86400" & vbCrLf & _ " If QP_NR_LastProgress>=0 And Elapsed<1 And Not (Total>0 And Done=Total) Then Exit Sub" & vbCrLf & _ " SafeFlag=0:If QP_NR_Safe Then SafeFlag=1" & vbCrLf & _ " QP_NR_Sequence=QP_NR_Sequence+1" & vbCrLf & _ " ProgressPath=QP_Fso.BuildPath(QP_Root,""monitor-"" & QP_AttemptToken & "".native-progress"")" & vbCrLf & _ " On Error Resume Next" & vbCrLf & _ " Set Stream=QP_Fso.CreateTextFile(ProgressPath,True,False)" & vbCrLf & _ " Stream.WriteLine ""DX_NATIVE_PROGRESS"" & vbTab & QP_AttemptToken & vbTab & QP_NR_MonitorJob & vbTab & _" & vbCrLf & _ " CStr(QP_NR_ParentSequence) & vbTab & QP_NR_CurrentJob & vbTab & CStr(QP_NR_Sequence) & vbTab & _" & vbCrLf & _ " CStr(Done) & vbTab & CStr(Total) & vbTab & CStr(SafeFlag)" & vbCrLf & _ " Stream.Close:Err.Clear" & vbCrLf & " On Error GoTo 0" & vbCrLf & _ " QP_NR_LastProgress=CurrentTime" & vbCrLf & "End Sub" & vbCrLf QPBuildNativeReadScript = Body End Function Function QPNativeExpression(ByVal Value) QPNativeExpression = "QPDecode(" & Chr(34) & Replace(QPEncode(Value), Chr(34), Chr(34) & Chr(34)) & Chr(34) & ")" End Function