Using Shapes

Hi,

I've been trying to produce a visio diagram showing a Server network.

I have created a blank 'Network' style visio diagram, and saved it as a template.

The following code compiles and runs, but the output file does not open directly, and when I download it and then try to open it, errors are displayed.

Any help in spottting the error would be much appreciated.

Thanks...

   Private Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
    <SPAN style="COLOR: blue">Dim</SPAN> st <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">FileStream</SPAN>(Server.MapPath(<SPAN style="COLOR: #a31515">"~\Drawing1.vdx"</SPAN>), <SPAN style="COLOR: #2b91af">FileMode</SPAN>.Open)

    vsdDiagram = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Diagram</SPAN>(st)

    <SPAN style="COLOR: blue">Dim</SPAN> i1 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN> = AddItem(<SPAN style="COLOR: #a31515">"Fred"</SPAN>, 0, 6)
    <SPAN style="COLOR: blue">Dim</SPAN> i2 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN> = AddItem(<SPAN style="COLOR: #a31515">"Wilma"</SPAN>, 0, 4)
    <SPAN style="COLOR: blue">Dim</SPAN> i3 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN> = AddItem(<SPAN style="COLOR: #a31515">"Barney"</SPAN>, 0, 2)
    <SPAN style="COLOR: blue">Dim</SPAN> i4 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN> = AddItem(<SPAN style="COLOR: #a31515">"Betty"</SPAN>, 0, 0)

    ' note: i1, i2, i3 and i4 are all non-zero at this point</PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px">        <SPAN style="COLOR: green">'   AddConnector(i1, i2)</SPAN>
    <SPAN style="COLOR: green">'   AddConnector(i2, i3)</SPAN>
    <SPAN style="COLOR: green">'   AddConnector(i3, i4)</SPAN>

    st.Close()
    <SPAN style="COLOR: blue">Me</SPAN>.Response.ContentType = <SPAN style="COLOR: #a31515">"application/vnd.ms-visio"</SPAN>
    <SPAN style="COLOR: blue">Me</SPAN>.Response.AppendHeader(<SPAN style="COLOR: #a31515">"Content-Disposition"</SPAN>, <SPAN style="COLOR: #a31515">"attachment; filename=Diagram.vdx"</SPAN>)
    <SPAN style="COLOR: blue">Me</SPAN>.Response.Flush()
    <SPAN style="COLOR: blue">Dim</SPAN> str <SPAN style="COLOR: blue">As</SPAN> System.IO.<SPAN style="COLOR: #2b91af">Stream</SPAN> = <SPAN style="COLOR: blue">Me</SPAN>.Response.OutputStream
    vsdDiagram.Save(str, Aspose.Diagram.<SPAN style="COLOR: #2b91af">SaveFileFormat</SPAN>.VDX)
    <SPAN style="COLOR: blue">Me</SPAN>.Response.End()

<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Sub</SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"> <SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Function</SPAN> AddItem(<SPAN style="COLOR: blue">ByVal</SPAN> sText <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">String</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> lX <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Long</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> lY <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Long</SPAN>) <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN>

    <SPAN style="COLOR: green">'Get first page</SPAN>
    <SPAN style="COLOR: blue">If</SPAN> vsdDiagram.Pages.Count = 0 <SPAN style="COLOR: blue">Then</SPAN>
        <SPAN style="COLOR: blue">Return</SPAN> 0
    <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>

    <SPAN style="COLOR: blue">Dim</SPAN> page0 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN> = vsdDiagram.Pages(0)
    <SPAN style="COLOR: green">'Get the rectangle master</SPAN>
    <SPAN style="COLOR: blue">Dim</SPAN> masterRectangle <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN> = <SPAN style="COLOR: blue">Nothing</SPAN>

    <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> master <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN> <SPAN style="COLOR: blue">In</SPAN> vsdDiagram.Masters
        <SPAN style="COLOR: blue">If</SPAN> master.Name = <SPAN style="COLOR: #a31515">"Server"</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
            masterRectangle = master
            <SPAN style="COLOR: blue">Exit For</SPAN>
        <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
    <SPAN style="COLOR: blue">Next</SPAN> master

    <SPAN style="COLOR: blue">If</SPAN> masterRectangle <SPAN style="COLOR: blue">Is</SPAN> <SPAN style="COLOR: blue">Nothing</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
        <SPAN style="COLOR: blue">Return</SPAN> 0
    <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>

    <SPAN style="COLOR: green">'Get next shape ID</SPAN>
    <SPAN style="COLOR: blue">Dim</SPAN> nextID <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Long</SPAN> = -1L

    <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> page <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN> <SPAN style="COLOR: blue">In</SPAN> vsdDiagram.Pages
        <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> shape <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> <SPAN style="COLOR: blue">In</SPAN> page.Shapes
            <SPAN style="COLOR: blue">Dim</SPAN> temp <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Long</SPAN> = GetMaxShapeID(shape)

            <SPAN style="COLOR: blue">If</SPAN> temp > nextID <SPAN style="COLOR: blue">Then</SPAN>
                nextID = temp
            <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>

        <SPAN style="COLOR: blue">Next</SPAN> shape
    <SPAN style="COLOR: blue">Next</SPAN> page
    nextID += 1

    <SPAN style="COLOR: green">'Set shape properties and add it in the shapes' collection</SPAN>
    <SPAN style="COLOR: blue">Dim</SPAN> rectangle <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>()
    rectangle.Master = masterRectangle
    rectangle.MasterShape = masterRectangle.Shapes(0)
    rectangle.ID = nextID
    rectangle.XForm.PinX.Value = lX
    rectangle.XForm.PinY.Value = lY
    rectangle.Type = <SPAN style="COLOR: #2b91af">TypeValue</SPAN>.Shape
    rectangle.Text.Value.Add(<SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Txt</SPAN>(sText))
    rectangle.TextStyle = vsdDiagram.StyleSheets(3)

    <SPAN style="COLOR: green">'rectangle.Line.LineColor.Value = page0.Shapes(1).Fill.FillForegnd.Value</SPAN>
    <SPAN style="COLOR: green">'rectangle.Line.LineWeight.Value = 0.030416666666666672</SPAN>
    <SPAN style="COLOR: green">'rectangle.Line.Rounding.Value = 0.1</SPAN>
    <SPAN style="COLOR: green">'rectangle.Fill.FillBkgnd.Value = page0.Shapes(0).Fill.FillBkgnd.Value</SPAN>
    <SPAN style="COLOR: green">'rectangle.Fill.FillForegnd.Value = "#ebf8df"</SPAN>

    page0.Shapes.Add(rectangle)

    <SPAN style="COLOR: blue">Return</SPAN> nextID

<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Function</SPAN></PRE>

Dear Steve,

Please replace the following line

rectangle.Type = TypeValue.Shape

with the following line of code.

rectangle.Type = TypeValue.Group

Please also note that this is just a temporary solution. API to add new shapes and connectors is still not available. You will be able to add shapes and connectors easily once we implement these features.

Best Regards,

Muhammad,

Thanks for your quick response.

Do you have an approximate delivery date for the version with easier API's for shapes and connectors?

Cheers,

Steve

Dear Steve,

These features have been planned for the next quarter (Q4 2011) so you can expect these features till the end of this year.

Please feel free to contact us in case you have further comments or questions.

Best Regards,

OK,

Thanks for that. That means I have to carry on trying to get the current version to work :)

I have changed that one line, and re-ran the code.

The output only displayed one of the items ('Wilma'). An error file was generated when trying to load it up in Visio:
[Warning] DataType:
Context: Line 1 --- ID='0'
Description: An attribute in this file contains data that is valid but inconsistent with the rest of the file. Visio has ignored the element that contains the attribute.

[Warning] DataType:
Context: Line 1 --- ID='2'
Description: This file contains a duplicate of an element that can exist only once. Visio has ignored the duplicate element.

[Warning] DataType:
Context: Line 1 --- ID='3'
Description: This file contains a duplicate of an element that can exist only once. Visio has ignored the duplicate element.

Does this surprise you? (and is there a work-around?)

Cheers,

Steve

Dear Steve,

The exception shows that there are some duplicate IDs. To avoid this exception, you can start next ID from 100 or 1000 or you can replace the following line in your code

nextID += 1

with the following line.

nextID += 100

This will resolve the issue but again this is just a workaround and you cannot consider it as an ideal solution for every situation.

Please feel free to contact us in case you have further comments or questions.

Best Regards,