CAD+ Toolset extensibility via .NET (CAD++)
CAD+ can be extended using C# and VB.NET via CAD++ framework. It is possible to create custom modules and access existing modules for the automation.
Extension is possible by accessing the APIs of other modules or replacing the services in the Dependency Injection containers
Creating Module Project
In order to register custom module
- Create new type library
- Add reference to the Xarial.CadPlusPlus nuget package
- Create public class implementing IModule
- Decorate the class with ModuleAttribute attribute. It is possible to customize filtering for the module (e.g. what host and application it supports)
- Set the target framework to NET Framework v4.7.2
- Compile the dll with .Module.dll suffix into the %localappdata%\Xarial\CADPlusToolset\Plus\[Module Name] folder
Example of *.csproj or *.vbproj files
<PropertyGroup> <ModuleName>MyModuleName</ModuleName> <TargetFramework>net472</TargetFramework> <RootNamespace>Xarial.CadPlus.Plus.Samples</RootNamespace> <OutputPath>$(LocalAppData)\Xarial\CADPlusToolset\Plus\$(MSBuildProjectName)</OutputPath> <AssemblyName>$(MSBuildProjectName).Module</AssemblyName> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> </PropertyGroup>
//Custom logger public class MyLogger : IXLogger { public void Log(string msg, LoggerMessageSeverity_e severity = LoggerMessageSeverity_e.Information) { //TODO: implement logging } } //Module is evailable in CAD Extension host (e.g. SOLIDWORKS add-in) [Module(typeof(IHostCadExtension))] public class MyModule : IModule { //Method is called when module is loaded and host is initiated public void Init(IHost host) { host.ConfigureServices += OnConfigureServices; host.Initialized += OnHostInitialized; } //Configure custom services or override existing private void OnConfigureServices(IContainerBuilder builder) { //overwriting the default logger with custom one builder.Register<IXLogger, MyLogger>(RegistrationConflictResolveStrategy_e.Replace); } //Called when host and all its modules initialized private void OnHostInitialized(IApplication app, IServiceProvider svcProvider, IModuleCollection modules) { //accessing APIs of modules var toolbarPlus = modules.Get<IToolbarModule>(); var propertiesPlus = modules.Get<IPropertiesModule>(); var qrCodePlus = modules.Get<IDrawingQrCodeModule>(); var flatPatternPlus = modules.Get<IDrawingFlatPatternExportModule>(); var bomPlus = modules.Get<IBomModule>(); //accessing services var excelWriter = svcProvider.GetService<IExcelWriter>(); var msgSvc = svcProvider.GetService<IMessageService>(); var logger = svcProvider.GetService<IXLogger>(); } //Called when module is unloading public void Dispose() { } }
Find module implementation examples here