Archi Forum

Archi Plug-ins => jArchi => Topic started by: jsimoncello on March 10, 2023, 16:41:38 PM

Title: Is it possible to get the absolute position of a visual connection in a view ?
Post by: jsimoncello on March 10, 2023, 16:41:38 PM
Hi
I am trying to auto-number the relations shown in a view starting from top and then left to right.
Problem is I haven't found any reliable way to get start or end or text position for a connection (except if this relation has bendpoints).
Is there any way to do it with the SDK ?
Title: Re: Is it possible to get the absolute position of a visual connection in a view ?
Post by: Jean-Baptiste Sarrodie on March 10, 2023, 20:04:11 PM
Hi,

As the anchor points (exact place where a connection connects to an object) are dynamically computed based on the chosen rendering algorithm and some other factors, it is not possible to get them through jArchi API.

Regards,

JB
Title: Re: Is it possible to get the absolute position of a visual connection in a view ?
Post by: jsimoncello on March 13, 2023, 08:00:33 AM
Thanks for the quick reply. I'll find another way :-)

regards !
Title: Re: Is it possible to get the absolute position of a visual connection in a view ?
Post by: jsimoncello on March 13, 2023, 09:06:27 AM
just in case, a very basic version using centers of source and target element of the flow. I thing it can be refined by taking bendpoints into account.


var archimateView = $(selection).first();
console.clear();
if (archimateView.type != "archimate-diagram-model") {
    window.alert("Please select a view before running this script");

} else {
    let flowsInView = [];
    $(archimateView).children("flow-relationship").each(function (f) {
        let srcCoord = getElementCenter(f.source);
        let tgtCoord = getElementCenter(f.target);
        let x = (srcCoord.x + tgtCoord.x) / 2;
        let y = (srcCoord.y + tgtCoord.y) / 2;
        flowsInView.push([x, y, f]);
    });

    flowsInView.sort(([a, b, c], [d, e, f]) => b - e || a - d);
    for (let i = 0; i < flowsInView.length; i++) {
        let flow = flowsInView[i][2];
        let newFlowName = "" + (i + 1);
        flow.labelExpression = "F" + newFlowName.padStart(3, "0") + " " + flow.name.replace(/F[0-9a-z]*/gm, "");
    }
}
//get the center of the visual element from the origin of the view (top left)
function getElementCenter(el) {
    let absCoord = getAbsoluteCoord(el);
    return { x: absCoord.x + (absCoord.width) / 2, y: absCoord.y + absCoord.height / 2, }

}
/**
 *  compute x,y of the element starting from the origin of the view (element.bounds returns x,y from the parent element not from the view)
 * @param el
 *
 */
function getAbsoluteCoord(el) {
    let result = el.bounds;

    $(el).parents().each(function (pa) {
        if (pa.bounds !== null && pa.bounds !== undefined) {
            result.x += pa.bounds.x
            result.y += pa.bounds.y
        }
    })
    return result;
}