• vs2015对revit2018二次开发之不启动Revit,读取rvt文件里的墙


    不启动Revit,读取rvt文件里的墙

    1.使用vs2015新建控制台应用程序

    2.在项目中引入

    RevitNet.dll,Revit.dll

    3.代码实现

    using Autodesk.Revit;
    using Autodesk.Revit.DB;
    using System;
    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/xx2018.rvt";
                Document doc = _application.OpenDocumentFile(_modelPath);
                Console.WriteLine("RVT文件已经打开");
                FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
                var walls = wallCollector.OfClass(typeof(Wall)).ToArray();
                Console.WriteLine("Walls:");
                foreach (Wall wall in walls)
                {
                    Console.WriteLine("ElementId:{0}", wall.Id.IntegerValue);
                    Console.WriteLine("   name:{0}", wall.Name.ToString());
                    Console.WriteLine("   type:{0}", wall.WallType.ToString());
                    Console.WriteLine("   {0}", wall.Width.ToString());
                    Console.WriteLine("   UniqueId:{0}",wall.UniqueId.ToString());
                    Console.WriteLine("   ===============   ");
                }
                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;
            }
        }
    }

    运行结果

    注:

      从其他人那拿过来改的,由于机器上安装了多个revit版本,因此为 searchs 直接指定的 Revit 2018

       如果改成 static readonly string[] searchs = RevitProductUtility.GetAllInstalledRevitProducts().Select(x => x.InstallLocation).ToArray();

      则会出现如下错误

    4.读取墙的属性

    using Autodesk.Revit;
    using Autodesk.Revit.DB;
    using Autodesk.RevitAddIns;
    using System;
    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/xx2018.rvt";
                Document doc = _application.OpenDocumentFile(_modelPath);
                Console.WriteLine("RVT文件已经打开");
                FilteredElementCollector wallCollector = new FilteredElementCollector(doc);
                var walls = wallCollector.OfClass(typeof(Wall)).ToArray();
                Console.WriteLine("Walls:");
                foreach (Wall wall in walls)
                {
                    Console.WriteLine("ElementId:{0}", wall.Id.IntegerValue);
                    Console.WriteLine("   name:{0}", wall.Name.ToString());
                    Console.WriteLine("   type:{0}", wall.WallType.ToString());
                    Console.WriteLine("   {0}", wall.Width.ToString());
                    Console.WriteLine("   UniqueId:{0}",wall.UniqueId.ToString());
                    var enumer = wall.Parameters.GetEnumerator();
                    Console.WriteLine("   Parameters");
                    int i = 0;
                    while (enumer.MoveNext())
                    {
                        Parameter p = (Parameter)enumer.Current;
                        if (p.HasValue)
                        {
                            Console.WriteLine("      " + (i++) + ": asString:" + p.AsString() + "    asValue:" + p.AsValueString() + "    elementid:" + p.AsElementId());
                        }
                    }
                    Console.WriteLine("   ===============   ");
                }
                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;
            }
        }
    }

    结果

    RVT文件已经打开
    Walls:
    ElementId:200639
    name:基本墙:墙 1
    type:Autodesk.Revit.DB.WallType
    0.656167979002625
    UniqueId:3bd13677-6190-4cab-a469-1e3456519fcb-00030fbf
    Parameters
    0: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    1: asString: asValue:2873 mm elementid:-1
    2: asString: asValue:承重 elementid:-1
    3: asString:基本墙: 墙 1 asValue: elementid:-1
    4: asString: asValue:4000 mm elementid:-1
    5: asString: asValue:0 mm elementid:-1
    6: asString: asValue:2.218 m3 elementid:-1
    7: asString: asValue:基本墙 elementid:200622
    8: asString: asValue:是 elementid:-1
    9: asString: asValue:墙 elementid:-2000011
    10: asString: asValue:基本墙: 基本墙:墙 1 elementid:200622
    11: asString: asValue:否 elementid:-1
    12: asString: asValue:是 elementid:-1
    13: asString: asValue:无 elementid:-1
    14: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    15: asString: asValue:200622 elementid:200622
    16: asString: asValue:否 elementid:-1
    17: asString: asValue:新构造 elementid:110001
    18: asString: asValue:-4000 mm elementid:-1
    19: asString:基本墙:墙 1:1523 asValue: elementid:-1
    20: asString:基本墙: 墙 1 asValue: elementid:-1
    21: asString: asValue:标高 1 elementid:200627
    22: asString:基本墙:墙 1:2365 asValue: elementid:-1
    23: asString: asValue:否 elementid:-1
    24: asString: asValue:否 elementid:-1
    25: asString:基本墙: 墙 1 asValue: elementid:-1
    26: asString:10S48K_H51XuptsWnf03xH asValue: elementid:-1
    27: asString:墙 asValue: elementid:-1
    28: asString: asValue:是 elementid:-1
    29: asString: asValue:0 mm elementid:-1
    30: asString:基本墙: 墙 1 asValue: elementid:-1
    31: asString: asValue:-1 elementid:-1
    32: asString: asValue:基本墙:墙 1 elementid:200622
    33: asString: asValue:墙 elementid:-2000011
    34: asString: asValue:0 mm elementid:-1
    35: asString:墙 1 asValue: elementid:-1
    36: asString: asValue:11.091 m2 elementid:-1
    37: asString: asValue:墙中心线 elementid:-1
    38: asString: asValue:未连接 elementid:-1
    39: asString:2365 asValue: elementid:-1
    40: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    41: asString: asValue:是 elementid:-1
    42: asString: asValue:是 elementid:-1
    ===============
    ElementId:200662
    name:基本墙:墙 1
    type:Autodesk.Revit.DB.WallType
    0.656167979002625
    UniqueId:3bd13677-6190-4cab-a469-1e3456519fcb-00030fd6
    Parameters
    0: asString: asValue:-4000 mm elementid:-1
    1: asString:墙 asValue: elementid:-1
    2: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    3: asString: asValue:4000 mm elementid:-1
    4: asString: asValue:1.120 m3 elementid:-1
    5: asString: asValue:无 elementid:-1
    6: asString:基本墙: 墙 1 asValue: elementid:-1
    7: asString: asValue:新构造 elementid:110001
    8: asString: asValue:否 elementid:-1
    9: asString: asValue:是 elementid:-1
    10: asString:基本墙: 墙 1 asValue: elementid:-1
    11: asString:基本墙:墙 1:1523 asValue: elementid:-1
    12: asString: asValue:200622 elementid:200622
    13: asString: asValue:墙中心线 elementid:-1
    14: asString: asValue:0 mm elementid:-1
    15: asString: asValue:墙 elementid:-2000011
    16: asString: asValue:基本墙 elementid:200622
    17: asString:墙 1 asValue: elementid:-1
    18: asString:基本墙: 墙 1 asValue: elementid:-1
    19: asString:10S48K_H51XuptsWnf03uD asValue: elementid:-1
    20: asString: asValue:否 elementid:-1
    21: asString: asValue:基本墙:墙 1 elementid:200622
    22: asString: asValue:1300 mm elementid:-1
    23: asString: asValue:-1 elementid:-1
    24: asString: asValue:未连接 elementid:-1
    25: asString: asValue:是 elementid:-1
    26: asString: asValue:5.600 m2 elementid:-1
    27: asString: asValue:基本墙: 基本墙:墙 1 elementid:200622
    28: asString: asValue:否 elementid:-1
    29: asString: asValue:是 elementid:-1
    30: asString: asValue:标高 1 elementid:200627
    31: asString: asValue:墙 elementid:-2000011
    32: asString: asValue:承重 elementid:-1
    33: asString:基本墙: 墙 1 asValue: elementid:-1
    34: asString: asValue:否 elementid:-1
    35: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    36: asString:基本墙:墙 1:2529 asValue: elementid:-1
    37: asString:2529 asValue: elementid:-1
    38: asString: asValue:是 elementid:-1
    39: asString: asValue:0 mm elementid:-1
    40: asString: asValue:钢筋保护层 1 <25 mm> elementid:124652
    41: asString: asValue:是 elementid:-1
    42: asString: asValue:0 mm elementid:-1
    ===============

      

  • 相关阅读:
    7.3---直线是否相交(CC150)
    7.2---蚂蚁相遇问题(CC150)
    5.6---交换整数的奇数位和偶数位(CC150)
    5.5---整数A转成整数B(CC150)
    5.2---小数的二进制表示(CC150)
    5.1---二进制数插入(CC150)
    4.6---找二叉树中序后继(CC150)
    4.5---判断是否是二叉排序树BST(CC150)
    4.4---建立二叉树的链表
    linux中查看java进程
  • 原文地址:https://www.cnblogs.com/baby123/p/13931710.html
Copyright © 2020-2023  润新知