Document.MaintainsPartId problem


When I add nodes to a GoView.Document collection with the MaintainsPartId property set to true, occasionally the partID of the added part gets changed by GoDiagram. This is causing me a big problem as once the partID is changed, I can no longer reference the part. For example:
BasicNode node = BuildNode(tile.ConfigType, tile.PartId, tile.ConfigId, tile.X, tile.Y);
Debug.WriteLine("Before: " + node.PartID);
Debug.WriteLine("After: " + node.PartID);
…results in
Before 122
After 125
Any ideas?


Had you explicitly set the node’s PartID to 122 in your BuildNode method?
Is there any chance that you add the node to the document within your BuildNode method, before you set PartID? Of course you cannot set the PartID once it is already part of a document, but maybe you didn’t realize it (and maybe we should check for that).
Alternatively, and perhaps more likely, is there any chance that some other part in the document already has ID 122, at the time you add the node to the document?
Basically, when you add an IGoIdentifiablePart to a GoDocument, it checks the PartID. If the value is not -1 because you had already initialized it to some ID, then if the document doesn’t know about any part with that ID, the document just remembers it. If the document already knows about a part with that ID and that part is the same object, then no-op. Otherwise it assigns the object to a new ID, which might be happening in your case.


Right - this is completely my fault! I was storing the PartIDs of each node in an XML file, then restoring the nodes from the XML file. What I did not realise is that you also need to store and restore the PartID’s of each Port associated with each node - in my case I only have one port per node. Once again many thanks to Walter for spotting this in an instant


OK now I’ve got the nodes saving and loading correctly, I’m working on saving and loading the links. Currently these are being renumbered just as the nodes were being renumbered. What information do I need to store for each link in order to restore it with the same PartID as when it was created? My current code for loading a saved link looks something like this…
GoLabeledLink l = new GoLabeledLink();
l.PartID = myPartId;
l.FromPort = fromNode.Port;
l.ToPort = toNode.Port;

Debug.WriteLine("before: " + l.PartID.ToString();
Debug.WriteLine("after: " + l.PartID.ToString();
Gives me different PartIDs for every link.
Update: If I create GoLinks instead of GoLabeledLinks the PartIDs do not get renumbered.


This is a bug with GoLabeledLinks. (The problem is that GoLabeledLink implements PartID by delegating to its RealLink, a GoLink. So that means there are effectively two different objects with the same PartID.)
But it’s very likely that this shouldn’t be a problem for you, since most applications don’t really need to refer to the PartID of a link. So the fact that the PartID changes each time you reload a document shouldn’t disrupt your application.