Code:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.Windows.Forms;
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in12.MyCommands))]
namespace AutoCAD_CSharp_plug_in12
{
public class MyCommands
{
public class MyMessageFilter : IMessageFilter
{
public const int WM_KEYDOWN = 256;
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_KEYDOWN) // WM_KEYDOWN
{
Document doc = AcApp.DocumentManager.MdiActiveDocument;
if ((int)m.WParam == 76) // "L" = 76
{
doc.SendStringToExecute("L" + "\x0d", true, false, true); // "L" + ENTER
return true;
}
else
{
if ((int)m.WParam == 27) // "ESC" = 27
{
doc.SendStringToExecute("\x03", true, false, true);//Escape
return true;
}
else
{
doc.SendStringToExecute("\x08", true, false, true);//Zeichen löschen
return true;
}
}
}
return false;
}
}
[CommandMethod("q1", CommandFlags.UsePickSet | CommandFlags.Redraw | CommandFlags.Modal)]
public void q1()
{
Document doc = AcApp.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
// Setze neue Systemvariablen
int PICKADDVar = System.Convert.ToInt32(AcApp.GetSystemVariable("PICKADD"));
int PICKAUTOVar = System.Convert.ToInt32(AcApp.GetSystemVariable("PICKAUTO"));
int CMDECHOVar = System.Convert.ToInt32(AcApp.GetSystemVariable("CMDECHO"));
// int OSMODEVar = System.Convert.ToInt32(Application.GetSystemVariable("OSMODE"));
int DBLCLKEDITVar = System.Convert.ToInt32(AcApp.GetSystemVariable("DBLCLKEDIT"));
// Setze neue Systemvariablen
AcApp.SetSystemVariable("PICKADD", 0);
AcApp.SetSystemVariable("PICKAUTO", 0);
AcApp.SetSystemVariable("CMDECHO", 0);
AcApp.SetSystemVariable("DBLCLKEDIT", 0);
PromptNestedEntityResult Auswahl = null;
bool EignerAbbruch = false;
MyMessageFilter MessageFilter = new MyMessageFilter();
System.Windows.Forms.Application.AddMessageFilter(MessageFilter);
ed.SetImpliedSelection(new ObjectId[0]); //Aktive Auswahl löschen
PromptNestedEntityOptions pneo = new PromptNestedEntityOptions("\nWähle ein Attribut oder Auswahl : ");
pneo.Keywords.Add("Löschen");
while (true)
{
Auswahl = ed.GetNestedEntity(pneo);
if (Auswahl.Status == PromptStatus.Keyword && Auswahl.StringResult == "Löschen")
{
PromptSelectionResult selectionRes = ed.SelectImplied();
if (selectionRes.Status == PromptStatus.OK)
{
doc.LockDocument();// <----- Wichtig zum Löschen !!!
using (Transaction acTrans = doc.TransactionManager.StartTransaction())
{
foreach (ObjectId teil in selectionRes.Value.GetObjectIds())
{
Entity asdsad = (Entity)acTrans.GetObject(teil, OpenMode.ForWrite);
asdsad.Erase();
}
acTrans.Commit();
}
ed.Regen();
doc.SendStringToExecute("\x03", true, false, true);//Escape
EignerAbbruch = true;
}
}
if (Auswahl.Status == PromptStatus.OK && Auswahl.ObjectId.ObjectClass.DxfName == "ATTRIB")
{
using (Transaction acTrans = doc.TransactionManager.StartTransaction())
{
AttributeReference acBlkTbl;
acBlkTbl = acTrans.GetObject(Auswahl.ObjectId, OpenMode.ForRead) as AttributeReference;
string AttributSuchString = acBlkTbl.TextString;
TypedValue[] ObjektFilter = { new TypedValue(0, "INSERT"), new TypedValue(410, AcApp.GetSystemVariable("CTAB").ToString()), new TypedValue(66, 1) };
PromptSelectionResult BlöckeMitAttributen = ed.SelectAll(new SelectionFilter(ObjektFilter));
List<ObjectId> Blocksammlung = new List<ObjectId>();
if (BlöckeMitAttributen.Status == PromptStatus.OK)
{
foreach (ObjectId BlockID in BlöckeMitAttributen.Value.GetObjectIds())
{
BlockReference BlockRefID = (BlockReference)acTrans.GetObject(BlockID, OpenMode.ForRead);
foreach (ObjectId AttributeId in BlockRefID.AttributeCollection)
{
AttributeReference AttributWerte = (AttributeReference)acTrans.GetObject(AttributeId, OpenMode.ForRead);
if (AttributWerte.TextString == AttributSuchString)
{
Blocksammlung.Add(BlockID);
}
}
}
ed.SetImpliedSelection(Blocksammlung.ToArray());
}
}
doc.SendStringToExecute("\x03", true, false, true);//Escape
EignerAbbruch = true;
}
// //
if (Auswahl.Status == PromptStatus.Cancel | | Auswahl.Status == PromptStatus.Error)
{
if (EignerAbbruch == true)
{
EignerAbbruch = false;
Auswahl = null;
}
else
{
System.Windows.Forms.Application.RemoveMessageFilter(MessageFilter);
AcApp.SetSystemVariable("PICKADD", PICKADDVar);
AcApp.SetSystemVariable("PICKAUTO", PICKAUTOVar);
AcApp.SetSystemVariable("CMDECHO", CMDECHOVar);
AcApp.SetSystemVariable("DBLCLKEDIT", DBLCLKEDITVar);
break;
}
}
}
}
}
}