Skip to main content

Edit SOLIDWORKS macro feature definition

Edit definition allows to modify the parameters of an existing feature. Edit definition is called when Edit Feature command is clicked form the feature manager tree.

Edit Feature Command{ width=250 }

The typical workflow which should be followed when feature is edited

  • Get the definition of the feature via IFeature::GetDefinition
  • Rollback the feature in the tree via IMacroFeatureData::AccessSelections. This will ensure that all the feature selections and edit bodies are available.
  • Get the parameters of current macro feature via GetParameters
  • Create user interface and allow user to edit parameters. The recommended way to use Property Manager Pages to have a native look and feel of your feature. Use SwEx.PMPage framework for simplified way of creating property manager pages.
  • Once user interface is closed
using CodeStack.SwEx.MacroFeature;
using SolidWorks.Interop.sldworks;

namespace CodeStack.SwEx
{
public class EditMacroFeatureDefinitionParameters
{
//TODO: add properties
}

public class EditMacroFeatureDefinition:MacroFeatureEx<EditMacroFeatureDefinitionParameters>
{
protected override bool OnEditDefinition(ISldWorks app, IModelDoc2 model, IFeature feature)
{
var featData = feature.GetDefinition() as IMacroFeatureData;

//rollback feature
featData.AccessSelections(model, null);

//read current parameters
var parameters = GetParameters(feature, featData, model);

var res = ShowPage(parameters);

if (res)
{
//set parameters and update feature data
SetParameters(model, feature, featData, parameters);
feature.ModifyDefinition(featData, model, null);
}
else
{
//cancel modifications
featData.ReleaseSelectionAccess();
}

return true;
}

private bool ShowPage(EditMacroFeatureDefinitionParameters parameters)
{
//TODO: Show property page or any other user interface
return true;
}
}
}

It is important to use the same pointer to IMacroFeatureData while calling the IMacroFeatureData::AccessSelections, GetParameters, SetParameters, IFeature::ModifyDefinition and IMacroFeatureData::ReleaseSelectionAccess methods.