This is a migrated thread and some comments may be shown as answers.

Getting the classes of custom created shapes

1 Answer 96 Views
Diagram, DiagramRibbonBar, DiagramToolBox
This is a migrated thread and some comments may be shown as answers.
Jacques
Top achievements
Rank 1
Jacques asked on 31 Jan 2019, 06:11 AM

Hi,

I have made classes for custom shapes that i have added to my toolbox. All these classes are derived from ElementShape. My problem however is that i would like to get the type of each shape as the way i created them with my own separate classes. Once these custom shapes are dragged from the toolbox and dropped onto the diagram, they are identified as RadDiagramShape. This is a problem because i need to identify of which type each shape is, as the way that i have declared them. For example i have declared a class with an Input shape, and a class with an Output shape. I need to know from the diagram, which shape is of what type, for example is it an input shape or is it an output shape. Because my current code only tells me that it is a RadDiagramShape. This does not allow me to know whether it is an input or an output. I have attached screenshot of the problem and of my code. 

Thank you,
Jacques.

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 31 Jan 2019, 11:30 AM
Hi Jacques,

When creating the custom toolbox it will be necessary to also override the CreateDiagramShape method and return a custom type inheriting the base RadDiagramShape class: 
public class CustomDiagramToolBox : RadDiagramToolbox
{
    public CustomDiagramToolBox()
    {
    }
 
    protected override void CreateDataItems()
    {
        base.CreateDataItems();
 
        this.ListViewElement.CollapseAll();
 
        ListViewDataItemGroup customGroup = new ListViewDataItemGroup("Custom Group");
        this.Groups.Add(customGroup);
 
        DiagramListViewDataItem item = new DiagramListViewDataItem() { Key = "CustomShape" };
        ElementShape shape = new CustomElementShape();
        item.Shape = shape;
        item.Group = customGroup;
        this.Items.Add(item);
    }
 
    protected override RadDiagramShape CreateDiagramShape(PreviewDragStartEventArgs e)
    {
        DiagramListViewVisualItem sourceItem = e.DragInstance as DiagramListViewVisualItem;
        DiagramListViewDataItem dataItem = sourceItem.Data as DiagramListViewDataItem;
 
        if (dataItem.Shape is CustomElementShape)
        {
            return new CustomDiagramShape
            {
                Shape = dataItem.Shape,
                Text = dataItem.Key.ToString()
            };
        }
 
        return base.CreateDiagramShape(e);
    }
}
 
public class CustomElementShape : ElementShape
{
    public override GraphicsPath CreatePath(System.Drawing.Rectangle bounds)
    {
        GraphicsPath path = new GraphicsPath();
 
        path.AddLine(0, 0, bounds.Width, 0);
        path.AddLine(bounds.Width, 0, bounds.Width, bounds.Height - (bounds.Height * 0.5f));
        path.AddLine(bounds.Width, bounds.Height - (bounds.Height * 0.5f), bounds.Width / 2, bounds.Height);
        path.AddLine(bounds.Width / 2, bounds.Height, 0, bounds.Height - (bounds.Height * 0.5f));
        path.AddLine(0, bounds.Height - (bounds.Height * 0.5f), 0, 0);
 
        return path;
    }
}
 
public class CustomDiagramShape : RadDiagramShape
{
    public CustomDiagramShape()
    {
        this.Size = new System.Drawing.Size(80, 60);
        this.BackColor = System.Drawing.Color.LightBlue;
        this.DrawBorder = true;
    }
}

Then in the form, you can iterate the Shapes collection and check for the particular custom types: 
private void button1_Click(object sender, EventArgs e)
{
    foreach (Telerik.Windows.Diagrams.Core.IShape shape in this.radDiagram1.Shapes)
    {
        if (shape.GetType().IsSubclassOf(typeof(RadDiagramShape)))
        {
            Debug.WriteLine(shape.GetType().Name);
        }
    }
}

I hope this will help. Let me know if you need further assistance.

Regards,
Hristo
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Diagram, DiagramRibbonBar, DiagramToolBox
Asked by
Jacques
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or