Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Cannot correctly load custom shapes (Read 1923 times)
Centron
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 24
Joined: Feb 11th, 2015
Cannot correctly load custom shapes
Mar 10th, 2015 at 10:56am
Print Post  
When I save a diagram with custom shapes to a string, and load the diagram from that string, the custom shapes appear as rounded rectangles instead of their original shape. I have put together a simple test program to illustrate the issue:
Code
Select All
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <diag:TabbedDiagramView Name="diag" Grid.Row="1" />
        <Button Name="Load"  Height="50" Click="AddCustomShape" HorizontalAlignment="Left">
            Add Custom Shape
        </Button>
        <Button Name="Save"  Height="50" Click="ReloadDocument" HorizontalAlignment="Center">
            Reload the document
        </Button>
    </Grid>
 



Code
Select All
public MainWindow()
        {
            InitializeComponent();
            //Diagram.RegisterItemClass(typeof(DiagramCustomNode), "my:DiagramCustomNode", 1);
            diag.Document = new DiagramDocument();
        }

        private void AddCustomShape(object sender, System.Windows.RoutedEventArgs e)
        {
            string xml = "<Shape id=\"Monitor\" fill-mode=\"Winding\"> <outline> <round-rectangle dash-style=\"Solid\" width=\"-1\" x=\"14\" y=\"8.25\" w=\"76.5\" h=\"63\" r=\"5\" /> </outline> <decorations> <round-rectangle dash-style=\"Solid\" width=\"-1\" x=\"20\" y=\"14.75\" w=\"63.5\" h=\"48\" r=\"5\" /> <round-rectangle dash-style=\"Solid\" width=\"-1\" x=\"39\" y=\"74.75\" w=\"29.5\" h=\"13\" r=\"5\" /> </decorations> <shape-decorations /></Shape>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            Shape s = new Shape(new byte[] { 0, 0, 1, 1, 2, 2 });
            if (diag.Document == null)
                diag.Document = new DiagramDocument();
            if (diag.Document.Pages.Count == 0)
                diag.Document.Pages.Add(new DiagramPage());
            s.LoadFromXml(doc.ChildNodes[0] as XmlElement, new XmlPersistContext(diag.Document.Pages[0], doc, 1));
            diag.Document.Pages[0].Items.Add(new ShapeNode() { Shape = s });
        }

        private void ReloadDocument(object sender, RoutedEventArgs e)
        {
            string s = diag.Document.SaveToString();
            diag.Document.LoadFromString(s);
        } 



The first method, adding a custom shape originally created in the shape designer, works fine. It's the symbol for a computer monitor. Click the first button as often as you want and spread out the nodes. Reloading the document, however, turns all the monitor shapes into round rectangles.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Cannot correctly load custom shapes
Reply #1 - Mar 10th, 2015 at 2:47pm
Print Post  
Shapes are saved in diagram files only by their Id attribute if it's present. Later when loading, ShapeNode looks for registered shape with the saved id, and if it's missing you'll get the default round-rect shape.

Shapes are registered automatically if you call Shape constructor with an id parameter, or load a ShapeLibrary object. So you could fix this either by passing "Monitor" to the Shape constructor you invoke, or by loading your custom shapes using ShapeLibrary.LoadFromXml method before loading the diagram file.

Alternatively, remove the Monitor id from the shape XML string. In that case ShapeNodes would save and load the full shape definitions rather than only their identifiers. However Shape instances would not be shared between nodes in that case.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint