XCAD.NET
XCAD.NET
Framework for .NET (C# and VB.NET) to create modern application for CAD systems (SOLIDWORKS, SOLIDWORKS Document Manager, Autodesk Inventor, etc.)
Get technical support Connect to xCAD community on Discord server Explore code and application examples xCAD.NET Templates for Visual Studio and VS Code Access source code

Defining commands buttons in SOLIDWORKS toolbar using xCAD framework


Defining Commands

xCAD framework allows defining the commands in the enumeration (enum). In this case the enumeration value become the id of the corresponding command.

public enum CommandsA_e
{
    CommandA1,
    CommandA2
}

Commands Decoration

Commands can be decorated with the additional attributes to define look and feel of the command.

Title

User friendly title can be defined using the TitleAttribute. Alternatively, any attribute class which inherits DisplayNameAttribute is supported as a title.

Description

Description is a text displayed in the SOLIDWORKS command bar when user hovers the mouse over the command. Description can be defined using the DescriptionAttribute

Icon

Icon can be set using the IconAttribute.

xCAD framework will scale the icon appropriately to match the version of SOLIDWORKS. For example for SOLIDWORKS 2016 onwards, 6 icons will be created to support high resolution, for older SOLIDWORKS, 2 icons will be created (large and small).

Transparency is supported. xCAD framework will automatically assign the required transparency key for compatibility with SOLIDWORKS.

Icons can be referenced from any static class. Usually this should be a resource class. It is required to specify the type of the resource class as first parameter, and the resource names as additional parameters. Use nameof keyword to load the resource name to avoid usage of 'magic' strings.

using System.ComponentModel;
using Xarial.XCad.Base.Attributes;
using Xarial.XCad.Documentation.Properties;

[Title(typeof(Resources), nameof(Resources.ToolbarTitle))]
[Description("Command Group Title")]
[Icon(typeof(Resources), nameof(Resources.commands))]
public enum CommandsB_e
{
    [Title("First Command")]
    [Description("Hint text for first command")]
    [Icon(typeof(Resources), nameof(Resources.command1))]
    CommandB1,

    [Title("Second Command")]
    [Description("Hint text for second command")]
    [Icon(typeof(Resources), nameof(Resources.command2))]
    CommandB2,

    [Title("Third Command")]
    [Description("Hint text for third command")]
    [Icon(typeof(Resources), nameof(Resources.command3))]
    CommandB3
}

Refer Command Group Icons Example for the demonstration of hosting different icons with various sizes in SOLIDWORKS toolbar. menu and command tab box.

Commands Scope

Each command can be assigned with the operation scope (i.e. the environment where this command can be executed, e.g. Part, Assembly etc.). Scope can be assigned with CommandItemInfoAttribute attribute by specifying the values in suppWorkspaces parameter of the attribute's constructor. The WorkspaceTypes_e is a flag enumeration, so it is possible to combine the workspaces.

Framework will automatically disable/enable the commands based on the active environment as per the specified scope. For additional logic for assigning the state visit Custom Enable Command State article.

using Xarial.XCad.UI.Commands.Attributes;
using Xarial.XCad.UI.Commands.Enums;

public enum CommandsD_e
{
    [CommandItemInfo(WorkspaceTypes_e.Part)]
    CommandD1,

    [CommandItemInfo(WorkspaceTypes_e.Part | WorkspaceTypes_e.Assembly)]
    CommandD2
}

User Assigned Command Group IDs

CommandGroupInfoAttribute allows to assign the static command id to the group. This should be applied to the enumerator definition. If this attribute is not used SwEx framework will assign the ids automatically.

using Xarial.XCad.Geometry;
using Xarial.XCad.UI.Commands.Attributes;

[ContextMenuCommandGroupInfo(2, typeof(IXFace))]
public enum CommandsE_e
{
    CommandE1,
    CommandE2
}


Powered by Docify