Nihar Sahoo
Nihar Sahoo

Reputation: 35

Adding a text to the AutoConnect(line) in visio drawing using C# code

I have create two shapes in Visio and using Shape.AutoConnect method I am able to connect the two shapes using the below code. I want to add name to the connector line using C# code as per the below screen shot.

Sample Visio Drawing: Sample Visio Drawing

Following is my code to connect two shapes.

   Visio.Shape visioRectShape = visioPage.Drop(visioRectMaster, 4.25, 5.25);
   visioRectShape.Text = from[i];

   Visio.Master visioCircleMaster3 = visioStencil.Masters.get_ItemU(@"Circle");
   Visio.Shape visioCircleShape3 = visioPage.Drop(visioCircleMaster3, 4.85, 5.85);
   visioCircleShape3.Text = to[i];

   visioRectShape.AutoConnect(visioCircleShape3,Visio.VisAutoConnectDir.visAutoConnectDirRight);```


**But can not get any information how to add a text for the connector line. Please help**

Upvotes: 0

Views: 479

Answers (3)

JohnGoldsmith
JohnGoldsmith

Reputation: 2698

I agree with Paul and Surrogate that passing in the connector object is preferable, but there is also the Shape.GluedShapes method, which could be helpful here. For example, given this diagram where I've added the shape IDs for reference:

flowchart sample

    var vApp = MyExtensions.GetRunningVisio();
    // See this link for more details on GetRunningVisio():
    // https://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html

    var fromShp = vApp.ActivePage.Shapes.ItemFromID[6];
    var toShp = vApp.ActivePage.Shapes.ItemFromID[8];
    
    var connIds = fromShp.GluedShapes(Visio.VisGluedShapesFlags.visGluedShapesOutgoing1D, "", toShp);

connIds is then an array of the respective connector IDs:

enter image description here

Normally this would be a single item but you could then go on to inspect the shapes if your scenario is different.

Upvotes: 1

Surrogate
Surrogate

Reputation: 1734

At you side you have not defined shape - connector line !
This shape is last shape which added to this page.
I have not there IDE with C#, may be this code can work:

Visio.Shape vsoConnectorShape = visioPage.Shapes(visioPage.Shapes.Count);
vsoConnectorShape.Text = @"ololo";

My code in VBA works fine, I just changed the code a little bit from the example from the MS website, I assigned values to variables using IDs.

Sub AutoConnect_Example()
Dim vsoShape1 As Visio.Shape
Dim vsoShape2 As Visio.Shape
Dim vsoConnectorShape As Visio.Shape
Set vsoShape1 = Visio.ActivePage.Shapes(1)
Set vsoShape2 = Visio.ActivePage.Shapes(2)
vsoShape1.AutoConnect vsoShape2, visAutoConnectDirRight
' define last added shape '    
Set vsoConnectorShape = ActivePage.Shapes(ActivePage.Shapes.Count) 
' change text '
vsoConnectorShape.Text = "ololo"    
End Sub

This gif show how VBA code works at my side
Find last shape aka connector

Upvotes: 0

Nihar Sahoo
Nihar Sahoo

Reputation: 35

I have tested the code by taking the last shape count, then we can get the shape used for the connector line. Then only i will be able to add text to the connector object.

Sample Code for connector Text

Visio.Shape vsoConnectorShape = visioPage.Shapes.get_ItemU(visioPage.Shapes.Count);
vsoConnectorShape.Text = @"ololo";```

Upvotes: 1

Related Questions