archimatetool / archi-scripting-plugin

jArchi - Scripting for Archi: ArchiMate Modelling Tool
https://www.archimatetool.com
118 stars 33 forks source link

cannot add children to objects in diagram #108

Closed giulio1979 closed 1 year ago

giulio1979 commented 1 year ago

Hi, I have been trying to add a child element to another element when there is a composition relation in order to generate a layout automatically.

Is this even possible ?

e = relationship

$(e.source).children().add(e.target)

Phillipus commented 1 year ago

Hi, off the top of my head I can't remember how to do this.

The best place to get help with jArchi is on the Archi forum - https://forum.archimatetool.com/

jbsarrodie commented 1 year ago

Wait a bit, I'm writing a detailed answer :-)

jbsarrodie commented 1 year ago

Hi,

Is this even possible ?

Yes :-)


$(e.source).children().add(e.target)

It doesn't work like that. In your code, .add() is called on a collection (think of it as a simple list) and thus simply adds an object to it. But even though you populated this collection with e.source's chidren, changing this collection has no impact on e.source itself.

In order to add a child you have to call the add() method on the (visual) element. This implies to first get the visual object associated with the source, and then add related elements.

Assuming you have the start element already in a view, adding all model elements that are the target of a composition relationship looks like this:

selectedObject = selection.first();
selectedElement = selectedObject.concept;
compositionTargets = $(selectedElement).outRels('composition-relationship').targetEnds();

// Some vars needed for layout
childWidth = 120;
childHeight = 50;
maxCols = 5;
padding = 10;
posX = padding;
posY = 30; // leave some room for the main element name

// Iterate on all children
compositionTargets.each(function(e) {
  // Add child element into parent visual object
  selectedObject.add(e, posX, posY, childWidth, childHeight)

  // Compute next position
  posX += padding + childWidth;
  if(posX > (maxCols * (childWidth + padding))) {
    posX = padding;
    posY += padding + childHeight;
  }
});

// Bonus: resize parent visual object to match content
selectedObject.bounds = {width: (maxCols * (childWidth + padding)) + padding, height: posY + childHeight + padding};
giulio1979 commented 1 year ago

Let me give it a shot, i want to reorganize via script an Auto-Generated diagram. If i manage I share the script :)

giulio1979 commented 1 year ago

Kinda works, it creates new elements in the diagram. I wanted actually to manipulate existing elements in the diagram and create the parent/child relationship only.

jbsarrodie commented 1 year ago

Kinda works, it creates new elements in the diagram. I wanted actually to manipulate existing elements in the diagram and create the parent/child relationship only.

Can you describe what you want to achieve with more details ?

giulio1979 commented 1 year ago

The most basic scenario would be on a diagram with a number of existing elements and relationship, develop a script to apply a particular layout and group at same time elements by composition.

giulio1979 commented 1 year ago

I will close the issue, i managed to develop some code to readd the elements inside container objects one by one and then delete the originals.