MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

xarial/xcad 深度技术分析

项目背景与痛点

SOLIDWORKS 是一款主流的 CAD 软件,广泛应用于机械设计与工程领域。然而,其内置的功能和定制化能力有限,无法满足一些开发者在特定应用场景下的需求。此时,第三方开发框架便成为了关键。

xcad 框架正是为这种情况应运而生。它不仅简化了开发者与 SOLIDWORKS 的交互过程,还提供了丰富的扩展功能,例如添加插件、独立应用程序、宏功能、属性管理界面等。xcad 解决的核心问题,在于它大幅降低了开发者对接 CAD 软件的门槛,使得原本复杂且耗时的任务变得轻松而高效。

核心技术揭秘

技术架构与设计思想

xcad 框架采用了松耦合的设计,确保了其高效性和灵活性。通过使用 AOP(面向切面编程)技术和 IoC(控制反转)容器,xcad 实现了逻辑的清晰分离,使得不同功能模块间的协作更加高效。

其核心组件包括:

  • Add-In Manager:负责集成和管理 SOLIDWORKS 插件。
  • Data Manager:处理各种数据读取和存储任务,保证插件能够进行恰当的数据操作。
  • UI Manager:用于开发或集成用户体验(GUI)功能,如属性页。

技术栈与设计模式

xcad 框架主要使用 C# 编写,支持 .NET Framework 和 .NET Core。框架内部运用了:

  • IoC 容器:管理对象生命周期,使代码更加松耦合。
  • AOP 切面编程:实现了跨模块的日志记录和异常处理功能。
数据流说明

xcad 的请求处理流程如下:

graph TD A[用户请求] --> B{"路 由 校 验"} B -->|"通过"| C[模 块 引 导] B -->|"失败"| D[报 错] C --> E[事 务 处 理] E --> F[状 态 同 步] F --> G[数 据 操 作] G --> H[站 点 回 应]

从图中可以看出,请求是由用户发起,在经过路由校验和模块引导后,执行实际的事务处理,接着进行状态同步和数据操作。最终将结果反馈给用户端。

功能亮点与差异

与其他同类产品相比,xcad 的主要优势在于其灵活性和易扩展性。内置的 IoC 容器和 AOP 机制,显著提升了开发与维护效率。相比直接通过 SOLIDWORKS 插件方式进行开发,使用 xcad 可以快速进行组件化开发,不仅简化了代码结构,还能大幅减少 BUG 出现的可能性。

应用场景与落地建议

xcad 适合于需要频繁进行 CAD 文件交互的企业和项目。设计者可以在 xcad 的基础上快速开发插件或独立应用,例如,使用 xcad 构建定制化的物料管理工具、模型导入/导出插件等。

在生产环境中,最重要的是需要确保插件间的兼容性和稳定性。因此,在实际部署前,建议进行充分的测试,验证各模块在不同环境下的兼容性和运行效果。同时,需要注意的是 xcadd 在大规模应用时的性能瓶颈,如并发请求处理、数据同步等,需要通过调优来解决。

综合评价

从总体上看,xcad 框架在设计理念和技术实现上均达到了较高的水平,极大提升了开发 CAD 应用的效率,并在灵活性和可维护性上做了大量工作。最后,它提供了一个通用而强大的基础平台,为开发者提供了更为便捷的工具以达到工作目标,可以说在 CAD 应用开发领域占据了一席之地。

其不足之处则在于功能复杂性带来的学习成本,以及在一些高性能应用场景下可能会遇到的限制。不过这些问题也可以通过社区的交流和文档学习来解决。

关于

GitHub 原始描述(英文)

Framework for developing CAD applications for SOLIDWORKS, including add-ins, stand-alone applications, macro features, property manager pages, etc.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
482
价值分
149
活跃状态
活跃

语言占比

Batchfile
C#
PowerShell
Visual Basic .NET

README

xCAD.NET: SOLIDWORKS API development made easy

Logo

NuGet version (xCAD.NET) Build status

Templates User Guide Examples Videos

xCAD.NET is a framework for building CAD agnostic applications. It allows developers to implement complex functionality with a very simple innovative approach. This brings the best user experience to the consumers of the software.

Templates

Visual Studio and Visual Studio Code templates can be installed from NuGet

> dotnet new install Xarial.XCad.Templates

SOLIDWORKS Add-in Applications

It has never been easier to create SOLIDWORKS add-ins with toolbar and menu commands.

[ComVisible(true)]
public class XCadAddIn : SwAddInEx
{
    public enum Commands_e
    {
        Command1,
        Command2
    }

    public override void OnConnect()
    {
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += OnCommandsButtonClick;
    }

    private void OnCommandsButtonClick(Commands_e cmd)
    {
        //TODO: handle the button click
    }
}

Property Manager Pages

Framework reinvents the way you work with Property Manager Pages. No need to code a complex code behind for adding the controls and handling the values. Simply define your data model and the framework will build the suitable Property Manager Page automatically and two-way bind controls to the data model.

[ComVisible(true)]
public class IntroPmpPageAddIn : SwAddInEx
{
    [ComVisible(true)]
    public class MyPMPageData : SwPropertyManagerPageHandler
    {
        public string Text { get; set; }
        public int Number { get; set; }
        public IXComponent Component { get; set; }
    }

    private enum Commands_e
    {
        ShowPmpPage
    }

    private IXPropertyPage<MyPMPageData> m_Page;
    private MyPMPageData m_Data = new MyPMPageData();

    public override void OnConnect()
    {
        m_Page = this.CreatePage<MyPMPageData>();
        m_Page.Closed += OnPageClosed;
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += ShowPmpPage;
    }

    private void ShowPmpPage(Commands_e cmd)
    {
        m_Page.Show(m_Data);
    }

    private void OnPageClosed(PageCloseReasons_e reason)
    {
        Debug.Print($"Text: {m_Data.Text}");
        Debug.Print($"Number: {m_Data.Number}");
        Debug.Print($"Selection component name: {m_Data.Component.Name}");
    }
}

Macro Features

Complex macro features became an ease with xCAD.NET

[ComVisible(true)]
public class IntroMacroFeatureAddIn : SwAddInEx 
{
    [ComVisible(true)]
    public class BoxData : SwPropertyManagerPageHandler
    {
        public double Width { get; set; }
        public double Length { get; set; }
        public double Height { get; set; }
    }

    [ComVisible(true)]
    public class BoxMacroFeature : SwMacroFeatureDefinition<BoxData, BoxData>
    {
        public override ISwBody[] CreateGeometry(ISwApplication app, ISwDocument model, ISwMacroFeature<BoxData> feat)
        {
            var data = feat.Parameters;

            var body = (ISwBody)app.MemoryGeometryBuilder.CreateSolidBox(new Point(0, 0, 0),
                new Vector(1, 0, 0), new Vector(0, 1, 0),
                data.Width, data.Length, data.Height).Bodies.First();

            return new ISwBody[] { body };
        }

    }

    public enum Commands_e
    {
        InsertMacroFeature,
    }

    public override void OnConnect()
    {
        this.CommandManager.AddCommandGroup<Commands_e>().CommandClick += OnCommandsButtonClick;
    }

    private void OnCommandsButtonClick(Commands_e cmd)
    {
        switch (cmd) 
        {
            case Commands_e.InsertMacroFeature:
                Application.Documents.Active.Features.CreateCustomFeature<BoxMacroFeature, BoxData, BoxData>();
                break;
        }
    }
}

SOLIDWORKS And Document Manager API

xCAD.NET allows to write the same code targeting different CAD implementation in a completely agnostic way. Example below demonstrates how to perform opening of assembly, traversing components recursively and closing the assembly via SOLIDWORKS API and SOLIDWORKS Document Manager API using the same code base.

static void Main(string[] args)
{
    var assmFilePath = @"C:\sample-assembly.sldasm";

    //print assembly components using SOLIDWORKS API
    var swApp = SwApplicationFactory.Create(SwVersion_e.Sw2022, ApplicationState_e.Silent);
    PrintAssemblyComponents(swApp, assmFilePath);

    //print assembly components using SOLIDWORKS Document Manager API
    var swDmApp = SwDmApplicationFactory.Create("[Document Manager Lincese Key]");
    PrintAssemblyComponents(swDmApp, assmFilePath);
}

//CAD-agnostic function to open assembly, print all components and close assembly
private static void PrintAssemblyComponents(IXApplication app, string filePath) 
{
    using (var assm = app.Documents.Open(filePath, DocumentState_e.ReadOnly))
    {
        IterateComponentsRecursively(((IXAssembly)assm).Configurations.Active.Components, 0);
    }
}

private static void IterateComponentsRecursively(IXComponentRepository compsRepo, int level) 
{
    foreach (var comp in compsRepo)
    {
        Console.WriteLine(Enumerable.Repeat("  ", level) + comp.Name);

        IterateComponentsRecursively(comp.Children, level + 1);
    }
}

Target Frameworks

xCAD.NET is compatible with multiple target frameworks: .NET Framework 4.6.1, .NET Core 3.1, .NET 6.0, .NET 7.0 and number of additional computed target frameworks (e.g. .NET Framework 4.8)

When building the SOLIDWORKS add-ins see the information below

.NET Framework

  • Run Visual Studio as an Administrator
  • Install Xarial.XCad.SolidWorks package from the nuget and create add-in class as shown above
  • Build the solution. Add-in will be automatically registered. Clean the solution to unregister the add-in.
  • Set the Embed Interop option to True for all SOLIDWORKS type libraries (e.g. SolidWorks.Interop.SldWorks.tlb, SolidWorks.Interop.SwConst.tlb, SolidWorks.Interop.SwPublished.tlb). Note this might not be required as nuget will set this flag automatically.

.NET Core/.NET 6/.NET 7

  • Run Visual Studio as an Administrator
  • Install Xarial.XCad.SolidWorks package from the nuget and create add-in class as shown above
  • Add the following property into the project file (*.csproj or *.vbproj)
<PropertyGroup>
    <EnableComHosting>true</EnableComHosting>
</PropertyGroup>
  • Build the solution. Add-in will be automatically registered. Clean the solution to unregister the add-in.

.NET Core Only

Automatic registration does not work in .NET Core and it needs to be called manually by adding the following code into the add-in (this is not required for .NET6)

[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
    SwAddInEx.RegisterFunction(t);
}

[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
    SwAddInEx.UnregisterFunction(t);
}

Watch the video demonstrations YouTube playlist of xCAD in action.

Visit User Guide page and start exploring the framework.

Unit Tests

Solution contains unit and integration tests

To execute integration tests

  • Download the Test Data
  • Unzip into the folder
  • Create an environment variable XCAD_TEST_DATA and set its value to the path of the folder above
  • To test SOLIDWORKS Document Manager, add an environment variable SW_DM_KEY and set its value to your Document Manager Key
  • Run tests

评论

暂无评论