2022-09-21 12:28:58 +02:00
|
|
|
---
|
2022-10-18 21:00:50 +02:00
|
|
|
title: Part.getId()
|
2022-09-21 12:28:58 +02:00
|
|
|
---
|
|
|
|
|
2022-12-14 12:52:37 -08:00
|
|
|
The `Part.getId()` method will return an integer the can be used as an
|
2022-09-21 12:28:58 +02:00
|
|
|
for ID Points/Paths/Snippets. This method will ensure the ID is unique by
|
|
|
|
keeping an internal incremental counter of the IDs that have been used.
|
|
|
|
It is typically used when programatically adding points, paths, or snippets.
|
|
|
|
|
2024-09-28 13:13:48 +02:00
|
|
|
:::tip
|
2022-10-18 21:00:50 +02:00
|
|
|
This method can be destructured as `getID`
|
|
|
|
in [a part's draft method](/reference/api/part/draft).
|
2024-09-28 13:13:48 +02:00
|
|
|
:::
|
2022-10-18 21:00:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Part.getId() signature
|
2022-09-21 12:28:58 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
int|string getId(prefix='')
|
|
|
|
```
|
|
|
|
|
2022-12-14 12:52:37 -08:00
|
|
|
This method takes an optional parameter that will be used as a prefix for the ID.
|
2022-09-21 12:28:58 +02:00
|
|
|
|
2022-10-18 21:00:50 +02:00
|
|
|
## Part.getId() example
|
2022-09-21 12:28:58 +02:00
|
|
|
|
|
|
|
```js
|
|
|
|
cont part = {
|
|
|
|
name: 'examples.getid',
|
|
|
|
draft: ({ Point, points, getId, part }) => {
|
|
|
|
for (let i=0;i<10;i++) {
|
|
|
|
points[getId()] = new Point(i*10, i*10)
|
|
|
|
}
|
|
|
|
|
|
|
|
return part
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|