The script demonstrates how to:
Use the CreateObject method to launch an application
Check for a running process.
Check for an open document.
Use the Input Box.
Open, write to, and close a text file.
Open and write to an Excel file.
Access component properties.
Identify the block type of a component symbol.
Select a component.
Execute a command line command (zooms into the selected component).
Deselect all objects on a page.
Acquire the project directory.
Create a collection of component objects.
Launch a Windows based executable on a file.
You can use this script via any of the methods described in “Running Scripts”.
Mentor Graphics recommends that you use COM versioning syntax in script examples that use GetObject and CreateObject. Without COM versioning, the script will access the last installation to which the release switcher pointed.
Option Explicit
Dim vdapp, vddoc, vddocName, vdview, CurBlock, CurProjDir, Application
Dim OpenMySchematic, BadEntry, OpenSchematic, OpenSchematicCount,UboundOpenSch
Dim fso, CompRefXref
Dim CompRefdes, CompColl, CompObj, CompSymType, RefDesAttr, RefDesVal
Dim CompXLoc, CompYLoc, RefDesName, RefDesInstValue, RefDesSymbValue
Dim NoAppOpen, NoDocOpen
Dim VDM_COMP, VD_ALL, VDDT_SCHEMATIC
Dim CompCollCount, ProcCompCount
' Initializing counting variables
ProcCompCount = 0
' Seting up variables that will be used while checking for a running
' application, available document, and a prompt for the user for a
' schematic name.
NoAppOpen = True
NoDocOpen = True
BadEntry = True
'
' The following constants are known within Xpedition Designer and
' DO NOT need to be defined when the script is run from within
' Xpedition Designer. Running the script as a client using a debugger,
' Xpedition Designer doesn't understand it's'
' internal enumerated variables
' and their values, so we are providing them here.
'
' Once a script has been debugged, the intention is to run them
' within the Xpedition Designer application, it is not necessary to set
' the enumerations as they will be provided within the application.
'
VDM_COMP = 128
VD_ALL = 0
VDDT_SCHEMATIC = 0
' Set script to continue if an error occurs. This is required because
' there is "error trapping" code and we need the script, which will stop
' on an error, to continue to the error trapping code.
On Error Resume Next
' Check to see if the application is running. If it isn't
' prompt the user for a schematic name.
Set Application = GetObject(,"ViewDraw.Application")
If IsEmpty(Application) Then
MsgBox "Application is NOT running." & vbCrLf &_
"It will be launched after you click OK.",,"Empty Check"
' Launch the Xpedition Designer Application for X-ENTP VX.1 (COM Version
' 9)
Set vdapp = CreateObject("Viewdraw.Application.9")
' Set the application to visible (e.g. GUI is displayed, else the
' application runs in the "background".
vdapp.Visible = True
' The OpenProject method is an Application method. The msgbox has been
' placed here to allow the application to launch so that the Application ' method may be executed.
MsgBox "Wait for Xpedition Designer to Open",,"Pause"
' For the purpose of this example, the OpenProject method is hard coded.
' In actual application, the user may be prompted for the project file.
vdapp.OpenProject ("c:\MGTraining2007.1\solutions\DXD2007\DXD2007.prj")
' Scripting.AddTypeLibrary("Viewdraw.Application")
' Prompt the user for a schematic name. Loop on the dialog if
' they don't enter a name, or simply accept the default.
While NoAppOpen
If Not OpenMySchematic = "NameRequired" Or Not OpenMySchematic = "" Then
' Prompt user for the desired schematic name and page number.
OpenMySchematic = InputBox ("Schematic Name, Page Number (e.g. Top,1): ",_
"User Prompt","NameRequired",_
500, 500)
' Split the user input so the user input can be put in the appropriate
' format for the SchematicSheetDocuments.Open method below.
OpenSchematic = Split (OpenMySchematic,",",-1)
' Use the UBound function to get the upper limit of the resulting
' array. This could also be used with some code as an "error"
' check (e.g. did the user enter the right number of strings?, etc.)
UboundOpenSch = UBound (OpenSchematic)
' The array generated by the Split function is a zero reference array.
' So, the actual quantity of elements is the UBound value plus 1.
OpenSchematicCount = UboundOpenSch + 1
' A message box is a good way of displaying the data to ensure that it
' what one expects. OpenSchematic is a two field array of elements (0)
' and (1).
MsgBox "OpenSchematicCount = "& OpenSchematicCount & vbCrLf &_
"OpenSchematic (0) = " & OpenSchematic(0) & vbCrLf &_
"OpenSchematic (1) = " &_
OpenSchematic(1),,"OpenSchematic Array"
' Use the Documents Open method to open the desired schematic.
Set vddoc = vdapp.SchematicSheetDocuments.Open (OpenSchematic(0),OpenSchematic(1))
vddocName = vddoc.Name
MsgBox "Vddoc Name is: " & vddocName
' Create the document and View objects.
Set vddoc = vdapp.ActiveDocument
Set vdview = vdapp.ActiveView
'Set the NoAppOpen variable False to terminate While loop.
NoAppOpen = False
Else
' If conditional not satisfied, therefore the user needs to see
'the prompt again.
NoAppOpen = True
End If
Wend
Else
' Application is running message to the user
MsgBox "Application is running",,"Empty Check"
' Use the GetObject method to acquire a "link" to the application
Set vdapp = GetObject(,"ViewDraw.Application")
' Create the document object
Set vddoc = vdapp.ActiveDocument
' Test to see if a drawing is open or only the application
If vddoc Is Nothing Then
MsgBox "No Schemtic is open.",,"Schematic Test"
' Prompt the user for a schematic to open if there isn't an open document.
While NoDocOpen
If Not OpenMySchematic = "NameRequired" Or Not OpenMySchematic = "" Then
' Prompt user for the desired schematic name and page number.
OpenMySchematic = InputBox ("Schematic Name, Page Number (e.g. Top,1): ",_
"User Prompt","NameRequired",_
500, 500)
' Split the user input so the user input can be put in the appropriate
' format for the SchematicSheetDocuments.Open method below.
OpenSchematic = Split (OpenMySchematic,",",-1)
' Use the UBound function to get the upper limit of the resulting
' array. This could also be used with some code as an "error"
' check (e.g. did the user enter the right number of strings?, etc.)
UboundOpenSch = UBound (OpenSchematic)
' The array generated by the Split function is a zero reference array.
' So, the actual quantity of elements is the UBound value plus 1.
OpenSchematicCount = UboundOpenSch + 1
' A message box is a good way of displaying the data to ensure that it
' what one expects. OpenSchematic is a two field array of elements (0)
' and (1).
MsgBox "OpenSchematicCount = "& OpenSchematicCount & vbCrLf &_
"OpenSchematic (0) = " & OpenSchematic(0) & vbCrLf &_
"OpenSchematic (1) = " & OpenSchematic(1),,"OpenSchematic Array"
' Use the Documents Open method to open the desired schematic.
Set vddoc = vdapp.SchematicSheetDocuments.Open_
(OpenSchematic(0),OpenSchematic(1))
' Create the Document and View objects
Set vddoc = vdapp.ActiveDocument
Set vdview = vdapp.ActiveView
NoDocOpen = False
Else
NoDocOpen = True
End If ' OpenMySchematic
Wend ' NoDocOpen
' Create the View object if the document exists.
Set vdview = vdapp.ActiveView
End If ' IsEmpty vddoc
Set vdview = vdapp.ActiveView
End If ' IsEmpty Application
On Error GoTo 0
' Set a string variable to contain the derived project directory.
CurProjDir = vdapp.GetProjectData.GetProjectPath
MsgBox "CurProjDir is " & CurProjDir
' Create an object variable, CurBlock, whose value is the current block.
' Will be used for a statement which deselects everything on the page.
Set CurBlock = vdview.Block
' Clears the status bar.
vdapp.StatusBarText("")
' Create a filesystem object and text file
Set FSO = CreateObject("Scripting.FileSystemObject")
Set CompRefXref = FSO.CreateTextFile(CurProjDir & "\CompRefXref.txt", True)
' Write a header line to the text file.
CompRefXref.Write "Inst REFDES" & vbTab & "Symbol REFDES" & vbTab & "Xloc" & vbTab & "Yloc" & vbTab & "Internal ID " & vbCrLf
' Create an Xcel file with the same data as in the .txt file
Dim xl, ActSheet, ActCell, wb
Dim inc
inc = 2
Set xl = CreateObject("Excel.Application")
xl.Visible = True
Set wb = xl.Workbooks.Add
Set ActSheet = wb.ActiveSheet
Actsheet.name = "RefDes Crossreference"
Set ActCell = xl.Cells(1,1)
ActCell.Value = "Inst REFDES"
Set ActCell = xl.Cells(1,2)
ActCell.Value = "Symbol REFDES "
Set ActCell = xl.Cells(1,3)
ActCell.Value = "Xloc"
Set ActCell = xl.Cells(1,4)
ActCell.Value = "Yloc"
Set ActCell = xl.Cells(1,5)
ActCell.Value = "Internal ID"
' Create a collection of components from the Xpedition Designer schematic.
Set CompColl = vdview.query(VDM_COMP, VD_ALL)
' Populate the CompCollCount Variable with number of components to process
CompCollCount = CompColl.count
' Process each element of the collection.
For Each CompObj In CompColl
ProcCompCount = ProcCompCount + 1
' Issue a message to the status bar.
vdapp.StatusBarText("Processing component " & ProcCompCount & " of " & CompCollCount & " components.")
' Identify the symbol block type. Required for hierarchical traversal
' although, its purpose in this script is to determine if it we should
' check for a REFDES.
CompSymType = CompObj.SymbolBlock.SymbolType
' Select the component
CompObj.Selected = True
' Zoom in on the selected component
vdapp.executecommand "zselect"
' Test for block type of Pin, then get attributes.
' Could be used to identify the NETNAME attribute value which
' becomes the implicit netname of the net connected to the
' component object whose symbol type is Pin.
If CompSymType = 4 Then
MsgBox "Block type Pin; No REFDES.",,"Block Type Test"
ElseIf CompSymType = 3 Then
MsgBox "Block type Annotate; No REFDES.",,"Block Type Test"
Else
' Create a variable whose value represents the REFDES attribute
' object within the schematic.
Set RefDesAttr = CompObj.FindAttribute("REF DESIGNATOR")
' Test for presence of REFDES
If RefDesAttr Is Nothing Then
MsgBox "Part will not be processed. No RefDes found.",,"Refdes?"
Else
' Create two variables whose values hold the name and value of the
' Refdes attribute.
RefDesName = RefDesAttr.Name
RefDesVal = RefDesAttr.value
RefDesInstValue = RefDesAttr.InstanceValue
' Instance level value of attribute
' Create two variables whose values hold the x and y coordinates of the
' component.
CompXLoc = CompObj.GetLocation.X
CompYLoc = CompObj.GetLocation.Y
' Write to a text file. The vbTab and the "white space" are used
' formatting technique examples.
CompRefXref.WriteLine " " & RefDesInstValue & vbTab & vbTab & " " &_
RefDesVal & " " & vbTab & CompXLoc & vbTab & CompYLoc & vbTab & CompObj.UID
' Write to the Excel file
Set ActCell = xl.Cells(inc,1)
ActCell.Value = RefDesInstValue
Set ActCell = xl.Cells(inc,2)
ActCell.Value = RefDesVal
Set ActCell = xl.Cells(inc,3)
ActCell.Value = CompXLoc
Set ActCell = xl.Cells(inc,4)
ActCell.Value = CompYLoc
Set ActCell = xl.Cells(inc,5)
ActCell.Value = CompObj.UID
' Increment the row count
inc = inc +1
End If ' RefDesAttr
End If ' CompSymType
' Deselect the parts
CurBlock.DeselectAll
Next
' Close the text file.
CompRefXref.Close
'Set View of schematic to full page
vdview.ViewFull
' Issue a message to the status bar.
vdapp.StatusBarText("Script successfully completed! Excel has launched with the results.")
' Launch Excel on text file created by the FSO.CreateTextFile method.
Dim Win
Set win =CreateObject("WScript.shell") ' Create the windows object
'Use the run method to launch an application
win.run "notepad.exe " & CurProjDir & "\CompRefXref.txt"