C# for AUTOCAD ActiveX获取图形对象坐标程序
C# for AUTOCAD ActiveX获取图形对象坐标程序
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
namespace CAD获取图形对象坐标
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AcadApplication CadApp;
AcadDocument CadDoc;
AcadModelSpace CadMspace;
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "所有文件*.*|*.*|CAD文件|*.dwg";
open.FilterIndex = 2;
DialogResult diaresult = open.ShowDialog();
if (diaresult == DialogResult.OK)
{
textBox1.Text = open.FileName;
if (System.IO.Path.GetExtension(textBox1.Text) != ".dwg")
{
MessageBox.Show("这不是一个CAD图形文件!");
return;
}
panel1.Enabled = true;
button1.Enabled = true;
CadApp = new AcadApplication();
CadDoc = CadApp.Documents.Open(textBox1.Text);
CadMspace = CadDoc.ModelSpace;
CadApp.Visible = true;
comboBox1.Items.Clear();
comboBox1.Items.Add("所有图层");
foreach (AcadLayer layer in CadDoc.Layers)
{
comboBox1.Items.Add(layer.Name);
}
comboBox1.Text = "所有图层";
}
}
private void button3_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
comboBox1.Items.Add("所有图层");
foreach (AcadLayer layer in CadDoc.Layers)
{
comboBox1.Items.Add(layer.Name);
}
comboBox1.Text = "所有图层";
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
namespace CAD获取图形对象坐标
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
AcadApplication CadApp;
AcadDocument CadDoc;
AcadModelSpace CadMspace;
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "所有文件*.*|*.*|CAD文件|*.dwg";
open.FilterIndex = 2;
DialogResult diaresult = open.ShowDialog();
if (diaresult == DialogResult.OK)
{
textBox1.Text = open.FileName;
if (System.IO.Path.GetExtension(textBox1.Text) != ".dwg")
{
MessageBox.Show("这不是一个CAD图形文件!");
return;
}
panel1.Enabled = true;
button1.Enabled = true;
CadApp = new AcadApplication();
CadDoc = CadApp.Documents.Open(textBox1.Text);
CadMspace = CadDoc.ModelSpace;
CadApp.Visible = true;
comboBox1.Items.Clear();
comboBox1.Items.Add("所有图层");
foreach (AcadLayer layer in CadDoc.Layers)
{
comboBox1.Items.Add(layer.Name);
}
comboBox1.Text = "所有图层";
}
}
private void button3_Click(object sender, EventArgs e)
{
comboBox1.Items.Clear();
comboBox1.Items.Add("所有图层");
foreach (AcadLayer layer in CadDoc.Layers)
{
comboBox1.Items.Add(layer.Name);
}
comboBox1.Text = "所有图层";
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Clear();
AcadSelectionSet MyCadSelect = CadDoc.SelectionSets.Add("MySelect");
Int16[] FilterType = new Int16[1];
object[] FilterDate = new object[1];
if (comboBox1.Text == "所有图层")
{
FilterType[0] = 0;
FilterDate[0] = "*";
}
else
{
FilterType[0] = 8;
FilterDate[0] = comboBox1.Text;
}
if (radioButton1.Checked == true)
{
MyCadSelect.SelectOnScreen(FilterType, FilterDate);
}
else
{
double[] point01 = new double[3];
double[] point02 = new double[3];
point01[0] = 0; point01[1] = 0; point01[2] = 0;
point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
MyCadSelect.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType, FilterDate);
}
double[] d;
for (int i = 0; i < MyCadSelect.Count; i++)
{
if (MyCadSelect.Item(i).ObjectName == "AcDbLine" && checkBox2.Checked == true)
{
AcadLine line;
line = (AcadLine)MyCadSelect.Item(i);
d =(double[]) line.StartPoint;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
d = (double[])line.EndPoint;
str += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbPoint" && checkBox1.Checked == true)
{
AcadPoint point;
point = (AcadPoint)MyCadSelect.Item(i);
d = (double[])point.Coordinates;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbPolyline" && checkBox3.Checked == true)
{
AcadLWPolyline poly;
poly = (AcadLWPolyline)MyCadSelect.Item(i);
d = (double[])poly.Coordinates;
for (int j = 0; j < d.Length - 1; j = j + 2)
{
textBox2.Text += "\n\r" + "X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " +poly.Elevation.ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDb3dPolyline" && checkBox8.Checked == true)
{
Acad3DPolyline poly;
poly = (Acad3DPolyline)MyCadSelect.Item(i);
d = (double[])poly.Coordinates;
for (int j = 0; j < d.Length - 2; j = j + 3)
{
textBox2.Text += "\n\r" + "X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " + d[j + 2].ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbCircle" && checkBox4.Checked == true)
{
AcadCircle circle;
circle = (AcadCircle)MyCadSelect.Item(i);
d = (double[])circle.Center;
textBox2.Text += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbArc" && checkBox5.Checked == true)
{
AcadArc arc;
arc = (AcadArc)MyCadSelect.Item(i);
d = (double[])arc.StartPoint;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
d = (double[])arc.EndPoint;
str += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbSpline" && checkBox6.Checked == true)
{
AcadSpline spline = (AcadSpline)MyCadSelect.Item(i);
d = (double[])spline.ControlPoints;
for (int j = 0; j < d.Length - 2; j = j + 3)
{
textBox2.Text += "\n\r" + " X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " + d[j + 2].ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbBlockReference" && checkBox7.Checked == true)
{
AcadBlockReference block = (AcadBlockReference)MyCadSelect.Item(i);
d = (double[])block.InsertionPoint;
textBox2.Text += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
}
}
MessageBox.Show("共处理完成" + MyCadSelect.Count + "个对象");
CadDoc.SelectionSets.Item("MySelect").Delete();
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:深海,QQ454138882");
}
}
}
{
textBox2.Clear();
AcadSelectionSet MyCadSelect = CadDoc.SelectionSets.Add("MySelect");
Int16[] FilterType = new Int16[1];
object[] FilterDate = new object[1];
if (comboBox1.Text == "所有图层")
{
FilterType[0] = 0;
FilterDate[0] = "*";
}
else
{
FilterType[0] = 8;
FilterDate[0] = comboBox1.Text;
}
if (radioButton1.Checked == true)
{
MyCadSelect.SelectOnScreen(FilterType, FilterDate);
}
else
{
double[] point01 = new double[3];
double[] point02 = new double[3];
point01[0] = 0; point01[1] = 0; point01[2] = 0;
point02[0] = 1000; point02[1] = 1000; point02[2] = 0;
MyCadSelect.Select(AcSelect.acSelectionSetAll, point01, point02, FilterType, FilterDate);
}
double[] d;
for (int i = 0; i < MyCadSelect.Count; i++)
{
if (MyCadSelect.Item(i).ObjectName == "AcDbLine" && checkBox2.Checked == true)
{
AcadLine line;
line = (AcadLine)MyCadSelect.Item(i);
d =(double[]) line.StartPoint;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
d = (double[])line.EndPoint;
str += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbPoint" && checkBox1.Checked == true)
{
AcadPoint point;
point = (AcadPoint)MyCadSelect.Item(i);
d = (double[])point.Coordinates;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbPolyline" && checkBox3.Checked == true)
{
AcadLWPolyline poly;
poly = (AcadLWPolyline)MyCadSelect.Item(i);
d = (double[])poly.Coordinates;
for (int j = 0; j < d.Length - 1; j = j + 2)
{
textBox2.Text += "\n\r" + "X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " +poly.Elevation.ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDb3dPolyline" && checkBox8.Checked == true)
{
Acad3DPolyline poly;
poly = (Acad3DPolyline)MyCadSelect.Item(i);
d = (double[])poly.Coordinates;
for (int j = 0; j < d.Length - 2; j = j + 3)
{
textBox2.Text += "\n\r" + "X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " + d[j + 2].ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbCircle" && checkBox4.Checked == true)
{
AcadCircle circle;
circle = (AcadCircle)MyCadSelect.Item(i);
d = (double[])circle.Center;
textBox2.Text += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbArc" && checkBox5.Checked == true)
{
AcadArc arc;
arc = (AcadArc)MyCadSelect.Item(i);
d = (double[])arc.StartPoint;
string str = "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
d = (double[])arc.EndPoint;
str += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
textBox2.Text += str;
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbSpline" && checkBox6.Checked == true)
{
AcadSpline spline = (AcadSpline)MyCadSelect.Item(i);
d = (double[])spline.ControlPoints;
for (int j = 0; j < d.Length - 2; j = j + 3)
{
textBox2.Text += "\n\r" + " X= " + d[j].ToString() + " Y= " + d[j + 1].ToString() + " Z= " + d[j + 2].ToString() + "\n\r";
}
}
else if (MyCadSelect.Item(i).ObjectName == "AcDbBlockReference" && checkBox7.Checked == true)
{
AcadBlockReference block = (AcadBlockReference)MyCadSelect.Item(i);
d = (double[])block.InsertionPoint;
textBox2.Text += "\n\r" + "X= " + d[0] + " Y= " + d[1] + " Z= " + d[2] + "\n\r";
}
}
MessageBox.Show("共处理完成" + MyCadSelect.Count + "个对象");
CadDoc.SelectionSets.Item("MySelect").Delete();
}
private void button5_Click(object sender, EventArgs e)
{
MessageBox.Show("作者:深海,QQ454138882");
}
}
}