How to set the Initial Value for a Property of a Block (Enterprise Architect)

Viewed 297

Problem Statement

I have a Block called "Block1". In the Block I have inserted a standard Property type with name "est_Mass". I want to define the Initial Value of using a script (which pulls the number from somewhere else). However I couldn't find a way to do so.

My pseudocode was as follows

define theBlock as EA.Element;
theBlock = Repository.GetTreeSelectedObject();
someValue = 49;
numOfElements = theBlock.Elements.Count;
counter = 0;
while (counter < numOfElements) {
    if (theBlock.Elements.GetAt(counter).Name = "est_Mass"){
    //how to set the "Initial" value for this property to someValue?
    }
    counter = counter + 1;
}

image of property tab in Properties window

image of block diagram with properties

picture of block with properties

Solution with CustomProperties

Updated based on the provided answers and re-iterated here in JavaScript for completeness

I've written a script that updates the 'default' value using the CustomProperties class.

Script

var theBlock as EA.Element;
var eaPart as EA.Element;

var numOfElements;
var numOfCustomProperties;
var someValue = 49;

let counter = 0;
let customPropCounter = 0;

theBlock = Repository.GetTreeSelectedObject();
numOfElements = theBlock.Elements.Count;

//loop through elements that are owned by the Block
loop:
while (counter < numOfElements){
    if (theBlock.Elements.GetAt(counter).Name === "est_Mass"){
        
        eaPart = theBlock.Elements.GetAt(counter);
        numOfCustomProperties = eaPart.CustomProperties.Count;
        
        //loop through custom properties of the desired part
        while (customPropCounter < numOfCustomProperties) {
            if (eaPart.CustomProperties.GetAt(customPropCounter).Name === "default") {
                Session.Output( `***\n Before assignment ${eaPart.CustomProperties.GetAt(customPropCounter).Name} \
                                custom property of ${theBlock.Elements.GetAt(counter).Name} \
                                is ${eaPart.CustomProperties.GetAt(customPropCounter).Value} \n***`);
                //assign new value
                eaPart.CustomProperties.GetAt(customPropCounter).Value = someValue;
                //update the part that owns the custom propert
                eaPart.Update();
                Session.Output( `***\n After assignment ${eaPart.CustomProperties.GetAt(customPropCounter).Name} \
                                custom property of ${theBlock.Elements.GetAt(counter).Name} \
                                is ${eaPart.CustomProperties.GetAt(customPropCounter).Value} \n***`);
                //let's get outta here
                break loop;
            }
            customPropCounter = customPropCounter + 1;
        }
    }   
    counter = counter + 1;
}

Output

***
 Before assignment default custom property of est_Mass is 0 
***
***
 After assignment default custom property of est_Mass is 49 
***

Close the diagram that shows the block and reopen it. The value for the "est_Mass" is now 49

2 Answers

Important: Please refer to Geert's answer. This one is the way "behind the scenes" and should be avoided in normal cases. I leave it here - just in case.


I'm not familiar with Javascript, so I can only answer using an abstract language. Also I don't have SysML but assuming they reference the initial value of a property, this should get you going:

sub = theBlock.Elements.GetAt(counter)
query = "SELECT xrefid, description FROM t_xref WHERE client = '" + sub.elementGuid m+ "' AND name = 'CustomProperties'"
res = repository.SQLQuery(query)

You need to parse the XML result which contains 0..n rows; first column with a GUID and the 2nd with the description.One of them looks like

 @PROP=@NAME=default@ENDNAME;@TYPE=String@ENDTYPE;@VALU=initial@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;@PROP=@NAME=isReference@ENDNAME;@TYPE=boolean@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;

Yes, this is Sparxian coding :-(

This is were you get the initial in between @VALU=...@ENDVALU.

In 13.5 that is defined here:

enter image description here

Now if you need to alter that it can be done like this

repository.execute("UPDATE t_xref SET default='<the modfied desc>' WHERE xrefid='<theGuid>'")

This is an undocumented and unsupported operation which however is working since EA exists and the Sparxians will not block it since all their consultants are using it. You just don't get support if for that. Especially not when you clobber your DB by using it. So: backup please and test in the sandbox!

<the modfied desc> must be the same string as the description you retrieved except for the @VALU=...@ENDVALU part where you need to string-substitue the .... <theGuid> is the one retrieved for the according row.

(Since I could not test it for JS please come back with issues if you run into some - likely...)


As a sample: the query could have returned

<?xml version="1.0"?>
<EADATA version="1.0" exporter="Enterprise Architect">
<Dataset_0><Data><Row><xrefid>{91790A06-2119-411d-8308-FCFC9D7EE3E5}</xrefid><description>@PROP=@NAME=isReference@ENDNAME;@TYPE=boolean@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;@PROP=@NAME=default@ENDNAME;@TYPE=String@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;</description></Row></Data></Dataset_0></EADATA>

So the description column contains

@PROP=@NAME=isReference@ENDNAME;@TYPE=boolean@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;@PROP=@NAME=default@ENDNAME;@TYPE=String@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;

and the guid is {91790A06-2119-411d-8308-FCFC9D7EE3E5}

In order to change the default from 0 to 49 you need to issue the following SQL:

UPDATE t_xref SET description = '@PROP=@NAME=isReference@ENDNAME;@TYPE=boolean@ENDTYPE;@VALU=0@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;@PROP=@NAME=default@ENDNAME;@TYPE=String@ENDTYPE;@VALU=49@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;' WHERE xrefid='{91790A06-2119-411d-8308-FCFC9D7EE3E5}'

These properties are stored in the EA.Element.CustomProperties collection

You can iterate over them and update their Value.

Remember to call Update() on the EA.Element that owns the CustomProperties to make sure your changes are saved.

In vbscript that would be something like

dim customProperty as EA._CustomProperty
'loop custom properties to find the one we need
for each customProperty in eaPart.CustomProperties
    if customProperty.Name = "Max_Mass" then
        customProperty.Value = "50"
        exit for 'found it, no need to continue
    end if
next
'remember to update the element to save changes
eaPart.Update

Reload the diagram you are looking at to make sure the changes in the database are also reflected on the diagram. Diagram keep data in memory and are not always notified when elements are changed by the API.

Related