Reusing partids

As nodes are added and deleted from a document do the partids increase forever or are they reused?

Forever. This is desired to avoid any confusion when re-adding a deleted part, or on undo/redo.

I have an example where a label node with 2 ports is deleted and a new one added. The new one uses the same partids as the deleted one. In addition the partid of one of the ports is the same as another nodes partid.
How does it work out what is the next partid to use?

Well, the GoDocument code to assign IGoIdentifiablePart.PartID values keeps an internal counter. It only increments that counter, looking for the first value that isn’t already in the hashtable referring to some IGoIdentifiablePart.
When I tried in Demo1 what you saw, I found it worked fine.
But I have discovered that there can be a problem with documents that have been deserialized or loaded from some earlier document state. The issue is that the internal counter is also document state that needs to be saved and restored. In the next release, we’ll make that internal counter public by adding a GoDocument.LastPartID property. GoDocument.EnsureUniquePartID will also make sure that GoDocument.LastPartID is no less than the maximum PartID in the document.
Since this property is not public in the current release, you cannot easily save and restore it.
Here are some document methods that you can use when storing your document, to find the current maximum PartID that is in use:
public int FindMaxPartID() {
int maxid = -1;
foreach (GoObject obj in this) {
maxid = Math.Max(maxid, MaxPartID(obj));
return maxid;
private int MaxPartID(GoObject obj) {
int m = -1;
IGoIdentifiablePart p = obj as IGoIdentifiablePart;
if (p != null) {
m = Math.Max(m, p.PartID);
GoGroup g = obj as GoGroup;
if (g != null) {
foreach (GoObject o in g.GetEnumerator()) {
m = Math.Max(m, MaxPartID(o));
return m;
When loading your saved document, you cannot set the not-yet-public property GoDocument.LastPartID. In order to get that effect, you’ll need to add “n” IGoIdentifiableParts to the document, before you start adding any real objects. Here’s another document method you could use:
public void IncrementLastPartID(int n) {
GoPort p = new GoPort(); // a simple IGoIdentifiablePart
for (int i = 0; i < n; i++) {
p.PartID = -1; // get a new PartID assigned when Add’ed
Remove§; // might as well reuse the same port

I think i will just maintain the partid myself.