Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: QuimSA on June 26, 2019, 08:05:01 pm

Title: Creating new constraints in a collection
Post by: QuimSA 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
Title: Re: Creating new constraints in a collection
Post by: Geert Bellekens 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-VBScript-Library)
- https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework (https://github.com/GeertBellekens/Enterprise-Architect-Add-in-Framework)

Geert
Title: Re: Creating new constraints in a collection
Post by: QuimSA 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");
}
}
}
Title: Re: Creating new constraints in a collection
Post by: qwerty on June 26, 2019, 10:51:48 pm
Oops. Thanks for the flowers, but it was Geert answering your question ;-)

q.
Title: Re: Creating new constraints in a collection
Post by: QuimSA on June 26, 2019, 11:09:49 pm
Oops haha

fixed