Revit API创建一个拷贝房间内对象布局命令

本课程演示创建一个拷贝房间内对象布局命令,完整演示步骤和代码。这个命令把选中房间内的对象复制到其它选中的一个或多个房间中,而且保持与源房间一致的相对位置。通过本讲座使听众知道创建一个二次开发程序很简单,创建一个实用的命令也很快。
//复制房间
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class cmdCopyRoom : IExternalCommand
{
    //房间过滤器
    public class RoomFilter : ISelectionFilter
    {
        bool ISelectionFilter.AllowElement(Element elem)
        {
            return elem is Room;
        }
        bool ISelectionFilter.AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
    public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
    {
        UIApplication app = commandData.Application;
        Document doc = app.ActiveUIDocument.Document;
        //1.选择房间
        Selection sel = app.ActiveUIDocument.Selection;
        RoomFilter filter = new RoomFilter();
        Reference refRoom = sel.PickObject(ObjectType.Element, filter, "请选择房间");
        //Reference refRoom = sel.PickObject(ObjectType.Element, "请选择房间");
        Room room = doc.GetElement(refRoom) as Room;

        //2.获取目标房间的名称
        string roomName = room.get_Parameter(BuiltInParameter.ROOM_NAME).AsString();
        string roomDepartment = room.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT).AsString();
        string roomOccupancy = room.get_Parameter(BuiltInParameter.ROOM_OCCUPANCY).AsString();

        //循环创建房间
        Boolean bContinue = true;
        while (bContinue)
        {
            XYZ point;
            try
            {
                //3.获取用户输入的点
                point = sel.PickPoint("点击要创建的房间中的一点");
            }
            //捕获右击取消与esc
            catch (Autodesk.Revit.Exceptions.InvalidOperationException ex)
            {
                bContinue = false;
                break;
            }
            catch (Exception)
            {
                bContinue = false;
                break;
            }
            //4.根据选中点,创建房间
            Transaction trans = new Transaction(doc);
            trans.Start("http://revit.5d6d.com");
            Room newRoom = doc.Create.NewRoom(doc.ActiveView.GenLevel, new UV(point.X, point.Y));
            if (newRoom == null)
            {
                messages = "创建房间失败";
                return Result.Failed;
            }
            //5.读取房间的中心位置
            GeometryObjectArray geomObjectArray = newRoom.ClosedShell.Objects;
            GeometryObject geoObject = geomObjectArray.get_Item(0);
            Solid roomSolid = geoObject as Solid;
            XYZ centriod = roomSolid.ComputeCentroid();
            XYZ roomCenter = new XYZ(centriod.X, centriod.Y, doc.ActiveView.GenLevel.Elevation);
            //6.修改房间十字叉的位置
            LocationPoint roomLocation = newRoom.Location as LocationPoint;
            roomLocation.Point = roomCenter;
            //7.创建标签,放在中心
            RoomTag tag = doc.Create.NewRoomTag(newRoom, new UV(roomCenter.X, roomCenter.Y), doc.ActiveView);
            //8.赋值三个参数值
            newRoom.get_Parameter(BuiltInParameter.ROOM_NAME).Set(roomName);
            newRoom.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT).Set(roomDepartment);
            newRoom.get_Parameter(BuiltInParameter.ROOM_OCCUPANCY).Set(roomOccupancy);

            trans.Commit();
        }




        return Result.Succeeded;
    }
}
url:http://greatverve.cnblogs.com/p/CreateRoomAndCopyProperties.html
posted @ 2013-11-25 17:41  大气象  阅读(1911)  评论(0编辑  收藏  举报
http://www.tianqiweiqi.com