Creating Property set and assigning properies value - .NET C#

Creating Property set and assigning properies value - .NET C#

or_levi5LSMC
Participant Participant
733 Views
1 Reply
Message 1 of 2

Creating Property set and assigning properies value - .NET C#

or_levi5LSMC
Participant
Participant

hey all, 

im tryng to create a utill class that will create property-set to an object, and attatch propetries to every object  instance("set values" to the property set of each instance of an object)

 i can create or get  new property-set by the following method:

 

  public static ObjectId GetOrCreatePropertySetDefinition(Transaction tr, string propertySetName, List<(string Name, Type Type)> properties)
  {
      // Access the Named Object Dictionary (NOD)
      DBDictionary nod = tr.GetObject(
          HostApplicationServices.WorkingDatabase.NamedObjectsDictionaryId,
          OpenMode.ForRead
      ) as DBDictionary;
      foreach (var entry in nod)
      {
          var key = entry.Key;
          var value = entry.Value;
          System.Diagnostics.Debug.WriteLine($"Key: {key}, Value: {value}");
      }

      // Check if Property Set Definitions Dictionary exists
      if (!nod.Contains("AEC_PROPERTY_SET_DEFS"))
      {
          throw new Exception("Property Set Definitions Dictionary not found.");
      }

      // Open the Property Set Definitions Dictionary
      ObjectId propSetDictId = nod.GetAt("AEC_PROPERTY_SET_DEFS");
      DBDictionary propSetDict = tr.GetObject(propSetDictId, OpenMode.ForRead) as DBDictionary;

      // Check if the Property Set already exists
      if (propSetDict.Contains(propertySetName))
      {
          return propSetDict.GetAt(propertySetName);
      }

      // If it doesn't exist, create a new one
      propSetDict.UpgradeOpen();
      PropertySetDefinition newPropSet = new PropertySetDefinition();
      newPropSet.AlternateName = propertySetName;

      // Add properties dynamically based on input list
      foreach (var prop in properties)
      {
          // Convert C# type to Autodesk Civil 3D property data type
          Autodesk.Aec.PropertyData.DataType aecDataType = ConvertToAecDataType(prop.Type);

          // Create the property definition
          PropertyDefinition propertyDefinition = new PropertyDefinition
          {
              Name = prop.Name,
              DataType = aecDataType
          };

          newPropSet.Definitions.Add(propertyDefinition);
      }

      // Add the new Property Set to the dictionary
      ObjectId newPropSetId = propSetDict.SetAt(propertySetName, newPropSet);
      tr.AddNewlyCreatedDBObject(newPropSet, true);

      return newPropSetId;
  }

  // Mapping function for data types
  private static Autodesk.Aec.PropertyData.DataType ConvertToAecDataType(Type type)
  {
      if (type == typeof(int))
          return Autodesk.Aec.PropertyData.DataType.Integer;
      if (type == typeof(double) || type == typeof(float))
          return Autodesk.Aec.PropertyData.DataType.Real;
      if (type == typeof(string))
          return Autodesk.Aec.PropertyData.DataType.Text;
      if (type == typeof(bool))
          return Autodesk.Aec.PropertyData.DataType.TrueFalse;
      if (type == typeof(DateTime))
          return Autodesk.Aec.PropertyData.DataType.Text;

      throw new ArgumentException($"Unsupported data type: {type.Name}");
  }

   im trying to set values to the property set using this:

 /// <summary>
 /// Attaches a Property Set to a Civil 3D object and assigns property values.
 /// </summary>
 /// <param name="tr">Active transaction</param>
 /// <param name="objectId">The ObjectId of the Civil 3D object</param>
 /// <param name="propertySetId">ObjectId of the Property Set</param>
 /// <param name="propertyValues">Dictionary containing property names and values</param>
 public static void AttachPropertySet(Transaction tr, ObjectId objectId, ObjectId propertySetId, Dictionary<string, object> propertyValues)
 {
     // Check if the object has the property set attached
     bool hasPropertySet = PropertyDataServices.GetAllPropertySetsUsingDefinition(propertySetId, false)
         .Contains(objectId);

     // Attach the property set if it doesn't exist
     if (!hasPropertySet)
     {
         DBObject dbObject = tr.GetObject(objectId, OpenMode.ForWrite);
         PropertyDataServices.AddPropertySet(dbObject, propertySetId);

     }

     // Retrieve the Property Set
     PropertySet propSet = tr.GetObject(propertySetId, OpenMode.ForWrite) as PropertySet;
     if (propSet == null)
     {
         throw new Exception("Failed to retrieve the Property Set.");
     }

     // Assign values to properties
     foreach (var kvp in propertyValues)
     {
         int propertyId = propSet.PropertyNameToId(kvp.Key);
         if (propertyId != -1)
         {
             propSet.SetAt(propertyId, kvp.Value);
         }
         else
         {
             throw new Exception($"Property '{kvp.Key}' not found in Property Set '{propSet.Name}'.");
         }
     }
 }



i always get propSet==null  in here: 
// Retrieve the Property Set
PropertySet propSet = tr.GetObject(propertySetId, OpenMode.ForWrite) as PropertySet;

for some reason i cant get the propertyset that i created... 

what am i doing wrong? why cant i get the propertyset and set values to each elemtnt?

0 Likes
Accepted solutions (1)
734 Views
1 Reply
Reply (1)
Message 2 of 2

or_levi5LSMC
Participant
Participant
Accepted solution

i solved the problem. 

i got help from this post ( using vb) : 


https://forums.autodesk.com/t5/civil-3d-customization/property-sets-a-quick-how-to/td-p/12502844

and translated it to C# 

0 Likes