Book a Demo

Author Topic: Shape Scripts: Text in Subshapes  (Read 30841 times)

M1TO

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Shape Scripts: Text in Subshapes
« 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");
}
}
}
« Last Edit: November 24, 2023, 12:13:15 am by M1TO »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13519
  • Karma: +573/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Shape Scripts: Text in Subshapes
« Reply #1 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

M1TO

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Shape Scripts: Text in Subshapes
« Reply #2 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.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13519
  • Karma: +573/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Shape Scripts: Text in Subshapes
« Reply #3 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

M1TO

  • EA User
  • **
  • Posts: 22
  • Karma: +0/-0
    • View Profile
Re: Shape Scripts: Text in Subshapes
« Reply #4 on: November 24, 2023, 07:16:27 pm »
Ahh, I see. Thanks for clarifying that for me!

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1403
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: Shape Scripts: Text in Subshapes
« Reply #5 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))
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Takeshi K

  • EA User
  • **
  • Posts: 632
  • Karma: +43/-1
    • View Profile
    • Sparx Systems Japan
Re: Shape Scripts: Text in Subshapes
« Reply #6 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,



--
t-kouno

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1403
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: Shape Scripts: Text in Subshapes
« Reply #7 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.
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13519
  • Karma: +573/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Shape Scripts: Text in Subshapes
« Reply #8 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 for a number of examples.

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

Geert

Guillaume

  • EA Practitioner
  • ***
  • Posts: 1403
  • Karma: +42/-2
    • View Profile
    • www.umlchannel.com
Re: Shape Scripts: Text in Subshapes
« Reply #9 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
Guillaume

Blog: www.umlchannel.com | Free utilities addin: www.eautils.com


Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13519
  • Karma: +573/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Shape Scripts: Text in Subshapes
« Reply #10 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