I need my plugin to apply a stereotype to new diagrams. For that it needs to add a Diagram Note element to the diagram.
To add a Diagram Note element I am following
* Add Text element to a Diagram: the Scripting EA book, section "Adding special objects" to diagrams.
* Make a text element a Diagram Note:
https://sparxsystems.com/forums/smf/index.php?topic=37907.0My resulting code is:
public static void addDiagramNoteToDiagram(Diagram diagram, Package myPackage, Repository repository)
{
Element text = myPackage.Elements.AddNew("", "Text");
String vbCrLf = ((char)13).ToString() + ((char)10).ToString();
String nnn = "Name: " + diagram.Name + vbCrLf + "Author: " + diagram.Author + vbCrLf + "Version: " + diagram.Version + vbCrLf + "Created: " + diagram.CreatedDate + vbCrLf + "Updated: " + diagram.ModifiedDate;
text.Notes = nnn;
text.Subtype = 18;
text.Update();
string pos = "l=200;r=300;t=-20;b=-40";
dynamic dia_obj = diagram.DiagramObjects.AddNew(pos, "");
diagram.Update();
dia_obj.ElementID = text.ElementID;
dia_obj.Update();
repository.ReloadDiagram(diagram.DiagramID);
}
The text Element is created fine.
but nothing is added to diagram.DiagramObjects by the line
dynamic dia_obj = diagram.DiagramObjects.AddNew(pos, "");
When walking through in debug mode, diagram.DiagramObjects.count remains at 0 at all times.
When I open the diagram the note element is showing, which I dont get.
But my code which gets the Diagram Note does not find it.
// get the diagram element which will hold the Stereotype
public Element getMyDiagramsElement(Repository repository)
{
Collection diagramObjects = myElement.DiagramObjects; // myElement is a Diagram property of the non static class
foreach (DiagramObject o in diagramObjects)
{
int eid = o.ElementID;
Element element = repository.GetElementByID(eid);
if (element.MetaType.Contains("Text"))
{
return element;
}
}
return null;
}
}
When the Diagram Note is added manually, through the front end, this code works fine.
Why does getMyDiagramsElement() work for manually added Diagram Notes and not for the addDiagramNoteToDiagram() method way?
Note: If I re-run the code it looks like the Diagram Note is there? So I tried adding
repository.SaveDiagram(diagram.DiagramID);
at various places, as suggested by Scripting EA, but could not make it make a difference.