//根據名稱獲取圖層ObjectId,若不存在就創建該圖層
public static ObjectId GetLayerObjectId(this Database db, string layerName)
{
LayerTable lt = (LayerTable)db.LayerTableId.GetObject(OpenMode.ForRead);
if (!lt.Has(layerName))
{
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = layerName;
lt.UpgradeOpen();
lt.Add(ltr);
db.TransactionManager.AddNewlyCreatedDBObject(ltr, true);
lt.DowngradeOpen();
}
return lt[layerName];
}
[CommandMethod("PostInActiveLayer")]
public void PostInActiveLayer()
{
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
try
{
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForAdding = "\n請選擇實體";
PromptSelectionResult psr = ed.GetSelection(pso);
if (psr.Status == PromptStatus.OK)
{
SelectionSet ss = psr.Value;
ObjectId[] objids = ss.GetObjectIds();
ObjectIdCollection objidclt = new ObjectIdCollection(objids);
Group grp = new Group();
grp.Append(objidclt);
grp.SetLayer(db.GetLayerObjectId("目標圖層名字"));
//將對象移動到當前圖層
//grp.SetLayer(db.Clayer);
}
}
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.ToString());
ed.WriteMessage(ex.ToString());
}
}