I would like to use both the xCAD.net and SolidDNA framework in an add-in that I writing in C#.
The questions are: can I do it? And, most importantly, should I do it?
Why would I would to do it?
From xCAD.net I like that @artem has created a lot of video tutorial that help a beginner like me to get started; I like that the framework handles the add-in registration; then I also appreciate the fact that it exposes the native SolidWorks API (even PDM and Document Manager) and there is no need to dispose the SolidWorks objects.
From SolidDNA I like that the code is well documented, there are the YouTube videos from Luke, and has some very powerful and yet simple method for most the operations I need.
I’m aware of this comparison: SW-API Custom-Frameworks: xCAD vs SolidDNA, but I would like the best of both world.
I started testing it and seems to work without issues but I just started and I’m not very knowledgeable in C#.
I created an add-in with xCAD, then I connect\disconnect SolidDNA in the OnConnect
and OnDisconnect
method:
using CADBooster.SolidDna;
using CodeWorksLibrary.Properties;
using SolidWorks.Interop.sldworks;
using System.ComponentModel;
using Xarial.XCad.Base.Attributes;
using Xarial.XCad.UI.Commands;
using Xarial.XCad.Extensions.Attributes;
using CodeWorksLibrary.Macros.Files;
namespace CodeWorksLibrary
{
[System.Runtime.InteropServices.ComVisible(true)]
public class AddIn : Xarial.XCad.SolidWorks.SwAddInEx
{
#region Enumeration
/// <summary>
/// Enumeration that contains the commands to be added to SolidWorks
/// </summary>
[Title("CodeWorks")]
[Description("A collection of macros for SolidWorks")]
public enum CwCommands_e
{
[Title("Set author")]
[Description("Write the component author in the custom properties")]
[Icon(typeof(Resources), nameof(Resources.SetAuthor))]
SetAuthorE,
[Title("Export file")]
[Description("Export the current file in different formats")]
ExportFileE
}
#endregion
#region Public properties
public static SldWorks swApp { get; set; }
#endregion
/// <summary>
/// Handle the connection to SolidWorks
/// </summary>
public override void OnConnect()
{
AddInIntegration.ConnectToActiveSolidWorks(this.Application.Sw.RevisionNumber(), this.AddInId);
CommandManager.AddCommandGroup<CwCommands_e>().CommandClick += OnCommandClick;
swApp = (SldWorks)this.Application.Sw;
}
public override void OnDisconnect()
{
AddInIntegration.TearDown();
}
private void OnCommandClick(CwCommands_e spec)
{
switch (spec)
{
case CwCommands_e.SetAuthorE:
SetAuthorMacro.SetAuthor();
break;
case CwCommands_e.ExportFileE:
ExportFileMacro.ExportFile();
break;
}
}
}
}
The use of xCAD allows to write a method like this:
/// <summary>
/// Set the value of a custom property
/// </summary>
/// <param name="swModel">The model object that needs the property to be changed</param>
/// <param name="propertyName">The name of the property to be changed</param>
/// <param name="prpValue">The value of the property to be changed</param>
public void SetCustomProperty(ModelDoc2 swModel, string propertyName, string prpValue)
{
CustomPropertyManager swCustPrpMgr = swModel.Extension.get_CustomPropertyManager("");
swCustPrpMgr.Add3(propertyName, (int)swCustomInfoType_e.swCustomInfoText, prpValue, (int)swCustomPropertyAddOption_e.swCustomPropertyReplaceValue);
}
and in the same project with SolidDNA:
/// <summary>
/// Save the active drawing as DWG in a sub-folder "\DWG\" of GlobalConfig.ExportPath
/// </summary>
/// <param name="model">The Model object of the drawing</param>
public static void ExportDrawingAsDwg(Model model)
{
// Check if the model is a drawing
if (model?.IsDrawing != true)
{
Application.ShowMessageBox("Export to PDF allowed only for drawings", SolidWorksMessageBoxIcon.Stop);
return;
}
// Get the full path for the export
var path = ComposeOutFileName("DWG");
// Check is the export full path is empty or null
if (string.IsNullOrEmpty(path))
{
Application.ShowMessageBox("The export path isn't valid", SolidWorksMessageBoxIcon.Stop);
return;
}
// Save new file
var result = model.SaveAs(
path,
options: SaveAsOptions.Silent | SaveAsOptions.Copy | SaveAsOptions.UpdateInactiveViews,
pdfExportData: null);
if (!result.Successful)
Application.ShowMessageBox("Failed to export drawing as PDF", SolidWorksMessageBoxIcon.Stop);
}
The code is available on Github.
Sorry for the lengthy post, and thanks in advance to whoever will respond.