Profile Picture

Creating a new shape from Visio Stencil

Posted By Anthony Suralta 12 Years Ago
Author
Message
Anthony Suralta
Posted 12 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: Forum Members
Last Active: 12 Years Ago
Posts: 1, Visits: 1

Hi,

I am trying to create a new shape and add the shape into a document. However I am getting an error "An item with the same key has already been added." when I try to add the same shape to a document. This happens even if I  assign a new unique ID to the shape. I am creating the shape from an Nmodel object obtained from the stencil library.

 

The error is on this line of code ((NLayer)document.Layers.Children(null)[layerID]).AddChild((NShape)newShape);

What am I doing wrong here? Thanks.

Here's my code

private void CreateShapesFromVisioDocPage(Aspose.Diagram.Page page)

{

listShapes.Items.Clear();

listConnects.Items.Clear();

if (page.PageSheet.Layers.Count > 0)

{

NLayer nlayer = new NLayer();

foreach (Aspose.Diagram.Layer layer in page.PageSheet.Layers)

{

nlayer = new NLayer();

nlayer.Name = layer.Name.Value;

nlayer.Id = layer.IX;

document.Layers.AddChild(nlayer);

listLayers.Items.Add(layer.IX.ToString() + "-" + layer.Name.Value);

}

document.ActiveLayerUniqueId = ((NLayer)document.Layers.Children(null)[document.Layers.ChildrenCount(null) - 1]).UniqueId;

}

if (page.Shapes.Count > 0)

{

Random rndm = new Random(DateTime.Now.Millisecond);

int layers = 0;

foreach (Aspose.Diagram.Shape shape in page.Shapes)

{

string shapeLabel = shape.LayerMem.LayerMember.Value == "0" ? "Background" : (shape.Master == null ? shape.Name : shape.Master.Name);

listShapes.Items.Add(shape.ID.ToString() + "-" + shapeLabel);

if (shapeLabel != "Background")

{

try

{

//// create a shape in Nevron

AddShapeToDocument(shapeLabel, new NRectangleF(

(float)shape.XForm.LocPinX.Value,

(float)shape.XForm.LocPinY.Value,

(float)shape.XForm.Width.Value,

(float)shape.XForm.Height.Value), layers, shape.ID);

layers++;

}

catch (Exception ex)

{

string error = ex.Message;

}

}

else

{

//AddBackgroundToDocument(shape);

}

}

if (page.Connects.Count > 0)

{

foreach (Aspose.Diagram.Connect connect in page.Connects)

{

listConnects.Items.Add("From shape " + connect.FromSheet + " to shape" + connect.ToSheet);

}

}

}

}

private void AddShapeToDocument(string shapeName, NRectangleF bounds, int layerID, long id)

{

Random rndm = new Random(DateTime.Now.Millisecond);

//// create a device mapped to future device for demonstration

NModel newShape = ((IPAAShapes.GetChildByName(shapeName) as NMaster).Nodes[0].Clone() as NModel);

newShape.Tag = new ShapeInfo { Device = new DeviceImport(), ShapeType = shapeName };

newShape.Bounds = new NRectangleF(rndm.Next(900), rndm.Next(900), 25f, 25f);

newShape.UniqueId = Guid.NewGuid();

(newShape.Tag as ShapeInfo).Device.ShapeGuid = newShape.UniqueId;

((NShape)newShape).CreateShapeElements(ShapeElementsMask.Ports);

((NShape)newShape).Id = (int)id;

((NShape)newShape).UniqueId = Guid.NewGuid();

((NShape)newShape).Name = "shapeName" + layerID.ToString();

NDynamicPort port = new NDynamicPort(newShape.UniqueId, ContentAlignment.MiddleCenter, DynamicPortGlueMode.GlueToContour);

((NShape)newShape).Ports.AddChild(port);

((NShape)newShape).Ports.DefaultInwardPortUniqueId = port.UniqueId;

//document.ActiveLayer.AddChild(oldShape);

((NLayer)document.Layers.Children(null)[layerID]).AddChild((NShape)newShape);

}





Similar Topics


Reading This Topic