107 lines
3.8 KiB
C#
107 lines
3.8 KiB
C#
|
|
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<GeometryObject> 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;
|
|||
|
|
}
|
|||
|
|
}
|