using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Runtime.InteropServices;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcCl = Autodesk.AutoCAD.Colors.Color;
[assembly: CommandClass(typeof(AcadTest.Commands))]
namespace AcadTest
{
public class Commands
{
[DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "acedCmd")]
private static extern int acedCmd(System.IntPtr vlist);
[CommandMethod("testCmd")]
public void CmdTest()
{
Document acDoc = AcAp.DocumentManager.MdiActiveDocument;
if (acDoc == null)
return;
Database acCurDb = acDoc.Database;
Editor ed = acDoc.Editor;
using (DocumentLock docLock = acDoc.LockDocument())
{
AcCl CurrentColor = acCurDb.Cecolor;
acCurDb.Cecolor = Color.FromColorIndex(ColorMethod.None, 30);
LineWeight CurrentLWHT = acCurDb.Celweight;
acCurDb.Celweight = LineWeight.LineWeight100;
ObjectId CurrentLayerID = acCurDb.Clayer;
ObjectId newLayer = new ObjectId();
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
LinetypeTable acLineTypTbl = acTrans.GetObject(acCurDb.LinetypeTableId, OpenMode.ForWrite) as LinetypeTable;
LayerTable acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable;
if (!acLyrTbl.Has("New Layer"))
{
LayerTableRecord acLyrTblRec = new LayerTableRecord();
acLyrTblRec.Name = "New Layer";
acLyrTbl.UpgradeOpen();
acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec, true);
ed.WriteMessage("\n'New Layer' created");
newLayer = acLyrTblRec.ObjectId;
}
else
newLayer = acLyrTbl["New Layer"];
acTrans.Commit();
}
acCurDb.Clayer = newLayer;
ResultBuffer rb = new ResultBuffer();
rb.Add(new TypedValue(5005, "_.INSERT"));
rb.Add(new TypedValue(5005, "PICKET"));
rb.Add(new TypedValue(5005, "Scale"));
rb.Add(new TypedValue(5005, "1"));
acedCmd(rb.UnmanagedObject);
short echo = (short)AcAp.GetSystemVariable("CMDECHO");
if (echo == 0)
AcAp.SetSystemVariable("CMDECHO", 1);
bool quit = false;
while (!quit)
{
string cmdNames = (string)AcAp.GetSystemVariable("CMDNAMES");
if (cmdNames.ToUpper().IndexOf("INSERT") >= 0)
{
rb = new ResultBuffer();
rb.Add(new TypedValue(5005, "\\"));
acedCmd(rb.UnmanagedObject);
}
else
quit = true;
}
AcAp.SetSystemVariable("CMDECHO", echo);
acCurDb.Cecolor = CurrentColor;
acCurDb.Clayer = CurrentLayerID;
acCurDb.Celweight = CurrentLWHT;
}
}
}
}