hello - I am tring to read the value of an element's BuiltInParameter.ASSEMBLY_NAME value and return it as a string value in another parameter. No matter what I try, the value returned is simply "ASSEMBLY_NAME". I know that I am missing something simple, but it keeps eluding me... If I set the value of var aph to a test string without attempting to read a parameter value, the code works well.
public void Set_Host_Assemblies()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
string cph = "Construction.Product.Host";
SelElementSet selSet = uidoc.Selection.Elements;
using (Transaction t = new Transaction(doc, "Assign Construction Product Host"))
{
t.Start();
foreach (Element e in selSet)
{
var aph = BuiltInParameter.ASSEMBLY_NAME.ToString();
foreach (Parameter param in e.Parameters)
{
if (param.Definition.Name.Equals(cph))
{
param.Set(aph);
}
}
}
t.Commit();
}
Thanks in advance for any help,
Keith
Solved! Go to Solution.
Solved by vchekalin. Go to Solution.
Solved by Joe.Ye. Go to Solution.
Solved by vchekalin. Go to Solution.
Hi kwhite,
You have made a mistake when you read ASSEMBLY_NAME parameter. Actually you do not read this parameter at all.
You have to read parameter from the element.
To properly read the parameter value you should get this parameter from the element at first. If the element has such parameter, get the parameter value.
// Get the parameter from the element var aphParameter = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME); // Get the paramater value var aphValue = aphParameter.AsString(); // consider AsValueString
And one more advide. To get the parameter by name or using BuiltInParameter you should not iterate all parameters. Use Element.get_Parameter method.
The full code snippet you may use for your task:
foreach (Element e in elementSet) { // Get the parameter from the element var aphParameter = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME); // Get the paramater value if (aphParameter == null) { // Assembly name prameter doesn't exist for this element continue; } var aphValue = aphParameter.AsString(); // consider AsValueString var cphParameter = e.get_Parameter(cph); if (cphParameter == null) { //cph parameter doesn't exist continue; } cphParameter.Set(aphValue); }
HI Keith,
Thanks for logging your question via the API forums.
To get the value of the parameter which builtin parameter is ASSEMBLY_NAME, you need to get the parameter that this builtin parameter points to. Then from the returned parameter, we get its value by AsString() method.
var aph = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME).AsString();
BuiltInParameter.ASSEMBLY_NAME.ToString() just simply returns the parameter name, instead of the parameter value.
So here is the corrected one.
public void Set_Host_Assemblies()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
string cph = "Construction.Product.Host";
SelElementSet selSet = uidoc.Selection.Elements;
using (Transaction t = new Transaction(doc, "Assign Construction Product Host"))
{
t.Start();
foreach (Element e in selSet)
{
var aph = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME).AsString();
foreach (Parameter param in e.Parameters)
{
if (param.Definition.Name.Equals(cph))
{
param.Set(aph);
}
}
}
t.Commit();
}
______________________________________________________________
If my post answers your question, please click the "Accept as Solution" button. This helps everyone find answers more quickly!
Thank you for the reply and the improved code. I was able to get the code working yesterday evening using the code snippet below.
Parameter aph = e.get_Parameter("Assembly Name");
string aphData = aph.AsString();
Thanks again for the code.
Keith