using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Collections.Generic;
using System.Linq;
namespace DynamicTyping
{
public class Commands
{
[CommandMethod("CLN")]
public static void ChangeLayerNames()
{
Database db = HostApplicationServices.WorkingDatabase;
using (
Transaction tr = db.TransactionManager.StartTransaction()
)
{
LayerTable lt =
(LayerTable)tr.GetObject(
db.LayerTableId, OpenMode.ForRead
);
foreach (ObjectId ltrId in lt)
{
LayerTableRecord ltr =
(LayerTableRecord)tr.GetObject(ltrId, OpenMode.ForRead);
if (ltr.Name != "0")
{
ltr.UpgradeOpen();
ltr.Name = "First Floor " + ltr.Name;
}
}
tr.Commit();
}
}
[CommandMethod("CLN2")]
public static void ChangeLayerNamesDynamically()
{
dynamic layers =
HostApplicationServices.WorkingDatabase.LayerTableId;
foreach (dynamic l in layers)
if (l.Name != "0")
l.Name = "First Floor " + l.Name;
}
// Adds a custom dictionary in the extension dictionary of
// selected objects. Uses dynamic capabilities, but not LINQ.
[CommandMethod("AddMyDict")]
public void AddMyDict()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptSelectionResult res = ed.GetSelection();
if (res.Status != PromptStatus.OK)
return;
foreach (dynamic ent in res.Value.GetObjectIds())
{
dynamic dictId = ent.ExtensionDictionary;
if (dictId == ObjectId.Null)
{
ent.CreateExtensionDictionary();
dictId = ent.ExtensionDictionary();
}
if (!dictId.Contains("MyDict"))
dictId.SetAt("MyDict", new DBDictionary());
}
}
// Adds a custom dictionary in the extension dictionary of
// selected objects, but this time using dynamic capabilities
// with LINQ. Conceptually simpler, but with some performance
// overhead, as we're using two queries: one to get entities
// without extension dictionaries (and then add them) and the
// other to get entities with extension dictionaries.
[CommandMethod("AddMyDict2")]
public void AddMyDict2()
{
PromptSelectionResult res =
Application.DocumentManager.MdiActiveDocument.Editor.
GetSelection();
if (res.Status != PromptStatus.OK)
return;
// Query for ents in selset without ExtensionDictionaries
var noExtDicts =
from ent in res.Value.GetObjectIds().Cast<dynamic>()
where ent.ExtensionDictionary == ObjectId.Null
select ent;
// Add extension dictionaries
foreach (dynamic ent in noExtDicts)
ent.CreateExtensionDictionary();
// Now we've added the ext dicts, we add our dict to each
var noMyDicts =
from ent in res.Value.GetObjectIds().Cast<dynamic>()
where !ent.ExtensionDictionary.Contains("MyDict")
select ent.ExtensionDictionary;
foreach (dynamic dict in noMyDicts)
dict.SetAt("MyDict", new DBDictionary());
}
// Access various bits of information using LINQ
[CommandMethod("IUL")]
public void InfoUsingLINQ()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
dynamic bt = db.BlockTableId;
// Dynamic .NET loop iteration
ed.WriteMessage("\n*** BlockTableRecords in this DWG ***");
foreach (dynamic btr in bt)
ed.WriteMessage("\n" + btr.Name);
// LINQ query - returns startpoints of all lines
ed.WriteMessage(
"\n\n*** StartPoints of Lines in ModelSpace ***"
);
dynamic ms = SymbolUtilityServices.GetBlockModelSpaceId(db);
var lineStartPoints =
from ent in (IEnumerable<dynamic>)ms
where ent.IsKindOf(typeof(Line))
select ent.StartPoint;
foreach (Point3d start in lineStartPoints)
ed.WriteMessage("\n" + start.ToString());
// LINQ query - all entities on layer '0'
ed.WriteMessage("\n\n*** Entities on Layer 0 ***");
var entsOnLayer0 =
from ent in (IEnumerable<dynamic>)ms
where ent.Layer == "0"
select ent;
foreach (dynamic e in entsOnLayer0)
ed.WriteMessage(
"\nHandle=" + e.Handle.ToString() + ", ObjectId=" +
((ObjectId)e).ToString() + ", Class=" + e.ToString()
);
ed.WriteMessage("\n\n");
// Using LINQ with selection sets
PromptSelectionResult res = ed.GetSelection();
if (res.Status != PromptStatus.OK)
return;
// Select all entities in selection set that have an object
// called "MyDict" in their extension dictionary
var extDicts =
from ent in res.Value.GetObjectIds().Cast<dynamic>()
where ent.ExtensionDictionary != ObjectId.Null &&
ent.ExtensionDictionary.Contains("MyDict")
select ent.ExtensionDictionary.Item("MyDict");
// Erase our dictionary
foreach (dynamic myDict in extDicts)
myDict.Erase();
}
}
}