<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Creating Property set and assigning properies value -  .NET C# in Civil 3D Customization Forum</title>
    <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/creating-property-set-and-assigning-properies-value-net-c/m-p/13387362#M213</link>
    <description>&lt;P&gt;i solved the problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i got help from this post ( using vb) :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="https://forums.autodesk.com/t5/civil-3d-customization/property-sets-a-quick-how-to/td-p/12502844" target="_blank"&gt;https://forums.autodesk.com/t5/civil-3d-customization/property-sets-a-quick-how-to/td-p/12502844&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;and translated it to C#&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Mar 2025 14:58:43 GMT</pubDate>
    <dc:creator>or_levi5LSMC</dc:creator>
    <dc:date>2025-03-24T14:58:43Z</dc:date>
    <item>
      <title>Creating Property set and assigning properies value -  .NET C#</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/creating-property-set-and-assigning-properies-value-net-c/m-p/13385852#M212</link>
      <description>&lt;P&gt;hey all,&amp;nbsp;&lt;/P&gt;&lt;P&gt;im tryng to create a utill class that will create property-set to an object, and attatch propetries to every object&amp;nbsp; instance("set values" to the property set of each instance of an object)&lt;/P&gt;&lt;P&gt;&amp;nbsp;i can create or get&amp;nbsp; new property-set by the following method:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;  public static ObjectId GetOrCreatePropertySetDefinition(Transaction tr, string propertySetName, List&amp;lt;(string Name, Type Type)&amp;gt; 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}");
  }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;im trying to set values to the property set using this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; /// &amp;lt;summary&amp;gt;
 /// Attaches a Property Set to a Civil 3D object and assigns property values.
 /// &amp;lt;/summary&amp;gt;
 /// &amp;lt;param name="tr"&amp;gt;Active transaction&amp;lt;/param&amp;gt;
 /// &amp;lt;param name="objectId"&amp;gt;The ObjectId of the Civil 3D object&amp;lt;/param&amp;gt;
 /// &amp;lt;param name="propertySetId"&amp;gt;ObjectId of the Property Set&amp;lt;/param&amp;gt;
 /// &amp;lt;param name="propertyValues"&amp;gt;Dictionary containing property names and values&amp;lt;/param&amp;gt;
 public static void AttachPropertySet(Transaction tr, ObjectId objectId, ObjectId propertySetId, Dictionary&amp;lt;string, object&amp;gt; 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}'.");
         }
     }
 }&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;i always get&amp;nbsp;propSet==null&amp;nbsp; in here:&amp;nbsp;&lt;/STRONG&gt;&lt;BR /&gt;// Retrieve the Property Set&lt;BR /&gt;PropertySet propSet = tr.GetObject(propertySetId, OpenMode.ForWrite) as PropertySet;&lt;BR /&gt;&lt;BR /&gt;for some reason i cant get the propertyset that i created...&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;what am i doing wrong? why cant i get the propertyset and set values to each elemtnt?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 16:36:18 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/creating-property-set-and-assigning-properies-value-net-c/m-p/13385852#M212</guid>
      <dc:creator>or_levi5LSMC</dc:creator>
      <dc:date>2025-03-23T16:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating Property set and assigning properies value -  .NET C#</title>
      <link>https://forums.autodesk.com/t5/civil-3d-customization-forum/creating-property-set-and-assigning-properies-value-net-c/m-p/13387362#M213</link>
      <description>&lt;P&gt;i solved the problem.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i got help from this post ( using vb) :&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;A href="https://forums.autodesk.com/t5/civil-3d-customization/property-sets-a-quick-how-to/td-p/12502844" target="_blank"&gt;https://forums.autodesk.com/t5/civil-3d-customization/property-sets-a-quick-how-to/td-p/12502844&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;and translated it to C#&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 14:58:43 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/civil-3d-customization-forum/creating-property-set-and-assigning-properies-value-net-c/m-p/13387362#M213</guid>
      <dc:creator>or_levi5LSMC</dc:creator>
      <dc:date>2025-03-24T14:58:43Z</dc:date>
    </item>
  </channel>
</rss>

