You can combine any number of model elements to create an assembly, which can then be edited, tagged, scheduled, and filtered.
The static Create() method of the AssemblyInstance class is used to create a new assembly instance in the project. The Create() method must be created inside a transaction and the transaction must be committed before performing any action on the newly created assembly instance. The assembly type is assigned after the transaction is complete. Each unique assembly has its own AssemblyType.
The following example creates a new assembly instance, changes the name of its AssemblyType and then creates some views for the assembly instance.
Code Region: Create Assembly and Views |
AssemblyInstance CreateAssemblyAndViews(Autodesk.Revit.DB.Document doc, ICollection<ElementId> elementIds) { AssemblyInstance assemblyInstance = null; using (Transaction transaction = new Transaction(doc)) { ElementId categoryId = doc.GetElement(elementIds.First()).Category.Id; // use category of one of the assembly elements if (AssemblyInstance.IsValidNamingCategory(doc, categoryId, elementIds)) { transaction.Start("Create Assembly Instance"); assemblyInstance = AssemblyInstance.Create(doc, elementIds, categoryId); transaction.Commit(); // commit the transaction that creates the assembly instance before modifying the instance's name if (transaction.GetStatus() == TransactionStatus.Committed) { transaction.Start("Set Assembly Name"); assemblyInstance.AssemblyTypeName = "My Assembly Name"; transaction.Commit(); } if (assemblyInstance.AllowsAssemblyViewCreation()) // create assembly views for this assembly instance { if (transaction.GetStatus() == TransactionStatus.Committed) { transaction.Start("View Creation"); View3D view3d = AssemblyViewUtils.Create3DOrthographic(doc, assemblyInstance.Id); ViewSchedule partList = AssemblyViewUtils.CreatePartList(doc, assemblyInstance.Id); transaction.Commit(); } } } } return assemblyInstance; } |
Various assembly views can be created for an assembly instance using static methods of the AssemblyViewUtils class, including an orthographic 3D assembly view, a detail section assembly view, a material takeoff multicategory schedule assembly view, a part list multicategory schedule assembly view, a single-category schedule and a sheet assembly view. All of these except sheet views have overloaded creation methods which create the schedule or view from a template. In addition to a template Id, these overloads have a parameter to indicate if the template will be assigned or applied.
Note that assembly views must all be assigned to the same assembly instance of the assembly type. AssemblyInstance.AllowsAssemblyViewCreation() returns true if that assembly instance can accept new assembly views (either because it already has views or because no assembly instance has views).
The following example creates a new single-category schedule for an assembly from a given template.
Code Region: Create assembly view from template |
private ViewSchedule CreateScheduleForAssembly(Document doc, AssemblyInstance assemblyInstance, ElementId viewTemplateId) { ViewSchedule schedule = null; if (assemblyInstance.AllowsAssemblyViewCreation()) // create assembly views for this assembly instance { using (Transaction transaction = new Transaction(doc)) { transaction.Start("Create Schedule"); // use naming category for the schedule if (ViewSchedule.IsValidCategoryForSchedule(assemblyInstance.NamingCategoryId)) { schedule = AssemblyViewUtils.CreateSingleCategorySchedule(doc, assemblyInstance.Id, assemblyInstance.NamingCategoryId, viewTemplateId, false); } transaction.Commit(); if (schedule != null && transaction.GetStatus() == TransactionStatus.Committed) { transaction.Start("Edit Schedule"); schedule.ViewName = "AssemblyViewSchedule"; transaction.Commit(); } } } return schedule; } |
The document must be regenerated before using any of these newly created assembly views. You'll note in the example above that a transaction is committed after creating a new assembly view. The Commit() method automatically regenerates the document.