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

Leave a Reply