Hi,
As you had been advised, you should familiarize yourself with the .NET environment outside AutoCAD before attempting to use the AutoCAD API.
Anyway, IMO a simple way should be, in a single project (class library) referencing the AutoCAD libraries with a class for the AutoCAD commands, to add a WPF window (right click the
project in the solution explorer > Add > Window...).
So you'd have on one side the dialog described in the Window1.xaml and the code behind in window1.xaml.cs to define the click event handler and, on the other side a class related to AutoCAD where you'd define a command to show the dialog and a static method to draw the line.
The Window1.xaml file
<Window x:Class="WpfDialogSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="200">
<Grid>
<Button Content="Draw Line" Margin="10" Width="100" Height="30" Click="Draw_Click"/>
</Grid>
</Window>
The code behind (Window1.xaml.cs)
using System.Windows;
namespace WpfDialogSample
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Draw_Click(object sender, RoutedEventArgs e)
{
CommandMethods.DrawLine();
this.Close();
}
}
}
The CommandMethod class:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
[assembly
: CommandClass
(typeof(WpfDialogSample
.CommandMethods))]
namespace WpfDialogSample
{
public class CommandMethods
{
// AutoCAD command to display the dialog
[CommandMethod("TEST", CommandFlags.Modal)]
public static void Test()
{
System.Windows.Window win
= new Window1
(); AcAp.ShowModalWindow(win);
}
// draws a line in model space
internal static void DrawLine()
{
Document doc = AcAp.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var mSpace = (BlockTableRecord)tr.GetObject(
SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
var line
= new Line
(Point3d
.Origin,
new Point3d
(10.0,
10.0,
0.0)); mSpace.AppendEntity(line);
tr.AddNewlyCreatedDBObject(line, true);
tr.Commit();
}
}
}
}