public partial class PolylineWindow : System.Windows.Window
{
PolylineModel data;
public PolylineWindow(PolylineModel polylineModel)
{
InitializeComponent();
data = polylineModel;
this.DataContext = data;
}
private void BtnColor_Click(object sender, RoutedEventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
var result = colorDialog.ShowModal();
if (result != true) return;
var acColor = colorDialog.Color;
byte byt = Convert.ToByte(acColor.ColorIndex);
int rgb = Autodesk.AutoCAD.Colors.EntityColor.LookUpRgb(byt);
byte b = Convert.ToByte((rgb & 0xffL));
byte g = Convert.ToByte((rgb & 0xff00L) >> 8);
byte r = Convert.ToByte(rgb >> 16);
data.Color = (r, g, b);
Color color = Color.FromRgb(r, g, b);
rectColor.Fill = new SolidColorBrush(color);
}
private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
{
var fileDialog = new Microsoft.Win32.OpenFileDialog();
fileDialog.Filter = "Text files (*.txt)|*.txt|All files(*.*)|*.*";
var result = fileDialog.ShowDialog();
if (result != true) return;
string filePath = fileDialog.FileName;
string[] coord;
using (StreamReader sr = new StreamReader(filePath))
{
coord = sr.ReadToEnd().Split('\n', ' ').Select(s => s.Trim("\n \r".ToArray())).ToArray();
}
for (int i = 0; i < coord.Count(); i += 2)
{
data.Coords.Add(new PolylineModel.Coord
{
X = Convert.ToDouble(coord[i]),
Y = Convert.ToDouble(coord[i + 1])
});
}
if (data.IsValid)
btnOK.IsEnabled = true;
if (coord.Count() % 2 != 0)
data.Coords.RemoveAt(data.Coords.Count - 1);
}
private void BtnMouseCoord_Click(object sender, RoutedEventArgs e)
{
Hide();
//var window = Autodesk.AutoCAD.ApplicationServices.Application.DocumentWindowCollection.ActiveDocumentWindow;
//window.Visible = false;
var acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
PromptPointResult pPtRes;
PromptPointOptions pPtOpts = new PromptPointOptions("");
pPtRes = acDoc.Editor.GetPoint(pPtOpts);
if (pPtRes.Status != PromptStatus.OK)
return;
Point3d plStart = pPtRes.Value;
data.Coords[0].X = plStart.X;
data.Coords[0].Y = plStart.Y;
textX.Text = $"{plStart.X}"; // элемент окна
textY.Text = $"{plStart.Y}"; // элемент окна
Show();
//window.Visible = true;
}
private void BtnOK_Click(object sender, RoutedEventArgs e)
{
if (!data.IsValid)
return;
var acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
var acCurDb = acDoc.Database;
using (var acTrans = acCurDb.TransactionManager.StartTransaction())
{
var acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
var acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
using (var acPoly = new Polyline2d())
{
for (int i = 1; i < data.Coords.Count; i++)
{
acPoly.NonDBAppendVertex(new Vertex2d(
new Point3d(data.Coords[0].X + data.Coords[i].X, data.Coords[0].Y + data.Coords[i].Y, 0), 0, 0, 0, 0
));
}
acPoly.Thickness = data.Thickness;
acPoly.Closed = data.IsClose;
if (data.IsSmoothing)
acPoly.ConvertToPolyType(Poly2dType.FitCurvePoly);
acPoly.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(data.Color.R, data.Color.G, data.Color.B);
acBlkTblRec.AppendEntity(acPoly);
acTrans.AddNewlyCreatedDBObject(acPoly, true);
}
acTrans.Commit();
}
DialogResult = true;
Close();
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
}