#region Example_DisplayOverrideDefaultDrawing
[CommandMethod("ACAClassCode", "ExDisplayOverrideDefaultDrawing", CommandFlags.Modal)]
public void DisplayOverrideDefaultDrawing()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
PromptEntityOptions entopts
= new PromptEntityOptions
("Pick an entity of your choice from the drawing to change colors."); entopts.Message = "Pick an entity of your choice from the drawing to change colors.";
PromptEntityResult entRes = null;
try
{
entRes = ed.GetEntity(entopts);
}
catch
{
ed.WriteMessage("You did not select a valid entity");
return;
}
if (entRes.Status != PromptStatus.Error)
{
ObjectId entid = entRes.ObjectId;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction trans1 = tm.StartTransaction())
{
// we have object level overrides if this is > 0
ObjectIdCollection dispIdsFromEntity = DisplayProperties.GetDisplayPropertiesFromOverride(entid);
foreach (ObjectId id in dispIdsFromEntity)
{
DoColorOverrideChange(id, 1);
}
// See if there is a style ovverride and change its color only if present
ObjectId wallStyleId
= new ObjectId
();
using (Transaction trans2 = tm.StartTransaction())
{
Autodesk.AutoCAD.DatabaseServices.Entity ent = (Autodesk.AutoCAD.DatabaseServices.Entity)tm.GetObject(entid, OpenMode.ForRead, true);
if (ent
.GetType() == typeof(Wall
)) {
Wall wall = ent as Wall;
wallStyleId = wall.StyleId;
}
trans2.Commit();
}
if (!wallStyleId.IsNull)
{
ObjectIdCollection dispIdsFromStyle = DisplayProperties.GetDisplayPropertiesFromOverride(wallStyleId);
foreach (ObjectId id in dispIdsFromStyle)
{
DoColorOverrideChange(id, 2);
}
}
// Now, just blanket the drawing level override with a new color for the wall class.
DisplayRepresentationManager mgr
= new DisplayRepresentationManager
(); ObjectIdCollection idsReps
= mgr
.GetDisplayRepresentationIdsFromCurrentViewport(RXObject
.GetClass(typeof(Wall
))); using (Transaction trans3 = tm.StartTransaction())
{
foreach (ObjectId id in idsReps)
{
DisplayRepresentation rep = (DisplayRepresentation)tm.GetObject(id, OpenMode.ForRead, true);
if (rep.Name == "AecDbDispRepWallPlan") // this is a sanity check, I believe because we are getting the displayreps frmo the current viewport, based on wall, there can be only this one per class per drawing...
{
Autodesk.AutoCAD.DatabaseServices.Entity ent = (Autodesk.AutoCAD.DatabaseServices.Entity)tm.GetObject(entid, OpenMode.ForRead, true);
ObjectId idDispProp = rep.GetDisplayPropertiesId(ent);
DoColorOverrideChange(idDispProp, 6);
}
}
trans3.Commit();
}
trans1.Commit();
}
}
}
catch (System.Exception e)
{
ed.WriteMessage("\nException: " + e.Message);
}
finally
{
}
}
void DoColorOverrideChange(ObjectId id, short color)
{
Database db = HostApplicationServices.WorkingDatabase;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction trans = tm.StartTransaction())
{
DisplayProperties dispProps = (DisplayProperties)tm.GetObject(id, OpenMode.ForRead, true);
System.Collections.Specialized.StringCollection compNames
= new System.Collections.Specialized.StringCollection(); DisplayComponent[] comps = dispProps.GetDisplayComponents(out compNames);
if (compNames.Contains("Shrink Wrap"))
{
if (comps
[compNames
.IndexOf("Shrink Wrap")].GetType() == typeof(DisplayComponentEntity
)) {
DisplayComponentEntity compEntity = comps[compNames.IndexOf("Shrink Wrap")] as DisplayComponentEntity;
dispProps.UpgradeOpen();
if (compEntity.Color.ColorMethod == Autodesk.AutoCAD.Colors.ColorMethod.ByAci)
compEntity.ColorIndex = color;
else
compEntity.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, color);
}
}
trans.Commit();
}
}
#endregion
#region Example_DisplayOverrideAddEntity
[CommandMethod("ACAClassCode", "ExDisplayOverrideAddEntity", CommandFlags.Modal)]
public void DisplayOverrideEntity()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
try
{
PromptEntityOptions entopts
= new PromptEntityOptions
("Pick an entity of your choice from the drawing to add color override."); entopts.Message = "Pick an entity of your choice from the drawing to add color override.";
PromptEntityResult entRes = null;
try
{
entRes = ed.GetEntity(entopts);
}
catch
{
ed.WriteMessage("You did not select a valid entity");
return;
}
if (entRes.Status != PromptStatus.Error)
{
ObjectId entid = entRes.ObjectId;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager;
using (Transaction trans1 = tm.StartTransaction())
{
DisplayRepresentationManager mgr
= new DisplayRepresentationManager
(); ObjectIdCollection idsReps
= mgr
.GetDisplayRepresentationIdsFromCurrentViewport(RXObject
.GetClass(typeof(Wall
))); ObjectId idRep = idsReps[0];
DisplayRepresentation rep = (DisplayRepresentation)tm.GetObject(idRep, OpenMode.ForRead, true);
if (rep.Name == "AecDbDispRepWallPlan")
{
DisplayProperties newProps = rep.CreateNewDisplayProperties();
newProps.SetToStandard(db);
newProps.SubSetDatabaseDefaults(db);
newProps.SetDisplayRepDefaults(rep);
//
System.Collections.Specialized.StringCollection compNames
= new System.Collections.Specialized.StringCollection(); DisplayComponent[] comps = newProps.GetDisplayComponents(out compNames);
if (compNames.Contains("Shrink Wrap"))
{
if (comps
[compNames
.IndexOf("Shrink Wrap")].GetType() == typeof(DisplayComponentEntity
)) {
DisplayComponentEntity compEntity = comps[compNames.IndexOf("Shrink Wrap")] as DisplayComponentEntity;
compEntity.ResetEnt();
if (compEntity.Color.ColorMethod == Autodesk.AutoCAD.Colors.ColorMethod.ByAci)
compEntity.ColorIndex = 30; // Made a code change here so we could see it!!!!
else
compEntity.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(Autodesk.AutoCAD.Colors.ColorMethod.ByColor, 30); // Made a code change here so we could see it!!!!
}
}
//
ObjectId idNOD = db.NamedObjectsDictionaryId;
Autodesk.AutoCAD.DatabaseServices.DBDictionary nodDict = (Autodesk.AutoCAD.DatabaseServices.DBDictionary)tm.GetObject(idNOD, OpenMode.ForRead, true);
ObjectId idOverrideExtDict
= new ObjectId
(); try
{
idOverrideExtDict = nodDict.GetAt(Override.ExtensionDictionaryName);
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
if (e.ErrorStatus == Autodesk.AutoCAD.Runtime.ErrorStatus.KeyNotFound)
{
DBDictionary dict
= new DBDictionary
(); nodDict.UpgradeOpen();
idOverrideExtDict = nodDict.SetAt(Override.ExtensionDictionaryName, dict);
nodDict.DowngradeOpen();
trans1.AddNewlyCreatedDBObject(dict, true);
}
}
if (idOverrideExtDict.IsNull)
return;
Autodesk.AutoCAD.DatabaseServices.DBDictionary overrideExtDict = (Autodesk.AutoCAD.DatabaseServices.DBDictionary)tm.GetObject(idOverrideExtDict, OpenMode.ForWrite, true);
ObjectId idNewProps = overrideExtDict.SetAt("*A", newProps);
trans1.AddNewlyCreatedDBObject(newProps, true);
OverrideDisplayProperties overProps
= new OverrideDisplayProperties
(); overProps.ViewId = idRep;
overProps.DisplayPropertyId = idNewProps;
Autodesk.Aec.DatabaseServices.Entity ent = (Autodesk.Aec.DatabaseServices.Entity)tm.GetObject(entid, OpenMode.ForWrite, true);
ent.Overrides.Add(overProps);
}
trans1.Commit();
}
}
}
catch (System.Exception e)
{
ed.WriteMessage("\nError: " + e.Message);
}
finally
{
}
}
#endregion