Macro - how to iterate on a selection of elements

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Macro - how to iterate on a selection of elements

Postby christophe.waterplas@incose.org » Thu Mar 07, 2019 1:29 am

Hello,

I would like to write a macro that deletes the names (i.e. renames to "") of each member of an association. Using the record macro feature I can see how to rename a single association:

Code: Select all
    var ele2 = AutomatonMacroAPI.getSelectedElementFromContainmentTree();
 
    var ele1 = ele2.getMember_End().get(0);
    ele1.setName('');
    var ele3 = ele2.getMember_End().get(1);
    ele3.setName('');


My question is how do I change the macro so that it works (iterates) over a selection of associations?

By the way, the reason for this macro is to remove the names added automatically by Cameo/MagicDraw when creating an association between two elements.

Regards
Christophe.
christophe.waterplas@incose.org
Forum Newbie
Forum Newbie
 
Posts: 16
Posts Rating:1
Joined: Sun Aug 10, 2014 8:00 pm

Re: Macro - how to iterate on a selection of elements

Postby bcole » Tue Apr 30, 2019 4:25 am

If the above is JavaScript (which I'm assuming), loops are a bit of a pain. Groovy and Python have a 'for .. in' construct that is pretty natural to use. If you try to use JavaScript's in the same way, you are given the index rather than the item. Multiple approaches to loops are described below:

https://codeburst.io/javascript-the-dif ... 2db038e4c2
bcole
Forum Newbie
Forum Newbie
 
Posts: 16
Posts Rating:0
Joined: Sat Feb 10, 2018 8:03 am

Re: Macro - how to iterate on a selection of elements

Postby christophe.waterplas@incose.org » Tue Apr 30, 2019 11:16 pm

Yes, this code snippet is in Javascript. My question is not so much on how to write an iteration loop. What I would like to know is how do I get the list of selected elements? As shown in my earlier post, the
Code: Select all
AutomatonMacroAPI.getSelectedElementFromContainmentTree()
function returns a single element. Is there a function I can use to access a list of selected elements?
christophe.waterplas@incose.org
Forum Newbie
Forum Newbie
 
Posts: 16
Posts Rating:1
Joined: Sun Aug 10, 2014 8:00 pm


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests

cron