Files
yiyezhqiu-refactored/integration/dxdesigner795/expedition/ExpeditionBridge.vbs
T

489 lines
15 KiB
Plaintext

' Expedition PCB bridge for the assembly refdes tidy-up. Keep this file ASCII-only.
' Native migration of the legacy MoveAssemblyRef.vbs: the host exports fab
' texts and component footprints, the portable exe computes the plan, and the
' host applies it inside one undoable transaction.
Dim DxToolsHome
Dim DxExpeditionMenu
Call DxEpInitialize()
Sub DxEpInitialize()
Dim host
Set host = Nothing
On Error Resume Next
Set host = Application
Err.Clear
On Error GoTo 0
If host Is Nothing Then Exit Sub
Call DxToolsInstallExpeditionMenu()
If DxToolsHome = "" Then Exit Sub
On Error Resume Next
Scripting.DontExit = True
Err.Clear
On Error GoTo 0
End Sub
Sub DxToolsInstallExpeditionMenu()
Dim menuBar
Dim existing
Dim button
Dim i
Dim caption
DxToolsHome = DxEpResolveRoot()
If DxToolsHome = "" Then
MsgBox "Run Start in DxDesignerTools.exe before importing this script.", _
vbExclamation
Exit Sub
End If
caption = "Dx " & ChrW(&H6548) & ChrW(&H7387) & ChrW(&H5DE5) & ChrW(&H5177)
Set menuBar = Application.Gui.CommandBars("Document Menu Bar")
For i = menuBar.Controls.Count To 1 Step -1
Set existing = menuBar.Controls.Item(i)
If existing.Caption = caption Then
On Error Resume Next
existing.Delete
Err.Clear
On Error GoTo 0
End If
Next
Set DxExpeditionMenu = menuBar.Controls.Add(1) ' cmdControlPopup
DxExpeditionMenu.Caption = caption
Set button = DxExpeditionMenu.Controls.Add
button.Caption = ChrW(&H81EA) & ChrW(&H52A8) & ChrW(&H8C03) & _
ChrW(&H6574) & ChrW(&H88C5) & ChrW(&H914D) & ChrW(&H5C42) & _
ChrW(&H4F4D) & ChrW(&H53F7)
button.Target = ScriptEngine
button.ExecuteMethod = "DxEpAssemblyMenuCommand"
End Sub
Sub DxEpAssemblyMenuCommand(nID)
Call DxExpeditionRunAssemblyRefdes()
End Sub
Sub DxExpeditionRunAssemblyRefdes()
Dim app
Dim docObj
Dim requestText
Dim planText
Dim planPath
Dim requestPath
Dim fso
Dim shell
Set app = Application
Set docObj = DxEpGetLicensedDoc(app)
If docObj Is Nothing Then Exit Sub
Set fso = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")
requestPath = shell.ExpandEnvironmentStrings( _
"%TEMP%\dx-assembly-request.txt")
planPath = shell.ExpandEnvironmentStrings("%TEMP%\dx-assembly-plan.txt")
requestText = DxEpExportRequest(docObj)
Call DxEpWriteAscii(requestPath, requestText)
Dim exec
Dim commandLine
commandLine = DxEpQuote(DxToolsHome & "\DxDesignerTools.exe") & _
" compute assembly-refdes --request " & DxEpQuote(requestPath) & _
" --output " & DxEpQuote(planPath)
On Error Resume Next
Set exec = shell.Exec(commandLine)
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
MsgBox "Cannot run DxDesignerTools.exe", vbExclamation
Exit Sub
End If
Do While exec.Status = 0
Call DxEpSleep(50)
Loop
On Error GoTo 0
If fso.FileExists(planPath) Then
planText = DxEpReadAscii(planPath)
Else
planText = ""
End If
Call DxEpApplyPlan(docObj, app, planText)
End Sub
Function DxEpExportRequest(docObj)
Dim fabtxts
Dim fabtxt
Dim lines
Dim textIndex
Dim componentLines
Dim places
Dim place
Dim pa
Dim dx
Dim dy
Dim sorted
Dim x
Dim y
Dim temp
Dim spans
Dim componentKey
Dim orientation
Dim hasComponent
Dim assemblyFlag
Set spans = CreateObject("Scripting.Dictionary")
lines = ""
componentLines = ""
textIndex = 0
Set fabtxts = docObj.FabricationLayerTexts( _
epcbFabAll, epcbSelectAll, epcbSideTopOrBottom, True)
For Each fabtxt In fabtxts
componentKey = ""
orientation = ""
hasComponent = False
On Error Resume Next
If Not fabtxt.Component Is Nothing Then
hasComponent = True
componentKey = CStr(fabtxt.Component.Name)
If Err.Number <> 0 Then
Err.Clear
componentKey = ""
End If
orientation = CStr(fabtxt.Component.Orientation)
If Err.Number <> 0 Then
Err.Clear
orientation = ""
End If
If componentKey <> "" And Not spans.Exists(componentKey) Then
fabtxt.Component.Selected = True
Err.Clear
Set places = docObj.PlacementOutlines(epcbSelectSelected)
If Err.Number <> 0 Then
Err.Clear
Set places = Nothing
End If
dx = 0
dy = 0
If Not places Is Nothing Then
For Each place In places
pa = place.Geometry.Outline
sorted = False
Do While Not sorted
sorted = True
For x = 0 To UBound(pa, 2) - 1
If pa(0, x) > pa(0, x + 1) Then
temp = pa(0, x + 1)
pa(0, x + 1) = pa(0, x)
pa(0, x) = temp
sorted = False
End If
Next
Loop
dx = (pa(0, UBound(pa, 2)) - pa(0, 0)) / 2
sorted = False
Do While Not sorted
sorted = True
For y = 0 To UBound(pa, 2) - 1
If pa(1, y) > pa(1, y + 1) Then
temp = pa(1, y + 1)
pa(1, y + 1) = pa(1, y)
pa(1, y) = temp
sorted = False
End If
Next
Loop
dy = (pa(1, UBound(pa, 2)) - pa(1, 0)) / 2
Next
End If
fabtxt.Component.Selected = False
Err.Clear
If componentLines <> "" Then componentLines = _
componentLines & vbCrLf
componentLines = componentLines & "C|" & componentKey & _
"|" & CStr(fabtxt.Component.PositionX) & "|" & _
CStr(fabtxt.Component.PositionY) & "|" & _
CStr(dx) & "|" & CStr(dy)
If Err.Number <> 0 Then Err.Clear
spans.Add componentKey, True
End If
End If
Err.Clear
On Error GoTo 0
If lines <> "" Then lines = lines & vbCrLf
assemblyFlag = "0"
If fabtxt.Type = epcbFabAssembly Then assemblyFlag = "1"
lines = lines & "T|" & textIndex & _
"|" & CStr(fabtxt.TextType) & _
"|" & CStr(fabtxt.Side) & _
"|" & assemblyFlag & _
"|" & fabtxt.TextString & _
"|" & orientation & _
"|" & componentKey
textIndex = textIndex + 1
Next
DxEpExportRequest = lines & vbCrLf & componentLines & vbCrLf
End Function
Sub DxEpApplyPlan(docObj, app, planText)
Dim fabtxts
Dim fabtxt
Dim index
Dim lines
Dim line
Dim parts
Dim planLines
Set planLines = CreateObject("Scripting.Dictionary")
lines = Split(Replace(CStr(planText), vbCr, ""), vbLf)
For Each line In lines
If Len(line) > 0 Then
parts = Split(line, "|")
If UBound(parts) >= 1 Then
If Not planLines.Exists(CStr(parts(1))) Then
planLines.Add CStr(parts(1)), line
End If
End If
End If
Next
docObj.TransactionStart (epcbDRCModeDRC)
app.LockServer
index = 0
Set fabtxts = docObj.FabricationLayerTexts( _
epcbFabAll, epcbSelectAll, epcbSideTopOrBottom, True)
For Each fabtxt In fabtxts
If planLines.Exists(CStr(index)) Then
Call DxEpApplyLine(planLines(CStr(index)), fabtxt)
End If
index = index + 1
Next
app.UnlockServer
docObj.TransactionEnd (True)
End Sub
Sub DxEpApplyLine(line, fabtxt)
Dim parts
parts = Split(line, "|")
On Error Resume Next
If parts(0) = "DELETE" Then
fabtxt.Delete()
ElseIf parts(0) = "MOVE" Then
fabtxt.Format.VerticalJust = epcbJustifyVCenter
fabtxt.Format.HorizontalJust = epcbJustifyHCenter
fabtxt.Format.Font = parts(6)
If parts(5) <> "" Then
fabtxt.Format.Height(epcbUnitMM) = CDbl(parts(5))
End If
fabtxt.Format.Orientation(epcbAngleUnitDegrees) = CDbl(parts(4))
' Blank height represents the legacy nonassembly/cardinal branch:
' change text format while retaining the existing text position.
If parts(5) <> "" Then
Call fabtxt.Move(CDbl(parts(2)), CDbl(parts(3)))
End If
End If
Err.Clear
On Error GoTo 0
End Sub
Sub DxEpWriteAscii(path, text)
Dim stream
Set stream = CreateObject("Scripting.FileSystemObject"). _
OpenTextFile(CStr(path), 2, True, 0)
stream.Write CStr(text)
stream.Close
End Sub
Function DxEpQuote(value)
Dim text, expression
text = CStr(value)
Set expression = CreateObject("VBScript.RegExp")
expression.Pattern = "\\+$"
expression.Global = False
text = expression.Replace(text, "$&$&")
DxEpQuote = Chr(34) & Replace(text, Chr(34), _
Chr(92) & Chr(34)) & Chr(34)
End Function
Function DxEpReadAscii(path)
Dim stream
Set stream = CreateObject("Scripting.FileSystemObject"). _
OpenTextFile(CStr(path), 1, False, 0)
DxEpReadAscii = stream.ReadAll
stream.Close
End Function
Sub DxEpSleep(milliseconds)
On Error Resume Next
CreateObject("WScript.Shell").Run _
"%ComSpec% /d /s /c ping -n 1 -w 100 127.0.0.1 > nul", 0, True
Err.Clear
On Error GoTo 0
End Sub
Function DxEpResolveRoot()
Dim candidate
DxEpResolveRoot = ""
On Error Resume Next
candidate = CStr(WScript.ScriptFullName)
Err.Clear
On Error GoTo 0
DxEpResolveRoot = DxEpRootForScript(candidate)
If DxEpResolveRoot <> "" Then Exit Function
candidate = ""
On Error Resume Next
candidate = CStr(Scripting.CurrentScriptFullName)
Err.Clear
On Error GoTo 0
DxEpResolveRoot = DxEpRootForScript(candidate)
If DxEpResolveRoot <> "" Then Exit Function
candidate = ""
On Error Resume Next
candidate = CStr(Scripting.ScriptFullName)
Err.Clear
On Error GoTo 0
DxEpResolveRoot = DxEpRootForScript(candidate)
If DxEpResolveRoot <> "" Then Exit Function
DxEpResolveRoot = DxEpRegisteredRoot()
End Function
Function DxEpRootForScript(path)
Dim fso, root
DxEpRootForScript = ""
If CStr(path) = "" Then Exit Function
Set fso = CreateObject("Scripting.FileSystemObject")
' expedition\<script> -> tool root is two levels up.
root = fso.GetParentFolderName(fso.GetParentFolderName(CStr(path)))
If fso.FileExists(fso.BuildPath(root, "DxDesignerTools.exe")) And _
fso.FileExists(root & "\expedition\ExpeditionBridge.vbs") Then
DxEpRootForScript = root
End If
End Function
Function DxEpRegisteredRoot()
Dim path
path = CreateObject("WScript.Shell").ExpandEnvironmentStrings( _
"%LOCALAPPDATA%\DxProductivity\startup-refactored\startup.json")
DxEpRegisteredRoot = DxEpStartupRootFromFile(path)
End Function
Function DxEpStartupRootFromFile(path)
Dim fso, stream, text, root
DxEpStartupRootFromFile = ""
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(CStr(path)) Then Exit Function
Set stream = Nothing
On Error Resume Next
Set stream = CreateObject("ADODB.Stream")
stream.Type = 2
stream.Charset = "utf-8"
stream.Open
stream.LoadFromFile CStr(path)
text = stream.ReadText
If Err.Number <> 0 Then
Err.Clear
If Not stream Is Nothing Then stream.Close
Err.Clear
On Error GoTo 0
Exit Function
End If
stream.Close
On Error GoTo 0
If Not DxEpJsonString(text, "tool_root", root) Then Exit Function
If fso.FileExists(fso.BuildPath(root, "DxDesignerTools.exe")) And _
fso.FileExists(root & "\expedition\ExpeditionBridge.vbs") Then
DxEpStartupRootFromFile = root
End If
End Function
Function DxEpJsonString(jsonText, key, ByRef value)
Dim marker, markerAt, colonAt, quoteAt, index, character, escaped
Dim hexValue, expression
DxEpJsonString = False
marker = Chr(34) & CStr(key) & Chr(34)
markerAt = InStr(1, CStr(jsonText), marker, vbBinaryCompare)
If markerAt = 0 Then Exit Function
colonAt = InStr(markerAt + Len(marker), CStr(jsonText), ":")
If colonAt = 0 Then Exit Function
quoteAt = colonAt + 1
Do While quoteAt <= Len(CStr(jsonText))
character = Mid(CStr(jsonText), quoteAt, 1)
If InStr(" " & vbTab & vbCr & vbLf, character) = 0 Then Exit Do
quoteAt = quoteAt + 1
Loop
If Mid(CStr(jsonText), quoteAt, 1) <> Chr(34) Then Exit Function
escaped = False
value = ""
For index = quoteAt + 1 To Len(CStr(jsonText))
character = Mid(CStr(jsonText), index, 1)
If escaped Then
Select Case character
Case Chr(34), Chr(92), "/"
value = value & character
Case "r"
value = value & vbCr
Case "n"
value = value & vbLf
Case "t"
value = value & vbTab
Case "b"
value = value & Chr(8)
Case "f"
value = value & Chr(12)
Case "u"
hexValue = Mid(CStr(jsonText), index + 1, 4)
Set expression = CreateObject("VBScript.RegExp")
expression.Pattern = "^[0-9A-Fa-f]{4}$"
If Not expression.Test(hexValue) Then Exit Function
value = value & ChrW(CLng("&H" & hexValue))
index = index + 4
Case Else
Exit Function
End Select
escaped = False
ElseIf character = Chr(92) Then
escaped = True
ElseIf character = Chr(34) Then
DxEpJsonString = True
Exit Function
Else
value = value & character
End If
Next
End Function
Function DxEpGetLicensedDoc(app)
On Error Resume Next
Dim key, licenseServer, licenseToken, docObj
Set DxEpGetLicensedDoc = Nothing
Set docObj = app.ActiveDocument
If Err.Number <> 0 Then
Call app.Gui.StatusBarText( _
"No active document: " & Err.Description, epcbStatusFieldError)
Err.Clear
On Error GoTo 0
Exit Function
End If
key = docObj.Validate(0)
Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
licenseToken = licenseServer.GetToken(key)
Set licenseServer = Nothing
Err.Clear
docObj.Validate (licenseToken)
If Err.Number <> 0 Then
Call app.Gui.StatusBarText( _
"No active document license: " & Err.Description, _
epcbStatusFieldError)
Err.Clear
On Error GoTo 0
Exit Function
End If
Set DxEpGetLicensedDoc = docObj
On Error GoTo 0
End Function