Using Attribute Sets in C#

By Augusto Goncalves

This sample code adds an Attribute Set containing a single Attribute to the first Edge in the Part Document. If the Edge already has the given Attribute, then it replaces with a new value.

Since the value type of an Attribute cannot be changed, if the Edge contains a different Attribute type with the same name, then first delete it and then add an Attribute with the new value.

The code below illustrates how to use Attributes from C#. It expects an open Part Document containing at least one Edge.

// attribute names

const string attributeSetName = "MyAttributeSet";

const string attributeName = "MyAttribute";

 

Inventor.Application app = System.Runtime.InteropServices.Marshal.

  GetActiveObject("Inventor.Application") as Inventor.Application;

Inventor.PartDocument doc =

  app.ActiveDocument as Inventor.PartDocument;

 

// Create a collection of objects

object obj = null;

Inventor.ObjectCollection objectCollection =

  app.TransientObjects.CreateObjectCollection(obj);

 

// Get one surface body edge

objectCollection.Add(

  doc.ComponentDefinition.SurfaceBodies[1].Edges[1]);

// … you can keep on adding the objects you need to …

 

// if OpenAttributeSets function does not find the given attribute

// set for an object in the collection, it creates one for it

Inventor.AttributeSetsEnumerator attributeSets =

  doc.AttributeManager.OpenAttributeSets(

  objectCollection, attributeSetName);

 

// you can keep changing this to check if

// assigning the new value works fine

byte[] newData = new byte[] { 5, 8, 10 };

 

// attributeSets contains the attributeSet of

// each object you added to the objectCollection

// in this case only one, the first Edge

foreach (Inventor.AttributeSet attributeSet in attributeSets)

{

  try

  {

    // if it throws exception, then it did not find the attribute

    Inventor.Attribute attribute = attributeSet[attributeName];

    // we have to initialize the array before it can be used

    byte[] oldData = attribute.Value as byte[];

 

    // if oldData is null, then it found the attribute

    // but it contains a different data type

    if (oldData == null)

    {

      // we’d better delete the old value with different

      // data type if you want ValueType to be consistent

      // with the object it stores, since we cannot change

      // the attribute.ValueType(read-only)

      attribute.Delete();

      // and then create it again with the new

      // value type – by throwing exception

      // (same as if the attribute wasn’t there in the first place)

      throw new System.Exception(

        "The data type is different, " +

        " and therefore the attribute has been deleted");

    }

 

    attribute.Value = newData;

  }

  catch

  {

    // add the attribute with the new value

    attributeSet.Add(attributeName,

      Inventor.ValueTypeEnum.kByteArrayType, newData);

  }

}


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading