东西南北FilledRegion的CurveLoop计算
上一篇以东立面视图为例创建FilledRegion,接下来我们将立面视图创建FilledRegion的CurveLoop汇总一下。
上图是对四个立面坐标系间的绘制方便我们计算FilledRegion的CurveLoop。
东立面CurveLoop计算
private CurveLoop GetEastCurveLoop(XYZ leftUpPoint, double width, double height)
{XYZ point1 = leftUpPoint;XYZ point2 = point1 + XYZ.BasisY * width;XYZ point3 = point2 + -XYZ.BasisZ * height;XYZ point4 = point1 + -XYZ.BasisZ * height;CurveLoop curveloop = new CurveLoop();Line line1 = Line.CreateBound(point1, point2);Line line2 = Line.CreateBound(point2, point3);Line line3 = Line.CreateBound(point3, point4);Line line4 = Line.CreateBound(point4, point1);curveloop.Append(line1);curveloop.Append(line2);curveloop.Append(line3);curveloop.Append(line4);return curveloop;
}
西立面CurveLoop计算
private CurveLoop GetWestCurveLoop(XYZ leftUpPoint, double width, double height)
{XYZ point1 = leftUpPoint;XYZ point2 = point1 - XYZ.BasisY * width;XYZ point3 = point2 - XYZ.BasisZ * height;XYZ point4 = point1 - XYZ.BasisZ * height;CurveLoop curveloop = new CurveLoop();Line line1 = Line.CreateBound(point1, point2);Line line2 = Line.CreateBound(point2, point3);Line line3 = Line.CreateBound(point3, point4);Line line4 = Line.CreateBound(point4, point1);curveloop.Append(line1);curveloop.Append(line2);curveloop.Append(line3);curveloop.Append(line4);return curveloop;
}
南立面CurveLoop计算
private CurveLoop GetSouthCurveLoop(XYZ leftUpPoint, double width, double height)
{XYZ point1 = leftUpPoint;XYZ point2 = point1 + XYZ.BasisX * width;XYZ point3 = point2 - XYZ.BasisZ * height;XYZ point4 = point1 - XYZ.BasisZ * height;CurveLoop curveloop = new CurveLoop();Line line1 = Line.CreateBound(point1, point2);Line line2 = Line.CreateBound(point2, point3);Line line3 = Line.CreateBound(point3, point4);Line line4 = Line.CreateBound(point4, point1);curveloop.Append(line1);curveloop.Append(line2);curveloop.Append(line3);curveloop.Append(line4);return curveloop;
}
北立面CurveLoop计算
private CurveLoop GetNorthCurveLoop(XYZ leftUpPoint, double width, double height)
{XYZ point1 = leftUpPoint;XYZ point2 = point1 - XYZ.BasisX * width;XYZ point3 = point2 - XYZ.BasisZ * height;XYZ point4 = point1 - XYZ.BasisZ * height;CurveLoop curveloop = new CurveLoop();Line line1 = Line.CreateBound(point1, point2);Line line2 = Line.CreateBound(point2, point3);Line line3 = Line.CreateBound(point3, point4);Line line4 = Line.CreateBound(point4, point1);curveloop.Append(line1);curveloop.Append(line2);curveloop.Append(line3);curveloop.Append(line4);return curveloop;
}
创建SketchPlane的代码
public void CreateSketchPlane(ExternalCommandData CommandData)
{ExternalCommandData externalCommandData = CommandData as ExternalCommandData;UIDocument uiDoc = externalCommandData.Application.ActiveUIDocument;Document rvtDoc = uiDoc.Document;using (Transaction tran = new Transaction(rvtDoc, "Create SketchPlane")){tran.Start();// 创建一个新的平面,这里以XY平面为例Plane plane = Plane.CreateByNormalAndOrigin(uiDoc.Document.ActiveView.ViewDirection, uiDoc.Document.ActiveView.Origin);// 使用Document对象创建一个新的SketchPlaneSketchPlane sketchPlane = SketchPlane.Create(rvtDoc, plane);sketchPlane.Name = rvtDoc.ActiveView.Name;rvtDoc.ActiveView.SketchPlane = sketchPlane;rvtDoc.ActiveView.ShowActiveWorkPlane();rvtDoc.Regenerate();tran.Commit();}
}
相关调用的代码
protected override Result OnExecute(ExternalCommandData commandData, ref string message, ElementSet elements)
{try{// 获取当前活动的文档UIDocument uiDoc = commandData.Application.ActiveUIDocument;Document rvtDoc = uiDoc.Document;View activeView = uiDoc.ActiveView;// 获取当前文档中的选择集Selection selection = uiDoc.Selection;CreateSketchPlane(commandData);// 捕捉鼠标位置XYZ mousePosition = selection.PickPoint("请点击鼠标位置");using (Transaction tran = new Transaction(rvtDoc, "Create FilledRegion")){tran.Start();var filter = new FilteredElementCollector(rvtDoc);var dates = filter.OfClass(typeof(GraphicsStyle)).ToElements();ElementId defaultTypeId = rvtDoc.GetDefaultElementTypeId(ElementTypeGroup.FilledRegionType);FilledRegionType filledRegionType = rvtDoc.GetElement(defaultTypeId) as FilledRegionType;//过滤填充图案FilteredElementCollector fillPatternFilter = new FilteredElementCollector(rvtDoc);fillPatternFilter.OfClass(typeof(FillPatternElement));//获取实体填充FillPatternElement fp = fillPatternFilter.First(m => (m as FillPatternElement).GetFillPattern().IsSolidFill) as FillPatternElement;double width = UnitUtils.ConvertToInternalUnits(350, DisplayUnitType.DUT_MILLIMETERS);double height = UnitUtils.ConvertToInternalUnits(350, DisplayUnitType.DUT_MILLIMETERS);//东立面//CurveLoop curvesLoop = GetEastCurveLoop(mousePosition, width, height);//西立面//CurveLoop curvesLoop = GetWestCurveLoop(mousePosition, width, height);//南立面//CurveLoop curvesLoop = GetSouthCurveLoop(mousePosition, width, height);//北立面CurveLoop curvesLoop = GetNorthCurveLoop(mousePosition, width, height);IList<CurveLoop> curves = new List<CurveLoop>();curves.Add(curvesLoop);FilledRegion filledRegion = FilledRegion.Create(rvtDoc, defaultTypeId, activeView.Id, curves);OverrideGraphicSettings ogs = activeView.GetElementOverrides(filledRegion.Id);设置 投影/表面 ->填充图案->填充图案ogs.SetProjectionFillPatternId(fp.Id);设置 投影/表面 ->填充图案->颜色ogs.SetProjectionFillColor(new Color(255, 0, 0));应用到视图activeView.SetElementOverrides(filledRegion.Id, ogs);tran.Commit();}return Result.Succeeded;}catch (Exception e){return Result.Failed;}
}