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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值