Returning value of BuiltInParameter.ASSEMBLY_NAME as a string

Returning value of BuiltInParameter.ASSEMBLY_NAME as a string

Anonymous
Not applicable
1,790 Views
4 Replies
Message 1 of 5

Returning value of BuiltInParameter.ASSEMBLY_NAME as a string

Anonymous
Not applicable

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

0 Likes
Accepted solutions (3)
1,791 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Accepted 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);

            }

 

 

Message 3 of 5

Joe.Ye
Alumni
Alumni
Accepted solution

 

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!

 



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
0 Likes
Message 4 of 5

Anonymous
Not applicable

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

0 Likes
Message 5 of 5

Anonymous
Not applicable
Accepted solution
I would recommend you to use BuiltInParameter.ASSEMBLY_NAME instead of parameter name. It have to work a bit faster.

Also, BuiltInParameters are language independent. It may help to avoid the problems in the future if you will use your AddIn in localized Revit.

And do not forget to check aph variable to null. If the element doesn't have such parameter you will get an exception on the next line.
0 Likes