I am on version 5.2.0.46. Prototyping using the BasicApp, I want to allow users to move selected nodes to different layers. When I invoke GoLayer.Add method it always returns System.ArgumentException, “Cannot add an object to a layer when it is already part of a different document’s or view’s layer.”.
The objects were created using the BasicApp’s InsertNode method without modification. What did I do wrong?
Here is the snippets used. Thanks.
static int MaxLayerIdx = 2;
//create multiple layers (in this case 2).
private void CreateLayers()
{
GoLayer topMostLayer, bottomMostLayer;
topMostLayer = this.goView1.Layers.Default;
bottomMostLayer = this.goView1.Layers.Default;
this.goView1.Layers.Default.Identifier = "0";
for (int i = 1; i <= MaxLayerIdx; i++)
{
topMostLayer = this.goView1.Layers.CreateNewLayerAfter(topMostLayer);
topMostLayer.Identifier = i.ToString();
bottomMostLayer = this.goView1.Layers.CreateNewLayerBefore(bottomMostLayer);
bottomMostLayer.Identifier = "-" + i.ToString();
}
}
public void MoveObjectsLayerUp()
{
if (this.goView1 == null || this.goView1.Selection.Primary == null)
{
return;
}
GoDocument doc = goView1.Document;
doc.StartTransaction();
foreach (GoObject obj in this.goView1.Selection.ToList())
{
int layerIdx = int.Parse(obj.Layer.Identifier.ToString());
if (layerIdx < MaxLayerIdx)
{
var newLayer = this.goView1.Layers.Find((layerIdx + 1).ToString());
newLayer.Add(obj);//always return System.ArgumentException, "Cannot add an object to a layer when it is already part of a different document's or view's layer."
}
}
doc.FinishTransaction("Move objects up 1 layer");
}
private void button1_Click(object sender, EventArgs e)
{
this.MoveObjectsLayerUp();
}
Layers are part of the document… all objects added to the Document are added to a Layer.
So… to change the selection to a different layer, you have to remove it from the one it is in and add it to the new one.
doc.Remove(obj);
layer.Add(obj);
If you just want to pop the object to the top of the layer it is in…
// When an object is selected, move it forward/frontward in the Z-order,
// so that it won't be occluded/hidden by another Display or its Items.
private void goView1_ObjectGotSelection(object sender, Northwoods.Go.GoSelectionEventArgs e) {
GoObject top = e.GoObject.TopLevelObject;
top.Layer.MoveAfter(null, top);
}
Thank you for your reply. I do want to maintain different layers so that I can categorize and do other things (such as show/hide) at the layer level later.
Here is the layer.add() method’s documentation remarks and with my understandings in bold:
The obj must not already belong to a different document or view, nor to a group. (does not apply. Object is in the same document, but in a different layer.)
If the object already belongs to this layer, nothing happens. (could be a possible scenario, but this particular example does not apply)
The object’s GoObject.Layer property will be changed to be this layer. If the object already belonged to a different layer in this same document or view, the Changed hint will be ChangedObjectLayer, otherwise it will be InsertedObject. (Shouldn’t this scenario apply to my example, object already belonged to a different layer and will be change to the designated layer after the call?)
As for your suggestion of remove from one layer then add to the other layer, if that is the only route, any suggestions on maintaining links, node positions? In other words, make users feel that they are just pull or push objects into different layers while maintaining everything else?
and it works… even with links attached. Note the “TopLevelObject” there… it might be your were trying to move a Port or something else that was part of the node (group).
I have double checked and traced my code and am sure that I am indeed moving GoBasicNode. However, the exception is still there. Can I email my sample project to you privately? If so, what is the your email address?
Thank you very much for your help! Indeed, change my code from goView1.Layers to goView1.Document.Layers works!
I didn’t realize that layers can be created at both view and document level. I believe at the document level is what I need. Do you have any best practices/recommendations on the usage of document level layers vs view level layers?