traci._gui
1# -*- coding: utf-8 -*- 2# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 3# Copyright (C) 2011-2026 German Aerospace Center (DLR) and others. 4# This program and the accompanying materials are made available under the 5# terms of the Eclipse Public License 2.0 which is available at 6# https://www.eclipse.org/legal/epl-2.0/ 7# This Source Code may also be made available under the following Secondary 8# Licenses when the conditions for such availability set forth in the Eclipse 9# Public License 2.0 are satisfied: GNU General Public License, version 2 10# or later which is available at 11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 13 14# @file _gui.py 15# @author Michael Behrisch 16# @author Daniel Krajzewicz 17# @author Mirko Barthauer 18# @date 2011-03-09 19 20from __future__ import absolute_import 21from .domain import Domain 22from . import constants as tc 23 24_RETURN_VALUE_FUNC = {tc.VAR_HAS_VIEW: lambda result: bool(result.read("!i")[0]), 25 tc.VAR_SELECT: lambda result: bool(result.read("!i")[0])} 26 27 28class GuiDomain(Domain): 29 DEFAULT_VIEW = 'View #0' 30 31 def __init__(self): 32 Domain.__init__(self, "gui", tc.CMD_GET_GUI_VARIABLE, tc.CMD_SET_GUI_VARIABLE, 33 tc.CMD_SUBSCRIBE_GUI_VARIABLE, tc.RESPONSE_SUBSCRIBE_GUI_VARIABLE, 34 tc.CMD_SUBSCRIBE_GUI_CONTEXT, tc.RESPONSE_SUBSCRIBE_GUI_CONTEXT, 35 _RETURN_VALUE_FUNC) 36 37 def getZoom(self, viewID=DEFAULT_VIEW): 38 """getZoom(string): -> double 39 40 Returns the current zoom factor. 41 """ 42 return self._getUniversal(tc.VAR_VIEW_ZOOM, viewID) 43 44 def getOffset(self, viewID=DEFAULT_VIEW): 45 """getOffset(string): -> (double, double) 46 47 Returns the x and y offset of the center of the current view. 48 """ 49 return self._getUniversal(tc.VAR_VIEW_OFFSET, viewID) 50 51 def getSchema(self, viewID=DEFAULT_VIEW): 52 """getSchema(string): -> string 53 54 Returns the name of the current coloring scheme. 55 """ 56 return self._getUniversal(tc.VAR_VIEW_SCHEMA, viewID) 57 58 def getAngle(self, viewID=DEFAULT_VIEW): 59 """getAngle(string): -> double 60 61 Returns the angle of the current view. 62 """ 63 return self._getUniversal(tc.VAR_ANGLE, viewID) 64 65 def getBoundary(self, viewID=DEFAULT_VIEW): 66 """getBoundary(string): -> ((double, double), (double, double)) 67 68 Returns the coordinates of the lower left and the upper right corner of the currently visible view. 69 """ 70 return self._getUniversal(tc.VAR_VIEW_BOUNDARY, viewID) 71 72 def setZoom(self, viewID, zoom): 73 """setZoom(string, double) -> None 74 75 Set the current zoom factor for the given view. 76 """ 77 self._setCmd(tc.VAR_VIEW_ZOOM, viewID, "d", zoom) 78 79 def setOffset(self, viewID, x, y): 80 """setOffset(string, double, double) -> None 81 82 Set the current offset for the given view. 83 """ 84 self._setCmd(tc.VAR_VIEW_OFFSET, viewID, "o", [x, y]) 85 86 def setSchema(self, viewID, schemeName): 87 """setSchema(string, string) -> None 88 89 Set the current coloring scheme for the given view. 90 """ 91 self._setCmd(tc.VAR_VIEW_SCHEMA, viewID, "s", schemeName) 92 93 def setAngle(self, viewID, angle): 94 """setAngle(string, double) -> None 95 96 Set the current angle for the given view. 97 """ 98 self._setCmd(tc.VAR_ANGLE, viewID, "d", angle) 99 100 def addView(self, viewID, schemeName="", in3D=False): 101 """addView(string, string, bool) -> None 102 Adds new view and sets it to the given settings scheme (optionally as a 3D view) 103 """ 104 self._setCmd(tc.ADD, viewID, "tsi", 2, schemeName, 1 if in3D else 0) 105 106 def removeView(self, viewID): 107 """removeView(string) -> None 108 109 Removes the view with the given id 110 """ 111 self._setCmd(tc.REMOVE, viewID) 112 113 def setBoundary(self, viewID, xmin, ymin, xmax, ymax): 114 """setBoundary(string, double, double, double, double) -> None 115 Sets the boundary of the visible network. If the window has a different 116 aspect ratio than the given boundary, the view is expanded along one 117 axis to meet the window aspect ratio and contain the given boundary. 118 """ 119 self._setCmd(tc.VAR_VIEW_BOUNDARY, viewID, "p", [[xmin, ymin], [xmax, ymax]]) 120 121 def screenshot(self, viewID, filename, width=-1, height=-1): 122 """screenshot(string, string, int, int) -> None 123 124 Save a screenshot for the given view to the given filename 125 at the next call to simulationStep. 126 The fileformat is guessed from the extension, the available 127 formats differ from platform to platform but should at least 128 include ps, svg and pdf, on linux probably gif, png and jpg as well. 129 Width and height of the image can be given as optional parameters. 130 """ 131 self._setCmd(tc.VAR_SCREENSHOT, viewID, "tsii", 3, filename, width, height) 132 133 def trackVehicle(self, viewID, vehID): 134 """trackVehicle(string, string) -> None 135 136 Start visually tracking the given vehicle on the given view. 137 Stop tracking when an empty string is used as vehID. 138 """ 139 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", vehID) 140 141 def hasView(self, viewID=DEFAULT_VIEW): 142 """hasView(string): -> bool 143 144 Check whether the given view exists. 145 """ 146 return self._getUniversal(tc.VAR_HAS_VIEW, viewID) 147 148 def getTrackedVehicle(self, viewID=DEFAULT_VIEW): 149 """getTrackedVehicle(string): -> string 150 151 Returns the id of the currently tracked vehicle 152 """ 153 return self._getUniversal(tc.VAR_TRACK_VEHICLE, viewID) 154 155 def track(self, objID, viewID=DEFAULT_VIEW): 156 """track(string, string) -> None 157 Start visually tracking the given vehicle or person on the given view. 158 Stop tracking when an empty string is used as objID. 159 """ 160 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", objID) 161 162 def isSelected(self, objID, objType="vehicle"): 163 """isSelected(string, string) -> int 164 Return 1 if the object of the given type and id is select, 0 otherwise 165 """ 166 return self._getUniversal(tc.VAR_SELECT, objID, "s", objType) 167 168 def toggleSelection(self, objID, objType="vehicle"): 169 """toggleSelection(string, string) -> None 170 Toggle selection status for the object of the given type and id 171 """ 172 self._setCmd(tc.VAR_SELECT, objID, "s", objType)
29class GuiDomain(Domain): 30 DEFAULT_VIEW = 'View #0' 31 32 def __init__(self): 33 Domain.__init__(self, "gui", tc.CMD_GET_GUI_VARIABLE, tc.CMD_SET_GUI_VARIABLE, 34 tc.CMD_SUBSCRIBE_GUI_VARIABLE, tc.RESPONSE_SUBSCRIBE_GUI_VARIABLE, 35 tc.CMD_SUBSCRIBE_GUI_CONTEXT, tc.RESPONSE_SUBSCRIBE_GUI_CONTEXT, 36 _RETURN_VALUE_FUNC) 37 38 def getZoom(self, viewID=DEFAULT_VIEW): 39 """getZoom(string): -> double 40 41 Returns the current zoom factor. 42 """ 43 return self._getUniversal(tc.VAR_VIEW_ZOOM, viewID) 44 45 def getOffset(self, viewID=DEFAULT_VIEW): 46 """getOffset(string): -> (double, double) 47 48 Returns the x and y offset of the center of the current view. 49 """ 50 return self._getUniversal(tc.VAR_VIEW_OFFSET, viewID) 51 52 def getSchema(self, viewID=DEFAULT_VIEW): 53 """getSchema(string): -> string 54 55 Returns the name of the current coloring scheme. 56 """ 57 return self._getUniversal(tc.VAR_VIEW_SCHEMA, viewID) 58 59 def getAngle(self, viewID=DEFAULT_VIEW): 60 """getAngle(string): -> double 61 62 Returns the angle of the current view. 63 """ 64 return self._getUniversal(tc.VAR_ANGLE, viewID) 65 66 def getBoundary(self, viewID=DEFAULT_VIEW): 67 """getBoundary(string): -> ((double, double), (double, double)) 68 69 Returns the coordinates of the lower left and the upper right corner of the currently visible view. 70 """ 71 return self._getUniversal(tc.VAR_VIEW_BOUNDARY, viewID) 72 73 def setZoom(self, viewID, zoom): 74 """setZoom(string, double) -> None 75 76 Set the current zoom factor for the given view. 77 """ 78 self._setCmd(tc.VAR_VIEW_ZOOM, viewID, "d", zoom) 79 80 def setOffset(self, viewID, x, y): 81 """setOffset(string, double, double) -> None 82 83 Set the current offset for the given view. 84 """ 85 self._setCmd(tc.VAR_VIEW_OFFSET, viewID, "o", [x, y]) 86 87 def setSchema(self, viewID, schemeName): 88 """setSchema(string, string) -> None 89 90 Set the current coloring scheme for the given view. 91 """ 92 self._setCmd(tc.VAR_VIEW_SCHEMA, viewID, "s", schemeName) 93 94 def setAngle(self, viewID, angle): 95 """setAngle(string, double) -> None 96 97 Set the current angle for the given view. 98 """ 99 self._setCmd(tc.VAR_ANGLE, viewID, "d", angle) 100 101 def addView(self, viewID, schemeName="", in3D=False): 102 """addView(string, string, bool) -> None 103 Adds new view and sets it to the given settings scheme (optionally as a 3D view) 104 """ 105 self._setCmd(tc.ADD, viewID, "tsi", 2, schemeName, 1 if in3D else 0) 106 107 def removeView(self, viewID): 108 """removeView(string) -> None 109 110 Removes the view with the given id 111 """ 112 self._setCmd(tc.REMOVE, viewID) 113 114 def setBoundary(self, viewID, xmin, ymin, xmax, ymax): 115 """setBoundary(string, double, double, double, double) -> None 116 Sets the boundary of the visible network. If the window has a different 117 aspect ratio than the given boundary, the view is expanded along one 118 axis to meet the window aspect ratio and contain the given boundary. 119 """ 120 self._setCmd(tc.VAR_VIEW_BOUNDARY, viewID, "p", [[xmin, ymin], [xmax, ymax]]) 121 122 def screenshot(self, viewID, filename, width=-1, height=-1): 123 """screenshot(string, string, int, int) -> None 124 125 Save a screenshot for the given view to the given filename 126 at the next call to simulationStep. 127 The fileformat is guessed from the extension, the available 128 formats differ from platform to platform but should at least 129 include ps, svg and pdf, on linux probably gif, png and jpg as well. 130 Width and height of the image can be given as optional parameters. 131 """ 132 self._setCmd(tc.VAR_SCREENSHOT, viewID, "tsii", 3, filename, width, height) 133 134 def trackVehicle(self, viewID, vehID): 135 """trackVehicle(string, string) -> None 136 137 Start visually tracking the given vehicle on the given view. 138 Stop tracking when an empty string is used as vehID. 139 """ 140 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", vehID) 141 142 def hasView(self, viewID=DEFAULT_VIEW): 143 """hasView(string): -> bool 144 145 Check whether the given view exists. 146 """ 147 return self._getUniversal(tc.VAR_HAS_VIEW, viewID) 148 149 def getTrackedVehicle(self, viewID=DEFAULT_VIEW): 150 """getTrackedVehicle(string): -> string 151 152 Returns the id of the currently tracked vehicle 153 """ 154 return self._getUniversal(tc.VAR_TRACK_VEHICLE, viewID) 155 156 def track(self, objID, viewID=DEFAULT_VIEW): 157 """track(string, string) -> None 158 Start visually tracking the given vehicle or person on the given view. 159 Stop tracking when an empty string is used as objID. 160 """ 161 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", objID) 162 163 def isSelected(self, objID, objType="vehicle"): 164 """isSelected(string, string) -> int 165 Return 1 if the object of the given type and id is select, 0 otherwise 166 """ 167 return self._getUniversal(tc.VAR_SELECT, objID, "s", objType) 168 169 def toggleSelection(self, objID, objType="vehicle"): 170 """toggleSelection(string, string) -> None 171 Toggle selection status for the object of the given type and id 172 """ 173 self._setCmd(tc.VAR_SELECT, objID, "s", objType)
38 def getZoom(self, viewID=DEFAULT_VIEW): 39 """getZoom(string): -> double 40 41 Returns the current zoom factor. 42 """ 43 return self._getUniversal(tc.VAR_VIEW_ZOOM, viewID)
getZoom(string): -> double
Returns the current zoom factor.
45 def getOffset(self, viewID=DEFAULT_VIEW): 46 """getOffset(string): -> (double, double) 47 48 Returns the x and y offset of the center of the current view. 49 """ 50 return self._getUniversal(tc.VAR_VIEW_OFFSET, viewID)
getOffset(string): -> (double, double)
Returns the x and y offset of the center of the current view.
52 def getSchema(self, viewID=DEFAULT_VIEW): 53 """getSchema(string): -> string 54 55 Returns the name of the current coloring scheme. 56 """ 57 return self._getUniversal(tc.VAR_VIEW_SCHEMA, viewID)
getSchema(string): -> string
Returns the name of the current coloring scheme.
59 def getAngle(self, viewID=DEFAULT_VIEW): 60 """getAngle(string): -> double 61 62 Returns the angle of the current view. 63 """ 64 return self._getUniversal(tc.VAR_ANGLE, viewID)
getAngle(string): -> double
Returns the angle of the current view.
66 def getBoundary(self, viewID=DEFAULT_VIEW): 67 """getBoundary(string): -> ((double, double), (double, double)) 68 69 Returns the coordinates of the lower left and the upper right corner of the currently visible view. 70 """ 71 return self._getUniversal(tc.VAR_VIEW_BOUNDARY, viewID)
getBoundary(string): -> ((double, double), (double, double))
Returns the coordinates of the lower left and the upper right corner of the currently visible view.
73 def setZoom(self, viewID, zoom): 74 """setZoom(string, double) -> None 75 76 Set the current zoom factor for the given view. 77 """ 78 self._setCmd(tc.VAR_VIEW_ZOOM, viewID, "d", zoom)
setZoom(string, double) -> None
Set the current zoom factor for the given view.
80 def setOffset(self, viewID, x, y): 81 """setOffset(string, double, double) -> None 82 83 Set the current offset for the given view. 84 """ 85 self._setCmd(tc.VAR_VIEW_OFFSET, viewID, "o", [x, y])
setOffset(string, double, double) -> None
Set the current offset for the given view.
87 def setSchema(self, viewID, schemeName): 88 """setSchema(string, string) -> None 89 90 Set the current coloring scheme for the given view. 91 """ 92 self._setCmd(tc.VAR_VIEW_SCHEMA, viewID, "s", schemeName)
setSchema(string, string) -> None
Set the current coloring scheme for the given view.
94 def setAngle(self, viewID, angle): 95 """setAngle(string, double) -> None 96 97 Set the current angle for the given view. 98 """ 99 self._setCmd(tc.VAR_ANGLE, viewID, "d", angle)
setAngle(string, double) -> None
Set the current angle for the given view.
101 def addView(self, viewID, schemeName="", in3D=False): 102 """addView(string, string, bool) -> None 103 Adds new view and sets it to the given settings scheme (optionally as a 3D view) 104 """ 105 self._setCmd(tc.ADD, viewID, "tsi", 2, schemeName, 1 if in3D else 0)
addView(string, string, bool) -> None Adds new view and sets it to the given settings scheme (optionally as a 3D view)
107 def removeView(self, viewID): 108 """removeView(string) -> None 109 110 Removes the view with the given id 111 """ 112 self._setCmd(tc.REMOVE, viewID)
removeView(string) -> None
Removes the view with the given id
114 def setBoundary(self, viewID, xmin, ymin, xmax, ymax): 115 """setBoundary(string, double, double, double, double) -> None 116 Sets the boundary of the visible network. If the window has a different 117 aspect ratio than the given boundary, the view is expanded along one 118 axis to meet the window aspect ratio and contain the given boundary. 119 """ 120 self._setCmd(tc.VAR_VIEW_BOUNDARY, viewID, "p", [[xmin, ymin], [xmax, ymax]])
setBoundary(string, double, double, double, double) -> None Sets the boundary of the visible network. If the window has a different aspect ratio than the given boundary, the view is expanded along one axis to meet the window aspect ratio and contain the given boundary.
122 def screenshot(self, viewID, filename, width=-1, height=-1): 123 """screenshot(string, string, int, int) -> None 124 125 Save a screenshot for the given view to the given filename 126 at the next call to simulationStep. 127 The fileformat is guessed from the extension, the available 128 formats differ from platform to platform but should at least 129 include ps, svg and pdf, on linux probably gif, png and jpg as well. 130 Width and height of the image can be given as optional parameters. 131 """ 132 self._setCmd(tc.VAR_SCREENSHOT, viewID, "tsii", 3, filename, width, height)
screenshot(string, string, int, int) -> None
Save a screenshot for the given view to the given filename at the next call to simulationStep. The fileformat is guessed from the extension, the available formats differ from platform to platform but should at least include ps, svg and pdf, on linux probably gif, png and jpg as well. Width and height of the image can be given as optional parameters.
134 def trackVehicle(self, viewID, vehID): 135 """trackVehicle(string, string) -> None 136 137 Start visually tracking the given vehicle on the given view. 138 Stop tracking when an empty string is used as vehID. 139 """ 140 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", vehID)
trackVehicle(string, string) -> None
Start visually tracking the given vehicle on the given view. Stop tracking when an empty string is used as vehID.
142 def hasView(self, viewID=DEFAULT_VIEW): 143 """hasView(string): -> bool 144 145 Check whether the given view exists. 146 """ 147 return self._getUniversal(tc.VAR_HAS_VIEW, viewID)
hasView(string): -> bool
Check whether the given view exists.
149 def getTrackedVehicle(self, viewID=DEFAULT_VIEW): 150 """getTrackedVehicle(string): -> string 151 152 Returns the id of the currently tracked vehicle 153 """ 154 return self._getUniversal(tc.VAR_TRACK_VEHICLE, viewID)
getTrackedVehicle(string): -> string
Returns the id of the currently tracked vehicle
156 def track(self, objID, viewID=DEFAULT_VIEW): 157 """track(string, string) -> None 158 Start visually tracking the given vehicle or person on the given view. 159 Stop tracking when an empty string is used as objID. 160 """ 161 self._setCmd(tc.VAR_TRACK_VEHICLE, viewID, "s", objID)
track(string, string) -> None Start visually tracking the given vehicle or person on the given view. Stop tracking when an empty string is used as objID.
163 def isSelected(self, objID, objType="vehicle"): 164 """isSelected(string, string) -> int 165 Return 1 if the object of the given type and id is select, 0 otherwise 166 """ 167 return self._getUniversal(tc.VAR_SELECT, objID, "s", objType)
isSelected(string, string) -> int Return 1 if the object of the given type and id is select, 0 otherwise
169 def toggleSelection(self, objID, objType="vehicle"): 170 """toggleSelection(string, string) -> None 171 Toggle selection status for the object of the given type and id 172 """ 173 self._setCmd(tc.VAR_SELECT, objID, "s", objType)
toggleSelection(string, string) -> None Toggle selection status for the object of the given type and id