check [Solved] Creating New SysML Block from Jython Script

5 months 2 weeks ago - 5 months 2 weeks ago #5527 by defu
It is very easy to create a new element package using the Jython script:
newPackage = Modelio.getInstance().getModelingSession().getModel().createElement("Package")
newPackage.setName("myPackage")

elt.getOwnedElement().add(newPackage)

But I wish to add a SysML element like a Block ("SysML Architect/Block") using the command, but the following snippet fails
newBlock = Modelio.getInstance().getModelingSession().getModel().createElement("Block")
java.lang.NullPointerException: java.lang.NullPointerException in <script> at line number 1

Where could I look up the name that I should use in the createElement command? I reckoned from the exported xmi, that I could add a class and then add stereotype block (maybe using createClass command), but where do I look up the name of the stereotype that I should use?

Thanks!

Please Log in or Create an account to join the conversation.

5 months 2 weeks ago - 5 months 2 weeks ago #5528 by defu
It seems that I could get name and module name of the stereotype by selecting the according Block element and run the following code:
stereotype = elt.getExtension()[0]
print stereotype # the first string being the stereotype name
print stereotype.getModule()

And then add stereotype by
newBlock = Modelio.getInstance().getModelingSession().getModel().createElement("Class")
newBlock.setName("newBlock")
newBlock.addStereotype("SysMLArchitect", "Block")
elt.getOwnedElement().add(newBlock)

Please Log in or Create an account to join the conversation.

5 months 2 weeks ago #5531 by ebr
Hi defu,

Just two little hints.

First, if you want to know the kind of element currently selected in order to use it in the createElement command just go to the "Properties" tab.
You will find it as depicted below.



Now concerning SysML Blocks.You are right, a SysML Block is a UML Class on which the SysML Block stereotype is applied.
So to create a SysML Block in Jython you can use your code or the following one which is shorter.
newBlock = Modelio.getInstance().getModelingSession().getModel().createClass("newBlock", elt, "SysMLArchitect", "Block")

More documentation can be found at herehttps://www.modelio.org/documentation/javadoc-3.8/index.html
Attachments:
The following user(s) said Thank You: defu

Please Log in or Create an account to join the conversation.

5 months 2 weeks ago #5539 by defu
Thanks!

Reading more in the forum, I found that it is also very handy to use the Semantic model View to under the underlying data structure.

Please Log in or Create an account to join the conversation.

Moderators: chmcma
Time to create page: 0.155 seconds
^ Back to Top