首页 > Windows开发 > 详细

Revit API创建房间

时间:2014-03-17 21:29:35      阅读:949      评论:0      收藏:0      [点我收藏+]
start
bubuko.com,布布扣
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
public class CreateRoomAndCopyProperties : IExternalCommand
{
    public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elements)
    {
        UIDocument uiDoc = cmdData.Application.ActiveUIDocument;
        Document doc = uiDoc.Document;
        Selection sel = uiDoc.Selection;
        //1.选择房间
        RoomFilter filter = new RoomFilter();
        Reference refRoom = sel.PickObject(ObjectType.Element, filter, "选择房间:");
        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();
        //循环创建房间
        bool bContinue = true;
        while (bContinue)
        {
            XYZ point;
            try
            {
                //3.获取用户输入的点
                point = sel.PickPoint("点击要创建的房间中的一点:");
            }
            catch (Autodesk.Revit.Exceptions.InvalidOperationException ex)
            {
                bContinue = false;
                break;
            }
            catch (Exception)
            {
                bContinue = false;
                break;
            }
            //4.根据选中点,创建房间
            Transaction ts = new Transaction(doc, "http://revit.5d6d.com");
            ts.Start();
            //当前视图的楼层doc.ActiveView.GenLevel
            Room newRoom = doc.Create.NewRoom(doc.ActiveView.GenLevel, new UV(point.X, point.Y));
            if (newRoom == null)
            {
                msg = "创建房间失败。";
                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);

            ts.Commit();
        }

        return Result.Succeeded;
    }
    public class RoomFilter : ISelectionFilter
    {

        public bool AllowElement(Element elem)
        {
            return elem is Room;
        }

        public bool AllowReference(Reference reference, XYZ position)
        {
            return true;
        }
    }
}
bubuko.com,布布扣
url:http://greatverve.cnblogs.com/p/CreateRoomAndCopyProperties1.html

Revit API创建房间,布布扣,bubuko.com

Revit API创建房间

原文:http://www.cnblogs.com/greatverve/p/CreateRoomAndCopyProperties1.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!