Can someone help? I am trying to list all of the assemblies in a document to spool pipe and duct assemblies. This is what I have so far, but I am getting an error saying " 'Collectors' does not exist in the current context". Could someone tell me how to define this?
List<AssemblyType> assemblyTypes = Collectors.GetAssemblyTypesFromProject(doc); foreach(var assemblyType in assemblyTypes) { Console.WriteLine(assemblyType.Id); Console.WriteLine(assemblyType.Name); }
public static List<AssemblyType> GetAssemblyTypesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyType)); return collector.Cast<AssemblyType>().ToList(); }
Can someone help? I am trying to list all of the assemblies in a document to spool pipe and duct assemblies. This is what I have so far, but I am getting an error saying " 'Collectors' does not exist in the current context". Could someone tell me how to define this?
List<AssemblyType> assemblyTypes = Collectors.GetAssemblyTypesFromProject(doc); foreach(var assemblyType in assemblyTypes) { Console.WriteLine(assemblyType.Id); Console.WriteLine(assemblyType.Name); }
public static List<AssemblyType> GetAssemblyTypesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyType)); return collector.Cast<AssemblyType>().ToList(); }
Nobody can answer because nobody knows where the function GetAssemblyTypesFromProject is in relation to the class that calls it?
The below is how I filter I think the .OfClass/.OfCategory methods are older and return a FilteredElementCollector whereas you can use multiple ElementFilters with a FilteredElementCollector before you call .ToElements or .ToElementIDs.
public Result TObj42(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { if (commandData.Application.ActiveUIDocument == null) return Result.Cancelled; Document Doc = commandData.Application.ActiveUIDocument.Document; FilteredElementCollector FEC = new FilteredElementCollector(Doc); ElementClassFilter ECF = new ElementClassFilter(typeof(AssemblyType)); IList<Element> Els = FEC.WherePasses(ECF).ToElements(); Int32 i; for (i = 0; i <= Els.Count - 1; i++) { Console.WriteLine(Els[i].Id.IntegerValue); Console.WriteLine(Els[i].Name); } return Result.Succeeded; }
You probably want to cast to the AssemblyInstance class not the AssemblyType class.
The AssemblyInstance.GetMemberIds can be used to find out what an Assembly is made up of.
AssemblyType.Category will tell you it's an assembly (not useful)
AssemblyType.Name will take the form of the naming category selected by the user (Assemblies can contain multiple categories and one is selected for naming).
Therefore a more granular approach with AssemblyInstance is probably required.
Nobody can answer because nobody knows where the function GetAssemblyTypesFromProject is in relation to the class that calls it?
The below is how I filter I think the .OfClass/.OfCategory methods are older and return a FilteredElementCollector whereas you can use multiple ElementFilters with a FilteredElementCollector before you call .ToElements or .ToElementIDs.
public Result TObj42(Autodesk.Revit.UI.ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements) { if (commandData.Application.ActiveUIDocument == null) return Result.Cancelled; Document Doc = commandData.Application.ActiveUIDocument.Document; FilteredElementCollector FEC = new FilteredElementCollector(Doc); ElementClassFilter ECF = new ElementClassFilter(typeof(AssemblyType)); IList<Element> Els = FEC.WherePasses(ECF).ToElements(); Int32 i; for (i = 0; i <= Els.Count - 1; i++) { Console.WriteLine(Els[i].Id.IntegerValue); Console.WriteLine(Els[i].Name); } return Result.Succeeded; }
You probably want to cast to the AssemblyInstance class not the AssemblyType class.
The AssemblyInstance.GetMemberIds can be used to find out what an Assembly is made up of.
AssemblyType.Category will tell you it's an assembly (not useful)
AssemblyType.Name will take the form of the naming category selected by the user (Assemblies can contain multiple categories and one is selected for naming).
Therefore a more granular approach with AssemblyInstance is probably required.
Richard, I tried to simplify what I posted but apparently I removed a little too much. I am trying to create a function that loops through existing assemblies in the Revit document and creates sheets, views, schedules, etc. and puts them on said sheet. I am having trouble with the portion that loops through to get the list of existing assemblies though, here is what I have so far:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("ED37A463-3779-4448-AB0E-2CBBA6D354A6")]
public partial class ThisApplication
{
public void CreateAssemblyViewsAndSheet() { // Get current Revit Document ID Document doc = this.ActiveUIDocument.Document; // Get current Revit UIDocument ID UIDocument uidoc = this.ActiveUIDocument; try { /////////////////////////////// // Model-components selected // /////////////////////////////// // Get selected element IDs ICollection<ElementId> elementIds = uidoc.Selection.GetElementIds(); using (Transaction transaction = new Transaction(doc)) { // Get IDs of selected elements ElementId categoryId = doc.GetElement(elementIds.First()).Category.Id; if( AssemblyInstance.IsValidNamingCategory(doc, categoryId, uidoc.Selection.GetElementIds())) { // Create NEW 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 // Set assembly name if (transaction.GetStatus() == TransactionStatus.Committed) { transaction.Start("Set Assembly Name"); assemblyInstance.AssemblyTypeName = "New Assembly Name"; transaction.Commit(); } ///////////////////////////////////////// // Create Sheet, Views, Schedules, etc // ///////////////////////////////////////// createViews(doc, transaction, assemblyInstance); } } } catch //(Exception ex) { /////////////////////////////////// // Model-components NOT selected // /////////////////////////////////// try { /////////////////////////////////////////// // Create sheets for existing assemblies // /////////////////////////////////////////// // Dialog confirming components are not selected TaskDialog.Show("Dialog", "No components selected"); ///////////////////////////////////////////////////////////////// // Loop through assemblies in Document ///////////////////////////////////////////////////////////////// What am I missing here?? List<AssemblyType> assemblyTypes = Collectors.GetAssemblyTypesFromProject(doc); foreach(var assemblyType in assemblyTypes) { Console.WriteLine(assemblyType.Id); Console.WriteLine(assemblyType.Name); } } catch //(Exception ex) { //////////////////////////// // No existing assemblies //////////////////////////// TaskDialog.Show("Dialog", "No existing assemblies to create views, sheets, etc"); } } }
private void createViews(Document _doc, Transaction _transaction, AssemblyInstance _assemblyInstance)
{
///////////////////////////////////////////////////////
// Create sheet, views, schedules and place on sheet //
///////////////////////////////////////////////////////
if (_transaction.GetStatus() == TransactionStatus.Committed)
{
if(_assemblyInstance.AllowsAssemblyViewCreation())
{
// Create views of assembly
_transaction.Start("View Creation");
ElementId titleBlockId = new FilteredElementCollector(_doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_TitleBlocks).Cast<FamilySymbol>().FirstOrDefault().Id;
// Create sheet and views
ViewSheet viewSheet = AssemblyViewUtils.CreateSheet(_doc, _assemblyInstance.Id, titleBlockId);
View3D view3d = AssemblyViewUtils.Create3DOrthographic(_doc, _assemblyInstance.Id);
ViewSection elevationTop = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationTop);
ViewSection elevationLeft = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationLeft);
ViewSection elevationRight = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationRight);
ViewSection elevationFront = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationFront);
ViewSection detailSectionA = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.DetailSectionA);
ViewSection detailSectionB = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.DetailSectionB);
ViewSection detailSectionH = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.HorizontalDetail);
ViewSchedule materialTakeoff = AssemblyViewUtils.CreateMaterialTakeoff(_doc, _assemblyInstance.Id );
ViewSchedule partList = AssemblyViewUtils.CreatePartList(_doc, _assemblyInstance.Id);
// Create viewports and locate views on sheet
Viewport.Create(_doc, viewSheet.Id, view3d.Id, new XYZ(1, 1, 0));
Viewport.Create(_doc, viewSheet.Id, elevationTop.Id, new XYZ(2, 2, 0));
Viewport.Create(_doc, viewSheet.Id, elevationLeft.Id, new XYZ(1, 1.7, 0));
Viewport.Create(_doc, viewSheet.Id, elevationRight.Id, new XYZ(2.5, 2, 0));
Viewport.Create(_doc, viewSheet.Id, elevationFront.Id, new XYZ(2, 1, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionA.Id, new XYZ(1.5, 1.25, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionB.Id, new XYZ(0.5, 1.5, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionH.Id, new XYZ(1.5, 2, 0));
ScheduleSheetInstance.Create(_doc, viewSheet.Id, partList.Id, new XYZ(2.5, 2.5, 0));
ScheduleSheetInstance.Create(_doc, viewSheet.Id, materialTakeoff.Id, new XYZ(2, 2.5, 0));
_transaction.Commit();
// Confirm assembly and views are created
TaskDialog.Show("Dialog", "Assembly and views created: ID=" + Convert.ToString(_assemblyInstance.Id));
}
} } //////////////////////////////////// // Get List of Assemblies //////////////////////////////////// public static List<AssemblyInstance> GetAssemblyInstancesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyInstance)); return collector.Cast<AssemblyInstance>().ToList(); } public static List<AssemblyType> GetAssemblyTypesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyType)); return collector.Cast<AssemblyType>().ToList(); }
Richard, I tried to simplify what I posted but apparently I removed a little too much. I am trying to create a function that loops through existing assemblies in the Revit document and creates sheets, views, schedules, etc. and puts them on said sheet. I am having trouble with the portion that loops through to get the list of existing assemblies though, here is what I have so far:
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("ED37A463-3779-4448-AB0E-2CBBA6D354A6")]
public partial class ThisApplication
{
public void CreateAssemblyViewsAndSheet() { // Get current Revit Document ID Document doc = this.ActiveUIDocument.Document; // Get current Revit UIDocument ID UIDocument uidoc = this.ActiveUIDocument; try { /////////////////////////////// // Model-components selected // /////////////////////////////// // Get selected element IDs ICollection<ElementId> elementIds = uidoc.Selection.GetElementIds(); using (Transaction transaction = new Transaction(doc)) { // Get IDs of selected elements ElementId categoryId = doc.GetElement(elementIds.First()).Category.Id; if( AssemblyInstance.IsValidNamingCategory(doc, categoryId, uidoc.Selection.GetElementIds())) { // Create NEW 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 // Set assembly name if (transaction.GetStatus() == TransactionStatus.Committed) { transaction.Start("Set Assembly Name"); assemblyInstance.AssemblyTypeName = "New Assembly Name"; transaction.Commit(); } ///////////////////////////////////////// // Create Sheet, Views, Schedules, etc // ///////////////////////////////////////// createViews(doc, transaction, assemblyInstance); } } } catch //(Exception ex) { /////////////////////////////////// // Model-components NOT selected // /////////////////////////////////// try { /////////////////////////////////////////// // Create sheets for existing assemblies // /////////////////////////////////////////// // Dialog confirming components are not selected TaskDialog.Show("Dialog", "No components selected"); ///////////////////////////////////////////////////////////////// // Loop through assemblies in Document ///////////////////////////////////////////////////////////////// What am I missing here?? List<AssemblyType> assemblyTypes = Collectors.GetAssemblyTypesFromProject(doc); foreach(var assemblyType in assemblyTypes) { Console.WriteLine(assemblyType.Id); Console.WriteLine(assemblyType.Name); } } catch //(Exception ex) { //////////////////////////// // No existing assemblies //////////////////////////// TaskDialog.Show("Dialog", "No existing assemblies to create views, sheets, etc"); } } }
private void createViews(Document _doc, Transaction _transaction, AssemblyInstance _assemblyInstance)
{
///////////////////////////////////////////////////////
// Create sheet, views, schedules and place on sheet //
///////////////////////////////////////////////////////
if (_transaction.GetStatus() == TransactionStatus.Committed)
{
if(_assemblyInstance.AllowsAssemblyViewCreation())
{
// Create views of assembly
_transaction.Start("View Creation");
ElementId titleBlockId = new FilteredElementCollector(_doc).OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_TitleBlocks).Cast<FamilySymbol>().FirstOrDefault().Id;
// Create sheet and views
ViewSheet viewSheet = AssemblyViewUtils.CreateSheet(_doc, _assemblyInstance.Id, titleBlockId);
View3D view3d = AssemblyViewUtils.Create3DOrthographic(_doc, _assemblyInstance.Id);
ViewSection elevationTop = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationTop);
ViewSection elevationLeft = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationLeft);
ViewSection elevationRight = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationRight);
ViewSection elevationFront = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.ElevationFront);
ViewSection detailSectionA = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.DetailSectionA);
ViewSection detailSectionB = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.DetailSectionB);
ViewSection detailSectionH = AssemblyViewUtils.CreateDetailSection(_doc, _assemblyInstance.Id, AssemblyDetailViewOrientation.HorizontalDetail);
ViewSchedule materialTakeoff = AssemblyViewUtils.CreateMaterialTakeoff(_doc, _assemblyInstance.Id );
ViewSchedule partList = AssemblyViewUtils.CreatePartList(_doc, _assemblyInstance.Id);
// Create viewports and locate views on sheet
Viewport.Create(_doc, viewSheet.Id, view3d.Id, new XYZ(1, 1, 0));
Viewport.Create(_doc, viewSheet.Id, elevationTop.Id, new XYZ(2, 2, 0));
Viewport.Create(_doc, viewSheet.Id, elevationLeft.Id, new XYZ(1, 1.7, 0));
Viewport.Create(_doc, viewSheet.Id, elevationRight.Id, new XYZ(2.5, 2, 0));
Viewport.Create(_doc, viewSheet.Id, elevationFront.Id, new XYZ(2, 1, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionA.Id, new XYZ(1.5, 1.25, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionB.Id, new XYZ(0.5, 1.5, 0));
Viewport.Create(_doc, viewSheet.Id, detailSectionH.Id, new XYZ(1.5, 2, 0));
ScheduleSheetInstance.Create(_doc, viewSheet.Id, partList.Id, new XYZ(2.5, 2.5, 0));
ScheduleSheetInstance.Create(_doc, viewSheet.Id, materialTakeoff.Id, new XYZ(2, 2.5, 0));
_transaction.Commit();
// Confirm assembly and views are created
TaskDialog.Show("Dialog", "Assembly and views created: ID=" + Convert.ToString(_assemblyInstance.Id));
}
} } //////////////////////////////////// // Get List of Assemblies //////////////////////////////////// public static List<AssemblyInstance> GetAssemblyInstancesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyInstance)); return collector.Cast<AssemblyInstance>().ToList(); } public static List<AssemblyType> GetAssemblyTypesFromProject(Document _doc) { var collector = new FilteredElementCollector(_doc).OfClass(typeof(AssemblyType)); return collector.Cast<AssemblyType>().ToList(); }
Then you just need to change:
Collectors.GetAssemblyTypesFromProject(doc)
to
GetAssemblyTypesFromProject(doc)
Since they are both in the same class. Note also comment I made previous in relation to AssemblyType vs AssemblyInstance.
You are editing a macro which is also important to mention.
Then you just need to change:
Collectors.GetAssemblyTypesFromProject(doc)
to
GetAssemblyTypesFromProject(doc)
Since they are both in the same class. Note also comment I made previous in relation to AssemblyType vs AssemblyInstance.
You are editing a macro which is also important to mention.
Can't find what you're looking for? Ask the community or share your knowledge.