Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Returning value of BuiltInParameter.ASSEMBLY_NAME as a string

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
kwhite
1499 Views, 4 Replies

Returning value of BuiltInParameter.ASSEMBLY_NAME as a string

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

4 REPLIES 4
Message 2 of 5
vchekalin
in reply to: kwhite

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
in reply to: kwhite

 

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
Message 4 of 5
kwhite
in reply to: vchekalin

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

Message 5 of 5
vchekalin
in reply to: kwhite

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community