Revit 二次開發建立房間的兩種常用方法
1,使用閉合路徑
Transaction ts = new Transaction(doc, "BIM");
ts.Start();
try
{
Level level = uidoc.ActiveView.GenLevel;
PlanTopology pt = doc.get_PlanTopology(level);
foreach (PlanCircuit pc in pt.Circuits)
{
if (!pc.IsRoomLocated)
{
Room newRoom = doc.Create.NewRoom(null, pc);
LinkElementId elemid = new LinkElementId(newRoom.Id);
Location location = newRoom.Location;
LocationPoint locationPoint = location as LocationPoint;
XYZ point3d = locationPoint.Point;
UV point2d = new UV(point3d.X, point3d.Y);
RoomTag roomTag = doc.Create.NewRoomTag(elemid, point2d, view.Id);
if (family != null)
{
try
{
FamilySymbol symbol = family.Document.GetElement(family.GetFamilySymbolIds().First()) as FamilySymbol;
if (symbol != null)
{
roomTag.ChangeTypeId(symbol.Id);
}
}
catch { }
}
}
}
ts.Commit();
}
catch
{
ts.Commit();
}
2,使用點 建立 發現閉合路徑自動捕捉 拾取一個已存在的房間 複製屬性 房間的位置 暫時不能改動
Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "");
Room room = doc.GetElement(refer) as Room;
string roomDepartment = room.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT).AsString();
Transaction ts = new Transaction(doc,"BIM");
ts.Start();
XYZ xyz = uidoc.Selection.PickPoint();
LocationPoint roomPoint = room.Location as LocationPoint;
Room newRoom = doc.Create.NewRoom(doc.ActiveView.GenLevel,new UV(xyz.X,xyz.Y));
//doc.Create.NewSpace(room.Level, new UV(roomPoint.Point.X, roomPoint.Point.Y));
RoomTag tag = doc.Create.NewRoomTag(new LinkElementId(newRoom.Id), new UV(xyz.X, xyz.Y), doc.ActiveView.Id);
newRoom.get_Parameter(BuiltInParameter.ROOM_DEPARTMENT).Set(roomDepartment);
ts.Commit();
3,由於房間的位置是隻讀的 所以放置房間時 隨意選擇一點建立。獲得的幾何中心 刪除 再根據中心建立新的房間
UIDocument uidoc=commandData.Application.ActiveUIDocument; Document doc=uidoc.Document;
XYZ xyz = uidoc.Selection.PickPoint(); Solid sd = null; Transaction ts = new Transaction(doc,"BIM"); ts.Start();
Room newRoom = doc.Create.NewRoom(doc.ActiveView.GenLevel, new UV(xyz.X, xyz.Y)); GeometryElement geo = newRoom.ClosedShell; foreach (GeometryObject obj in geo) { if (obj is Solid) { Solid solid = obj as Solid; if (solid != null) { sd = solid;
break; }
} } XYZ Center = sd.ComputeCentroid(); XYZ roomCenter = new XYZ(Center.X, Center.Y, doc.ActiveView.GenLevel.ProjectElevation); Room newRoom2 = doc.Create.NewRoom(doc.ActiveView.GenLevel, new UV(roomCenter.X, roomCenter.Y)); RoomTag tag = doc.Create.NewRoomTag(new LinkElementId(newRoom2.Id), new UV(roomCenter.X, roomCenter.Y), doc.ActiveView.Id); doc.Delete(newRoom.Id); ts.Commit();