2019年4月28号。Revit版本2017,VS版本2017
先看实现的效果
通过加载一次代码实现多次重读创建墙的功能。原理比较简单,首先选取模型线,根据模型线生成默认墙,并将顶部约束在标高2上。具体的实现代码如下:
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Selection sel = uidoc.Selection;
View view = uidoc.ActiveView;
ElementId l1 = new FilteredElementCollector(doc).OfClass(typeof(Level)).Cast<Level>()
.Where(m => m.Name == "标高 1").First().Id;
bool isCoutine = true;
ISelectionFilter selectionFilter = new ModelLineFillter();
Transaction ts = new Transaction(doc, "外部事件");
try
{
while (isCoutine)
{
ts.Start();
try
{
ModelLine c1 =
doc.GetElement(sel.PickObject(ObjectType.Element, selectionFilter, "请选择模型线")) as ModelLine;
Curve curve = c1.GeometryCurve;
if (c1 != null)
{
Wall wall = Wall.Create(doc, curve, l1, false);
if (wall != null)
{
Parameter para = wall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE);
if (!para.IsReadOnly)
{
bool isC = para.Set(new ElementId(694));
}
}
ts.Commit();
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
{
TaskDialog.Show("Revit", "命令被取消!");
ts.Commit();
return Result.Succeeded;
}
}
}
catch (Exception ex)
{
if (ts.GetStatus() == TransactionStatus.Started)
{
ts.RollBack();
}
TaskDialog.Show("Revit", ex.Message);
}
return Result.Succeeded;
}
以下为ModelLineFillter的代码
/// <summary>
/// 选择限制类,以下为只能选择模型线
/// </summary>
public class ModelLineFillter : ISelectionFilter
{
public bool AllowElement(Element elem)
{
if (elem.Name == "模型线")
{
return true;
}
return false;
}
public bool AllowReference(Reference reference, XYZ position)
{
return false;
}
}