介绍
需要安装Revit,但不用打开Revit加载插件,而是运行一个控制台应用,就可以创建一个rvt文件(更多读写功能都可自行添加)。
本文内容主要参考:博客1,但对内容进行了简化,只保留了最核心的内容。
本文以Revit2019为例做了测试,有博客2说Revit2022不支持该功能。
示例
- 创建.net framework4.7.2的控制台应用
- 在【引用】中引入Revit2019安装目录中三个dll:
RevitNET.dll
,RevitAddInUtility.dll
和RevitAPI.dll
- 创建一个工具类
RevitContext
用来配置Revit API环境。
下文代码中的Revit路径写死了,可自行修改或增加配置文件。
using Autodesk.Revit;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using System;
using System.Configuration;
using System.Linq;
using System.Reflection;
namespace TestRvt
{
public class RevitContext
{
public static RevitContext Instance { get; } = new RevitContext();
private static Product _product;
public static Application App => _product.Application;
static RevitContext()
{
var revitDir = @"C:\Program Files\Autodesk\Revit 2019";
//revitDir = LoadConfig();//可以自行读取配置文件,读取自己的Revit安装路径
//从config中读取revit的路径
AddEnvironmentPaths(revitDir);
}
private static void AddEnvironmentPaths(params string[] paths)
{
var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths));
Environment.SetEnvironmentVariable("PATH", newPath);
}
public static void Init()
{
_product = Product.GetInstalledProduct();
var clientId = new ClientApplicationId(Guid.NewGuid(), "AppName", "RevitAPI");
_product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
}
public static void Exit()
{
_product?.Exit();
}
}
}
- 写入rvt的代码
using Autodesk.Revit.DB;
using System;
using System.IO;
namespace TestRvt
{
public class Program
{
static Program()
{
RevitContext.Init();//尝试合并到RevitContext中,但未成功,会报错
}
[STAThread]
static void Main(string[] args)
{
var app = RevitContext.App;
var projectrTemplate = app.DefaultProjectTemplate;
var document = app.NewProjectDocument(projectrTemplate);
//清空所有楼层平面视图
var elements = new FilteredElementCollector(document);
var levels = elements.OfClass(typeof(Level)).Cast<Level>();
using (var tran = new Transaction(document))
{
tran.Start("test");
document.Delete(levels.Select(x => x.Id).ToList());
tran.Commit();
}
//创建一个平面视图
Level level = null;
using (var tran = new Transaction(document))
{
tran.Start("创建楼层标高");
level = Level.Create(document, 0);
level.Name = "MyLevel0";
var classFilter = new ElementClassFilter(typeof(ViewFamilyType));
var filterElements = new FilteredElementCollector(document);
var floorViewFamily = filterElements.WherePasses(classFilter)
.Cast<ViewFamilyType>()
.First(x => x.ViewFamily == ViewFamily.FloorPlan);
ViewPlan view = ViewPlan.Create(document, floorViewFamily.Id, level.Id);
tran.Commit();
}
//创建墙
var p1 = new XYZ(0,0,0);
var p2 = new XYZ(0,10,0);
var p3 = new XYZ(5,10,0);
using (var tran = new Transaction(document))
{
tran.Start("创建墙");
Wall.Create(document, Line.CreateBound(p1, p2), level.Id, false);
Wall.Create(document, Line.CreateBound(p2, p3), level.Id, false);
tran.Commit();
}
document.SaveAs("./r.rvt");
RevitContext.Exit();
Console.WriteLine("Over");
Console.ReadLine();
}
}
}
效果图: