问题
I want to add notes to connectors in an Enterprise Architect diagram programmatically. So far I only managed to add notes to elements with the following code:
foreach (EA.Element element in Package.Elements)
{
foreach (EA.Connector conn in element.Connectors)
{
EA.Element newNote = Package.Elements.AddNew("MyNote", "Note");
newNote.Notes = "Some string";
newNote.Update();
//position calculation is left out here
EA.DiagramObject k = diagram.DiagramObjects.AddNew(position, "");
k.ElementID = newNote.ElementID;
k.Sequence = 9;
k.Update();
EA.Connector newConn = newNote.Connectors.AddNew("NewLink", "NoteLink");
newConn.SupplierID = conn.SupplierID;
newConn.Update();
EA.DiagramLink newLink = diagram.DiagramLinks.AddNew("newLink", "NoteLink");
newLink.ConnectorID = newConn.ConnectorID;
newLink.Update();
The image maybe makes it more clear what I actually want:
http://www.directupload.net/file/d/3536/6bkijpg2_png.htm
My question is: How do I get the note attached to the connector? I assume I have to change this line "newConn.SupplierID = conn.SupplierID;", but "newConn.SupplierID = conn.ConnectorID" causes an exception. I would be very happy if someone could help me!
Best regards
回答1:
EA handles note links to connectors very differently from note links to elements.
Connectors always run between two elements. In your example, there are four elements (two of type Activity
named O1 and O2, and two of type Note
; these are usually nameless) and three connectors (O1 - O2, "This is what I have" - O2, and one from O1 running off the edge of the image).
The thing that looks like a connector from "This is what I want" to the O1 - O2 connector is not, in fact, a connector at all -- it just looks like one. In the GUI, the link to a connector is unresponsive and you can't bring up a properties dialog for it. This is why.
The fact that the note is linked to the connector is stored in the note element itself, in the MiscData
collection. What you need to do is add the string idref=<connector_id>;
to MiscData(3)
. You may also need to set the Note
's Subtype
field to 1.
However, MiscData
is read-only so you'll have to go into the database and update t_object
(where elements are stored) directly. MiscData
in the API corresponds to PDATA1
etc in the table. Note that the indices differ by one, so MiscData(0)
corresponds to PDATA1
, etc.
You will also need to use the undocumented Repository.Execute()
since Repository.SQLQuery()
only allows select
statements.
So the following should work:
foreach (EA.Connector conn in element.Connectors) {
EA.Element newNote = Package.Elements.AddNew("MyNote", "Note");
newNote.Subtype = 1;
newNote.Notes = "Some string";
newNote.Update();
repository.Execute("update t_object set PDATA4='idref=" + conn.ConnectorID + ";' " +
where Object_ID=" + newNote.ElementID);
//position calculation is left out here
EA.DiagramObject k = diagram.DiagramObjects.AddNew(position, "");
k.ElementID = newNote.ElementID;
k.Sequence = 9;
k.Update();
}
You may need to set the element subtype after the database update, I'm not sure.
Element.Subtype
values are undocumented in the API, as are the contents of Element.MiscData
, so this solution isn't future-proof (but it's very unlikely EA will ever change the way it handles these things).
来源:https://stackoverflow.com/questions/21826184/enterprise-architect-9-add-note-to-connector