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

339 lines
10 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
Sub DxToolsInstallExpeditionMenu()
Dim menuBar
Dim existing
Dim button
Dim i
Dim caption
DxToolsHome = DxEpResolveRoot()
If DxToolsHome = "" Then
MsgBox "DxDesignerTools.exe not found next to this script.", _
vbExclamation
Exit Sub
End If
caption = "Dx " & ChrW(&H6548) & ChrW(&H7387) & ChrW(&H5DE5) & ChrW(&H5177)
Set menuBar = 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(10) ' 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.OnAction = "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 = DxToolsQuote(DxToolsHome & "\DxDesignerTools.exe") & _
" compute assembly-refdes --request " & DxToolsQuote(requestPath) & _
" --output " & DxToolsQuote(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
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
lines = lines & "T|" & textIndex & _
"|" & CStr(fabtxt.TextType) & _
"|" & CStr(fabtxt.Side) & _
"|" & IIf(fabtxt.Type = epcbFabAssembly, "1", "0") & _
"|" & 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))
Call fabtxt.Move(CDbl(parts(2)), CDbl(parts(3)))
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 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 fso
Dim candidate
Set fso = CreateObject("Scripting.FileSystemObject")
DxEpResolveRoot = ""
On Error Resume Next
candidate = CStr(WScript.ScriptFullName)
If Err.Number <> 0 Then
Err.Clear
candidate = CStr(Scripting.ScriptFullName)
End If
On Error GoTo 0
If candidate <> "" Then
' expedition\<script> -> tool root is two levels up
DxEpResolveRoot = fso.GetParentFolderName( _
fso.GetParentFolderName(candidate))
If Not fso.FileExists(fso.BuildPath( _
DxEpResolveRoot, "DxDesignerTools.exe")) Then
DxEpResolveRoot = ""
End If
End If
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