filename = "COM Basic Commands VBS.layx"
Vissim.LoadLayout Filename
linkName = Vissim.Net.Links.ItemByKey(linkNo).AttValue("Name")
MsgBox "Name of Link " + CStr(linkNo) +" is '" + CStr(linkName) + "'."
linkNameNew = "New Link Name"
Vissim.Net.Links.ItemByKey(linkNo).AttValue("Name") = linkNameNew
MsgBox "Name of Link " + CStr(linkNo) +" is now set to '" + CStr(linkNameNew) + "'." + vbcrlf + vbcrlf _
+ "About to change the signal program number..."
Set signalController = Vissim.Net.signalControllers.ItemByKey(controllerNo)
signalController.AttValue("ProgNo") = programNoNew
MsgBox "The program number of signal controller " + CStr(controllerNo) +" is now set to " + CStr(programNoNew) + "." + vbcrlf + vbcrlf _
+ "About to change a route relative flow..."
Vissim.Net.VehicleRoutingDecisionsStatic.ItemByKey(rouDecNo).VehRoutSta.ItemByKey(routeNo).AttValue("RelFlow(1)") = relativeFlowNew
msgString = "The relative flow of Route " + CStr(rouDecNo) + "-" + CStr(routeNo) + " is now set to " + CStr(relativeFlowNew) + "."
MsgBox msgString + vbcrlf + vbcrlf + "About to change the volume of vehicle input no. " + CStr(vehInputNo) + "..."
Vissim.Net.VehicleInputs.ItemByKey(vehInputNo).AttValue("Volume(1)") = volumeNew
MsgBox "The volume of the first time interval of vehicle input " + CStr(vehInputNo) + " is now set to " + CStr(volumeNew) + "."
Vissim.Net.VehicleInputs.ItemByKey(vehInputNo).AttValue("Cont(2)") = False
Vissim.Net.VehicleInputs.ItemByKey(vehInputNo).AttValue("Volume(2)") = volumeNew
MsgBox "The volume of the 2nd time interval of vehicle input " + CStr(vehInputNo) + " is now set to " + CStr(volumeNew) + "." + vbcrlf + vbcrlf _
+ "About to change a vehicle composition..."
relFlows = Vissim.Net.VehicleCompositions.ItemByKey(vehCompositionNo).VehCompRelFlows.GetAll
relFlows(0).AttValue("VehType") = 100
relFlows(0).AttValue("DesSpeedDistr") = 50
relFlows(0).AttValue("RelFlow") = 0.9
relFlows(1).AttValue("RelFlow") = 0.1
MsgBox "Vehicle composition " + CStr(vehCompositionNo) + " is now changed."
MsgBox "To see the effect of the script we recommend to open the links list."
linkNames = Vissim.Net.Links.GetMultiAttValues(attribute1)
For i = 0 to UBound(linkNames)
if linkNames(i, 0) = 1 then linkNames(i, 1) = "New name of link #1"
if linkNames(i, 0) = 2 then linkNames(i, 1) = "New name of link #2"
if linkNames(i, 0) = 4 then linkNames(i, 1) = "New name of link #4"
Vissim.Net.Links.SetMultiAttValues attribute1, linkNames
MsgBox "The names of links 1, 2 and 4 were changed."
attributeList = Array ("Name", "Length2D")
link_Name_Length = Vissim.Net.Links.GetMultipleAttributes(attributeList)
attributeList = Array ("Name", "CostPerKm")
link_Name_Cost = Vissim.Net.Links.GetMultipleAttributes(attributeList)
link_Name_Cost(0, 0) = "Name1"
link_Name_Cost(0, 1) = 12
link_Name_Cost(1, 0) = "Name2"
link_Name_Cost(2, 0) = "Name3"
link_Name_Cost(3, 0) = "Name4"
Vissim.Net.Links.SetMultipleAttributes attributeList, link_Name_Cost
MsgBox "The name and link cost of the first 4 links were changed."
linkName = "All Links have the same name"
Vissim.Net.Links.SetAllAttValues attribute1, linkName
MsgBox "All links now have the same name."
Vissim.Net.Links.SetAllAttValues attribute1, cost
MsgBox "All links now have a cost of " + CStr(cost)
Vissim.Net.Links.SetAllAttValues attribute1, cost, True
MsgBox "To the cost of all links an additional value of " + CStr(cost) + " was added."
Vissim.Simulation.AttValue("RandSeed") = randomSeed
Vissim.Simulation.AttValue("SimPeriod") = endOfSimulation
Vissim.Simulation.RunSingleStep
Vissim.Simulation.AttValue("SimBreakAt") = simBreakAt
Vissim.Simulation.AttValue("UseMaxSimSpeed") = True
MsgBox "The simulation will now run until the 'Break at' time at simulation second " + CStr(simBreakAt) + ". From there you can either continue the simulation (F5) or stop it (Escape).", vbInformation+vbOKOnly ,"Run Simulation"
Vissim.Simulation.RunContinuous
Sub AccessDuringSimulation()
Vissim.Simulation.AttValue("SimPeriod") = endOfSimulation
Vissim.Simulation.AttValue("SimBreakAt") = simBreakAt
MsgBox "The simulation will now run until simulation second " + CStr(simBreakAt) + ". Then various attributes are accessed while the simulation is active. Afterwards the simulation will run until the end of " + CStr(endOfSimulation) + " s." , vbInformation+vbOKOnly ,"Access Data During Simulation"
Vissim.Simulation.RunContinuous
Set signalGroup = Vissim.Net.signalControllers.ItemByKey(controllerNo).SGs.ItemByKey(signalGroupNo)
signalGroupState = signalGroup.AttValue("SigState")
MsgBox "Current state of signal group " + CStr(signalGroupNo) + " is " + signalGroupState + vbcrlf _
+ "It will be controlled by COM now and set to " + stateNew + "."
signalGroup.AttValue("SigState") = stateNew
Vissim.Simulation.RunSingleStep
Loop While Vissim.Simulation.AttValue("SimSec") < 29
MsgBox "The control of signal controller " + CStr(controllerNo) + " will now " + vbcrlf _
+ "be handed back from COM to the signal controller - " + vbcrlf _
+ "hence the state of signal group " + CStr(signalGroupNo) + " will change again."
signalGroup.AttValue("ContrByCOM") = False
Vissim.Simulation.RunSingleStep
Loop While Vissim.Simulation.AttValue("SimSec") < 30
allVehicles = Vissim.Net.Vehicles.GetAll
For vehCount = 0 To UBound(allVehicles)
vehNo = allVehicles(vehCount).AttValue("No")
vehType = allVehicles(vehCount).AttValue("VehType")
vehSpeed = allVehicles(vehCount).AttValue("Speed")
vehPos = allVehicles(vehCount).AttValue("Pos")
vehLinkLane = allVehicles(vehCount).AttValue("Lane")
msgText = "Number of vehicles in network: " + CStr(Vissim.Net.Vehicles.Count) + vbcrlf + vbcrlf _
+ "Information of last vehicle:" + vbCrlf _
+ "vehicle number = " + CStr(vehNo) + vbCrlf _
+ "Type = " + CStr(vehType) + vbCrlf _
+ "Speed = " + CStr(FormatNumber(vehSpeed,1)) + vbCrlf _
+ "Pos = " + CStr(FormatNumber(vehPos,2)) + vbCrlf _
+ "Link & lane = " + CStr(vehLinkLane)
MsgBox msgText,,"Loop over all vehicles"
For Each vehicle In Vissim.Net.Vehicles
vehNo = vehicle.AttValue("No")
vehType = vehicle.AttValue("VehType")
vehSpeed = vehicle.AttValue("Speed")
vehPos = vehicle.AttValue("Pos")
vehLinkLane = vehicle.AttValue("Lane")
msgText = "Number of vehicles in network: " + CStr(Vissim.Net.Vehicles.Count) + vbcrlf + vbcrlf _
+ "Information of last vehicle:" + vbCrlf _
+ "vehicle number = " + CStr(vehNo) + vbCrlf _
+ "Type = " + CStr(vehType) + vbCrlf _
+ "Speed = " + CStr(FormatNumber(vehSpeed,1)) + vbCrlf _
+ "Pos = " + CStr(FormatNumber(vehPos,2)) + vbCrlf _
+ "Link & lane = " + CStr(vehLinkLane)
MsgBox msgText,, "Loop (object enumeration)"
Set iteratorVehicles = Vissim.Net.Vehicles.Iterator
While iteratorVehicles.Valid
Set vehicle = iteratorVehicles.Item
vehNo = vehicle.AttValue("No")
vehType = vehicle.AttValue("VehType")
vehSpeed = vehicle.AttValue("Speed")
vehPos = vehicle.AttValue("Pos")
vehLinkLane = vehicle.AttValue("Lane")
msgText = "Number of vehicles in network: " + CStr(Vissim.Net.Vehicles.Count) + vbcrlf + vbcrlf _
+ "Information of last vehicle:" + vbCrlf _
+ "vehicle number = " + CStr(vehNo) + vbCrlf _
+ "Type = " + CStr(vehType) + vbCrlf _
+ "Speed = " + CStr(FormatNumber(vehSpeed,1)) + vbCrlf _
+ "Pos = " + CStr(FormatNumber(vehPos,2)) + vbCrlf _
+ "Link & lane = " + CStr(vehLinkLane)
MsgBox msgText,, "Loop (iterator)"
vehNumbers = Vissim.Net.Vehicles.GetMultiAttValues("No")
vehTypes = Vissim.Net.Vehicles.GetMultiAttValues("VehType")
vehSpeeds = Vissim.Net.Vehicles.GetMultiAttValues("Speed")
vehPositions = Vissim.Net.Vehicles.GetMultiAttValues("Pos")
vehLinkLanes = Vissim.Net.Vehicles.GetMultiAttValues("Lane")
For i = 0 To UBound(vehNumbers)
vehSpeed = vehSpeeds(i, 1)
vehPos = vehPositions(i, 1)
vehLinkLane = vehLinkLanes(i, 1)
msgText = "Number of vehicles in network: " + CStr(Vissim.Net.Vehicles.Count) + vbcrlf + vbcrlf _
+ "Information of last vehicle:" + vbCrlf _
+ "vehicle number = " + CStr(vehNo) + vbCrlf _
+ "Type = " + CStr(vehType) + vbCrlf _
+ "Speed = " + CStr(FormatNumber(vehSpeed,1)) + vbCrlf _
+ "Pos = " + CStr(FormatNumber(vehPos,2)) + vbCrlf _
+ "Link & lane = " + CStr(vehLinkLane)
MsgBox msgText,, "GetMultiAttValues"
allVehAttributes = Vissim.Net.Vehicles.GetMultipleAttributes(Array("No", "VehType", "Speed", "Pos", "Lane"))
For i = 0 To UBound(allVehAttributes)
vehNo = allVehAttributes(i, 0)
vehType = allVehAttributes(i, 1)
vehSpeed = allVehAttributes(i, 2)
vehPos = allVehAttributes(i, 3)
vehLinkLane = allVehAttributes(i, 4)
msgText = "Number of vehicles in network: " + CStr(Vissim.Net.Vehicles.Count) + vbcrlf + vbcrlf _
+ "Information of last vehicle:" + vbCrlf _
+ "vehicle number = " + CStr(vehNo) + vbCrlf _
+ "Type = " + CStr(vehType) + vbCrlf _
+ "Speed = " + CStr(FormatNumber(vehSpeed,1)) + vbCrlf _
+ "Pos = " + CStr(FormatNumber(vehPos,2)) + vbCrlf _
+ "Link & lane = " + CStr(vehLinkLane)
MsgBox msgText,, "GetMultipleAttributes"
Set vehicle = Vissim.Net.Vehicles.ItemByKey(vehNo)
MsgBox "Desired speed of vehicle " + CStr(vehNo) + " is currently " + CStr(FormatNumber(vehicle.AttValue("DesSpeed"),1)) + "."
vehicle.AttValue("DesSpeed") = desSpeedNew
MsgBox "Desired speed of vehicle " + CStr(vehNo) + " is now set to " + CStr(vehicle.AttValue("DesSpeed")) + "."
Dim linkNo, laneNo, linkCoordinate
MsgBox "Vehicle " + CStr(vehNo) + " is currently travelling on link " + vehicle.AttValue("Lane") + "." + vbCrlf _
+ "It will now be moved to link " + CStr(linkNo) + ", coordinate " + CStr(FormatNumber(linkCoordinate,2)) + "."
vehicle.MoveToLinkPosition linkNo, laneNo, linkCoordinate
Vissim.Simulation.RunSingleStep
vehNo = vehicle.AttValue("No")
MsgBox "Vehicle " + CStr(vehNo) + " will shortly be removed from the network..."
Vissim.Net.Vehicles.RemoveVehicle vehNo
MsgBox "A new vehicle will be added shortly on link " + CStr(linkNo) + ", coordinate " + CStr(FormatNumber(linkCoordinate,2)) + " with desired speed of " + CStr(desSpeedNew) + "."
Vissim.Net.Vehicles.AddVehicleAtLinkPosition vehType, linkNo, laneNo, linkCoordinate, desSpeedNew, interaction
Vissim.Simulation.RunContinuous
restoreGraphicsMode = false
if Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") <> 0 then
Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") = 0
restoreGraphicsMode = true
Vissim.Graphics.CurrentNetworkWindow.ZoomTo x1, y1, x2, y2
filenameScreenshot = "Screenshot 2D.jpg"
Vissim.Graphics.CurrentNetworkWindow.Screenshot filenameScreenshot, sizeFactor
MsgBox "The screenshot was saved to " + filenameScreenshot,, "Screenshot 2D"
if restoreGraphicsMode then
Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") = 1
restoreGraphicsMode = false
if Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") <> 1 then
Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") = 1
restoreGraphicsMode = true
Dim camPosX, camPosY, camPosZ
Vissim.Graphics.CurrentNetworkWindow.SetCameraPositionAndAngle camPosX, camPosY, camPosZ, yawAngle, pitchAngle
filenameScreenshot = "Screenshot 3D.jpg"
Vissim.Graphics.CurrentNetworkWindow.Screenshot filenameScreenshot, sizeFactor
MsgBox "The screenshot was saved to " + filenameScreenshot,, "Screenshot 3D"
if restoreGraphicsMode then
Vissim.Graphics.CurrentNetworkWindow.AttValue("3D") = 0
for each simRun in Vissim.Net.SimulationRuns
Vissim.Net.SimulationRuns.RemoveSimulationRun(simRun)
Vissim.Graphics.CurrentNetworkWindow.AttValue("QuickMode") = 1
Vissim.Simulation.AttValue("SimPeriod") = 600
Vissim.Simulation.AttValue("SimBreakAt") = 0
Vissim.Simulation.AttValue("RandSeed") = simRunIndex
Vissim.Simulation.RunContinuous
Vissim.ResumeUpdateGUI (true)
Vissim.Graphics.CurrentNetworkWindow.AttValue("QuickMode") = 0
Sub GetSimulationRunResults()
attributes = Array("Timestamp", "RandSeed", "SimEnd")
allSimulationRuns = Vissim.Net.SimulationRuns.GetMultipleAttributes(attributes)
For simRun = 0 To UBound(allSimulationRuns)
For attIndex = 0 To UBound(attributes)
resultString = resultString + CStr(allSimulationRuns(simRun, attIndex)) + ", "
resultString = resultString + vbCrlf
MsgBox resultString, vbOKOnly+vbInformation ,"All simulation runs"
Sub GetTravelTimeResults()
Dim vehTravelTimeMeasurement, vehTravTimeMeasurementNo
vehTravTimeMeasurementNo = 2
Set vehTravelTimeMeasurement = Vissim.Net.VehicleTravelTimeMeasurements.ItemByKey(vehTravTimeMeasurementNo)
travTime = vehTravelTimeMeasurement.AttValue("TravTm(Avg,Avg,All)")
vehCount = vehTravelTimeMeasurement.AttValue("Vehs(Avg,Avg,All)")
msgText = "Vehicle travel time measurement no. " + CStr(vehTravTimeMeasurementNo) + ":" + vbcrlf _
+ "Average travel time" + vbcrlf _
+ "· of all vehicle classes" + vbcrlf _
+ "· over all time intervals" + vbcrlf _
+ "· and over all simulation runs" + vbcrlf _
+ "= " + CStr(FormatNumber(travTime,1)) + " s for " + CStr(vehCount) + " vehicles."
MsgBox msgText,,"Travel Times"
travTime = vehTravelTimeMeasurement.AttValue("TravTm(Current,Max,20)")
vehCount = vehTravelTimeMeasurement.AttValue("Vehs(Current,Max,20)")
msgText = "Vehicle travel time measurement no. " + CStr(vehTravTimeMeasurementNo) + ":" + vbcrlf _
+ "Maximum travel time" + vbcrlf _
+ "· of all HGVs" + vbcrlf _
+ "· over all time intervals" + vbcrlf _
+ "· of the current simulation run" + vbcrlf _
+ "= " + CStr(FormatNumber(travTime,1)) + " s for " + CStr(vehCount) + " vehicles."
MsgBox msgText,,"Travel Times"
travTime = vehTravelTimeMeasurement.AttValue("TravTm(3,1,All)")
vehCount = vehTravelTimeMeasurement.AttValue("Vehs(3,1,All)")
msgText = "Vehicle travel time measurement no. " + CStr(vehTravTimeMeasurementNo) + ":" + vbcrlf _
+ "Travel time of " + vbcrlf _
+ "· all vehicle classes" + vbcrlf _
+ "· of the 1st time interval" + vbcrlf _
+ "· of the 2nd simulation run" + vbcrlf _
+ "= " + CStr(FormatNumber(travTime,1)) + " s for " + CStr(vehCount) + " vehicles."
MsgBox msgText,,"Travel Times"
Sub GetDataCollectionResults()
Dim dataCollMeasurement, dataCollMeasurementNo
dataCollMeasurementNo = 1
Set dataCollMeasurement = Vissim.Net.DataCollectionMeasurements.ItemByKey(dataCollMeasurementNo)
Dim vehCount, speed, acceleration, length
vehCount = dataCollMeasurement.AttValue("Vehs(Avg,1,All)")
speed = dataCollMeasurement.AttValue("Speed(Avg,1,All)")
acceleration = dataCollMeasurement.AttValue("Acceleration(Avg,1,All)")
length = dataCollMeasurement.AttValue("Length(Avg,1,All)")
msgText = "Vehicle data of data collection measurement no. " + CStr(dataCollMeasurementNo) + vbcrlf _
+ "· of all vehicle classes" + vbcrlf _
+ "· of the 1st time interval" + vbcrlf _
+ "· averaged over all simulation runs:" + vbcrlf + vbcrlf _
+ "Vehicle count = " + CStr(vehCount) + vbcrlf _
+ "Speed = " + CStr(FormatNumber(Speed,1)) + vbcrlf _
+ "Acceleration = " + CStr(FormatNumber(Acceleration,1)) + vbcrlf _
+ "Vehicle length = " + CStr(FormatNumber(Length,1))
MsgBox msgText,,"Data Collection"
Sub GetQueueLengthResults()
Dim queueCounterNo, queueLenAvg
queueLenAvg = Vissim.Net.QueueCounters.ItemByKey(queueCounterNo).AttValue("QLen(Avg, Avg)")
msgText = "Queue counter no. " + CStr(queueCounterNo) + ":" + vbcrlf _
+ "Average queue length of all time intervals" + vbcrlf _
+ "and over all simulation runs = " + CStr(FormatNumber(queueLenAvg,1))
MsgBox msgText,,"Queue Length"
Sub SaveNetworkAndLayout()
path = "C:\Users\Public\Documents\PTV Vision\PTV Vissim 9\Examples Training\COM\Basic Commands\"
filename = "COM Basic Commands VBS - Saved"
Vissim.SaveNetAs (path + filename + ".inpx")
Vissim.SaveLayout (path + filename + ".layx")
MsgBox "The network and layout files were saved to "+ vbcrlf + vbcrlf _
+ path + vbcrlf + vbcrlf _
+ "as '" + filename + "'" ,vbInformation,"Save Network and Layout"