Jython Script Example - Main Menu Configurator

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Jython Script Example - Main Menu Configurator

Postby gregory.d.hardy@boeing.com » Wed Aug 22, 2018 9:48 pm

I need help adding a main menu category/action via Jython.

The MagicDraw example in the <installdir>\plugins\com.nomagic.magicdraw.jpython directory does not work. I noticed the PluginDescriptor class has not been imported, so the script doesn't even run as is. But even when I import the class and run it, I don't see a new category/action show up in the "File" menu. I even tried adding code within the try and except statements that logs a message to the notification window. Nothing appears, exception or not, so I'm not sure the script does anything at all.

What is missing from the MD example code below?

Code: Select all
from com.nomagic.actions import AMConfigurator
from com.nomagic.magicdraw.actions import ActionsConfiguratorsManager
from com.nomagic.magicdraw.actions import MDAction
from com.nomagic.magicdraw.actions import MDActionsCategory
import os
import sys

# Action for menu item, it can be created by passing name to the constructor.
# Script with passed name and .py extension is invoked when executing action .
class ExampleAction( MDAction ):
   def __init__(self, name):
      MDAction.__init__( self,"", name, None, None )

   def actionPerformed(self, event):
      filename = pluginDescriptor.getPluginDirectory().getAbsolutePath()+os.sep+self.getName()+".py"z
      try:
         execfile( filename )
      except :
         print "exception when trying to execute script:", filename, sys.exc_info()[0]
   

   
# Configurator adds new menu item to First menu in main menu
class MainMenuConfigurator( AMConfigurator):
   
   def configure( self,manager ):
      try:
         category=MDActionsCategory( "", "" )
         category.addAction( ExampleAction("ScriptAction") )
         manager.getCategories().get(0).addAction( category )
      except:
         print "exception"
   def getPriority(self):
      return AMConfigurator.LOW_PRIORITY
   
# Script starts here
print "Starting script, descriptor", pluginDescriptor
ActionsConfiguratorsManager.getInstance().addMainMenuConfigurator( MainMenuConfigurator() )
gregory.d.hardy@boeing.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Nov 21, 2017 6:42 pm

Re: Jython Script Example - Main Menu Configurator

Postby tomas.lukosius@nomagic.com » Fri Aug 24, 2018 8:03 am

I would like to help you. Please let me know what MagicDraw version you are using, i will try it out.
tomas.lukosius@nomagic.com
Forum Newbie
Forum Newbie
 
Posts: 22
Posts Rating:3
Joined: Tue Aug 30, 2016 12:22 am

Re: Jython Script Example - Main Menu Configurator

Postby gregory.d.hardy@boeing.com » Fri Aug 24, 2018 8:12 am

I'm using 18.5 SP3
gregory.d.hardy@boeing.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Nov 21, 2017 6:42 pm

Re: Jython Script Example - Main Menu Configurator

Postby tomas.lukosius@nomagic.com » Tue Aug 28, 2018 7:23 am

I checked it out and it seems to work fine. All i did is copy pasted in same place <installdir>\plugins\com.nomagic.magicdraw.jpython\examples dir and renamed it to 'scripts' based on information found here https://docs.nomagic.com/display/MD185/Jython+Scripting. Basically executable jython plugins must be in scripts dir. I hope this helps.
tomas.lukosius@nomagic.com
Forum Newbie
Forum Newbie
 
Posts: 22
Posts Rating:3
Joined: Tue Aug 30, 2016 12:22 am

Re: Jython Script Example - Main Menu Configurator

Postby gregory.d.hardy@boeing.com » Tue Aug 28, 2018 8:42 am

Sorry, I'm trying to accomplish this as a macro rather than a plugin and failed to point that out to you.

It works as expected when executing as a plugin, but I can't get it to work when running it as a macro.

I assume it is possible to add menu items via Jython macro.

I tried modifying the example as shown below, but the menu item does not show up. I'm not sure if I'm missing/overlooking something.

Code: Select all
from com.nomagic.actions import AMConfigurator
from com.nomagic.magicdraw.actions import ActionsConfiguratorsManager
from com.nomagic.magicdraw.actions import MDAction
from com.nomagic.magicdraw.actions import MDActionsCategory
import os
import sys
from javax.swing import JOptionPane

# Action for menu item, it can be created by passing name to the constructor.
# Script with passed name and .py extension is invoked when executing action .
class ExampleAction( MDAction ):
   def __init__(self, name):
      MDAction.__init__( self,"", name, None, None )

   def actionPerformed(self, event):
      JOptionPane.showMessageDialog( None, "I am a script!!!")

   
# Configurator adds new menu item to First menu in main menu
class MainMenuConfigurator( AMConfigurator):
   
   def configure( self,manager ):
      try:
         category=MDActionsCategory( "", "" )
         category.addAction( ExampleAction("Action Name") )
         manager.getCategories().get(0).addAction( category )
      except:
         print "exception"
   def getPriority(self):
      return AMConfigurator.LOW_PRIORITY
   
# Script starts here
ActionsConfiguratorsManager.getInstance().addMainMenuConfigurator( MainMenuConfigurator() )
gregory.d.hardy@boeing.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Nov 21, 2017 6:42 pm

Re: Jython Script Example - Main Menu Configurator

Postby gregory.d.hardy@boeing.com » Tue Aug 28, 2018 2:53 pm

Thanks for your help. I found out how to accomplish this via macro.

I needed to figure out how to get the main menu actions manager. I stumbled upon the ActionsProvider class in the OpenApi so all is fine now.
gregory.d.hardy@boeing.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Tue Nov 21, 2017 6:42 pm

Re: Jython Script Example - Main Menu Configurator

Postby stephen.a.long@boeing.com » Thu Jan 10, 2019 3:55 pm

As a side-effect, I discovered that
Code: Select all
Application.getInstance().getGUILog().log(<message>)
prints to the Notification Window, while
Code: Select all
print
writes to the log file. I'm using CEA 18.5.
stephen.a.long@boeing.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Thu Jan 10, 2019 2:57 pm


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests

cron