#region References
// Standard References
using System;
// AutoCAD MEP References
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Aec.DatabaseServices;
using Autodesk.Aec.PropertyData.DatabaseServices;
using AcadDb = Autodesk.AutoCAD.DatabaseServices;
using AecDb = Autodesk.Aec.DatabaseServices;
using AecPropDb = Autodesk.Aec.PropertyData.DatabaseServices;
using ObjectId = Autodesk.AutoCAD.DatabaseServices.ObjectId;
using ObjectIdCollection = Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection;
#endregion
namespace MEPUtilities
{
/// <summary>
/// A collection of methods related to property sets.
/// </summary>
public static class PropertySetUtilities
{
#region CreatePropertySetOnObject
/// <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="MepObjectId">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 MepObjectId, 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(MepObjectId, 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(MepObjectId, 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
#endregion
#region CreatePropertySetOnObjects
/// <summary>
/// Creates a property set on a set of given objects.
/// Requires that the property set exists in the current database and
/// that the property set applies to the objects.
/// </summary>
/// <param name="MepObjectIds">The objectIDs of the objects to create the property set on.</param>
/// <param name="PropertySetId">The objectID of the property set to create on the object </param>
/// <returns> True if the property set was created on the objects, or false if there was a failure. </returns>
public static bool CreatePropertySetOnObject(ObjectIdCollection MepObjectIds, ObjectId PropertySetId)
{
Database db = HostApplicationServices.WorkingDatabase;
AcadDb.TransactionManager tm = db.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
// Try to attach the property set to the objects.
try
{
// Iterate through the objects, open them for write and then attach the property
// Set to the object
foreach (ObjectId MepObjectId in MepObjectIds)
{
// create a database object based on the ObjectId of the object
AcadDb.Entity dbObject = (AcadDb.Entity)tm.GetObject(MepObjectId, 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(MepObjectId, OpenMode.ForWrite, false, false);
AecPropDb.PropertyDataServices.AddPropertySet(dbobj, PropertySetId);
} // End If Block
} // End ForEach 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
#endregion
#region IsPropertySetAttachedToObject
/// <summary>
/// Finds a property set by its ObjectId on a given object.
/// </summary>
/// <param name="MepObj">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 MepObj, ObjectId PropertySetId)
{
// Create and set a temporary ObjectID to Null
ObjectId TempId = 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
TempId = AecPropDb.PropertyDataServices.GetPropertySet(MepObj, 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)
{
// Catching an error is not the best way to find out if a property set is
// Attached to an object. A better way would be to query the objects extension
// dictionary and see if the property set is in the extension dictionary
} // End Catch Block
// If the temp ObjectId is not null then we found the property set on the object
if (!TempId.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
#endregion
#region IsPropertySetAttachedToObject
/// <summary>
/// Finds the property set by its name on a given object.
/// </summary>
/// <param name="dbobj">The object to find the property set on.</param>
/// <param name="PropertySetName">The property name to find on the object.</param>
/// <returns> true if the property set with the given name was found, or false otherwise. </returns>
public static bool IsPropertySetAttachedToObject(AcadDb.DBObject MepObj, String PropertySetName)
{
// Get the property set ID from the property set name
ObjectId PropertySetId = GetPropertySetDefinitionIdByName(PropertySetName);
// Create and set a temporary ObjectId to null
ObjectId TempId = ObjectId.Null;
// Check to see if we actually got a property set ObjectId from the name
// If we did not then we know the property set does not exist.
if (!PropertySetId.IsNull)
{
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
TempId = AecPropDb.PropertyDataServices.GetPropertySet(MepObj, 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)
{
// Catching an error is not the best way to find out if a property set is
// Attached to an object. A better way would be to query the objects extension
// dictionary and see if the property set is in the extension dictionary
} // End Catch Block
} // End If Block
// If the temp ObjectId is not null then we found the property set on the object
if (!TempId.IsNull)
return true;
// If we are at this point then the property set was not found on the object
// or the property set does not exists and we can return false.
return false;
} //End IsPropertySetAttachedToObject
#endregion
#region GetPropertySetDefinitionIdbyName
/// <summary>
/// Finds the ObjectId of the property set with the given name.
/// </summary>
/// <param name="PropertySetName">The name of the property set.</param>
/// <returns>The ObjectId of the property set if found. If the property</returns>
/// <returns> set does not exist then returns null</returns>
public static ObjectId GetPropertySetDefinitionIdByName(string PropertySetName)
{
// Create objectId to hold the property set objectId and set the default value
// equal to null.
ObjectId PropertySetId = ObjectId.Null;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
AcadDb.TransactionManager tm = db.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
// Create a Property Set Dictionary
AecPropDb
.DictionaryPropertySetDefinitions psdDict
= new AecPropDb
.DictionaryPropertySetDefinitions(db
);
// Check to see if the dictionary contains the name of the property set we are looking for
if (psdDict.Has(PropertySetName, trans))
{
// Assign the return variable the ObjectId of the property set
PropertySetId = psdDict.GetAt(PropertySetName);
} // End If Block
trans.Commit();
} // End Using Statement
return PropertySetId;
} // End GetPropertySetDefintionIdByName Method
#endregion
} // End PropertySetUtilities Class
} // End MEPUtilities Namespace