一、前言

       BIM:Building Information Modeling 建筑信息模型,就是将建筑的相关信息附着于模型中,以管理该建筑在设计、算量、施工、运维全生命周期的情况。创建模型的主要主流软件有Autodesk(欧特克)的Revit、Bentley的microstation、达索的CATIA(曾被我国在80、90年代用于制造战斗机,比较高端)。我所在的企业是从事BIM软件的,隶属于一家上市企业。

我在两个部门工作过。第一个部门是算量软件部门,专门制作基于Revit的算量插件(桌面端)。第二个部门是BIM数据平台部门,专门为大型企业制作BIM施工管控平台(Web及移动App端)。刚研究生毕业即进入公司,在两年整的工作中主要负责WPF(DevExpress)、识别算法、网络通信编程(Remoting、WebService、WCF、Socket)以及Web开发。

架构的改造也是我工作的一部分,站在公司老司机的肩膀上还是很爽的。那么,我就从桌面端软件开始吧!

二、架构简图

制作Revit插件必须要引用2个dll:RevitAPI和RevitAPIUI,每年根据Autodesk发布Revit产品进行迭代,支持C#、Managed C++及VB编程。

该解决方案共含有15个C#项目,红色模块都需要引用Revit的dll,灰色部分不用引用。编译完之后,在C:\ProgramData\Autodesk\Revit\Addins\201X添加相应的.addin文件,并指向Product.App的位置即可,然后运行Revit的软件即可使用插件。

<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Application">
<Name>Product</Name>
<Assembly>X:Path\Product.App.dll</Assembly>
<ClientId>a21827a2--445b-a625-8d9f4a52f218</ClientId>
<FullClassName>Product.App.ExternalApplication</FullClassName>
<VendorId>BIMproduct</VendorId>
<VendorDescription>BIMproduct www.product.com</VendorDescription>
</AddIn>
</RevitAddIns> 

三、架构解读

1. App201X

该项目是Revit插件的入口,主要由ExternalApplicationExternalCommand组成。需要引用其它的项目。该项目的ExternalApplication需要实现一个Revit接口:IExternalApplication。

ExternalApplication在OnStartup函数中可以做一些初始化工作,比如初始化依赖反转容器、创建Ribbon按钮、初始化载入一些第三方dll、产品的授权检测等等。

namespace Product.App
{
public class ExternalApplication:IExternalApplication
{ public Result OnStartup(UIControlledApplication application)
{
try
{
#if !REVIT2014
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("zh-CN");
#endif
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN"); // Refer to: https://stackoverflow.com/questions/4041197/how-to-set-and-change-the-culture-in-wpf
// 使用“System.Windows.Documents.TextElement”以和QS或其他插件中的“FrameworkElement”区别
FrameworkElement.LanguageProperty.OverrideMetadata(
typeof(System.Windows.Documents.TextElement),
new FrameworkPropertyMetadata(
XmlLanguage.GetLanguage(CultureInfo.CurrentCulture.IetfLanguageTag)));
}
catch (Exception ex)
{
if (ex is System.ArgumentException)
{
// 有可能“System.Windows.Documents.TextElement”已被注册过. Do nothing
}
else
{
return Result.Succeeded;
}
} try
{
//Initialize
}
catch (Exception ex)
{
return Result.Succeeded;
}
return Result.Succeeded;
} public Result OnShutdown(UIControlledApplication application)
{
try
{
//shoutdown
}
catch (Exception)
{
// do nothing...
}
return Result.Succeeded;
}
}
}

ExternalCommand主要负责插件上的按钮,以“关于”按钮为例:

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
public class AboutCommand : ExternalCommandBase
{
protected override Result RunImpls(ExternalCommandData commandData, ref string message, ElementSet elements)
{
AboutWnd aboutWnd = new AboutWnd();
// 出现异常时,关闭窗体
try
{
aboutWnd.ShowDialog();
}
finally
{
aboutWnd.Close();
}
return Result.Succeeded;
} public override bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
{
return true;
} public override bool IsProductLicenseValid()
{
return true;
}
}

可以看到该命令并没有直接实现IExternalCommand, IExternalCommandAvailability两个接口。而是再封装一个抽象基类,让抽象基类实现Revit的这俩个接口。然后相关的Command再继承这个抽象基类。为什么这么做是因为抽象基类中还可以做一些检查授权等等的事情。

namespace Product.App
{
public abstract class ExternalCommandBase:IExternalCommand, IExternalCommandAvailability
{
protected static bool _allowMultiRun = false;
protected static bool _isRunning = false; /// <summary>
///
/// </summary>
/// <param name="commandData"></param>
/// <param name="message"></param>
/// <param name="elements"></param>
/// <returns></returns>
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
if(!_allowMultiRun && _isRunning)
{
return Result.Cancelled;
} if (!IsProductLicenseValid())
{
Container.Resolve<IMessageBox>().ShowInfo("授权失败,无法使用!");
return Result.Cancelled;
} _isRunning = true; try
{
string msg = $"run command '{this.GetType().Name}'";
Container.Resolve<ILog>().Info(msg);
var res = RunImpls(commandData, ref message, elements);
_isRunning = false;
return res;
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
_isRunning = false;
// generally, it's select cancel exception
return Result.Cancelled;
}
catch (Exception ex)
{
_isRunning = false;
if (ex.Message== "The active view is non-graphical and does not support capture of the focus for pick operations.")
{
Container.Resolve<IMessageBox>().ShowWarning("当前焦点不在视图区域,请将焦点停留在某个视图中。");
return Result.Failed;
}
string errorMsg = $"exception in command '{this.GetType().Name}': {ex}";
Container.Resolve<ILog>().Error(errorMsg, ex);
message = ex.Message;
return Result.Failed;
}
} protected string GetProductName()
{
return Container.Resolve<IApplication>().ProductLicenseName;
} /// <summary>
/// Implements this method to provide implementation for Execute
/// </summary>
/// <param name="commandData"></param>
/// <param name="message"></param>
/// <param name="elements"></param>
/// <returns></returns>
protected abstract Result RunImpls(ExternalCommandData commandData, ref string message, ElementSet elements); public virtual bool IsCommandAvailable(UIApplication applicationData, CategorySet selectedCategories)
{
return false;
} public virtual bool IsProductLicenseValid()
{
#if DEBUG
return true;
#else
return LicenseService.IsRegistered(GetProductName(), Container.Resolve<IApplication>().LicenseServerURL);
#endif
}
}
}

2. Common、Common.RevitExt201X

Common项目主要定义基本的数据接口和定义依赖反转容器,注意该项目中并没有引用Revit的dll,说明其中的数据定义是不依赖于Revit的。

与Revit有关的数据定义都放在Common.RevitExt201X,比如自定义的DockablePanel接口(继承IDockablePaneProvider)。

3. UI、UI.Common、UI.RevitExt201X

UI项目主要用于制作点击命令后产生的界面UI,该项目采用MVVM框架进行界面实现,文件夹梳理可如下:

UI.Common主要是用于制作可复用的自定义控件、进度条、常用Converter;

UI.RevitExt201X主要是用于控制DockablePanel和控制Revit构件显示效果,其依赖于Revit。

4. Resource、Unity4Net、Report201X

Resource项目用于多语言版本;

Unity4Net项目用于对Windows的一些操作进行封装;

Report201X项目用于输出报表。报表因为其有一定的业务逻辑,并不是一个单纯的UI界面汇总,所以单独做成一个project会比较好。

5. RevitAPIUtil201X

该项目用于对rvt中的构件进行直接的API操作,但不含有任何的业务逻辑。比如获取构件的若干面,获取长度等等。

6. Core201X

该项目用于核心业务逻辑。当用户在UI界面中点击某按钮,其执行的核心在该项目中。

7. Foundation、Foundation.RevitExt201X

Foundation项目用于进行基本的常数定义,全局定义,授权检查配置等等。

Foundation.RevitExt201XApp201X息息相关,App201X中的ExternalApplication的OnStartup中的相关初始化逻辑比较多,将核心的部分抽出放在该项目中。

8. DBManager

该项目用于对数据库进行操作,Revit插件如果只用自带的revit数据缓存是不能满足现实需求的,需要使用小型数据库,推荐使用SqlLite数据库。

9. ProjectManager

因为算量软件的第一步是工程设置,之后计算的正确性都需要正确的工程设置,所以将工程设置单独抽出而不是放在Core201X中。

最新文章

  1. Nova PhoneGap框架 第十章 开发环境
  2. kali 2.0 U盘安装错误Your installation cd-rom couldn&#39;t be mounted
  3. [android]判断位置服务是否打开
  4. [深入浅出WP8.1(Runtime)]文本框(TextBox)
  5. [ActionScript 3.0] AS3.0将图像的Alpha通道转换为黑白图像(复制通道方式)
  6. 【整理】更改MSSQL默认字符集
  7. .net IL 指令速查
  8. webform 简单的服务器控件。
  9. VC青睐服装订制网站 市场规模超3000亿美元 - 找VC - 创业邦
  10. WebDriver使用IE浏览器
  11. Kettle中忽略错误行继续执行
  12. Kotlin入门(11)江湖绝技之特殊函数
  13. nested exception is com.svorx.core.dao.PersistenceException
  14. python环境搭建和开发工具的配置【转】
  15. Unity使用OpenGL绘制经纬线圈
  16. 【Coursera】Fourth Week(1)
  17. Linux 下LNMP环境搭建_【all】
  18. GitLab使用总结[转]
  19. 微信小程序排行榜
  20. Android中常见的内存泄漏

热门文章

  1. python_ 基本语法
  2. 你想知道吗?come
  3. 我对CSS的认识
  4. IT名词概括与简单了解
  5. vue添加class类名
  6. python selenium-webdriver 下拉菜单处理 (九)
  7. CWMP开源代码研究6——libcwmp动态库开发
  8. 多线程shell脚本检测主机存活
  9. .NET MVC 表主外键关系 JSON 无限循环 方案二(推荐)
  10. Ntrip协议简介(转)