栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

Revit二次开发——不启动Revit,做rvt文件数据导出

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Revit二次开发——不启动Revit,做rvt文件数据导出

Revit二次开发——不启动Revit,做rvt文件数据导出
  • Node.js部分
    • 调用C#端供外部调用的dll
  • C#部分
    • 调用RevitNet.dll,做数据导出exe
    • 做外部调用dll
  • 总结

Node.js部分 调用C#端供外部调用的dll

由于.Net 的WebSevice在引用RevitNet.dll是出现环境不匹配的问题,这里我们的web服务端是用node.js来做的,通过引用edge.js模块来实现在node.js中调用C#的dll,代码如下:

exportModel: async function(file, params){
        try {
            let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExternalReference/bin/Debug/ExternalReference.dll');

            //let dllPath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModelControl/ExportModelControl/bin/Debug/ExportModelControl.dll');

            //let exePath = path.join(path.resolve(__dirname, '..', '..'), 'assets', 'ExportModel/ExportModel/bin/Debug/ExportModel.exe')

           

            
            let modelSavePath = sails.config.custom.model_sourcefilesPath;

            if (!fs.existsSync(modelSavePath)) await fs.mkdirSync(modelSavePath);

            let thisUpload = await fileHandleUtil.uploadSync(file, { dirname: modelSavePath });

            let modelPath = path.join(sails.config.custom.model_sourcefilesPath, path.basename(thisUpload[0].fd));

            var invokedll = null;

            // console.log("exe路径:" + exePath);

            console.log("模型源文件路径:" + modelPath);

            //console.log("导出文件路径:" + exePath);

    
            if (fs.existsSync(dllPath)) {
                invokeDll = edge.func({
                    assemblyFile: dllPath,
                    typeName: 'ExternalReference.Program',
                    methodName: 'Export'
                })
            }else{
                assertUtil(null, 'dll path does exist!');
            }
    
            var result = null;
            if(invokedll!= null){
                result = await invokedll(modelPath, true);
            }else{
                assertUtil(null, 'invokedll miss!');
            };

            result = result.replace(/rn/g,"");

            var arr = result.split(',');
            
            return {
                modelId: arr[0],
                modelPath: arr[1]
            };
        } catch (err) {
            throw err;
        }
    }
C#部分 调用RevitNet.dll,做数据导出exe
namespace ExportModel
{
    public class Startup
    {
        static readonly string[] searchs = RevitProductUtility.GetAllInstalledRevitProducts().Select(x => x.InstallLocation).ToArray();

        static Startup()
        {

            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 = @"H:/Models/TestHouse.rvt";

                string _modelPath = args[0];

                Document _doc = _application.OpenDocumentFile(_modelPath);

                List views = new List(from View3D v in new FilteredElementCollector(_doc).OfClass(typeof(View3D))
                                                      where v.CanBePrinted && !v.IsTemplate
                                                      select v);

                Export export = new Export(_doc, views, "", views[0]);

                UploadModel upload = new UploadModel(export.modelPath);

                string result = upload.modelId + "," + upload.modelPath;

                Console.WriteLine(result);

                Environment.Exit(0);
           
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }          
        }


        /// 
        /// 向系统中添加环境变量
        /// 
        /// 
        static void AddEnvironmentPaths(params string[] paths)
        {
            try
            {
                var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };

                var newPath = string.Join(System.IO.Path.PathSeparator.ToString(), path.Concat(paths));

                Environment.SetEnvironmentVariable("PATH", newPath);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }


        }

        /// 
        /// 重新加载dll
        /// 
        /// 
        /// 
        /// 
        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)
            {
                MessageBox.Show(ex.ToString());
            }
            return args.RequestingAssembly;

        }

    }
}
做外部调用dll
namespace ExternalReference
{
    public class Program
    {
        static Program()
        {

        }
        
        public async Task Export(string modelPath)
        {
            Process process = new Process();

            string path = System.IO.Directory.GetCurrentDirectory();

            string exePath = Path.Combine(path, @"assetsExportModelExportModelbinDebugExportModel.exe");

            var result =  CreateProcess(exePath, modelPath, process);

            return result;

        }

        public string CreateProcess(string exePath, string modelPath, Process process)
        {
            string msg = "";
            if(File.Exists(exePath))
            {
                process.StartInfo.FileName = exePath;

                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardInput = true;
                process.StartInfo.RedirectStandardOutput = true;
                process.StartInfo.RedirectStandardError = true;

                process.StartInfo.Arguments = modelPath;
                //MessageBox.Show("exe文件路径:" + exePath);

                //MessageBox.Show("模型文件路径:" + modelPath);

                process.Start();

                msg = process.StandardOutput.ReadToEnd();

                process.WaitForExit();

                process.Close();
            }
            return msg;
        }
    }
}
 
总结 

我尝试过把导出方法封装成dll,直接通过node.js或.net WebServer在外部调用。这样做Document _doc = _application.OpenDocumentFile(_modelPath);无法获取到doc,从而无法完成导出。后来只能尝把导出方法封装成一个exe,再通过一个中间的dll去调用exe,获取到了doc。

如果大家有更好的思路或解决方案,欢迎交流!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号