[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;
}
}
}
[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;
}
}
}