Groovy MagicDraw Element Edit Macro
February 24th, 2011 by Lou
I use MagicDraw with the SysML plugin for architectural models, including requirements management.
Here is a small groovy script macro that can be customized to edit elements with a bit fewer clicks than the standard interface. It works by opening an editor window for each element selected on the drawing surface.
I’ve installed this macro script to launch with a hot key. When I select an element and press the hot key, the editor pops up.
Thanks to Mindaugas Genutis at MagicDraw support for the fiddly bits that calculate where to put the editor dialog.
import com.nomagic.magicdraw.core.Application
import com.nomagic.magicdraw.automaton.AutomatonMacroAPI
import com.nomagic.magicdraw.openapi.uml.SessionManager
import groovy.swing.SwingBuilder
import com.nomagic.magicdraw.ui.dialogs.MDDialogParentProvider;
import java.awt.BorderLayout
import javax.swing.BoxLayout
import java.awt.Rectangle
import java.awt.Point
try {
SessionManager.getInstance().createSession("Automaton_Macro_Script_Execute");
modelData = AutomatonMacroAPI.getModelData();
project = Application.getInstance().getProject();
diagram = project.getActiveDiagram();
selected = diagram.getSelected();
selected.each{el ->
// this bit calculates a screen location for the editor
Point drawAreaLocation = el.getDiagramPresentationElement().getPanel().getDrawArea().getLocationOnScreen();
Rectangle symbolBounds = el.getBounds();
Point guiLocation = new Point((int)(drawAreaLocation.x + symbolBounds.x), (int)(drawAreaLocation.y + symbolBounds.y));
el.element.appliedStereotypeInstance.each{s ->
System.out.println(s.name)
s.classifier.each{cl ->
System.out.println(cl.name)
// we are looking for selected SysML requirements
if(cl.name.equals("Requirement")){
cl.member.each{mem ->
System.out.println(mem.humanName)
}
}
}
// getOpaqueObjectByPath makes it easy to get and set model element attributes
def element = el.element.qualifiedName
def req = AutomatonMacroAPI.getOpaqueObjectByPath(element)
new SwingBuilder().edt {
def f = frame(title:'Frame', size:[300,300], show: true, alwaysOnTop: true, location:guiLocation) {
textlabel = label(text:element, constraints: BorderLayout.NORTH)
def id
def t
panel{
id = textField(text: req.id, columns:16)
scrollPane(){
t = textArea(text: req.text, lineWrap: true, wrapStyleWord: true, columns:20, rows:10)
}
}
button(text:'Change Text',
actionPerformed: {
SessionManager.getInstance().createSession("Change_Requirement_Text");
req.text = t.text
req.id = id.text
SessionManager.getInstance().closeSession();
},
constraints:BorderLayout.SOUTH)
}
}
}
}
} finally {
SessionManager.getInstance().closeSession();
}
Here’s what this look’s like when I select a requirement and hit the hot key I’ve defined for the macro.
Great job, Louis!
I believe our specification dialog and quick properties window were not quick enough for you ;-)
P.S. There is a possibility to edit tags directly on the diagram, but then you have to show tags in tag compartment (”Show Tagged Values” symbol property).
Sincerely,
– Mindaugas Genutis