Sample code for connector?

Could you please provide sample code for creating a new connector going from Shape1 to Shape2.

I cant seem to get my connector to show up.

Also, it would be nice to be able to programatically route the connector path. (x indicates a path pin point containing the new vector)

Here is some sample code of how I wish it worked.

var connect = new Connect();
page0.Connects.Add(connect);
connect.fromShape = shape1;
connect.fromShape.snapPoint = rightMiddle; //The connector starts on the right side of the shape
connect.toShape = shape2;
connect.toShape.snapPoint = leftMiddle //The connector ends at the left of the shape
connect.addPathPin(3, 90); //Go 3 pixels from connection start and rotate 90 degrees (down)
connect.addPathPin(2, 90); //Go 2 pixels from the last pathPin and rotate 90 degrees (left)
connect.addPathPin(10, -90); //Go 10 pixels from the last pathPin and rotate -90 degrees (down)
connect.addPathPin(2,-90); //Go 2 pixels from the last pinPath and rotate -90 degrees (right)
//From this point the connect should find its own way to the leftMiddle of the toShape

[Shape1]—x

|

x------------x

|

x–>[Shape2]

Hi,

I've taken the code above, converted it to VB (hopefully!), and used the diagram file you had in the zip file.

Now, when I run the code, it falls over at the 'save' method, with the error:

Error in element Shapes in \VisioDocument\Pages\Page[0]. Object reference not set to an instance of an object.

The code I have used is:

(Note: I had to remove .ID from the line connector.Master = dynamicConnectorMaster.ID;)

Can you confirm whether the problem is with my code translation, or something else?

Many Thanks!

<?XML:NAMESPACE PREFIX = O />

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

    PopulateDiagram2()

    <SPAN style="COLOR: green">'Save inpupt VSD as VDX</SPAN>
     vsdDiagram.Save(<SPAN style="COLOR: #a31515">"Drawing3.vdx"</SPAN>, <SPAN style="COLOR: #2b91af">SaveFileFormat</SPAN>.VDX)

    st.Close()
<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"><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Sub</SPAN> PopulateDiagram2()
    <SPAN style="COLOR: blue">Dim</SPAN> dynamicConnectorMaster <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Master</SPAN>

    <SPAN style="COLOR: green">'Search for the dynamic connector master.</SPAN>
    <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oMaster <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> oMaster.Name = <SPAN style="COLOR: #a31515">"Dynamic connector"</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
            dynamicConnectorMaster = oMaster
            <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>

    <SPAN style="COLOR: green">'Now we can not read the master from stencil, so exit if the master was not found.</SPAN>
    <SPAN style="COLOR: blue">If</SPAN> dynamicConnectorMaster <SPAN style="COLOR: blue">Is</SPAN> <SPAN style="COLOR: blue">Nothing</SPAN> <SPAN style="COLOR: blue">Then</SPAN>
        <SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: #a31515">"The dynamic connector master was not found."</SPAN>)
    <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>

    <SPAN style="COLOR: blue">Dim</SPAN> oPage <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN> = vsdDiagram.Pages(0)

    <SPAN style="COLOR: blue">Dim</SPAN> shape1 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = findShapeByID(oPage, 1)

    <SPAN style="COLOR: blue">Dim</SPAN> oConnection <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN> = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()

    oConnection.IX = 0
    oConnection.X.Value = shape1.XForm.LocPinX.Value
    oConnection.Y.Value = 0
    oConnection.DirX.Value = 0
    oConnection.DirY.Value = 1
    shape1.Connections.Add(oConnection)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 1
    oConnection.X.Value = shape1.XForm.Width.Value
    oConnection.Y.Value = shape1.XForm.LocPinY.Value
    oConnection.DirX.Value = -1
    oConnection.DirY.Value = 0
    shape1.Connections.Add(oConnection)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 2
    oConnection.X.Value = shape1.XForm.LocPinX.Value
    oConnection.Y.Value = shape1.XForm.Height.Value
    oConnection.DirX.Value = 0
    oConnection.DirY.Value = -1
    shape1.Connections.Add(oConnection)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 3
    oConnection.X.Value = 0
    oConnection.Y.Value = shape1.XForm.LocPinY.Value
    oConnection.DirX.Value = 1
    oConnection.DirY.Value = 0
    shape1.Connections.Add(oConnection)

    <SPAN style="COLOR: blue">Dim</SPAN> shape2 <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = findShapeByID(oPage, 2)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 0
    oConnection.X.Value = shape2.XForm.LocPinX.Value
    oConnection.Y.Value = 0
    oConnection.DirX.Value = 0
    oConnection.DirY.Value = 1
    shape2.Connections.Add(oConnection)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 1
    oConnection.X.Value = shape2.XForm.Width.Value
    oConnection.Y.Value = shape2.XForm.LocPinY.Value
    oConnection.DirX.Value = -1
    oConnection.DirY.Value = 0
    shape2.Connections.Add(oConnection)
    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 2
    oConnection.X.Value = shape2.XForm.LocPinX.Value
    oConnection.Y.Value = shape2.XForm.Height.Value
    oConnection.DirX.Value = 0
    oConnection.DirY.Value = -1
    shape2.Connections.Add(oConnection)

    oConnection = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Connection</SPAN>()
    oConnection.IX = 3
    oConnection.X.Value = 0
    oConnection.Y.Value = shape2.XForm.LocPinY.Value
    oConnection.DirX.Value = 1
    oConnection.DirY.Value = 0
    shape2.Connections.Add(oConnection)

    <SPAN style="COLOR: blue">Dim</SPAN> oConnector <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> = <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>()
    oConnector.Master = dynamicConnectorMaster  <SPAN style="COLOR: green">' HAD TO REMOVE .ID FROM THIS LINE</SPAN>
    oConnector.ID = 3 <SPAN style="COLOR: green">'normally you have to find the maximum of shape IDs and take next value</SPAN>
    oConnector.Type = <SPAN style="COLOR: #2b91af">TypeValue</SPAN>.Shape

    oConnector.XForm1D.BeginX.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X1,{1}!Connections.Y1))"</SPAN>, _
        ShapeToFormulaString(shape1, vsdDiagram), ShapeToFormulaString(shape1, vsdDiagram))

    oConnector.XForm1D.BeginY.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X1,{1}!Connections.Y1))"</SPAN>, _
            ShapeToFormulaString(shape1, vsdDiagram), ShapeToFormulaString(shape1, vsdDiagram))

    oConnector.XForm1D.EndX.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X3,{1}!Connections.Y3))"</SPAN>, _
        ShapeToFormulaString(shape2, vsdDiagram), ShapeToFormulaString(shape2, vsdDiagram))

    oConnector.XForm1D.EndY.Ufe.F = <SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"PAR(PNT({0}!Connections.X3,{1}!Connections.Y3))"</SPAN>, _
            ShapeToFormulaString(shape2, vsdDiagram), ShapeToFormulaString(shape2, vsdDiagram))

    oPage.Shapes.Add(oConnector)


<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Sub</SPAN></PRE></SPAN></PRE><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px"><SPAN style="COLOR: blue"><PRE style="FONT-FAMILY: Consolas; BACKGROUND: white; COLOR: black; FONT-SIZE: 13px">    <SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Function</SPAN> findShapeByID(<SPAN style="COLOR: blue">ByVal</SPAN> oPage <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Page</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> id <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">Integer</SPAN>) <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>
    <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oShape <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN> <SPAN style="COLOR: blue">In</SPAN> oPage.Shapes
        <SPAN style="COLOR: blue">If</SPAN> oShape.ID = id <SPAN style="COLOR: blue">Then</SPAN>
            <SPAN style="COLOR: blue">Return</SPAN> oShape
        <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
    <SPAN style="COLOR: blue">Next</SPAN>
    <SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: blue">String</SPAN>.Format(<SPAN style="COLOR: #a31515">"The shape with ID = '{0}' was not found."</SPAN>, id))
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Function</SPAN>

<SPAN style="COLOR: blue">Private</SPAN> <SPAN style="COLOR: blue">Function</SPAN> ShapeToFormulaString(<SPAN style="COLOR: blue">ByVal</SPAN> oShape <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Shape</SPAN>, <SPAN style="COLOR: blue">ByVal</SPAN> oDiagram <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: #2b91af">Diagram</SPAN>) <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">String</SPAN>

    <SPAN style="COLOR: blue">Dim</SPAN> nameU <SPAN style="COLOR: blue">As</SPAN> <SPAN style="COLOR: blue">String</SPAN> = oShape.NameU

    <SPAN style="COLOR: blue">If</SPAN> (nameU & <SPAN style="COLOR: #a31515">""</SPAN>).Length > 0 <SPAN style="COLOR: blue">Then</SPAN>
        <SPAN style="COLOR: blue">Return</SPAN> nameU
    <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>

    <SPAN style="COLOR: blue">For</SPAN> <SPAN style="COLOR: blue">Each</SPAN> oMaster <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> oMaster.ID = oShape.Master.ID <SPAN style="COLOR: blue">Then</SPAN>
            <SPAN style="COLOR: blue">Return</SPAN> oMaster.NameU + <SPAN style="COLOR: #a31515">"."</SPAN> & oShape.ID
        <SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">If</SPAN>
    <SPAN style="COLOR: blue">Next</SPAN>

    <SPAN style="COLOR: blue">Throw</SPAN> <SPAN style="COLOR: blue">New</SPAN> <SPAN style="COLOR: #2b91af">ApplicationException</SPAN>(<SPAN style="COLOR: #a31515">"Cannot convert shape to its formula string."</SPAN>)
<SPAN style="COLOR: blue">End</SPAN> <SPAN style="COLOR: blue">Function</SPAN></PRE></SPAN></PRE>

Thanks Muhammad, that works great.

Hi there,

I've noticed that there's a 'connect' class in Aspose.diagram.

Does that mean that the code in this thread is out-of-date, and if so, can you point me to any more recent working code anywhere?

Thanks,

Steve

Hi Steve,

Connect class was already there and this code is not outdated yet however our developers are working on providing simple API to add connectors and we will update you in this thread once it is completed.

Our developers have already added the features to play with the connectors. You can use MoveTo and LineTo classes to reset the path of the connectors according to your requirement as you can see in the following example.

Diagram diagram = new Diagram("Coordinates.vsd", LoadFileFormat.VSD);

//Find connector shape

Shape shape = diagram.Pages[0].Shapes[0];

Geom geom = shape.Geoms[0];

//Add MoveTo

MoveTo moveTo = new MoveTo();

moveTo.IX = geom.NextCoordinateIX;

moveTo.X.Value = 0;

moveTo.Y.Value = 0;

geom.CoordinateCol.Add(moveTo);

//Add LineTo

LineTo lineTo = new LineTo();

lineTo.IX = geom.NextCoordinateIX;

lineTo.X.Value = 1;

lineTo.Y.Value = 1;

geom.CoordinateCol.Add(lineTo);

//Add ArcTo

ArcTo arcTo = new ArcTo();

arcTo.IX = geom.NextCoordinateIX;

arcTo.X.Value = 2;

arcTo.Y.Value = 2;

arcTo.A.Value = 2;

geom.CoordinateCol.Add(arcTo);

//Add Ellipse

Ellipse ellipse = new Ellipse();

ellipse.IX = geom.NextCoordinateIX;

ellipse.X.Value = -1;

ellipse.Y.Value = 1;

ellipse.A.Value = 0;

ellipse.B.Value = 0;

ellipse.C.Value = -1;

ellipse.D.Value = 0;

geom.CoordinateCol.Add(ellipse);

//Add EllipticalArcTo

moveTo = new MoveTo();

moveTo.IX = geom.NextCoordinateIX;

moveTo.X.Value = 2;

moveTo.Y.Value = 1;

geom.CoordinateCol.Add(moveTo);

EllipticalArcTo ellipticalArcTo = new EllipticalArcTo();

ellipticalArcTo.IX = geom.NextCoordinateIX;

ellipticalArcTo.X.Value = 0;

ellipticalArcTo.Y.Value = 0;

ellipticalArcTo.A.Value = 2;

ellipticalArcTo.B.Value = 2;

ellipticalArcTo.C.Value = Math.PI / 4;

ellipticalArcTo.D.Value = 2;

geom.CoordinateCol.Add(ellipticalArcTo);

//Add NURBSTo

NURBSTo nurbsTo = new NURBSTo();

nurbsTo.IX = geom.NextCoordinateIX;

nurbsTo.X.Value = 3;

nurbsTo.Y.Value = 0;

nurbsTo.A.Value = 2;

nurbsTo.B.Value = 2;

nurbsTo.C.Value = 0;

nurbsTo.D.Value = 1;

nurbsTo.E.Ufe.F = "=NURBS(3.6903, 3, 0, 0, 0.1502,-0.0509,0,1, 0.5292,1.8758,0,1, 0.9798,0.2797,0,1)";

geom.CoordinateCol.Add(nurbsTo);

diagram.Save("Coordinates.vdx", SaveFileFormat.VDX);

Best Regards,

The issues you have found earlier (filed as DIAGRAM-25948) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)

The issues you have found earlier (filed as ) have been fixed in this Aspose.Words for JasperReports 18.3 update.