Defining a property set definition in .NET

By Mikako Harada

Issue

I'm trying to define a property set definition in .NET.  Could you provide a code sample?

Solution

Below is a code sample in VB.NET.  It defines a property set definition called "ACADoorObjects", which applies to door objects. It adds a manual and two automatic property definitions. 

Not shown here, but a formula property is possible to add programmatically. Note that in UI, you can also add location, classification, materials, project, anchor and graphics properties.  They are not possible with API, yet. A workaround is to define them in a separate dwg and import it using cloning helper utility class which I posted earlier.    

' Create a minimum set of property set definition.
  '
  ' Minimum error checking for code readability. 
  ' In practice, you may want to check its existace, for example.
 
   _
  Public Shared Sub CreatePropertySetDefinition()
 
    Dim doc As Document =
      Application.DocumentManager.MdiActiveDocument
    Dim db As Database = doc.Database
    Dim ed As Editor = doc.Editor
 
    ' The name of the property set def. 
    ' Could be for objects or styles. Hard coding for simplicity.
    ' This will be the key in the dictionary 
 
    Dim propSetDefName As String = "ACADoorObjects"
    'Dim propSetDefName As String = "ACANetDoorStyles" 
 
    Try
 
      ' (1) create prop set def 
 
      Dim propSetDef As New PropertySetDefinition
      propSetDef.SetToStandard(db)
      propSetDef.SubSetDatabaseDefaults(db)
 
      ' alternatively, you can use dictionary's NewEntry 
      'Dim dictPropSetDef = New DictionaryPropertySetDefinitions(db)
      'Dim propSetDef As PropertySetDefinition = 
      '  dictPropSetDef.NewEntry()
 
      ' General tab 
 
      propSetDef.Description = "Property Set Definition by ACA.NET"
 
      ' Applies To tab
 
      ' apply to objects or styles. True if style, False if objects
      Dim isStyle As Boolean = False
      Dim appliedTo = New StringCollection()
      appliedTo.Add("AecDbDoor")       ' apply to a door object  
      'appliedTo.Add("AecDbDoorStyle") ' apply to a door style
 
      ' apply to more than one object type, add more here. 
      'appliedTo.Add("AecDbWindow")    
 
      propSetDef.SetAppliesToFilter(appliedTo, isStyle)
 
      ' Definition tab 
 
      ' (2) we can add a set of property definitions.  
      ' We first make a container to hold them.
      ' This is the main part. A property set definition can contain 
      ' a set of property definition.
 
      ' (2.1) let's first add manual property. 
      ' Here we use text type 
 
      Dim propDefManual = New PropertyDefinition()
      propDefManual.SetToStandard(db)
      propDefManual.SubSetDatabaseDefaults(db)
      propDefManual.Name = "ACAManualProp"
      propDefManual.Description = "Manual property by ACA.NET"
      propDefManual.DataType =
        Autodesk.Aec.PropertyData.DataType.Text
      propDefManual.DefaultData = "ACA Default"
      ' add to the prop set def 
      propSetDef.Definitions.Add(propDefManual)
 
      ' (2.2) let's add another one, automatic one this time
 
      Dim propDefAutomatic = New PropertyDefinition()
      propDefAutomatic.SetToStandard(db)
      propDefAutomatic.SubSetDatabaseDefaults(db)
      propDefAutomatic.Name = "ACAWidth"
      propDefAutomatic.Description = "Automatic property by ACA.NET"
      propDefAutomatic.SetAutomaticData("AecDbDoor", "Width")
      ' add to the prop set def 
      propSetDef.Definitions.Add(propDefAutomatic)
 
      ' similarly, add one with height 
 
      propDefAutomatic = New PropertyDefinition()
      propDefAutomatic.SetToStandard(db)
      propDefAutomatic.SubSetDatabaseDefaults(db)
      propDefAutomatic.Name = "ACAHeight"
      propDefAutomatic.Description = "Automatic property by ACA.NET"
      propDefAutomatic.SetAutomaticData("AecDbDoor", "Height")
      '  add to the prop set def 
      propSetDef.Definitions.Add(propDefAutomatic)
 
      ' (3)  finally add the prop set def to the database 
 
      Using tr As Transaction =
        db.TransactionManager.StartTransaction
 
        '  check the name 
        Dim dictPropSetDef = New DictionaryPropertySetDefinitions(db)
        If dictPropSetDef.Has(propSetDefName, tr) Then
          ed.WriteMessage(
            "error - the property set defintion already exists: " +
            propSetDefName + vbCrLf)
          Return
        End If
 
        dictPropSetDef.AddNewRecord(propSetDefName, propSetDef)
        tr.AddNewlyCreatedDBObject(propSetDef, True)
        tr.Commit()
 
      End Using
 
    Catch ex As Exception
      ed.WriteMessage(
        "error in CreatePropSetDef: " + ex.ToString + vbCrLf)
      Return
    End Try
 
    ed.WriteMessage(
      "property set definition " + propSetDefName +
      " is successfully created." + vbCrLf)
 
  End Sub

Comments

6 responses to “Defining a property set definition in .NET”

  1. Hi,
    I’m curious if support has been added in the API yet for graphic properties?
    I’m not concerned with adding a property into the property set (I’m happy to have that predefined in my templates), but I do need a way to override the default block/image on a specific object (like a structural member). This can be done manually in the extended data tab of the properties palette, but I’m hoping to automate it.
    In .NET I normally set the value of a schedule data property using something like this…
    propSet.SetAt(propSet.PropertyNameToId(propName), propVal)
    I have tried having propVal of type PropertyDefinitionGraphic/BlockTableRecord/ObjectId/String etc. but nothing is working (it works fine if it is not a manual property not a graphic one).
    PS. I only tried in 2013 version I will try again in 2015 to see if it has changed.
    Thanks,
    Graham

  2. Hi Graham,
    Unfortunately, Graphics property is not supported by API. I checked with the product team, and they confirmed. We will log a wish and post the ID after we do so. Sorry for not being help.

  3. Ok thanks Mikako for letting me know – it saves me a lot of time experimenting without success.

  4. I would like to add a list of values within a property set definition.
    How do you do that?
    There is a DataType.List value in the DataType enum.
    There is a ListDefinitionId in PropertyDefinition.
    Can I add a list of values like

    Part Id, Supplier Name, Catalogue number

    Heater bank 1, HotStuff, HOT1234
    Heater coil, CoilsAreUs, Coil123
    Silly example I know but the point is many objects have a list of sub-components that need to be specified. How is this done?

  5. Steven Houghton Avatar
    Steven Houghton

    Can you please send me the dwg with the “ACADoorObjects”.
    Thanks
    Steven Houghton

Leave a Reply to Steven HoughtonCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading