vs2015对revit2018二次开发之不启动Revit,读取rvt文件里墙及其属性

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

2.在项目中引入

RevitNet.dll,Revit.dll

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)
        {
            try
            {
                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:/项目2-2018.rvt";
                Document doc = _application.OpenDocumentFile(_modelPath);
                Console.WriteLine("RVT文件已经打开");                Console.WriteLine("   start   ");
                ReadRvt(doc);
                Console.WriteLine("   ok   ");
                doc.Close(false);
                _product?.Exit();
                Console.ReadKey(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        static void ReadRvt(Document doc)
        {
            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("   width:{0}", wall.Width.ToString());
                Console.WriteLine("   UniqueId:{0}", wall.UniqueId.ToString());
                Console.WriteLine("   Category:{0}", wall.Category.Name);
                Console.WriteLine("   WallType:{0}", wall.WallType.Name);
                Console.WriteLine("params ===============   ");
                ParameterSet parameters = wall.Parameters;
                Console.WriteLine("   Parameters");
                int i = 0;
                foreach (Autodesk.Revit.DB.Parameter param in parameters)
                {
                    if (param == null) continue;
                    String value = "";
                    switch (param.StorageType)
                    {
                        case StorageType.Double:
                            value = param.AsValueString();
                            break;
                        case StorageType.Integer:
                            value = param.AsDouble().ToString();
                            break;
                        case StorageType.ElementId:
                            ElementId elemId = new ElementId(param.AsElementId().IntegerValue);
                            Element elem = doc.GetElement(elemId);
                            value = elem != null ? elem.Name : "Not set";
                            break;
                        case StorageType.String:
                            value = param.AsString();
                            break;
                        case StorageType.None:
                            value = param.AsString();
                            break;
                        default:
                            break;
                    }
                    if (param.HasValue)
                    {
                        Console.Write("     name:{0}", param.Definition.Name);
                        Console.Write(" , value:{0}", value);
                        Console.Write(" , type:{0}", param.StorageType.ToString());
                        Console.WriteLine(" ");
                    }
                    
                }
            }
        }
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文件已经打开
start
Walls:
ElementId:467418
name:建筑外墙 - 砌块墙200mm
type:Autodesk.Revit.DB.WallType
width:0.761154855643045
UniqueId:c078a616-a3ac-4c7a-a695-e1c8a369a896-000721da
Category:墙
WallType:建筑外墙 - 砌块墙200mm
params ===============
Parameters
name:结构用途 , value:0 , type:Integer
name:顶部延伸距离 , value:0 , type:Double
name:类型 ID , value:建筑外墙 - 砌块墙200mm , type:ElementId
name:类别 , value:Not set , type:ElementId
name:启用分析模型 , value:0 , type:Integer
name:类别 , value:Not set , type:ElementId
name:面积 , value:495.253 m2 , type:Double
name:与体量相关 , value:0 , type:Integer
name:长度 , value:41000 , type:Double
name:设计选项 , value:Not set , type:ElementId
name:底部约束 , value:F1 , type:ElementId
name:顶部偏移 , value:0 , type:Double
name:拆除的阶段 , value:Not set , type:ElementId
name:类型 , value:建筑外墙 - 砌块墙200mm , type:ElementId
name:底部偏移 , value:0 , type:Double
name:房间边界 , value:0 , type:Integer
name:创建的阶段 , value:新构造 , type:ElementId
name:结构 , value:0 , type:Integer
name:已附着顶部 , value:0 , type:Integer
name:族 , value:建筑外墙 - 砌块墙200mm , type:ElementId
name:顶部约束 , value:Not set , type:ElementId
name:定位线 , value:0 , type:Integer
name:族与类型 , value:建筑外墙 - 砌块墙200mm , type:ElementId
name:无连接高度 , value:12079 , type:Double
name:体积 , value:114.899 m3 , type:Double
name:底部延伸距离 , value:0 , type:Double
name:已附着底部 , value:0 , type:Integer

 

posted @ 2020-12-27 23:02  慕尘  阅读(751)  评论(0编辑  收藏  举报