I’m currently working with Cameo System Modeler and developing plugins for it. My current task is to write a plugin that automatically applies a specific Stereotype to a diagram when it is created, but only if its owner has a specific name (e.g., if the owner is “Test,” the diagram should get the Stereotype “TestStereotype”).
Unfortunately, I couldn’t find sufficient information or examples in the jdocs or the User/Developer Guide to achieve this.
Has anyone implemented a similar functionality or knows how to trigger such an action in Cameo System Modeler? Any advice, code snippets, or pointers would be greatly appreciated!
Thank you in advance for your help!
I tried to read through the jdocs / tutorials of MagicDraw and on youtube but I couldn´t find a good solution
I’m currently working with Cameo System Modeler and developing plugins for it. My current task is to write a plugin that automatically applies a specific Stereotype to a diagram when it is created, but only if its owner has a specific name (e.g., if the owner is “Test,” the diagram should get the Stereotype “TestStereotype”).
Unfortunately, I couldn’t find sufficient information or examples in the jdocs or the User/Developer Guide to achieve this.
Has anyone implemented a similar functionality or knows how to trigger such an action in Cameo System Modeler? Any advice, code snippets, or pointers would be greatly appreciated!
Thank you in advance for your help!
I tried to read through the jdocs / tutorials of MagicDraw and on youtube but I couldn´t find a good solution
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Diagram;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.Profile;
import com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper;
import com.nomagic.magicdraw.core.Application;
import com.nomagic.magicdraw.uml.symbols.DiagramPresentationElement;
import com.nomagic.magicdraw.core.GUILog;
import com.nomagic.magicdraw.core.Project;
import java.util.Collection;
// Get the GUI log instance
GUILog guiLog = Application.getInstance().getGUILog();
// Get the current project instance
Project project = Application.getInstance().getProject();
String nameStereotype = "TestStereotype"; // Name of the stereotype to apply
String nameProfile = "MyProfile"; // Name of the profile containing the stereotype
// Retrieve the profile and stereotype
Profile profile = StereotypesHelper.getProfile(project, nameProfile);
Stereotype stereotype = StereotypesHelper.getStereotype(project, nameStereotype, profile);
// Check if the stereotype exists
if (stereotype == null) {
guiLog.log("Not found stereotype."); // Log message if stereotype is not found
System.exit(0); // Stop the program
}
// Get all diagrams in the project
Collection<DiagramPresentationElement> diagramsPres = project.getDiagrams();
// Iterate through the diagrams and apply the stereotype
for (DiagramPresentationElement diagramPres : diagramsPres) {
Diagram diagram = diagramPres.getDiagram();
// Check if the diagram owner's name contains "Test"
if (diagram.getOwner().getName().contains("Test")) {
// Apply the stereotype to the diagram
StereotypesHelper.addStereotype(diagram, stereotype);
guiLog.log("Applied stereotype to: " + diagram.getQualifiedName()); // Log the application of the stereotype
}
}
Hope this script could be helpful.
Javadoc of StereotypesHelper: https://jdocs.nomagic.com/2021x/com/nomagic/uml2/ext/jmi/helpers/StereotypesHelper.html