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.
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 |
void CreateAssemblyAndViews(Autodesk.Revit.DB.Document doc, ICollection<ElementId> elementIds) { Transaction transaction = new Transaction(doc); // use category of one of the assembly elements ElementId categoryId = doc.GetElement(elementIds.First()).Category.Id; if (AssemblyInstance.IsValidNamingCategory(doc, categoryId, elementIds)) { transaction.Start("Create Assembly Instance"); AssemblyInstance assemblyInstance = AssemblyInstance.Create(doc, elementIds, categoryId); // commit the transaction that creates the assembly instance before modifying the instance's name transaction.Commit(); transaction.Start("Set Assembly Name"); assemblyInstance.AssemblyTypeName = "My Assembly Name"; transaction.Commit(); // create assembly views for this assembly instance if (assemblyInstance.AllowsAssemblyViewCreation()) { transaction.Start("View Creation"); View3D view3d = AssemblyViewUtils.Create3DOrthographic(doc, assemblyInstance.Id); ViewSchedule partList = AssemblyViewUtils.CreatePartList(doc, assemblyInstance.Id); transaction.Commit(); } } } |
Various assembly views can be created for an assembly instance using 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, and a sheet assembly view. 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 two new assembly views. The Commit() method automatically regenerates the document.