Hi,
I have a macro which may do the same thing your question is about creating AssemblyInstance.
I hope this will give you some guidance and good luck!
Cheers,
So-chong
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace RevitApiForum
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("ED37A463-3779-4448-AB0E-2CBBA6D354A4")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void CreateAssemblyAndViewsAndSheet()
{
Document doc = this.ActiveUIDocument.Document;
UIDocument uidoc = this.ActiveUIDocument;
ICollection<ElementId> elementIds = uidoc.Selection.GetElementIds();
using (Transaction transaction = new Transaction(doc))
{
ElementId categoryId = doc.GetElement(elementIds.First()).Category.Id;
if( AssemblyInstance.IsValidNamingCategory( doc, categoryId, uidoc.Selection.GetElementIds() ) )
{
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)
{
transaction.Start("Set Assembly Name");
assemblyInstance.AssemblyTypeName = "My Assembly Name";
transaction.Commit();
}
if( assemblyInstance.AllowsAssemblyViewCreation() ) // check to see if views can be created for this assembly
{
if (transaction.GetStatus() == TransactionStatus.Committed)
{
transaction.Start("View Creation");
ElementId titleBlockId = new FilteredElementCollector(doc)
.OfClass(typeof(FamilySymbol))
.OfCategory(BuiltInCategory.OST_TitleBlocks)
.Cast<FamilySymbol>()
.FirstOrDefault().Id;
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 );
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();
}
}
}
}
}
}
}

