How can I add a shape to an existing group ?
@zkormos
Please ungroup the group shape first, then add shape to the shape array, and group this array.
Please try this sample code:
Page page = diagram.Pages[0];
Shape groupShape = page.Shapes.GetShape(4);
//Ungroup shapes
page.Shapes.UnGroup(groupShape);
//Add to shape list
Shape[] shapes = new Shape[3];
for (int _shapeId = 1; _shapeId <= 3; _shapeId++)
{
shapes[_shapeId - 1] = page.Shapes.GetShape(_shapeId);
}
//Grouping shapes
page.Shapes.Group(shapes);
Unfortunatelly, this solution does not work. In the code bellow, the group2 will contain only the Shape-3
List shapes = new List();
for (int i = 1; i <= 3; i++)
{
long shapeId = page.DrawRectangle(1, i, 1, 1);
Shape shape = page.Shapes.GetShape(shapeId);
shape.Text.Value.Add(new Txt($“Shape-{i}”));
shapes.Add(shape);
}
Aspose.Diagram.Shape[] ss = new Aspose.Diagram.Shape[2]
{
shapes[0],
shapes[1]
};
Shape group1 = page.Shapes.Group(ss);
page.Shapes.UnGroup(group1);
Shape group2 = page.Shapes.Group(shapes.ToArray());
@zkormos
Thanks for the sample code.
Please add these lines after ungrouping the shapes.
Shape group1 = page.Shapes.Group(ss);
page.Shapes.UnGroup(group1);
//Add these lines
var list = new List<Shape>();
foreach (Shape shape in diagram.Pages[0].Shapes)
{
//Add the shapes you want to group
list.Add(shape);
}
Shape group2 = page.Shapes.Group(list.ToArray());
//end
1 Like