All Resources
Code SnippetSolidWorks API2026-02-24

Store Persistent Data with Attributes

Use SolidWorks Attributes to store custom data directly on geometry — faces, edges, components. The hidden API feature that most developers never discover.

csharp
public void AttributeDemo(SldWorks swApp, ModelDoc2 model)
{
    // Step 1: Define the attribute (once per session)
    var attDef = (AttributeDef)swApp.DefineAttribute(
        "MyApp.FaceData");
    attDef.AddParameter("OperationId",
        (int)swParamType_e.swParamTypeString, 0, 0);
    attDef.AddParameter("Tolerance",
        (int)swParamType_e.swParamTypeDouble, 0.01, 0);
    attDef.Register();

    // Step 2: Get a face from the current selection
    var selMgr = model.SelectionManager;
    var face = (Face2)selMgr.GetSelectedObject6(1, -1);

    // Step 3: Create an attribute instance on the face
    var att = (SolidWorks.Interop.sldworks.Attribute)
        attDef.CreateInstance5(
            model, face,
            "MyApp.FaceData.1",
            0,  // options
            (int)swInConfigurationOpts_e.swAllConfiguration);

    // Step 4: Set parameter values
    var param1 = (Parameter)att.GetParameter("OperationId");
    param1.SetStringValue2("MILL-FACE-001",
        (int)swInConfigurationOpts_e.swAllConfiguration, "");

    var param2 = (Parameter)att.GetParameter("Tolerance");
    param2.SetDoubleValue2(0.005,
        (int)swInConfigurationOpts_e.swAllConfiguration, "");

    // Later: Read it back by traversing features
    var feat = (Feature)model.FirstFeature();
    while (feat != null)
    {
        if (feat.GetTypeName2() == "Attribute")
        {
            var readAtt = (SolidWorks.Interop.sldworks.Attribute)
                feat.GetSpecificFeature2();
            var p = (Parameter)readAtt.GetParameter("OperationId");
            Console.WriteLine($"Found: {p.GetStringValue()}");
        }
        feat = (Feature)feat.GetNextFeature();
    }
}

What Are Attributes?

Attributes are invisible (or optionally visible) features that store custom key-value data directly inside a SolidWorks model file. They're like custom properties, but with crucial differences:

  • Custom properties: Stored at file or config level. Key-value pairs of strings/numbers. No geometry association.
  • Attributes: Stored as features. Can be attached to specific faces, edges, vertices, or components. Multiple typed parameters per attribute. Survive rebuilds.

When to Use Attributes

  • Tagging geometry: Mark a specific face as a machining surface, a datum, or a critical dimension location
  • Storing process data: Attach operation IDs, tolerances, or surface finish requirements to individual faces
  • Cross-session persistence: Unlike selection marks (temporary), attributes save with the file and persist across sessions
  • Add-in data storage: Your add-in can store its own metadata without polluting the custom properties that users see

The Attribute Lifecycle

  1. Define: Create an AttributeDef with AddParameter calls. This is your schema.
  2. Register: Call attDef.Register(). After this, the definition is locked — you can't add more parameters.
  3. Instantiate: Call attDef.CreateInstance5() to create an attribute on a specific entity (or free-floating).
  4. Set values: Use GetParameter() and SetStringValue2()/SetDoubleValue2() to write data.
  5. Read back: Traverse features, find those with GetTypeName2() == "Attribute", and read parameters.

Important: Definition Must Be Registered First

You must call DefineAttribute + Register in your session before you can read existing attribute instances. If your add-in creates attributes, it must register the same definition on startup — otherwise, the attribute features exist in the model but you can't access their parameters.

Visibility

By default, attributes appear in the Feature Manager tree with a yellow icon. To hide them from users, set the visibility option when creating the instance. Hidden attributes still exist and can be traversed via the API.

Tips

  • Attribute definitions are per-session, not per-document — register them once at startup
  • Unlike macro features, attributes don't require your add-in to be installed for the model to open correctly
  • Use a naming convention for your attribute definition names (e.g., "CompanyName.AppName.DataType") to avoid conflicts with other add-ins
  • Attributes can store integers, doubles, and strings — for complex data, serialize to a JSON string parameter
SolidWorks APIC#AttributesData StorageMetadata