Namespace MEPUtilities
{
/// <summary>
/// A collection of methods related to property sets.
/// </summary>
public static class PropertySetUtilities
{
/// <summary>
/// Finds a property set by its ObjectId on a given object.
/// </summary>
/// <param name="dbobj">The object to find the property set on.</param>
/// <param name="PropertySetId">The property set ObjectId to find on the object.</param>
/// <returns> True if the property set with the given ObjectId was found, or false otherwise. </returns>
public static bool IsPropertySetAttachedToObject(AcadDb.DBObject dbobj, ObjectId PropertySetId)
{
// Create and set a temporary ObjectID to Null
ObjectId psId = ObjectId.Null;
try
{
// Attempt to set the temporary ObjectID equal to the objectID of the
// of the property set we are attempting to find on the object.
// If the property set exists on the object then the temporary ObjectId
// will be assigned a value
psId = AecPropDb.PropertyDataServices.GetPropertySet(dbobj, PropertySetId);
} // End Try Block
// If the property set does not exist on the object then an error will be raised
catch (Autodesk.AutoCAD.Runtime.Exception)
{
// More than likely eKeyNotFound, so this is a more specific
// place to handle a "failed to find" condition.
} // End Catch Block
// If the temp ObjectId is not null then we found the property set on the object
if (!psId.IsNull)
return true;
// If we are at this point then the property set was not found on the object
// and we can return false.
return false;
} // End IsPropertySetAttachedToObject Method
/// <summary>
/// Creates a property set on a given object.
/// Requires that the property set exists in the current database and
/// that the property set applies to the object.
/// </summary>
/// <param name="objObjectId">The objectID of the object to create the property set on.</param>
/// <param name="PropertySetObjectId">The objectID of the property set to create on the object </param>
/// <returns> True if the property set was created on the object, or false if there was a failure. </returns>
public static bool CreatePropertySetOnObject(ObjectId objObjectId, ObjectId PropertySetId)
{
Database db = Application.DocumentManager.MdiActiveDocument.Database;
AcadDb.TransactionManager tm = db.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
// Try to attach the property set to the object.
try
{
// First check to see if the property set already exists on the object
// create a database object based on the ObjectId of the object
AcadDb.Entity dbObject = (AcadDb.Entity)tm.GetObject(objObjectId, OpenMode.ForRead, true);
// Check to see if the property set is already attached to the object
if (!IsPropertySetAttachedToObject(dbObject, PropertySetId))
{
// Attach the property set to the object
AcadDb.DBObject dbobj = tm.GetObject(objObjectId, OpenMode.ForWrite, false, false);
AecPropDb.PropertyDataServices.AddPropertySet(dbobj, PropertySetId);
} // End If Block
} // End Try Block
// If there is an error catch it and return false
catch (Autodesk.AutoCAD.Runtime.Exception)
{
return false;
} // End Catch Block
// At this point the property set was successfully attached so commit the transaction
trans.Commit();
} // End Try-Catch Block
// The property set has been attached and the transaction committed
// So we can finish up and return true
return true;
} // End CreatePropertySetOnObject Method
} // End PropertySetUtilities Class
} // End MEPUtilities Namespace