Revit二次开发-创建遮罩区域
实质是创建空白的填充区域
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
UIDocument uidoc = uiApp.ActiveUIDocument;
Document doc = uidoc.Document;
{
// 创建遮罩区域
CreateMaskRegion(doc);
//return Result.Succeeded;
}
return Result.Succeeded;
}
private void CreateMaskRegion(Document doc)
{
using (Transaction trans = new Transaction(doc))
{
trans.Start("创建遮罩区域");
// 找到一个填充类型,复制创建一个新的填充类型“实体填充”
FilteredElementCollector collector = new FilteredElementCollector(doc);
List<FilledRegionType> lstFilledRegionType = collector.OfClass(typeof(FilledRegionType)).
Cast<FilledRegionType>().ToList();
FilledRegionType frtNew = null;
foreach (FilledRegionType item in lstFilledRegionType)
{
if (item.Name != "遮罩区域")
continue;
frtNew = item;
break;
}
if (lstFilledRegionType.Count == 0)
return;
if (frtNew == null)
{
frtNew = lstFilledRegionType[0].Duplicate("遮罩区域") as FilledRegionType;
}
#region /* 设置填充样式、背景、线宽、颜色 */
FillPatternElement fillPatternElement = null;
collector = new FilteredElementCollector(doc);
List<FillPatternElement> fillPatternElements = collector.OfClass(typeof(FillPatternElement)).Cast<FillPatternElement>().ToList();
foreach (var item in fillPatternElements)
{
FillPattern fillPatternTemp = item.GetFillPattern();
if (fillPatternTemp.Name != "垂直 1200mm")
continue;
fillPatternElement = item;
break;
}
// 填充样式
if (fillPatternElement != null)
{
frtNew.FillPatternId = fillPatternElement.Id;
}
// 背景
frtNew.Background = FilledRegionBackground.Opaque;
// 线宽
if (frtNew.LineWeight != 1)
{
frtNew.LineWeight = 1;// 这里异常
}
// 颜色
if (frtNew.Color.Red != 0 || frtNew.Color.Green != 0 || frtNew.Color.Blue != 0)
{
frtNew.Color = new Color(0, 0, 0);
}
#endregion
// 创建填充区域
List<CurveLoop> profileloops = new List<CurveLoop>();
XYZ[] points = new XYZ[5];
points[0] = new XYZ(0.0, 0.0, 0.0);
points[1] = new XYZ(10.0, 0.0, 0.0);
points[2] = new XYZ(10.0, 10.0, 0.0);
points[3] = new XYZ(0.0, 10.0, 0.0);
points[4] = new XYZ(0.0, 0.0, 0.0);
CurveLoop profileloop = new CurveLoop();
for (int i = 0; i < 4; i++)
{
Line line = Line.CreateBound(points[i], points[i + 1]);
//Line line = app.Create.NewLineBound(points[i], points[i + 1]);
profileloop.Append(line);
}
profileloops.Add(profileloop);
ElementId activeViewId = doc.ActiveView.Id;
FilledRegion filledRegion = FilledRegion.Create(doc, frtNew.Id, activeViewId, profileloops);
trans.Commit();
}
}