Accessing Project Usage in Macros

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Accessing Project Usage in Macros

Postby aconley@oshkoshcorp.com » Fri Jan 14, 2022 12:28 pm

I am using Jython to write a macro to assign a stereotype to an element, but the stereotype and element are not in the same model. For example, my model references another model that uses the SysML profile. The SysML profile shows up in my containment tree under Project Usages as Model [SysML Profile.mdzip].
2022-01-14_12-19-44.png

When I try to access the children of object "SysML", I get an exception: null AttributeError: 'NoneType' object has no attribute '_getChild' in "Untitled." at line number 16 (ele2 = ele1._getChild).

Code: Select all
import com
     
from com.nomagic.magicdraw.automaton import AutomatonMacroAPI
from com.nomagic.magicdraw.core import Application
from com.nomagic.magicdraw.openapi.uml import SessionManager

try: 
    SessionManager.getInstance().createSession("Automaton_Macro_Script_Execute")
   
except:
    SessionManager.getInstance().closeSession()
    SessionManager.getInstance().createSession("Automaton_Macro_Script_Execute")
               
element0 = AutomatonMacroAPI.getModelData()
ele1 = element0._getChild("SysML")
ele2 = ele1._getChild("Blocks")
ele3 = ele2._getChild("ValueType")
Application.getInstance().getGUILog().log(ele3.getName())
SessionManager.getInstance().closeSession()


Is there a different way to access the SysML folder?
You do not have the required permissions to view the files attached to this post.
aconley@oshkoshcorp.com
Forum Newbie
Forum Newbie
 
Posts: 2
Posts Rating:0
Joined: Tue Dec 21, 2021 10:06 am

Re: Accessing Project Usage in Macros

Postby aconley@oshkoshcorp.com » Thu Jan 20, 2022 8:09 am

I figured it out. I needed to use the ProjectUtilities.findAttachedProjectsByName method.

Code: Select all
   
mainProject = Application.getProject(Application.getInstance())
sysMlProfile = ProjectUtilities.findAttachedProjectByName(mainProject, "SysML Profile")   
aconley@oshkoshcorp.com
Forum Newbie
Forum Newbie
 
Posts: 2
Posts Rating:0
Joined: Tue Dec 21, 2021 10:06 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 1 guest