1.设计思路
和上章差不多,区别不大,很多函数略加修改就可以直接使用。
通过ui交互设置过滤房间的条件,提取房间标高和房间轮廓,生成墙体,连接墙体以及同步墙体和房间的信息。
2.界面设计
SetUiusing Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace CreateWallSuface
{
public partial class SetUi : System.Windows.Forms.Form
{
List<Element> rooms = null;
public SetUi(List<Element> room, List<string> roomParametersNames, List<string> wallTypeNames)
{
InitializeComponent();
roomParametersNames.Add("无");
parametersName.DataSource = roomParametersNames;
wallTypeName.DataSource = wallTypeNames;
rooms = room;
}
//传入属性名称,在属性值的combox中显示对应的值
private void parametersName_SelectedIndexChanged(object sender, EventArgs e)
{
if (parametersName.SelectedItem != null)
{
string parameterName = parametersName.SelectedItem.ToString();
List<string> parameterValuelist = new List<string>();
parameterValuelist.Add("无");
foreach (Element el in rooms)
{
parametersValue.DataSource = null;
IList<Parameter> roomsPa = el.GetParameters(parameterName);
foreach (Parameter pa in roomsPa)
{
if (pa.HasValue)
{
if (pa.StorageType != StorageType.String)
{
if (!parameterValuelist.Contains(pa.AsValueString()))
{
parameterValuelist.Add(pa.AsValueString());
}
}
else
{
if (!parameterValuelist.Contains(pa.AsString()))
{
parameterValuelist.Add(pa.AsString());
}
}
}
}
}
parametersValue.DataSource = parameterValuelist;
}
}
private void commitButton_Click(object sender, EventArgs e)
{
if (parametersName.SelectedItem != null
&& parametersValue.SelectedItem != null
&& wallTypeName.SelectedItem != null
&& double.Parse(wallHeight.Text) > 0)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
private void SetUi_Load(object sender, EventArgs e)
{
//设置控件显示的默认值
parametersName.SelectedIndex = 1;
parametersValue.SelectedIndex = 0;
wallTypeName.SelectedIndex = 0;
wallHeight.Text = "3000";
}
//限制只能输入数组
private void wallHeight_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
//限制只能输入数组
private void BottomOffset_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
}
}
3后台处理
部分函数和上章不同,贴重点。
获取房间轮廓中,传入房间集合和在ui界面设置的条件,返回边界集合,过滤后的房间集合和墙的集合(相邻的墙连接)。
//获取房间轮廓
private List<CurveLoop> RoomBoundaryList(List<Element> roomList,List<string> createSetting, out List<Element> roomToCreate, out List<List<ElementId>> wallToJoin)
{
//通过传入的creatSetting获取指定房间
List<Element> roomsList = new List<Element>();
string paraName = createSetting[0];
string paraValue = createSetting[1];
foreach (Element ele in roomList)
{
ParameterMap paraMap = ele.ParametersMap;
foreach (Parameter para in paraMap)
{
if (para.Definition.Name == paraName)
{
if (para.HasValue)
{
string value;
if (para.StorageType == StorageType.String)
value = para.AsString();
else
value = para.AsString();
if (value == paraValue)
roomsList.Add(ele);
}
}
}
}
List<CurveLoop> CurveLoopList = new List<CurveLoop>();
roomToCreate = new List<Element>();
wallToJoin = new List<List<ElementId>>();
foreach (Element element in roomsList)
{
Room room = element as Room;
SpatialElementBoundaryOptions seBoundaryOption = new SpatialElementBoundaryOptions();
IList<IList<BoundarySegment>> roomBoundaryListList = room.GetBoundarySegments(seBoundaryOption);
//获取房间的所有边界
if (roomBoundaryListList != null)
{
CurveLoop ca = new CurveLoop();
foreach (IList<BoundarySegment> roomBoundaryList in roomBoundaryListList)
{
ca = new CurveLoop();
List<ElementId> wall = new List<ElementId>();
foreach (BoundarySegment roomBoundary in roomBoundaryList)
{
ca.Append(roomBoundary.GetCurve());
wall.Add(roomBoundary.ElementId);
}
CurveLoopList.Add(ca);
roomToCreate.Add(element);
wallToJoin.Add(wall);
}
}
}
return CurveLoopList;
}
在创建墙体面层,需要注意连接相邻的墙。
//创建墙体面层
private bool CreateWallSuface(Document doc, List<CurveLoop> CurveLoopList, WallType walltype, List<Element> roomsList,List<string> createSetting,List<List<ElementId>> wallToJoin)
{
double wallHeight=double.Parse(createSetting[3]) / 304.8;//1英尺=304.8mm
double bottomOffset = double.Parse(createSetting[4]) / 304.8;
bool structural = false;
int boundary = 0;
if (createSetting[5] == bool.TrueString) structural = true;
if (createSetting[6] == bool.TrueString) boundary = 1;
using (Transaction trans = new Transaction(doc, "生成墙体面层"))
{
trans.Start();
for (int i = 0; i < CurveLoopList.Count; i++)
{
//更改偏移量
CurveLoop cLoop = CurveLoop.CreateViaOffset(CurveLoopList[i], walltype.Width / 2, -XYZ.BasisZ);
for(int j=0;j<cLoop.Count();j++)
{
Wall aWall = Wall.Create(doc, cLoop.ElementAt(j), walltype.Id, roomsList[i].LevelId, wallHeight, bottomOffset, false, structural);
aWall.get_Parameter(BuiltInParameter.WALL_ATTR_ROOM_BOUNDING).Set(boundary);
doc.Regenerate();//更新模型
//连接相邻的墙
if(wallToJoin[i][j].IntegerValue > 0)
{
Element secWall = doc.GetElement(wallToJoin[i][j]);
JoinGeometryUtils.JoinGeometry(doc,secWall, aWall);
}
}
}
trans.Commit();
}
return true;
}
网友评论