Author Topic: arranging ports in a diagram via script  (Read 7953 times)

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
arranging ports in a diagram via script
« on: September 24, 2018, 04:08:23 pm »
Hello!

I am trying to automatically generate some diagrams with components that have ports. I've tried a lot of arranging systems for the ports but none seem to work. The ports seem to all go to one spot or randomly place themselves on a margin from the component they belong to even tough the coordinates set for the ports are exactly on the margin of the component (i used the system explained in the scripting book of EA: the port is 15 pixels wide and 15 pixel high, and the center sport is on the margin of the component).
I have tried offsetting them by a set amount of pixels but is still would not work.

If anyone can help, I would be grateful!

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: arranging ports in a diagram via script
« Reply #1 on: September 24, 2018, 04:15:16 pm »
Positioning of embedded element broke in one version (can't remember exactly when, but quite a while ago). I don't know if it has been fixed in some recent version. Try an update.

q.

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: arranging ports in a diagram via script
« Reply #2 on: September 24, 2018, 04:22:34 pm »
I work on version 13.0.1309. Do you know if this version in specially is affected ? Unfortunately I cannot update

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: arranging ports in a diagram via script
« Reply #3 on: September 24, 2018, 06:39:15 pm »
I have just tried with 1309 and it appears to work. Can you post your code so we can eventually spot the bug?

q.

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: arranging ports in a diagram via script
« Reply #4 on: September 25, 2018, 04:08:00 pm »
I have just tried with 1309 and it appears to work. Can you post your code so we can eventually spot the bug?

q.

This is the class responsible for the position of the ports. At the constructor it gets a point in the top middle margin and from there it starts to go around the component.
Code: [Select]
private class portPosition{
Side side;
double l,r,t,b;
int portSpacing = 50;
Point M;
componentPosition cp;
public portPosition(Point Middle, componentPosition cp) {
side = Side.top;
this.cp = cp;
this.M = Middle;
l=M.getX()-7;
r=M.getX()+7;
t=M.getY()+7;
b=M.getY()-7;

new Point(l,t);
new Point(r,b);
}
public String getFormatedPosition() {
StringBuilder sb = new StringBuilder();
sb.append("l=").append(l).append(";r=").append(r).append(";t=").append(t).append(";b=").append(b);
//"l="+l+";r="+r+";t="+t+";b="+b;
return sb.toString();
}

public void nextPosition() {
switch(side) {
case top:
if(cp.getRight()>M.getX()+portSpacing) {
M.setX(M.getX()+portSpacing);
l=M.getX()-7;
r=M.getX()+7;
System.out.println("top "+getFormatedPosition());
}else {
M.setX(cp.getRight());
M.setY(cp.getTop());
this.side = Side.right;
l=M.getX()-7;
r=M.getX()+7;
t=M.getY()+7;
b=M.getY()-7;
System.out.println("top next"+getFormatedPosition());
//this.nextPosition();
}
break;
case right:
if(cp.getBottom()<M.getY()-portSpacing) {
M.setY(M.getY()-portSpacing);
t=M.getY()+7;
b=M.getY()-7;
System.out.println("right "+getFormatedPosition());
}else {
M.setY(cp.getBottom());
M.setX(cp.getRight());
this.side = Side.bottom;
l=M.getX()-7;
r=M.getX()+7;
t=M.getY()+7;
b=M.getY()-7;
System.out.println("right next "+getFormatedPosition());
//this.nextPosition();
}

break;

case bottom:
if(cp.getLeft()<M.getX()-portSpacing) {
M.setX(M.getX()-portSpacing);
l=M.getX()-7;
r=M.getX()+7;
///System.out.println("bottom "+getFormatedPosition());

}else {
M.setX(cp.getLeft());
M.setY(cp.getBottom());
this.side = Side.left;
l=M.getX()-7;
r=M.getX()+7;
t=M.getY()+7;
b=M.getY()-7;
System.out.println("bottmon next "+getFormatedPosition());
//this.nextPosition();

}
break;

case left:
if(cp.getTop()>M.getY()+portSpacing) {
M.setY(M.getY()+portSpacing);
l=M.getX()-7;
r=M.getX()+7;
System.out.println("left "+getFormatedPosition());
}
else {
M.setX(cp.getLeft());
M.setY(cp.getTop());
this.side = Side.top;
l=M.getX()-7;
r=M.getX()+7;
t=M.getY()+7;
b=M.getY()-7;
System.out.println("left next "+getFormatedPosition());
//this.nextPosition();
}
break;
default: System.out.println("Default case");
break;
}
}

@SuppressWarnings("unused")
public int getLeft() {
return (int)l;
}
@SuppressWarnings("unused")
public int getRight() {
return (int) r;
}
@SuppressWarnings("unused")
public int getTop() {
return (int)t;
}
@SuppressWarnings("unused")
public int getBottom() {
return (int)b;
}
}
private enum Side{
top,
right,
bottom,
left
}
« Last Edit: September 25, 2018, 04:51:29 pm by cristian.ignat »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: arranging ports in a diagram via script
« Reply #5 on: September 25, 2018, 04:41:05 pm »
"7" seems to be an important magical number  ;)

Geert

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: arranging ports in a diagram via script
« Reply #6 on: September 25, 2018, 04:48:50 pm »
"7" seems to be an important magical number  ;)

I used 7 because from what I read from the scripting manual a port is 15 by 15 pixels. One pixel in the middle that is on the margin plus 7 in all direction gives a 15 by 15 square... If I am not wrong   :D

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: arranging ports in a diagram via script
« Reply #7 on: September 25, 2018, 07:33:55 pm »
I was less interested in the way you calculated the position rather than how you update the data in the DB.

q.

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: arranging ports in a diagram via script
« Reply #8 on: September 25, 2018, 08:07:27 pm »
I was less interested in the way you calculated the position rather than how you update the data in the DB.

q.

This is how I add an element to a diagram.The ports are already part of the component and they seem to automatically snap to the component they belong to.

Code: [Select]
private DiagramObject addElement(Diagram d, Element e,String position) {

//Adds a diagram object (port or component) to the diagram given

DiagramObject newDiagramObject;


newDiagramObject = d.GetDiagramObjects().AddNew(position, "");



newDiagramObject.SetElementID(e.GetElementID());

if(!newDiagramObject.Update()) {
System.out.println(newDiagramObject.GetLastError());
}
repository.SaveDiagram(d.GetDiagramID());
repository.ReloadDiagram(d.GetPackageID());

newDiagramObject.Update();

d.Update();
d.GetDiagramObjects().Refresh();


return newDiagramObject;
}

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: arranging ports in a diagram via script
« Reply #9 on: September 25, 2018, 08:37:34 pm »
That's a bit messy what you're doing. You should be done without the save/reload of the diagram. Just put a repository.ReloadDiagram at the very end of it all to show the update.

q.

cristian.ignat

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
    • View Profile
Re: arranging ports in a diagram via script
« Reply #10 on: September 25, 2018, 09:41:01 pm »
Still does not fix my problem.

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: arranging ports in a diagram via script
« Reply #11 on: September 25, 2018, 11:52:15 pm »
It's a bit hard to debug this way. What you should do it to write a small test program to add an embedded element. Basically you
- create a diagram object with .AddNew("<position string>", "")
- add the according embedded element's ID and
- update() the diagram object.
- Finally you need a repos.reloadDiagram(id) to actually show the newly added element.

Compare the <position string> to what a manually added object has stored in the database (use an almost empty EAP) by looking into t_diagramobjects.

q.

P.S: I used this Perl snippet:

Code: [Select]
my $rep = $ENV{'REP'}; # get repository pointer "by magic"
my $dia = $rep->GetCurrentDiagram();
my $e = $rep->GetTreeSelectedObject();
my $do = $dia->DiagramObjects->AddNew ("t=-369;l=156;r=171;b=-384", "");
$do->{ElementID} = $e->ElementID;
$do->Update ();
$rep->ReloadDiagram($dia->DiagramID);
« Last Edit: September 25, 2018, 11:54:11 pm by qwerty »