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