Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: M1TO on November 24, 2023, 12:01:48 am

Title: Shape Scripts: Text in Subshapes
Post by: M1TO on November 24, 2023, 12:01:48 am
Hi,

I have a subshape located "outside" the main shape and I want to add text to it. How do I do that? The text always seems to be "attached" to the main shape, no matter what I try.

Code: [Select]
shape main
{
DrawNativeShape();
addsubshape("test", 100, 100);
setfillcolor(0,255,255);

shape test
{
h_align = "center";
v_align = "center";
RoundRect(-15,0,0,15,5,5);
if (HasTag("Test","Yes"))
{
SetFontColor(200,12,0);
print("TestText");
}
}
}
Title: Re: Shape Scripts: Text in Subshapes
Post by: Geert Bellekens on November 24, 2023, 12:27:21 am
Can you explain (with pictures) what you want to achieve, and what is going wrong now?

Geert
Title: Re: Shape Scripts: Text in Subshapes
Post by: M1TO on November 24, 2023, 01:20:10 am
The text is now shown in the centre of the main shape. But I want it to be in the centre of the subshape, the turquoise rectangle attached to the "north-west" corner of the main shape. Unfortunately I have no idea how to add a picture to this post.
Title: Re: Shape Scripts: Text in Subshapes
Post by: Geert Bellekens on November 24, 2023, 02:09:07 am
The text is now shown in the centre of the main shape. But I want it to be in the centre of the subshape, the turquoise rectangle attached to the "north-west" corner of the main shape. Unfortunately I have no idea how to add a picture to this post.
You can't attach images here. You can post images on a public image hosting site (e.g. imgur.com), and then link them here.

The problem is that you only think you have a subshape outside the main shape.
Actually ou have a subshape the exact same size and location of the main shape addsubshape("test", 100, 100);
Then inside the definition of you subshape you draw a rounded rectangle outside of the borders RoundRect(-15,0,0,15,5,5);
And then you print text in the middle of the subshape (which has the same size and location as the main shape)
Code: [Select]
h_align = "center";
v_align = "center";
print("TestText");

A possible solution:

Code: [Select]
shape main
{
DrawNativeShape();
addsubshape("test", 50, 50, -50, -50);
setfillcolor(0,255,255);

shape test
{
h_align = "center";
v_align = "center";
RoundRect(0,0,100,100,5,5);
if (HasTag("Test","Yes"))
{
SetFontColor(200,12,0);
print("TestText");
}
}
}

Geert
Title: Re: Shape Scripts: Text in Subshapes
Post by: M1TO on November 24, 2023, 07:16:27 pm
Ahh, I see. Thanks for clarifying that for me!
Title: Re: Shape Scripts: Text in Subshapes
Post by: Guillaume on January 28, 2026, 02:08:48 am
I just came across a similar issue.
Using a subshape to display the text is great but I can't find the parameters definition for "addsubshape" matching this signature: addsubshape("test", 50, 50, -50, -50);
I'm struggling moving the text to the right location by changing the 4 values (top? width ? ...)

The information I found from the user guide is addsubshape(string shapename(int width, int height))
Title: Re: Shape Scripts: Text in Subshapes
Post by: Takeshi K on January 28, 2026, 10:24:45 am
Hi Guillaume,

If I understand correctly, the third (3rd) and fourth (4th) parameters are relative position values.
But it is tricky to determine actual position to draw a subshape.

The important point is that the calculation of the reference point used for relative values is a bit cumbersome.

In this case, the layouttype is default, this is may be 'TopDown'. In the 'TopDown' type, the 3rd parameter is simple, just relative value from 0. If the 3rd value is -50, the subshape will be drawn 50 point left from the rectangle by the drawnativeshape command. After adding a subshape the reference point will be moved to lower (in the 'TopDown' type). In this case, the second parameter (height) is 50, so the reference point will be 50 (0+50). The Y position for the next subshape will be calculated from 50.

We can understand this behaviour by trying the following script. (and change 4th parameter of the second addsubshape)

addsubshape("test", 50, 50, -50, -50); //1
addsubshape("test", 50, 50, -50,  0); //2

Before the first addsubshape, the reference point is (0, 0), this is default.
By the first addsubshape, the reference point will be (0, 50) because the height (the 2nd parameter of the first addsubshape) is 50.
So, the second addsubshape will be drawn from (-50, 50) - in the 'TopDown' type, the 3rd parameter is just relative point from 0, the 4th parameter is calculated from the current reference point - 50 (the 2nd parameter of the first addsubshape) + 0 (the 4th parameter of the second addsubshape)

It is similar when the layouttype is LeftRight, the X position is relative.

This seems very tricky, but because of this behaviour, we can draw subshapes nicely without 3rd and 4th parameters (addsubshape of 2 parameters is commonly used).

HTH,



Title: Re: Shape Scripts: Text in Subshapes
Post by: Guillaume on January 28, 2026, 06:47:04 pm
Hi Takeshi,

Thank you for the explanations  :)

To summarize, the parameters are:
1- Shape name
2- Width
3- Height
4- Relative value (horizontal to move left/right ?)
5- Relative value from the reference point (vertical to move up/down?)

Hopefully Sparx team will add clarifications and examples to cover these additional parameters in the user guide.
Title: Re: Shape Scripts: Text in Subshapes
Post by: Geert Bellekens on January 30, 2026, 06:12:55 pm
The dowside of using actual text, is that you can't control it's size.
So what I did for a customer was draw characters in shapescripts.

See here https://github.com/GeertBellekens/Enterprise-Architect-Shapescript-Library/tree/master/RM%20Requirements (https://github.com/GeertBellekens/Enterprise-Architect-Shapescript-Library/tree/master/RM%20Requirements) for a number of examples.

I think by now I have a shape for allmost all characters, bot uppercase and lowercase.

Geert
Title: Re: Shape Scripts: Text in Subshapes
Post by: Guillaume on January 31, 2026, 01:57:50 am
Hi Geert,

Currently I have 3 or 4 characters in my list of predefined values which makes it tricky to have a consistent result i.e. it's currently fine for values with 3 chars, not 4.
I'll have a look at your examples as it sounds like a fine idea to fix the rendering issues, thanks  :D

Guillaume
Title: Re: Shape Scripts: Text in Subshapes
Post by: Geert Bellekens on January 31, 2026, 10:44:51 pm
There's still a lot of messing about with the parameters needed to get sort-of good results.
Shapescripts don't seem to work like an exact science.

Geert