问题
I'm writing an Add-In to Visio 2010 in Studio 2010 on C#. I need to read a diagram currently opened in Visio. I know how to read shapes of the diagram.
The question is
- if I have a shape object, which properties can give me coordinates of the shape on the page and other shapes (if any), the current one is connected with,
- if I have a connector object, which properties can give me shapes it connects and direction of the connection.
回答1:
Connections in Visio are handled through Connect objects. Each shape has a collection of incoming connect objects and outgoing connect objects. Their names are FromConnects and Connects, respectively.
Each connect object has a FromSheet and ToSheet property, which are just pointers to Shape objects, the FromSheet shape being the shape that connects to the ToSheet shape.
So, if you have a square shape (shape1) connected to another square shape (shape2) with a connector line (connector), what you actually have is this: connector is connected to shape1 connector is connected to shape2
So on shape1, you'd look at FromConnects and see one Connects item, with FromSheet referencing connector, and ToSheet referring to shape1. Shape 2 would be the same. If you look at Connects on the connector shape, you'd see the same Connects item, with the same objects referenced.
So figuring out whether shape1 connects to shape2 or vice versa is a matter of looking at the order on connector...Connects object 1 would be the "From" shape and Connects object 2 would be the "To" shape.
Here are two VBA routines that get Incoming and Outgoing glues on a shape you pass in, and return a collection object. I know you said you're using C#, but I do VBA for Visio. The code just illustrates raw connection information. I'd suggest you try stepping around in VBA and see how this all works, because it still confuses me.
Public Function GetShapesThatConnectTo(TheShp As Visio.Shape) As Collection
Dim Result As Collection
Set Result = New Collection
For i = 1 To TheShp.FromConnects.Count
Result.Add TheShp.FromConnects.Item(i).FromSheet
Next i
Set GetShapesThatConnectTo = Result
End Function
Public Function GetWhatShapeConnectsTo(TheShp As Visio.Shape) As Collection
Dim Result As Collection
Set Result = New Collection
For i = 1 To TheShp.Connects.Count
Result.Add TheShp.Connects.Item(i).ToSheet
Next i
Set GetWhatShapeConnectsTo = Result
End Function
回答2:
For coordinates:
foreach (Visio.Page Page in Pages)
{
Visio.Shapes Shapes = Page.Shapes;
foreach (Visio.Shape Shape in Shapes)
{
double x = Shape.Cells["PinX"].ResultIU;
double y = Shape.Cells["PinY"].ResultIU;
来源:https://stackoverflow.com/questions/6456831/shape-connectors-in-visio