API 範例:將節點資料寫入檔案

您正使用之程式設計語言中提供的標準檔案功能 (VBScript 的 File.Write) 用於寫入外部檔案及啟動外部程式。

此範例會建立新的逗號分隔檔案 (*.csv),並針對模型中的每個節點,將以下資訊寫入檔案:

完成檔案寫入之後,會啟動外部應用程式 (記事本) 以顯示檔案。

'@
'@ 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 Synergy
Set Synergy = CreateObject("synergy.Synergy")
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