using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Windows.Input;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
namespace WpfDialogSample
{
public class AcadCommand
{
// bound properties
public string[] Strings
{
get { return new[] { "aa",
"bb",
"cc",
"dd" }; } }
public int Index { get; set; }
public ICommand InsertCommand
{
get
{
SimpleCommand cmd
= new SimpleCommand
(); cmd.ExecuteDelegate = delegate (object x) { Insert(); };
return cmd;
}
}
// AutoCAD command
[CommandMethod("TEST")]
public void Test()
{
Dialog dlg
= new Dialog
(); AcAp.ShowModalWindow(dlg);
}
// action executed by InsertCommand
private void Insert()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
PromptPointResult result = ed.GetPoint("\nInsertion point: ");
if (result.Status != PromptStatus.OK)
return;
Point3d pt = result.Value.TransformBy(ed.CurrentUserCoordinateSystem);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord curSpace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
DBText text
= new DBText
(); text.TextString = Strings[Index];
text.Position = pt;
curSpace.AppendEntity(text);
tr.AddNewlyCreatedDBObject(text, true);
tr.Commit();
}
}
}
// Simplified ICommand implementation
// https://marlongrech.wordpress.com/2008/11/26/avoiding-commandbinding-in-the-xaml-code-behind-files/
public class SimpleCommand : ICommand
{
public Predicate<object> CanExecuteDelegate { get; set; }
public Action<object> ExecuteDelegate { get; set; }
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter)
{
if (CanExecuteDelegate != null)
return CanExecuteDelegate(parameter);
return true;
}
public void Execute(object parameter)
{
if (ExecuteDelegate != null)
ExecuteDelegate(parameter);
}
}
}