Get assembly IDs and names

Get assembly IDs and names

Anonymous
Not applicable
2,274 Views
4 Replies
Message 1 of 5

Get assembly IDs and names

Anonymous
Not applicable

I am working on C# application and I would like to get a list of assemblies in an Revit existing file. I was able to find an example showing how to create an assembly and name it, but if assemblies are already created how would I get a list of those assembly IDs (to create views) and names?

 

This is part of what I have so far:

// Create assembly of selected components
transaction.Start("Create Assembly Instance");
AssemblyInstance assemblyInstance = AssemblyInstance.Create(doc, uidoc.Selection.GetElementIds(), categoryId);
transaction.Commit(); // need to commit the transaction to complete the creation of the assembly instance so it can be accessed in the code below
	             	
if (transaction.GetStatus() == TransactionStatus.Committed)
{
	// Set assembly name
	transaction.Start("Set Assembly Name");
	assemblyInstance.AssemblyTypeName = "My Assembly Name";
	transaction.Commit();
}

 

Any help would be greatly appreciated. Thank you

 

0 Likes
2,275 Views
4 Replies
Replies (4)
Message 2 of 5

TheRealChrisHildebran
Advocate
Advocate
0 Likes
Message 3 of 5

Anonymous
Not applicable

Thank you for the link, but I am trying to figure out how to get all of the AssemblyID and names in a project without selecting them. It seems that most tutorials tell me how to work with parts or assemblies that are selected, but this has to be possible, right?

0 Likes
Message 4 of 5

TheRealChrisHildebran
Advocate
Advocate

Assemblies are Elements and have Ids. These two collectors will retrieve all Assembly Instances or Assembly Type from a project.

This will call the 'assemblyTypes' collector in the second code block.

	List<AssemblyType> assemblyTypes = Collectors.GetAssemblyTypesFromProject(_revitDoc);

Filtered Element Collectors - One to get Assembly Instances and anouther to get Assembly Types.

public static List<AssemblyInstance> GetAssemblyInstancesFromProject(Document revitDoc)
{
	var collector = new FilteredElementCollector(revitDoc).OfClass(typeof(AssemblyInstance));

	return collector.Cast<AssemblyInstance>().ToList();
}


public static List<AssemblyType> GetAssemblyTypesFromProject(Document revitDoc)
{
	var collector = new FilteredElementCollector(revitDoc).OfClass(typeof(AssemblyType));

	return collector.Cast<AssemblyType>().ToList();
}

For either list you would then iterate through the list appending each object with .Id or .Name

 

Interate over Assembly Types Example to Console

foreach(var assemblyType in assemblyTypes)
{
	Console.WriteLine(assemblyType.Id);
        Console.WriteLine(assemblyType.Name);
}

 

Message 5 of 5

Anonymous
Not applicable

Chris, thank you for the help. I am sure you have answered my question, but I am having trouble getting this to work. Can you tell me how to define the "Collectors" variable? I am getting an error saying "Collectors does not exist in the current context".

 

0 Likes