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: 

BuiltInParameter.ASSEMBLY_NAME as a string Part 2

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
kwhite
986 Views, 6 Replies

BuiltInParameter.ASSEMBLY_NAME as a string Part 2

Hello - It's me again.  I really appreciate the help previously on my code snippet to get the value of the BuiltinParameter ASSEMBLY_NAME and pass it through to another parameter as a string for elements that were selected.  The help was really appreciated!

 

Now I am trying to collect all structural connections in a model, test to see if they are part of an Assembly, and if so, collect the string value of the ASSEMBLY_NAME parameter and pass that through to a Shared Parameter. 

 

I took some code that I have used previously to iterate through structural members and change the value of a prameter.  This code created a list of elements through a FilteredElementCollector and then modified the parameter within each element.  

 

I merged the previous code with the code updated yesterday with the help of others in this forum hoping to perfome the same task to all elements in the model, but I get an error due to a read-only parameter (see attached image).

 

Any help with my code (and whatever silly error I made) would be appreciated again.

 

Thanks,

Keith

 

Code:

 

         public void Host_All_w()
        {
            //estamblish the current UI Document
            UIDocument uiDoc = this.Application.ActiveUIDocument;
            Document doc = uiDoc.Document;
            
            //establish Product Host Parameter Name as a sting variable
            string cph = "Construction.Product.Host";
            
            //establish a Filtered Element Collector
            FilteredElementCollector collector = new FilteredElementCollector(doc);
                        
            //use FilteredElementCollector to get all Structural Connections
            ElementCategoryFilter structuralConnectionFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructConnections);
            
            //get all of the filtered elements as a List
            //Also specify WhereElementIsNotElementType() so that we do not get Family Symbols
            IList<Element> allStructuralConnectionMembers = collector.WherePasses(structuralConnectionFilter).WhereElementIsNotElementType().ToElements();
            
            //start the transaction
            using (Transaction trans = new Transaction(doc, "Assign Product Host All"))
            {
                try 
                {
                    trans.Start();
                    
                    //establish an array of elements from the collector
                    foreach (Element e in allStructuralConnectionMembers) 
                        {
                        //get the value of the BuiltInParameter ASSEMBLY_NAME
                        var aphParameter = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME);
                        
                            //test is the parameter value is null
                            if (aphParameter == null)
                                {
                                //Assembly name parameter does not exist for this element
                                continue;
                                }
                        
                            //continue the routine if the value is not null
                                                
                            //set the value of the aphParameter to a string value which matches the Assembly Name
                            var aphValue = aphParameter.AsString();  
                            
                            //set the value of the Construction Product Host (cph)
                            var cphParameter = e.get_Parameter(cph);
                            
                            //test to see if cph Parameter exists
                            if (cphParameter == null)
                            {
                            //Construction Product Host Parameter does not exist
                            continue;
                            }
                            
                            //continue the routine if the value is not null
                        
                        //set the Construction Product Host Parameter
                        cphParameter.Set(aphValue);
                        
                        }
                    
                    trans.Commit();
                    
                    //show a task dialog indicating the work is complete
                    TaskDialog.Show("Product Hosting", "Structural Connections have been updated with the appropriate Construction Product Host");
                } 
                
                catch (Exception ex) 
                {
                    if (trans.GetStatus() == TransactionStatus.Started)
                    {
                        trans.RollBack();
                    }
                    
                    TaskDialog.Show("Product Hosting Error", "There was an error hosting the structural connections" + Environment.NewLine + Environment.NewLine + ex.ToString());
                }
                
            }
            
        }

6 REPLIES 6
Message 2 of 7
kwhite
in reply to: kwhite

Update - My previous code works with Structural Framing members.  In an attempt to see if the code above worked on Structural Framing members, it did with no errors.  So, it appears as though the problem is in the use of the Structural Connections Category.

 

One thing I failed to note previously is the fact that there are nested structural connection families in the test model.  Could this be the issue maybe?

 

Keith

Message 3 of 7
Joe.Ye
in reply to: kwhite

 

Hi Keith,

 

The reason is that the parameter with the name of "Construction.Product.Host" is read-only. 

For readonly parameters we cannot change their parameter value.

That means you cannot change this parameter value via the UI manually.

 

 



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
Message 4 of 7
kwhite
in reply to: Joe.Ye

Thanks for the reply, and sorry for the length post below.  It is necessary to convey the issues that I am having.

 

I understand that read-only parameters are not editable; I just don't understand why this particular code I have written is returning the read only status.  The code, from my part 1 posts will change the parameter of all items pre-selected, with the exception of nested families.  Nested families are not selected when performing a selection window.  So, I updated the code to use a filtered element collector to change the code of all structural connection elements in the project which were part of an assembly.  This is where the code acts strange.  See below for a synopsis of the issues.

 

Using a Selection Window to determine objects in which the code from part one will change the parameter value:

Using code from my part 1 post that uses pre-selection, I can select elements and have Revit read the value of BuiltInParameter.ASSEMBLY_NAME, return it as a string value, then update the Construction.Product.Host shared parameter with the string value collected.  However, this method does not work for nested families that do not have the Construction.Product.Host parameter mapped to the host family itself (Type A Families).  The code does work for nested families that have the Construction.Product.Host parameter value linked to the host family Construction.Product.Host parameter value (Type B Families).  Due to the fact that many of our connection families connect differing products to one another, I cannot map all nested families Construction.Product.Host parameter values to the host family Construction.Product.Host parameter value.

 

Using the pre-selection method, I have to tab-select to each nested family and manually add the Construction.Product.Host parameter value, or run the code again for the individually selected elements.  With a few thousand nested families in a project, this quickly becomes a time consuming effort.

 

Using a Filtered Element Collector to determine objects in which the code from part two will change the parameter value:

Using code from my part 2 post, I can use a filtered element collector and collect all structural connections, have Revit read the value of BuiltInParameter.ASSEMBLY_NAME, return it as a string value, then update the Construction.Product.Host shared parameter with the string value collected.  This works for some of the nested families nicely (Type B Families), but others throw the read only parameter error (Type A Families).  

 

Nested families which work well with the selection window code (Type A Families) throws a read only error with the filtered element collector code. Nested families without mapped parameters to the host family (Type B Families) seem to work great with the filtered element collector code.

 

My goal is to cycle through all structural connection families and assign a Construction.Product.Host parameter value to them, no matter how the family is hosted in the project.

 

I can provide 3 sandbox projects which illustrate the above issues, but they are over the filesize limit of the forum (+/- 5 MB ea).  I can email them or share them via a file sharing link to anyone interested in helping.

 

Thanks again for the support.

Keith

 

Message 5 of 7
Joe.Ye
in reply to: kwhite

Hi Keith,

It is useful to see the minimum model.
Can you upload your model via the ADN Devhelp Online if the model is not open to public/
if that's not secret, you can also share that via sandbox.


Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network
Message 6 of 7
kwhite
in reply to: Joe.Ye

Thanks for the reply Joe.  I have created a service request and uploaded the files.  The case number is 08892193.

 

Thanks again for looking into this.  If it can be determined what is causing the exceptions to be thrown, then maybe I can somehow avoid the error causing situation in my family building.

 

Keith

Message 7 of 7
Joe.Ye
in reply to: kwhite

 

Hi Keith,

 

I think this issue is not because of the way how you get the structural connection family instance. Because there are 3 family instance's parameter are readonly. However these three family instance doesn't cover the one you specified. I just add  one line to check if the parameter is readonly to filter out those readonly.

This can change the parameter value. See below code.

 

[TransactionAttribute(TransactionMode.Manual)]
public class RevitCommand3 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string messages, ElementSet elements1)
{

UIApplication app = commandData.Application;
Document document = app.ActiveUIDocument.Document;


//estamblish the current UI Document
UIDocument uidoc = app.ActiveUIDocument;
Document doc = uidoc.Document;
string appName = "All Elements Hosting";

string cph = "Construction.Product.Host";
//establish a Filtered Element Collector
FilteredElementCollector collector = new FilteredElementCollector(doc);

//use FilteredElementCollector to get all Structural Connections
ElementCategoryFilter structuralConnectionFilter = new ElementCategoryFilter(BuiltInCategory.OST_StructConnections);

//get all of the filtered elements as a List
//Also specify WhereElementIsNotElementType() so that we do not get Family Symbols
IList<Element> allStructuralConnectionMembers = collector.WherePasses(structuralConnectionFilter).WhereElementIsNotElementType().ToElements();


//start the transaction
using (Transaction trans = new Transaction(doc, "Assign Product Host All"))
{
try
{
trans.Start();

//establish an array of elements from the collector
foreach (Element e in allStructuralConnectionMembers)
{
//get the value of the BuiltInParameter ASSEMBLY_NAME
var aphParameter = e.get_Parameter(BuiltInParameter.ASSEMBLY_NAME);

//test is the parameter value is null
if (aphParameter == null)
{
//Assembly name parameter does not exist for this element
continue;
}

//continue the routine if the value is not null

//set the value of the aphParameter to a string value which matches the Assembly Name
var aphValue = aphParameter.AsString();

//set the value of the Construction Product Host (cph)
var cphParameter = e.get_Parameter(cph);

//test to see if cph Parameter exists
if (cphParameter == null)
{
//Construction Product Host Parameter does not exist
continue;
}

//continue the routine if the value is not null

//set the Construction Product Host Parameter
if(cphParameter.IsReadOnly == false)
cphParameter.Set(aphValue);

}

trans.Commit();


//show a task dialog indicating the work is complete
TaskDialog.Show("Product Hosting", "Structural Connections have been updated with the appropriate Construction Product Host");
}

catch (Exception ex)
{
if (trans.GetStatus() == TransactionStatus.Started)
{
trans.RollBack();
}

TaskDialog.Show("Product Hosting Error", "There was an error hosting the structural connections" + Environment.NewLine + Environment.NewLine + " " + ex.ToString());
}

}
return Result.Succeeded;
}

 

 


}



Joe Ye
Contractor
Developer Technical Services
Autodesk Developer Network

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