Author Topic: Creating new constraints in a collection  (Read 2488 times)

QuimSA

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Creating new constraints in a collection
« on: June 26, 2019, 08:05:01 pm »
Hi folks

I have to apply the same 2 constraints to a whole collection of interfaces and i'm wondering if there's a way to automatically do so by scripting. I've been searching and i'm not sure it's possible.
I'm thinking it should be possible using the 'Constraint class' method: Update(). But i dont know how

https://sparxsystems.com/enterprise_architect_user_guide/14.0/automation/constraint.html

I appreciate any help!
Thanks

P.D. By the way this is my first post in this forum, hi everyone!!! :D :D

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13288
  • Karma: +557/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Creating new constraints in a collection
« Reply #1 on: June 26, 2019, 09:27:18 pm »
Hi Quim,

Creating new stuff in EA via the API is done using the EA.Collection.AddNew() method.

This is the same for elements in a package
Code: [Select]
Package.Elements.AddNew("MyClass", "Class")attributes
Code: [Select]
Element.Attributes.AddNew("myAttribute","string")or constraints
Code: [Select]
Element.Constraints.AddNew("MyConstraint","invariant")
This method returns the new object.
After creating it make sure you Update() it or it won't be saved to the database.
You can search my github repositories for examples

- https://github.com/GeertBellekens/Enterprise-Architect-VBScript-Library
- https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework

Geert

QuimSA

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Creating new constraints in a collection
« Reply #2 on: June 26, 2019, 10:03:56 pm »
Thanks geert!

Finished it already, if someone is interested, it looks like this:

Code: [Select]
var theElement as EA.Element; //Inicialization of Enterprise Architect element

function CreateConstraints(interf){//-------------------------------CREATES CONSTRAINTS----------------------------------

newConstraint = interf.Constraints.AddNew("",""); //enter here desired constarint name and type

newConstraint.Update();
newConstraint = null;
Session.Output("Successfully created constraint @: " + interf.Name);
}
function Main(){
theElement = Repository.GetTreeSelectedObject(); //Gets the object selected in project browser
var portCounter = theElement.Elements.Count;
for(j=0;j<portCounter;j++){
if(theElement.Elements.GetAt(j).Stereotype == "AUTOSAR Sender Receiver Port"){
CreateConstraints(theElement.Elements.GetAt(j));
Session.Output("Done");
}
}
}
« Last Edit: June 26, 2019, 11:09:28 pm by QuimSA »

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Creating new constraints in a collection
« Reply #3 on: June 26, 2019, 10:51:48 pm »
Oops. Thanks for the flowers, but it was Geert answering your question ;-)

q.

QuimSA

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Creating new constraints in a collection
« Reply #4 on: June 26, 2019, 11:09:49 pm »
Oops haha

fixed