diff --git a/RvAddinTest/CADCurveToModelCurveCmd.cs b/RvAddinTest/CADCurveToModelCurveCmd.cs new file mode 100644 index 0000000..4d694f0 --- /dev/null +++ b/RvAddinTest/CADCurveToModelCurveCmd.cs @@ -0,0 +1,107 @@ +using System; + +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.UI; +using Autodesk.Revit.UI.Selection; + +using Sai.Toolkit.Revit.Assist; + +namespace RvAddinTest; +[Transaction(TransactionMode.Manual)] +[Regeneration(RegenerationOption.Manual)] +public class CADCurveToModelCurveCmd : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + //程序UI界面 + UIApplication uiapp = commandData.Application; + //获取元素(选择) 显示元素 视图(活动视图)管理(对象) + UIDocument uidoc = uiapp.ActiveUIDocument; + //程序 + Autodesk.Revit.ApplicationServices.Application app = uiapp.Application; + //获取位置和场地 视图(多个视图)管理 获取元素(Revit 项目里的全部元素) + Document doc = uidoc.Document; + //获取所有打开文档 + DocumentSet docset = uiapp.Application.Documents; + //当前视图 + View view = doc.ActiveView; + + #region SelectExecute + + using (Transaction trans = new Transaction(doc, "default")) + { + try + { + Reference refer = uidoc.Selection.PickObject(ObjectType.Element, "请选择CAD"); + Element e = uidoc.Document.GetElement(refer); + if (e is ImportInstance ins) + { + trans.Start(); + var cadGeo = e.GetGeometryObjects().FirstOrDefault(); + List curves = []; + if (cadGeo is GeometryInstance geometryInstance) + { + curves = [.. geometryInstance.GetInstanceGeometry()]; + } + foreach (var c in curves) + { + + if (c is NurbSpline nurb) + { + if (nurb.Length > 100 / 304.8) + { + var points = nurb.Tessellate(); + ReferencePointArray array = new ReferencePointArray(); + foreach (var p in points) + { + var rp = doc.FamilyCreate.NewReferencePoint(p); + array.Append(rp); + } + var curveBypoints = doc.FamilyCreate.NewCurveByPoints(array); + } + } + if (c is PolyLine poly) + { + ReferencePointArray array = new ReferencePointArray(); + + for (int i = 0; i < poly.NumberOfCoordinates - 1; i++) + { + var p = poly.GetCoordinate(i); + //Line.CreateBound(p, poly.GetCoordinate(i + 1)); + } + } + if (c is Line l) + { + + } + } + trans.Commit(); + } + + } + catch (Autodesk.Revit.Exceptions.OperationCanceledException ex) + { + if (trans.GetStatus() == TransactionStatus.Started) + { + trans.Commit(); + } + + return Result.Succeeded; + } + catch (Exception ex) + { + message = ex.Message; + if (trans.GetStatus() == TransactionStatus.Started) + { + trans.RollBack(); + } + return Result.Failed; + } + } + + #endregion SelectExecute + + return Result.Succeeded; + } +} \ No newline at end of file diff --git a/RvAddinTest/CorrectSlope.cs b/RvAddinTest/CorrectSlope.cs index 3e3c8f3..5e08b5e 100644 --- a/RvAddinTest/CorrectSlope.cs +++ b/RvAddinTest/CorrectSlope.cs @@ -28,23 +28,22 @@ internal class CorrectSlope : ExternalCommand var elem = Document.GetElement(id); if (elem is MEPCurve mep) { - var loc = mep.GetLocCurve() as Line; - var x = loc.Direction.X; - var y = loc.Direction.Y; - var z = loc.Direction.Z; - if (Math.Abs(loc.Direction.X) > 0 && Math.Abs(loc.Direction.X) < 10E-5) - { - x = 0; - } - if (Math.Abs(loc.Direction.Y) > 0 && Math.Abs(loc.Direction.Y) < 10E-5) - { - y = 0; - } - if (Math.Abs(loc.Direction.Z) > 0 && Math.Abs(loc.Direction.Z) < 10E-5) - { - z = 0; - } - + var loc = mep.GetCurve() as Line; + var x = Math.Round(loc.Direction.X); + var y = Math.Round(loc.Direction.Y); + var z = Math.Round(loc.Direction.Z); + //if (Math.Abs(x) < 10E-5) + //{ + // x = 0; + //} + //if (Math.Abs(y) < 10E-5) + //{ + // y = 0; + //} + //if (Math.Abs(z) < 10E-5) + //{ + // z = 0; + //} var dir = new XYZ(x, y, z); var endPoint = loc.Origin + dir * loc.Length; var li = Line.CreateBound(loc.Origin, endPoint); diff --git a/RvAddinTest/CreateWindow.xaml b/RvAddinTest/CreateWindow.xaml new file mode 100644 index 0000000..1f91a00 --- /dev/null +++ b/RvAddinTest/CreateWindow.xaml @@ -0,0 +1,25 @@ + + + +