Hello,I am working on copying a group from one document to another after I read your advice. However, the code always fails. Would you please check my code and give some advice. Thanks a lot! Following is my code:
using System;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace HelloWorld
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData revit, ref string message, ElementSet elements)
{
TaskDialog.Show("Start", "Hello World!");
Application app = revit.Application.Application;
UIDocument uiDoc = revit.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
using (Transaction trans = new Transaction(doc))
{
if (trans.Start("放置组1") == TransactionStatus.Started)
{
string address1 = @"C:\Users\machine\Desktop\组 1.rvt";
Document openDoc = app.OpenDocumentFile(address1);
FilteredElementCollector collector = new FilteredElementCollector(openDoc).OfClass(typeof(GroupType));
var query = from element in collector where element.Name == "组 1" select element;
List<Element> groups = query.ToList();
ElementId groupId = groups[0].Id;
ICollection<ElementId> copyIds = new Collection<ElementId>();
copyIds.Add(groupId);
ElementId copied = ElementTransformUtils.CopyElements(openDoc, copyIds, doc, Transform.Identity, new CopyPasteOptions()).First();
XYZ location = uiDoc.Selection.PickPoint("请指定插入房间的位置");
FilteredElementCollector collector1 = new FilteredElementCollector(doc).OfClass((typeof(GroupType)));
var groupTypes1 = from element in collector1 where element.Name == "组 1" select element;
GroupType groupType = groupTypes1.First() as GroupType;
Group groupCreated = doc.Create.PlaceGroup(location, groupType);
if (TransactionStatus.Committed == trans.Commit())
{
if (groupCreated != null)
{
TaskDialog.Show("创建成功", "组 1已包含在模型中");
}
else
{
TaskDialog.Show("创建失败", "没有创建组 1");
}
}
else
{
trans.RollBack();
}
}
}
return Result.Succeeded;
}
}
}
![20180621152832.png 20180621152832.png]()