archimatetool/archi-scripting-plugin

cannot add children to objects in diagram

giulio1979 opened this issue · 8 comments

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)

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/

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

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};

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

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.

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 ?

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.

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.