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();
}