09/26/2012

Attach a property set to a given object type

Yesterday, I posted a sample code which shows you how to attach a property set to a given object. Here is similar, slightly modified version. Here we attach a property set to a given object type; i.e., door in this case.

' Attach a property set to all door objects

'

<CommandMethod("ACANetScheduleLabs",

"AcaAttachPropertySetToAllDoors",

CommandFlags.Modal)> _

PublicSub AttachPropertySetToAllDoors()

' Top most objects

Dim doc AsDocument =

Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Dim ed AsEditor = doc.Editor

' (1) Find all the door objects in the model space

Dim ids AsObjectIdCollection = Utils.findObjects(GetType(Door))

' (2) Find the property set definition with the given name.

Dim dictPropSetDef = NewDictionaryPropertySetDefinitions(db)

' Find a property set definition called "ACADoorObjects".

' "ACADoorObjects" is what we defined in Lab1 or

' in the command "AcaCreatePropSetDef" or

' the function CreatePropertySetDefinition() we posted earlier.

' You can use any prop set def name that can work with door

' objects here.

Dim idPropSetDef AsObjectId =

Utils.findStyle(dictPropSetDef, "ACADoorObjects")

If idPropSetDef = NothingThenReturn

' (3) Attach the given property set to the given object.

Try

Using tr AsTransaction =

db.TransactionManager.StartTransaction

ForEach id AsObjectIdIn ids

Dim obj AsAcObject =

tr.GetObject(id, OpenMode.ForWrite, False, False)

' PropertyDataServices provide a convenient method

' to do the actual work.

PropertyDataServices.AddPropertySet(obj, idPropSetDef)

Next

tr.Commit()

EndUsing

Catch ex AsException

ed.WriteMessage(

"error in AttachPropertySetToAllDoors: " +

ex.ToString + vbCrLf)

EndTry

ed.WriteMessage(

"The property set was attached successfully." + vbCrLf)

EndSub

Here is the code for Utils.Objects(). Utils.findStyle() is the same as the one posted yesterday:

Comments

Yesterday, I posted a sample code which shows you how to attach a property set to a given object. Here is similar, slightly modified version. Here we attach a property set to a given object type; i.e., door in this case.

' Attach a property set to all door objects

'

<CommandMethod("ACANetScheduleLabs",

"AcaAttachPropertySetToAllDoors",

CommandFlags.Modal)> _

PublicSub AttachPropertySetToAllDoors()

' Top most objects

Dim doc AsDocument =

Application.DocumentManager.MdiActiveDocument

Dim db AsDatabase = doc.Database

Dim ed AsEditor = doc.Editor

' (1) Find all the door objects in the model space

Dim ids AsObjectIdCollection = Utils.findObjects(GetType(Door))

' (2) Find the property set definition with the given name.

Dim dictPropSetDef = NewDictionaryPropertySetDefinitions(db)

' Find a property set definition called "ACADoorObjects".

' "ACADoorObjects" is what we defined in Lab1 or

' in the command "AcaCreatePropSetDef" or

' the function CreatePropertySetDefinition() we posted earlier.

' You can use any prop set def name that can work with door

' objects here.

Dim idPropSetDef AsObjectId =

Utils.findStyle(dictPropSetDef, "ACADoorObjects")

If idPropSetDef = NothingThenReturn

' (3) Attach the given property set to the given object.

Try

Using tr AsTransaction =

db.TransactionManager.StartTransaction

ForEach id AsObjectIdIn ids

Dim obj AsAcObject =

tr.GetObject(id, OpenMode.ForWrite, False, False)

' PropertyDataServices provide a convenient method

' to do the actual work.

PropertyDataServices.AddPropertySet(obj, idPropSetDef)

Next

tr.Commit()

EndUsing

Catch ex AsException

ed.WriteMessage(

"error in AttachPropertySetToAllDoors: " +

ex.ToString + vbCrLf)

EndTry

ed.WriteMessage(

"The property set was attached successfully." + vbCrLf)

EndSub

Here is the code for Utils.Objects(). Utils.findStyle() is the same as the one posted yesterday: