当前位置: 首页> 财经> 产业 > 【Revit二次开发】创建rvt文件,但不打开Revit

【Revit二次开发】创建rvt文件,但不打开Revit

时间:2025/7/12 23:28:38来源:https://blog.csdn.net/liqian_ken/article/details/140206043 浏览次数:0次

介绍

需要安装Revit,但不用打开Revit加载插件,而是运行一个控制台应用,就可以创建一个rvt文件(更多读写功能都可自行添加)。

本文内容主要参考:博客1,但对内容进行了简化,只保留了最核心的内容。

本文以Revit2019为例做了测试,有博客2说Revit2022不支持该功能。

示例

  1. 创建.net framework4.7.2的控制台应用
  2. 在【引用】中引入Revit2019安装目录中三个dll:RevitNET.dllRevitAddInUtility.dllRevitAPI.dll
  3. 创建一个工具类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();}}
}
  1. 写入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();                        }}
}

效果图:

在这里插入图片描述
在这里插入图片描述

关键字:【Revit二次开发】创建rvt文件,但不打开Revit

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: