using System;
using Autodesk.AutoCAD.Runtime;
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace WPFexample
{
public class Class1
{
private static ModlessWindow _modlessWindow;
[CommandMethod("TestModless")]
public static void SecondExample()
{
if (_modlessWindow == null)
{
_modlessWindow = new ModlessWindow();
_modlessWindow.Closed += win_Closed;
}
if (_modlessWindow.IsLoaded)
_modlessWindow.Activate();
else
AcApp.ShowModelessWindow(AcApp.MainWindow.Handle, _modlessWindow, false);
}
static void win_Closed(object sender, EventArgs e)
{
_modlessWindow = null;
}
}
}
using System.Windows;
using System.Linq;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows.Data;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Windows.Input;
using System;
namespace WPFexample
{
/// <summary>
/// Логика взаимодействия для ModlessWindow.xaml
/// </summary>
public partial class ModlessWindow
{
DataItemCollection my_layers;
public ModlessWindow()
{
InitializeComponent();
my_layers = AcAp.UIBindings.Collections.Layers;
//привязка данных слоя к ComboBox
BindData();
// обновление ComboBox при изменении набора данных слоев
my_layers.CollectionChanged += (s, e) => BindData();
}
private void BtAccept_OnClick(object sender, RoutedEventArgs e)
{
{
double my_radius;
double.TryParse(TextBox.Text, out my_radius);
var doc = AcAp.DocumentManager.MdiActiveDocument;
if (doc != null)
{
// установить фокус на редактор AutoCAD
AcAp.MainWindow.Focus();
using (doc.LockDocument())
{
DrawCircle(doc, my_radius, (string)cbxLayer.SelectedItem);
}
}
}
}
private void DrawCircle(Document doc, double radius, string layer)
{
var db = doc.Database;
var ed = doc.Editor;
var ppr = ed.GetPoint("\nУкажите центр: ");
if (ppr.Status == PromptStatus.OK)
{
using (var tr = db.TransactionManager.StartTransaction())
{
var curSpace =
(BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
using (var circle = new Circle(ppr.Value, Vector3d.ZAxis, radius))
{
circle.TransformBy(ed.CurrentUserCoordinateSystem);
circle.Layer = layer;
curSpace.AppendEntity(circle);
tr.AddNewlyCreatedDBObject(circle, true);
}
tr.Commit();
}
}
}
private void BindData()
{
// привязка к источнику данных
// DataItemCollection преобразуется в массив имен слоев
cbxLayer.ItemsSource = my_layers.Select(x => ((INamedValue)x).Name).ToArray();
// выбор текущего слоя
cbxLayer.SelectedItem = ((INamedValue)my_layers.CurrentItem)?.Name;
}
private void BtCancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
}
private void textBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!(Char.IsDigit(e.Text, 0) || (e.Text == ".")
&& (!TextBox.Text.Contains(".")
&& TextBox.Text.Length != 0)))
{
e.Handled = true;
}
}
}