Thought I should put the portion of the code up I'm having trouble with:
// Transform points to selected clipping block ucs
pt1 = pt1.TransformBy(selectedBlock.BlockTransform);
pt2 = pt2.TransformBy(selectedBlock.BlockTransform);
pt3 = pt3.TransformBy(selectedBlock.BlockTransform);
pt4 = pt4.TransformBy(selectedBlock.BlockTransform);
var ms = (BlockTableRecord) tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);
foreach (ObjectId id in ms)
{
var ent = (Entity) tr.GetObject(id, OpenMode.ForRead);
if (ent != null)
{
if (ent.GetType() == typeof (BlockReference))
{
var br = (BlockReference) ent; // Get block reference
var btr = (BlockTableRecord) tr.GetObject(br.BlockTableRecord, OpenMode.ForRead);
// Check whether the blockreference is an external reference
if (btr.IsFromExternalReference && btr.IsResolved)
{
var pts = new Point2dCollection(4)
{
new Point2d(pt1.X, pt1.Y),
new Point2d(pt2.X, pt2.Y),
new Point2d(pt3.X, pt3.Y),
new Point2d(pt4.X, pt4.Y)
};
// Create spatial filter
var sfd = new SpatialFilterDefinition(pts, Vector3d.ZAxis, 0.0, 0.0, 0.0, true);
var sf = new SpatialFilter {Definition = sfd};
// Create extension dictionary if doesn't exist
if (br.ExtensionDictionary == ObjectId.Null)
{
br.UpgradeOpen();
br.CreateExtensionDictionary();
br.DowngradeOpen();
}
// Add spatial filter to extension dictionary
var extDict = (DBDictionary) tr.GetObject(br.ExtensionDictionary, OpenMode.ForWrite);
if (extDict.Contains(filterDictName))
{
var filterDict = (DBDictionary) tr.GetObject(extDict.GetAt(filterDictName), OpenMode.ForWrite);
if (filterDict.Contains(spatialName))
filterDict.Remove(spatialName);
filterDict.SetAt(spatialName, sf);
}
else
{
var filterDict = new DBDictionary();
extDict.SetAt(filterDictName, filterDict);
tr.AddNewlyCreatedDBObject(filterDict, true);
filterDict.SetAt(spatialName, sf);
}
tr.AddNewlyCreatedDBObject(sf, true);
}
}
}
}
tr.Commit();