189 lines
6.8 KiB
C#
189 lines
6.8 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
using Autodesk.Revit.DB;
|
|
|
|
namespace ShrlAlgoToolkit.RevitCore.Extensions;
|
|
|
|
public static class DebugExtensions
|
|
{
|
|
/// <summary>
|
|
/// 生成瞬态显示的方法
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static MethodInfo GenerateTransientDisplayMethod()
|
|
{
|
|
var geometryElementType = typeof(GeometryElement);
|
|
var geometryElementTypeMethods = geometryElementType.GetMethods(
|
|
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
|
|
);
|
|
return geometryElementTypeMethods.FirstOrDefault(x => x.Name == "SetForTransientDisplay");
|
|
//return Array.Find(geometryElementTypeMethods, x => x.Name == "SetForTransientDisplay");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建临时、瞬态的几何元素
|
|
/// </summary>
|
|
/// <param name="doc"></param>
|
|
/// <param name="geoms"></param>
|
|
/// <returns></returns>
|
|
public static ElementId CreateTransientElements(this Document doc, params GeometryObject[] geoms)
|
|
{
|
|
var list = new List<GeometryObject>(geoms);
|
|
var method = GenerateTransientDisplayMethod();
|
|
var argsM = new object[4];
|
|
argsM[0] = doc;
|
|
argsM[1] = ElementId.InvalidElementId;
|
|
argsM[2] = list;
|
|
argsM[3] = ElementId.InvalidElementId;
|
|
var transientElementId = (ElementId)method.Invoke(null, argsM);
|
|
return transientElementId;
|
|
}
|
|
public static void QueryPoint(this XYZ point, string prefix = "坐标点:", bool isMetric = false)
|
|
{
|
|
Debug.WriteLine($"{prefix}{(isMetric ? point * 304.8 : point)}");
|
|
}
|
|
|
|
public static void QueryDwgLayer(this Reference refer, Document doc)
|
|
{
|
|
var elem = doc.GetElement(refer);
|
|
var geo = elem.GetGeometryObjectFromReference(refer);
|
|
var graphicsStyle = doc.GetElement(geo.GraphicsStyleId) as GraphicsStyle;
|
|
|
|
elem.QueryLocation();
|
|
Debug.WriteLine(elem.Id);
|
|
Debug.WriteLine(graphicsStyle?.GraphicsStyleCategory.Name);
|
|
refer.GlobalPoint.QueryPoint();
|
|
}
|
|
|
|
public static void QueryLocation(this Element element)
|
|
{
|
|
if (element.Location is LocationCurve loc)
|
|
{
|
|
var point = loc.Curve.GetEndPoint(0);
|
|
var point1 = loc.Curve.GetEndPoint(1);
|
|
Debug.WriteLine($"{element.Name}-{element.Id}");
|
|
point.QueryPoint("起点:");
|
|
point1.QueryPoint("终点:");
|
|
}
|
|
else
|
|
{
|
|
var lop = element.Location as LocationPoint;
|
|
lop.Point.QueryPoint();
|
|
}
|
|
}
|
|
|
|
//public static void Query()
|
|
//{
|
|
// TaskDialog taskDialog =
|
|
// new("选择方式")
|
|
// {
|
|
// MainInstruction = "选择方式",
|
|
// MainContent = "请选择方式"
|
|
// //FooterText = "<a href=\"http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=2484975 \">"
|
|
// //+ "进入revit开发中心</a>"
|
|
// };
|
|
// taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, "选择元素,获取定位点");
|
|
// taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, "选择图层");
|
|
// taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, "选择点");
|
|
// taskDialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink4, "选择文件夹");
|
|
// switch (taskDialog.Show())
|
|
// {
|
|
// case TaskDialogResult.CommandLink1:
|
|
// {
|
|
// break;
|
|
// }
|
|
|
|
// case TaskDialogResult.CommandLink2:
|
|
// {
|
|
// break;
|
|
// }
|
|
// case TaskDialogResult.CommandLink3:
|
|
// {
|
|
// break;
|
|
// }
|
|
// default:
|
|
// throw new ArgumentOutOfRangeException();
|
|
// }
|
|
//}
|
|
|
|
public static void MakeDebug(this Curve curve, Document document)
|
|
{
|
|
var pl = Plane.CreateByThreePoints(curve.GetEndPoint(0), curve.GetEndPoint(1), XYZ.Zero);
|
|
//if (curve is Line li)
|
|
//{
|
|
// if (li.Direction.AngleOnPlaneTo(XYZ.BasisX, XYZ.BasisZ) < 1E-15)
|
|
// {
|
|
// pl = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, li.GetEndPoint(0));
|
|
// }
|
|
// else
|
|
// {
|
|
// pl = Plane.CreateByNormalAndOrigin(XYZ.BasisX.CrossProduct(li.Direction), li.GetEndPoint(0));
|
|
// }
|
|
//}
|
|
//else
|
|
//{
|
|
// pl = Plane.CreateByThreePoints(curve.GetEndPoint(0), curve.GetEndPoint(1), curve.Evaluate(0.5, true));
|
|
//}
|
|
var pl1 = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, curve.GetEndPoint(0));
|
|
var arc = Arc.Create(pl1, 0.1, 0, 2 * Math.PI);
|
|
document.Create.NewModelCurve(curve, SketchPlane.Create(document, pl));
|
|
document.Create.NewModelCurve(arc, SketchPlane.Create(document, pl1));
|
|
}
|
|
|
|
public static void MakeDebug(this Solid solid, Document document)
|
|
{
|
|
var ds = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel));
|
|
ds.SetName(ds.Id.ToString());
|
|
var option = ds.GetOptions();
|
|
option.ReferencingOption = DirectShapeReferencingOption.NotReferenceable;
|
|
ds.SetOptions(option);
|
|
ds.AppendShape([solid]);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 几何面
|
|
/// </summary>
|
|
/// <param name="face">有边界</param>
|
|
/// <param name="document"></param>
|
|
public static void MakeDebug(this Face face, Document document)
|
|
{
|
|
foreach (EdgeArray array in face.EdgeLoops)
|
|
{
|
|
foreach (Edge edge in array)
|
|
{
|
|
MakeDebug(edge.AsCurve(), document);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 圆心点
|
|
/// </summary>
|
|
/// <param name="point"></param>
|
|
/// <param name="document"></param>
|
|
/// <param name="radius"></param>
|
|
public static void DebugCreate(this XYZ point, Document document, double radius = 2)
|
|
{
|
|
var pl = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, point);
|
|
var arc = Arc.Create(pl, radius, 0, 2 * Math.PI);
|
|
document.Create.NewModelCurve(arc, SketchPlane.Create(document, pl));
|
|
//if (document.ActiveView.GenLevel != null)
|
|
//{
|
|
// var sketch = SketchPlane.Create(document, document.ActiveView.GenLevel.Id);
|
|
// var arc = Arc.Create(sketch.GetPlane(), 2, 0, 2 * Math.PI);
|
|
// document.Create.NewModelCurve(arc, sketch);
|
|
//}
|
|
//else
|
|
//{
|
|
// var pl = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, point);
|
|
// var arc = Arc.Create(pl, 2, 0, 2 * Math.PI);
|
|
// document.Create.NewModelCurve(arc, SketchPlane.Create(document, pl));
|
|
//}
|
|
}
|
|
public static void DebugCreate(this Edge edge, Document document)
|
|
{
|
|
MakeDebug(edge.AsCurve(), document);
|
|
}
|
|
}
|