Feed on

Groovy MagicDraw Element Edit Macro

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 {
	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 ->
			s.classifier.each{cl ->
				// we are looking for selected SysML requirements
					cl.member.each{mem ->

			// 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
					id = textField(text: req.id, columns:16)
				    	t = textArea(text: req.text, lineWrap: true, wrapStyleWord: true, columns:20, rows:10)
				button(text:'Change Text',
					actionPerformed: {
						req.text = t.text
						req.id = id.text

} finally {

Here’s what this look’s like when I select a requirement and hit the hot key I’ve defined for the macro.

Groovy Element Editor

Technorati , ,

One Response to “Groovy MagicDraw Element Edit Macro”

  1. on 24 Feb 2011 at 12:18 pm Mindaugas Genutis

    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).


    – Mindaugas Genutis

Trackback URI | Comments RSS

Leave a Reply