API example: Writing nodal data to a file

The standard file facilities provided in the programming language you are using (File.Write for VBScript) are used to write to external files and launch external programs.

This example creates a new comma delimited file (*.csv), and writes the following information to the file for each node in the model:

When the file has been written, an external application (Notepad) is launched to display the file.

'%RunPerInstance
'@
'@ DESCRIPTION
'@ Extract all Nodal Coordinates to a comma separated text file
'@ 
'@
'@ SYNTAX
'@ WriteNodalData
'@
'@ PARAMETERS
'@ none 
'@
'@ DEPENDENCIES/LIMITATIONS
'@  
'@
'@ History
'@ Created DRA 9/8/2006
'@@ 
Option Explicit
SetLocale("en-us")
Dim SynergyGetter, Synergy
On Error Resume Next
Set SynergyGetter = GetObject(CreateObject("WScript.Shell").ExpandEnvironmentStrings("%SAInstance%"))
On Error GoTo 0
If (Not IsEmpty(SynergyGetter)) Then
  Set Synergy = SynergyGetter.GetSASynergy
Else
  Set Synergy = CreateObject("synergy.Synergy")
End If
Synergy.SetUnits "METRIC"

Dim StudyDoc, App
Dim FS, TemporaryFolder, TempFolder, Name, lFile
Dim Str, Node, NodeNumber, Coord

Set StudyDoc = Synergy.StudyDoc()

'Open a File in the users temporary Directory
Set FS = CreateObject("Scripting.FileSystemObject")
TemporaryFolder = 2
Set TempFolder = FS.GetSpecialFolder(TemporaryFolder)
Name = "data.txt"
Set lFile = TempFolder.CreateTextFile(Name, True)

' Write File Header
Str = "Node" & "," & "X" & "," & "Y" & "," & "Z" &  vbCrLf
lFile.Write Str

' Loop through all nodal
Set Node = StudyDoc.GetFirstNode()
While Not Node Is Nothing
        NodeNumber = StudyDoc.GetEntityID(Node)
        Set Coord = StudyDoc.GetNodeCoord(Node)
        Str = NodeNumber & "," & Coord.X & "," & Coord.Y  & "," & Coord.Z & vbCRLF
        lFile.Write Str
        Set Node = StudyDoc.GetNextNode(Node)
Wend

' Close File
lFile.Close

' Notify user where the file is located
MsgBox "Nodal Data Recorded In File" & vbCRLF & TempFolder.Path & "\" & Name 
 
' Open the File in Notepad
Set App = WScript.CreateObject("WScript.Shell")
Dim Command
Command = "notepad.exe " & TempFolder.Path & "\" & Name 
App.Run Command

MsgBox "Script Complete"
Wscript.Quit