改编的Revit二次开发基础教程,适用Revit 2018,测试时自己载入公制常规模型,公制常规模型里要有模型线。
本代码在模型线位置建墙,以做程序测试效果。
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LearnTime2
{
//必备写法
[Transaction(TransactionMode.Manual)]
[Journaling(JournalingMode.NoCommandData)]
[Regeneration(RegenerationOption.Manual)]
class _59_GetInstanceGeometryCurve : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = commandData.Application.ActiveUIDocument.Document;
UIApplication uiApp = commandData.Application;
Application app = uiApp.Application;
FamilyInstance familyInstance = doc.GetElement(new ElementId(206090)) as FamilyInstance;
GetGeometryOptions getGeometryOptions = new GetGeometryOptions();
Options option = getGeometryOptions.GetGeometryOption(app);
Autodesk.Revit.DB.GeometryElement geomElement = familyInstance.get_Geometry(option);
foreach (GeometryObject geomObj in geomElement)
{
GeometryInstance geomInstance = geomObj as GeometryInstance;
if (null != geomInstance)
{
foreach (GeometryObject instObj in geomInstance.SymbolGeometry)
{
Curve curve = instObj as Curve;
if (null != curve)
{
// 把取到的线变换到实例的坐标系中
//TaskDialog.Show("Revit", curve.Length.ToString());
curve = curve.CreateTransformed(geomInstance.Transform);
Line line = curve as Line;
Level level = new FilteredElementCollector(doc).
OfClass(typeof(Level)).FirstOrDefault(x => x.Name == "标高 1") as Level;
Transaction transaction = new Transaction(doc,"创建墙");
transaction.Start();
Wall wall = Wall.Create(doc, line, level.Id, false);
transaction.Commit();
}
else
{
TaskDialog.Show("Revit", "没有curve");
}
}
}
else
{
TaskDialog.Show("Revit", "没有geomInstance");
}
}
return Result.Succeeded;
}
}
class GetGeometryOptions
{
public Options GetGeometryOption(Application app)
{
Autodesk.Revit.DB.Options option = app.Create.NewGeometryOptions();
option.ComputeReferences = true;
option.DetailLevel = ViewDetailLevel.Fine;
return option;
}
}
}