vs2015对revit2018二次开发之导出dwg

 

通过API:doc.Export()方法导出dwg

复制代码
using Autodesk.Revit;
using Autodesk.Revit.DB;
using Autodesk.RevitAddIns;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ReadRvt
{
    class Program
    {
        static readonly string[] searchs = new string[] { "D:/Program Files/Autodesk/Revit 2018" };

        static Program()
        {

            AddEnvironmentPaths(searchs);
            AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;

        }
        [STAThread]//一定要有
        static void Main(string[] args)
        {

            Product _product = Product.GetInstalledProduct();
            var clientId = new ClientApplicationId(Guid.NewGuid(), "RevitNetTest", "TEST");
            _product.Init(clientId, "I am authorized by Autodesk to use this UI-less functionality.");
            Autodesk.Revit.ApplicationServices.Application _application = _product.Application;
            string _modelPath = @"E:/测试项目/mine/xx.rvt";
            Document doc = _application.OpenDocumentFile(_modelPath);
            Console.WriteLine("RVT文件已经打开");
            //
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(View)).OfCategory(BuiltInCategory.OST_Views);string pathFullName = "E:/data/dwg/xx";
            DWGExportOptions dwgOptions = new DWGExportOptions
            {
                FileVersion = ACADVersion.R2010,
            };
            string folder = Path.GetDirectoryName(pathFullName);
            string name = Path.GetFileNameWithoutExtension(pathFullName);

            List<ElementId> viewIds = new List<ElementId>();
            try
            {//多个视图
                foreach (View view in collector.ToElements())
                {
                    if (view.CanBePrinted)
                    {
                        viewIds.Add(view.Id);
                    }
                }
                Console.WriteLine("   start   ");
                doc.Export(folder, name, viewIds, dwgOptions);
            }catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
            Console.WriteLine("   ok   ");
            doc.Close();
            _product?.Exit();
            Console.ReadKey(true);
        }

        static void AddEnvironmentPaths(params string[] paths)
        {
            try
            {
                var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
                var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), paths.Concat(path));
                Environment.SetEnvironmentVariable("PATH", newPath);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

        }
        private static Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                var assemblyName = new AssemblyName(args.Name);
                foreach (var item in searchs)
                {
                    var file = string.Format("{0}.dll", System.IO.Path.Combine(item, assemblyName.Name));

                    if (File.Exists(file))
                    {
                        return Assembly.LoadFile(file);
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            return args.RequestingAssembly;

        }
    }
}
复制代码

 

 

posted @   慕尘  阅读(682)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示