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

Managing SOLIDWORKS macro feature using xCAD.NET framework


xCAD provides utilities for simplified development of SOLIDWORKS macro features based on the data model.

What is macro feature?

Macro feature is a custom element which could be added into Feature Manager Design tree using SOLIDWORKS API. This element will behave in exactly the same way as any other standard feature (e.g. Boss-Extrude, Move-Copy Body, Mate, etc.)

Macro feature supports parametric nature of SOLIDWORKS and could regenerate if any of the parents changed.

Macro feature provides 3 main handlers

  • Regeneration - called when feature is rebuilt (either as a result of model force rebuild operation or as a result of the updated of any of the dependencies). Macro feature can create new body or bodies or be only a metadata element.
  • Edit - called when feature definition is requested to be edited by the user
  • State update - called every time state is updated (i.e. feature is selected, refreshed etc.)

Macro feature in the feature manager tree
Macro feature in the feature manager tree

Macro feature can store additional metadata parameters (including dimensions and selection references).

xCAD allows to insert, edit, preview, generate macro feature within the same definition.

Overview

Framework provides 3 main macro feature definition abstract classes which reside in the Xarial.XCad.SolidWorks.Features.CustomFeature namespace to be inherited in order to register new macro feature.

  • SwMacroFeatureDefinition - simple macro feature. Macro feature doesn't require any parameters and will perform a a simle operation
  • SwMacroFeatureDefinition parameters driven macro feature. All of the required inputs can be defined within the TParams structure (data model). [Macro feature data](\data) includes
    • Field Values (Named parameters)
    • Dimensions
    • Selections
    • Edit Bodies
  • SwMacroFeatureDefinition{TParams,TPage} - parameters driven macro feature bound to page providing seamless integration with property pages enabling edit and preview capabilities.

Macro feature class must be com visible.

It is recommended to explicitly assign guid and prog id for the macro feature.

using System;
using System.Runtime.InteropServices;
using Xarial.XCad.Base.Attributes;
using Xarial.XCad.Documentation.Properties;
using Xarial.XCad.Features.CustomFeature.Attributes;
using Xarial.XCad.Features.CustomFeature.Delegates;
using Xarial.XCad.Features.CustomFeature.Enums;
using Xarial.XCad.Features.CustomFeature.Structures;
using Xarial.XCad.SolidWorks;
using Xarial.XCad.SolidWorks.Documents;
using Xarial.XCad.SolidWorks.Features.CustomFeature;

namespace Xarial.XCad.Documentation
{
    public class MySimpleMacroFeatureParameters
    {
        public string Parameter1 { get; set; }
    }

    [ComVisible(true)]
    [Guid("47827004-8897-49F5-9C65-5B845DC7F5AC")]
    [ProgId("CodeStack.MyMacroFeature")]
    [Title("MyMacroFeature")]
    [CustomFeatureOptions(CustomFeatureOptions_e.AlwaysAtEnd)]
    [Icon(typeof(Resources), nameof(Resources.macro_feature_icon))]
    public class MySimpleMacroFeature : SwMacroFeatureDefinition<MySimpleMacroFeatureParameters>
    {
        public override CustomFeatureRebuildResult OnRebuild(ISwApplication app, ISwDocument model, 
            ISwMacroFeature<MySimpleMacroFeatureParameters> feature,
            out AlignDimensionDelegate<MySimpleMacroFeatureParameters> alignDim)
        {
            alignDim = null;
            return new CustomFeatureRebuildResult() { Result = true };
        }
    }
}

Icon

Custom macro feature icon can be assigned via IconAttribute. Icon can be loaded from the resources and supports transparency.

Options

Additional options can be assigned via CustomFeatureOptionsAttribute, such as displaying the feature at the end of the tree, caching body, etc.

Macro feature is a COM object which means it needs to be registered in order for macro feature to operate. Macro feature is stored within the model, but if model is opened in the environment where the Macro Feature COM object is not registered - rebuild error will be displayed. Furthermore this 'dangling' macro feature cannot be removed or suppressed.

User can specify the custom message to be displayed in the What's Wrong dialog via MissingDefinitionErrorMessage attribute. Specified message will be displayed after the predefined Add-in not found. Please contact

using System.Runtime.InteropServices;
using Xarial.XCad.Features.CustomFeature.Attributes;
using Xarial.XCad.SolidWorks.Features.CustomFeature;

namespace Xarial.XCad.Documentation
{
    [ComVisible(true)]
    [MissingDefinitionErrorMessage("xCAD. Download the add-in")]
    public class UnregisteredMacroFeature : SwMacroFeatureDefinition
    {
    }
}

Rebuild error message for unregistered macro feature
Rebuild error message for unregistered macro feature

To insert the macro feature use IXFeatureRepository::PreCreateCustomFeature or IXFeatureRepository::CreateCustomFeature methods.

Refer Parametric Box example which demonstrates how to create a simple parametric box SOLIDWORKS feature with property page, preview and dimensions.


Powered by Docify