如何为块的 属性 设置初始值(Enterprise Architect)
How to set the Initial Value for a Property of a Block (Enterprise Architect)
问题陈述
我有一个名为“Block1”的块。在块中,我插入了一个名为“est_Mass”的标准 属性 类型。我想定义使用脚本的初始值(从其他地方提取数字)。但是我找不到这样做的方法。
我的伪代码如下
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;
}
带有 CustomProperties 的解决方案
根据提供的答案进行了更新,并在 JavaScript 中重新重复以确保完整性
我编写了一个脚本,使用 CustomProperties class.
更新 'default' 值
脚本
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;
}
输出
***
Before assignment default custom property of est_Mass is 0
***
***
After assignment default custom property of est_Mass is 49
***
关闭显示块的图表并重新打开它。 “est_Mass”的值现在是 49
重要提示:请参考Geert的回答。这是“幕后”的方式,一般情况下应该避免。我把它留在这里 - 以防万一。
我不熟悉Javascript,所以我只能用抽象的语言来回答。此外,我没有 SysML,但假设它们引用了 属性 的初始值,这应该让你继续:
sub = theBlock.Elements.GetAt(counter)
query = "SELECT xrefid, description FROM t_xref WHERE client = '" + sub.elementGuid m+ "' AND name = 'CustomProperties'"
res = repository.SQLQuery(query)
您需要解析包含 0..n 行的 XML 结果;带有 GUID 的第一列和带有 description.One 的第二列看起来像
@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;
是的,这是 Sparxian 编码:-(
这是你在 @VALU=...@ENDVALU
之间得到的首字母。
在此处定义的 13.5 中:
现在如果你需要改变它可以这样做
repository.execute("UPDATE t_xref SET default='<the modfied desc>' WHERE xrefid='<theGuid>'")
这是一个未记录且不受支持的操作,但是由于 EA 存在并且 Sparxians 不会阻止它,因为他们所有的顾问都在使用它,所以它正在运行。如果那样的话,你只是得不到支持。尤其是当您使用它破坏您的数据库时。所以:请备份并在沙箱中测试!
<the modfied desc>
必须与您检索到的描述相同,但 @VALU=...@ENDVALU
部分除外,您需要用字符串替换 ...
。 <theGuid>
是为相应行检索的那个。
(因为我无法针对 JS 测试它,如果您 运行 遇到一些问题,请回来 - 可能...)
作为示例:查询可能已返回
<?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>
因此 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;
并且 guid 是 {91790A06-2119-411d-8308-FCFC9D7EE3E5}
为了将默认值从 0
更改为 49
您需要发出以下 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}'
这些属性存储在 EA.Element.CustomProperties
集合中
您可以遍历它们并更新它们的 Value
。
记得在拥有 CustomProperties
的 EA.Element
上调用 Update()
以确保保存您的更改。
在 vbscript 中类似于
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
重新加载您正在查看的图表以确保数据库中的更改也反映在图表上。图表将数据保存在内存中,当元素被 API.
更改时并不总是得到通知
问题陈述
我有一个名为“Block1”的块。在块中,我插入了一个名为“est_Mass”的标准 属性 类型。我想定义使用脚本的初始值(从其他地方提取数字)。但是我找不到这样做的方法。
我的伪代码如下
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;
}
带有 CustomProperties 的解决方案
根据提供的答案进行了更新,并在 JavaScript 中重新重复以确保完整性
我编写了一个脚本,使用 CustomProperties class.
更新 'default' 值脚本
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;
}
输出
***
Before assignment default custom property of est_Mass is 0
***
***
After assignment default custom property of est_Mass is 49
***
关闭显示块的图表并重新打开它。 “est_Mass”的值现在是 49
重要提示:请参考Geert的回答。这是“幕后”的方式,一般情况下应该避免。我把它留在这里 - 以防万一。
我不熟悉Javascript,所以我只能用抽象的语言来回答。此外,我没有 SysML,但假设它们引用了 属性 的初始值,这应该让你继续:
sub = theBlock.Elements.GetAt(counter)
query = "SELECT xrefid, description FROM t_xref WHERE client = '" + sub.elementGuid m+ "' AND name = 'CustomProperties'"
res = repository.SQLQuery(query)
您需要解析包含 0..n 行的 XML 结果;带有 GUID 的第一列和带有 description.One 的第二列看起来像
@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;
是的,这是 Sparxian 编码:-(
这是你在 @VALU=...@ENDVALU
之间得到的首字母。
在此处定义的 13.5 中:
现在如果你需要改变它可以这样做
repository.execute("UPDATE t_xref SET default='<the modfied desc>' WHERE xrefid='<theGuid>'")
这是一个未记录且不受支持的操作,但是由于 EA 存在并且 Sparxians 不会阻止它,因为他们所有的顾问都在使用它,所以它正在运行。如果那样的话,你只是得不到支持。尤其是当您使用它破坏您的数据库时。所以:请备份并在沙箱中测试!
<the modfied desc>
必须与您检索到的描述相同,但 @VALU=...@ENDVALU
部分除外,您需要用字符串替换 ...
。 <theGuid>
是为相应行检索的那个。
(因为我无法针对 JS 测试它,如果您 运行 遇到一些问题,请回来 - 可能...)
作为示例:查询可能已返回
<?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>
因此 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;
并且 guid 是 {91790A06-2119-411d-8308-FCFC9D7EE3E5}
为了将默认值从 0
更改为 49
您需要发出以下 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}'
这些属性存储在 EA.Element.CustomProperties
集合中
您可以遍历它们并更新它们的 Value
。
记得在拥有 CustomProperties
的 EA.Element
上调用 Update()
以确保保存您的更改。
在 vbscript 中类似于
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
重新加载您正在查看的图表以确保数据库中的更改也反映在图表上。图表将数据保存在内存中,当元素被 API.
更改时并不总是得到通知