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

读取rvt文件里墙及其材质

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:/test.rvt";
                Document doc = _application.OpenDocumentFile(_modelPath);
                Console.WriteLine("RVT文件已经打开");
                //
                Console.WriteLine("   start   ");
                /*
                using (Transaction trans = new Transaction(doc, "Export to IFC file"))
                {
                    trans.Start();
                    ExportToIFC(doc);
                    trans.Commit();
                }
                */

                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("materials ===============   ");
                ICollection<ElementId> ids = wall.GetMaterialIds(false);
                foreach (ElementId id in ids)
                {
                    Element te = doc.GetElement(id);
                    Console.WriteLine("         id:{0}", te.Id.IntegerValue);
                    Console.WriteLine("         name:{0}", te.Name);
                    Material m = (Material)te;
                    Console.WriteLine("         color: R "+m.Color.Red+" , G "+m.Color.Green+", B "+m.Color.Blue);
                    Console.WriteLine("         CutPatternId: " + m.CutPatternId);
                    if (m.CutPatternId.IntegerValue != -1)
                    {
                        Element elem1 = doc.GetElement(m.CutPatternId);
                        Console.WriteLine("         CutPattern name: " + elem1.Name);
                    }
                    
                    Console.WriteLine("         CutPatternColor: R " + m.CutPatternColor.Red + " , G " + m.CutPatternColor.Green + ", B " + m.CutPatternColor.Blue);
                    Console.WriteLine("         SurfacePatternId: " + m.SurfacePatternId);
                    if (m.SurfacePatternId.IntegerValue != -1)
                    {
                        Element elem2 = doc.GetElement(m.SurfacePatternId);
                        Console.WriteLine("         SurfacePattern name: " + elem2.Name);
                    }
                    Console.WriteLine("         SurfacePatternColor: R " + m.SurfacePatternColor.Red + " , G " + m.SurfacePatternColor.Green + ", B " + m.SurfacePatternColor.Blue);
                    Console.WriteLine("         Shininess:{0}", m.Shininess);
                    Console.WriteLine("         Smoothness:{0}", m.Smoothness);
                    Console.WriteLine("         Transparency:{0}", m.Transparency);
                    Console.WriteLine("         Category:{0}", te.Category.Name);
                    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
materials ===============
id:864
name:屋顶材料 - 板岩
color: R 104 , G 104, B 103
CutPatternId: -1
CutPatternColor: R 0 , G 0, B 0
SurfacePatternId: -1
SurfacePatternColor: R 0 , G 0, B 0
Shininess:128
Smoothness:50
Transparency:0
Category:材质

id:88453
name:涂层 - 内部 - 涂料
color: R 249 , G 249, B 249
CutPatternId: -1
CutPatternColor: R 0 , G 0, B 0
SurfacePatternId: -1
SurfacePatternColor: R 0 , G 0, B 0
Shininess:128
Smoothness:50
Transparency:0
Category:材质

id:323902
name:蒸压粉煤灰加气混凝土砌块
color: R 127 , G 127, B 127
CutPatternId: 88615
CutPattern name: 加气混凝土0.05
CutPatternColor: R 0 , G 0, B 0
SurfacePatternId: -1
SurfacePatternColor: R 0 , G 0, B 0
Shininess:64
Smoothness:50
Transparency:0
Category:材质

ok

 

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