Zuken.devices

  1import re
  2
  3from .e3 import Job
  4from re import sub
  5
  6_device = Job.CreateDeviceObject()
  7_ = None
  8
  9def devicesOnSheet(sheet: int, base=False) -> list:
 10    """
 11    Gets Device IDs for all the devices found on the sheet.
 12    Found by going through each symbol and seeing if it connects to a Device.
 13
 14    Args:
 15        sheet: Sheet to search through
 16        base: Boolean (default False) to decide whether to return Sheet ID or base (Schematic) ID
 17
 18    Returns:
 19        List of device IDs
 20    """
 21
 22    Sheet = Job.CreateSheetObject()
 23    Sheet.SetId(sheet)
 24    base &= Sheet.IsFormboard()
 25
 26    devices = []
 27    for sym in Sheet.GetSymbolIds(_)[1][1:]:
 28        if (devId := _device.SetId(sym)) == 0:
 29            continue
 30
 31        if base:
 32            devId = _device.GetOriginalId()
 33
 34        if devId not in devices:
 35            devices.append(devId)
 36
 37    return devices
 38
 39def deviceFormboardId(device: int, sheet: int) -> int:
 40    """
 41    Returns the device's ID on a given formboard sheet.
 42    Given ID can be a formboard ID, even from a different sheet.
 43
 44    Args:
 45        device: Device ID to search for
 46        sheet: Sheet to search for device on
 47
 48    Returns:
 49        ID of the device if found, 0 otherwise
 50
 51    """
 52    _device.SetId(device)
 53    if _device.GetFormboardSheetId() == sheet:
 54        return device
 55
 56    if _device.IsFormboard():
 57        _device.SetId(_device.GetOriginalId())
 58
 59    for d in _device.GetFormboardIds(_)[1][1:]:
 60        _device.SetId(d)
 61        if _device.GetFormboardSheetId() == sheet:
 62            return d
 63    return 0
 64
 65def tableSymbolId(device: int, sheet: int) -> int:
 66    """
 67    Returns the table symbol for a device on a specific sheet
 68
 69    Args:
 70        device: Device ID
 71        sheet: Sheet ID (should be formboard)
 72
 73    Returns:
 74        ID of the table symbol if found, 0 otherwise
 75    """
 76    _device.SetId(deviceFormboardId(device, sheet))
 77    return _device.GetTableSymbolId()
 78
 79def coreIds(device: int, nc=False) -> list:
 80    """
 81    Gets the core IDs for all wires/cables connected to a device
 82
 83    Args:
 84        device: Device ID
 85        nc: Whether to include N/C pins (will have no cores) as 0s in the list. Default False
 86    Returns:
 87        List of cores found connected to the device
 88    """
 89    Pin = Job.CreatePinObject()
 90    _device.SetId(device)
 91
 92    cores = []
 93    for p in _device.GetPinIds(_)[1][1:]:
 94        Pin.SetId(p)
 95
 96        c = list(Pin.GetCoreIds(_)[1][1:])
 97        if len(c) == 0 and nc:
 98            cores.append(0)
 99        cores += c
100    return cores
101
102def filterByDeviceCode(devices: list, code: str) -> list:
103    """
104    Filters a list of devices based on the device code, e.g. '-C' for connectors
105
106    Args:
107        devices: List of device IDs
108        code: Device Letter Code to filter by
109
110    Returns:
111        List of devices matching the filter code
112    """
113    def getCode(device) -> str:
114        _device.SetId(device)
115        return sub(r"\d", "", _device.GetName())
116
117    return [d for d in devices if getCode(d) == code]
118
119def filterByDeviceAssignment(devices: list, assignment: str) -> list:
120    """
121    Filters a list of devices based on the device assignment
122
123    Args:
124        devices: List of device IDs
125        assignment: Device Assignment to filter by
126
127    Returns:
128        List of devices matching the filter assignment
129    """
130    def getAssignment(device) -> str:
131        _device.SetId(device)
132        return _device.GetAssignment()[1:]
133
134    return [d for d in devices if getAssignment(d) == assignment]
135
136def filterByDeviceLocation(devices: list, location: str) -> list:
137    """
138    Filters a list of devices based on the device location
139
140    Args:
141        devices: List of device IDs
142        location: Device location to filter by
143
144    Returns:
145        List of devices matching the filter location
146    """
147
148    def getLocation(device) -> str:
149        _device.SetId(device)
150        return _device.GetLocation()[1:]
151
152    return [d for d in devices if getLocation(d) == location]
153
154def filterByDeviceAttribute(devices: list, attribute: str, value: str, component=False) -> list:
155    """
156    Filters a list of devices based on the device/component attribute value
157
158    Args:
159        devices: List of device IDs
160        attribute: Attribute to get value of
161        value: Value to compare the attribute again
162        component: True to use component attribute, False (default) to use device attribute
163
164    Returns:
165        List of devices matching the filter attribute
166    """
167    def getAttribute(device) -> str:
168        _device.SetId(device)
169
170        if component:
171            return _device.GetComponentAttributeValue(attribute)
172        else:
173            return _device.GetAttributeValue(attribute)
174
175    return [d for d in devices if getAttribute(d) == value]
176
177def filterByDeviceClass(devices: list, cls: str) -> list:
178    """
179    Filters a list of devices based on the device class
180
181    Args:
182        devices: List of device IDs
183        cls: Class to filter by
184
185    Returns:
186        List of devices matching the filter class
187    """
188    return filterByDeviceAttribute(devices, "Class", cls, True)
189
190def filterByDeviceRegex(devices: list, pattern: str | re.Pattern) -> list:
191    """
192    Filters a list of devices by name using a regex pattern. Function will use 're.match'
193
194    Args:
195        devices: List of device IDs
196        pattern: Regex pattern, either as a string or precompiled pattern
197
198    Returns:
199        List of devices matching the regex
200    """
201    def getName(device):
202        _device.SetId(device)
203        return _device.GetName()
204
205    if type(pattern) is str:
206        pattern = re.compile(pattern)
207    return [d for d in devices if re.match(pattern, getName(d))]
208
209def filterByComponent(devices: list, component: str) -> list:
210    """
211    Filters a list of devices based on the component name (exact match)
212
213    Args:
214        devices: List of device IDs
215        component: Component name
216
217    Returns:
218        List of devices with that component
219    """
220    def getComponent(device):
221        _device.SetId(device)
222        return _device.GetComponentName()
223    return [d for d in devices if getComponent(d) == component]
def devicesOnSheet(sheet: int, base=False) -> list:
10def devicesOnSheet(sheet: int, base=False) -> list:
11    """
12    Gets Device IDs for all the devices found on the sheet.
13    Found by going through each symbol and seeing if it connects to a Device.
14
15    Args:
16        sheet: Sheet to search through
17        base: Boolean (default False) to decide whether to return Sheet ID or base (Schematic) ID
18
19    Returns:
20        List of device IDs
21    """
22
23    Sheet = Job.CreateSheetObject()
24    Sheet.SetId(sheet)
25    base &= Sheet.IsFormboard()
26
27    devices = []
28    for sym in Sheet.GetSymbolIds(_)[1][1:]:
29        if (devId := _device.SetId(sym)) == 0:
30            continue
31
32        if base:
33            devId = _device.GetOriginalId()
34
35        if devId not in devices:
36            devices.append(devId)
37
38    return devices

Gets Device IDs for all the devices found on the sheet. Found by going through each symbol and seeing if it connects to a Device.

Arguments:
  • sheet: Sheet to search through
  • base: Boolean (default False) to decide whether to return Sheet ID or base (Schematic) ID
Returns:

List of device IDs

def deviceFormboardId(device: int, sheet: int) -> int:
40def deviceFormboardId(device: int, sheet: int) -> int:
41    """
42    Returns the device's ID on a given formboard sheet.
43    Given ID can be a formboard ID, even from a different sheet.
44
45    Args:
46        device: Device ID to search for
47        sheet: Sheet to search for device on
48
49    Returns:
50        ID of the device if found, 0 otherwise
51
52    """
53    _device.SetId(device)
54    if _device.GetFormboardSheetId() == sheet:
55        return device
56
57    if _device.IsFormboard():
58        _device.SetId(_device.GetOriginalId())
59
60    for d in _device.GetFormboardIds(_)[1][1:]:
61        _device.SetId(d)
62        if _device.GetFormboardSheetId() == sheet:
63            return d
64    return 0

Returns the device's ID on a given formboard sheet. Given ID can be a formboard ID, even from a different sheet.

Arguments:
  • device: Device ID to search for
  • sheet: Sheet to search for device on
Returns:

ID of the device if found, 0 otherwise

def tableSymbolId(device: int, sheet: int) -> int:
66def tableSymbolId(device: int, sheet: int) -> int:
67    """
68    Returns the table symbol for a device on a specific sheet
69
70    Args:
71        device: Device ID
72        sheet: Sheet ID (should be formboard)
73
74    Returns:
75        ID of the table symbol if found, 0 otherwise
76    """
77    _device.SetId(deviceFormboardId(device, sheet))
78    return _device.GetTableSymbolId()

Returns the table symbol for a device on a specific sheet

Arguments:
  • device: Device ID
  • sheet: Sheet ID (should be formboard)
Returns:

ID of the table symbol if found, 0 otherwise

def coreIds(device: int, nc=False) -> list:
 80def coreIds(device: int, nc=False) -> list:
 81    """
 82    Gets the core IDs for all wires/cables connected to a device
 83
 84    Args:
 85        device: Device ID
 86        nc: Whether to include N/C pins (will have no cores) as 0s in the list. Default False
 87    Returns:
 88        List of cores found connected to the device
 89    """
 90    Pin = Job.CreatePinObject()
 91    _device.SetId(device)
 92
 93    cores = []
 94    for p in _device.GetPinIds(_)[1][1:]:
 95        Pin.SetId(p)
 96
 97        c = list(Pin.GetCoreIds(_)[1][1:])
 98        if len(c) == 0 and nc:
 99            cores.append(0)
100        cores += c
101    return cores

Gets the core IDs for all wires/cables connected to a device

Arguments:
  • device: Device ID
  • nc: Whether to include N/C pins (will have no cores) as 0s in the list. Default False
Returns:

List of cores found connected to the device

def filterByDeviceCode(devices: list, code: str) -> list:
103def filterByDeviceCode(devices: list, code: str) -> list:
104    """
105    Filters a list of devices based on the device code, e.g. '-C' for connectors
106
107    Args:
108        devices: List of device IDs
109        code: Device Letter Code to filter by
110
111    Returns:
112        List of devices matching the filter code
113    """
114    def getCode(device) -> str:
115        _device.SetId(device)
116        return sub(r"\d", "", _device.GetName())
117
118    return [d for d in devices if getCode(d) == code]

Filters a list of devices based on the device code, e.g. '-C' for connectors

Arguments:
  • devices: List of device IDs
  • code: Device Letter Code to filter by
Returns:

List of devices matching the filter code

def filterByDeviceAssignment(devices: list, assignment: str) -> list:
120def filterByDeviceAssignment(devices: list, assignment: str) -> list:
121    """
122    Filters a list of devices based on the device assignment
123
124    Args:
125        devices: List of device IDs
126        assignment: Device Assignment to filter by
127
128    Returns:
129        List of devices matching the filter assignment
130    """
131    def getAssignment(device) -> str:
132        _device.SetId(device)
133        return _device.GetAssignment()[1:]
134
135    return [d for d in devices if getAssignment(d) == assignment]

Filters a list of devices based on the device assignment

Arguments:
  • devices: List of device IDs
  • assignment: Device Assignment to filter by
Returns:

List of devices matching the filter assignment

def filterByDeviceLocation(devices: list, location: str) -> list:
137def filterByDeviceLocation(devices: list, location: str) -> list:
138    """
139    Filters a list of devices based on the device location
140
141    Args:
142        devices: List of device IDs
143        location: Device location to filter by
144
145    Returns:
146        List of devices matching the filter location
147    """
148
149    def getLocation(device) -> str:
150        _device.SetId(device)
151        return _device.GetLocation()[1:]
152
153    return [d for d in devices if getLocation(d) == location]

Filters a list of devices based on the device location

Arguments:
  • devices: List of device IDs
  • location: Device location to filter by
Returns:

List of devices matching the filter location

def filterByDeviceAttribute(devices: list, attribute: str, value: str, component=False) -> list:
155def filterByDeviceAttribute(devices: list, attribute: str, value: str, component=False) -> list:
156    """
157    Filters a list of devices based on the device/component attribute value
158
159    Args:
160        devices: List of device IDs
161        attribute: Attribute to get value of
162        value: Value to compare the attribute again
163        component: True to use component attribute, False (default) to use device attribute
164
165    Returns:
166        List of devices matching the filter attribute
167    """
168    def getAttribute(device) -> str:
169        _device.SetId(device)
170
171        if component:
172            return _device.GetComponentAttributeValue(attribute)
173        else:
174            return _device.GetAttributeValue(attribute)
175
176    return [d for d in devices if getAttribute(d) == value]

Filters a list of devices based on the device/component attribute value

Arguments:
  • devices: List of device IDs
  • attribute: Attribute to get value of
  • value: Value to compare the attribute again
  • component: True to use component attribute, False (default) to use device attribute
Returns:

List of devices matching the filter attribute

def filterByDeviceClass(devices: list, cls: str) -> list:
178def filterByDeviceClass(devices: list, cls: str) -> list:
179    """
180    Filters a list of devices based on the device class
181
182    Args:
183        devices: List of device IDs
184        cls: Class to filter by
185
186    Returns:
187        List of devices matching the filter class
188    """
189    return filterByDeviceAttribute(devices, "Class", cls, True)

Filters a list of devices based on the device class

Arguments:
  • devices: List of device IDs
  • cls: Class to filter by
Returns:

List of devices matching the filter class

def filterByDeviceRegex(devices: list, pattern: str | re.Pattern) -> list:
191def filterByDeviceRegex(devices: list, pattern: str | re.Pattern) -> list:
192    """
193    Filters a list of devices by name using a regex pattern. Function will use 're.match'
194
195    Args:
196        devices: List of device IDs
197        pattern: Regex pattern, either as a string or precompiled pattern
198
199    Returns:
200        List of devices matching the regex
201    """
202    def getName(device):
203        _device.SetId(device)
204        return _device.GetName()
205
206    if type(pattern) is str:
207        pattern = re.compile(pattern)
208    return [d for d in devices if re.match(pattern, getName(d))]

Filters a list of devices by name using a regex pattern. Function will use 're.match'

Arguments:
  • devices: List of device IDs
  • pattern: Regex pattern, either as a string or precompiled pattern
Returns:

List of devices matching the regex

def filterByComponent(devices: list, component: str) -> list:
210def filterByComponent(devices: list, component: str) -> list:
211    """
212    Filters a list of devices based on the component name (exact match)
213
214    Args:
215        devices: List of device IDs
216        component: Component name
217
218    Returns:
219        List of devices with that component
220    """
221    def getComponent(device):
222        _device.SetId(device)
223        return _device.GetComponentName()
224    return [d for d in devices if getComponent(d) == component]

Filters a list of devices based on the component name (exact match)

Arguments:
  • devices: List of device IDs
  • component: Component name
Returns:

List of devices with that component