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 = "所有图层";
        }
        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");
        }
    }
}

 

编此程序的主要目的是,我要几张CAD图里图元的坐标 作为原始数据或者在没有原始数据的情况下,获取图上指定图元的坐标方法进行补图成图。

C# for AUTOCAD ActiveX获取图形对象坐标程序的更多相关文章

  1. C#/AutoCAD 2018/ObjectArx/二次开发添加图形对象步骤和添加直线的例子(三)

    1.创建一个图形对象的步骤如下(1)得到创建对象的图形数据库:(2)在内存中创建实体类的一个对象:(3)定义一个指向当前数据库的事务处理:(4)打开图形数据库的块表:(5)打开一个存储实体的块表记录( ...

  2. AutoCAD.NET中添加图形对象的基本步骤与实例演示

    https://blog.csdn.net/u011170962/article/details/37755201 要创建一个图形对象,需要遵循下面的步骤:1.得到创建对象的图形数据库:2.在内存中创 ...

  3. 基于.NET的CAD二次开发学习笔记二:AutoCAD .NET中的对象

    1.CAD对象:一个CAD文件(DWG文件)即对应一个数据库,数据库中的所有组成部分,看的见(包括点.多段线.文字.圆等)和看不见(图层.线型.颜色等)的都是对象,数据库本身也是一个对象. 直线.圆弧 ...

  4. 图形对象函数figure() 及 子图创建函数subplot()

    1 图像对象创建函数figure 创建图形Creates a new figure, 图形名既可以作为显示在图形窗口标题栏中的文本,也是该对象的名称 也可以通过mp.figure()获取(或激活)已创 ...

  5. matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象

    一起来学matlab-matlab学习笔记9 高级绘图命令_1 图形对象_根对象,轴对象,用户控制对象,用户菜单对象 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matl ...

  6. js鼠标及对象坐标控制属性详细解析

    对js鼠标及对象坐标控制属性进行了详细的分析介绍.  offsetTop获取对象相对于版面或由 offsetParent 属性指定的父坐标的计算顶端位置. offsetLeft获取对象相对于版面或由 ...

  7. jquery获取当前元素坐标

    1. jquery获取当前元素坐标 A) 获取对象

  8. JavaScript 获取 Div 的坐标

    示例代码: <html> <head> <script> function CPos(x, y) { this.x = x; this.y = y; } /** * ...

  9. 利用servlet产生随机数,原理是获取Graphics对象进行绘图

    public class ResonpeRandomImgDemo extends HttpServlet { int width=100; int height=30; public void do ...

随机推荐

  1. 数据库设置表的check约束出现乱码

    采用默认的方式见了一个数据库,但是有个表里需要建一个check约束.将约束保存之后再打开看到中文成了??.后来查了一下是数据库排序规则除了问题. 详见两图即可明白: 这里的约束中文显示乱码: 按下图设 ...

  2. Discuz资料整理

    1.截取字符串:messagecutstr(strip_tags($post['message']), 160);

  3. Ubunut 13.04下配置memcached、 python MySQLDB,python-memcache模块等

    一开始系统使用的是163的源,没有安装成功memcached,换了cn99的也不行,后来换了台湾的源,以下步骤才得以顺利进行. 更换源的方法可以参看我以前的帖子. 安装memached:sudo ap ...

  4. Android之指南针(电子罗盘)学习

    点我下载源码 5月12日更新到V5版:http://download.csdn.net/detail/weidi1989/5364243 今天,在小米的开源项目中下载了一个指南针源码学习了一下,感觉不 ...

  5. Java中万恶的注解

    本文由码农网 – 孙腾浩原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 当Java 1.5引入注解,企业开发者对简化EJB和其他企业产品开发抱有很大期望.可以看一看同一时期的一篇文章 ...

  6. PT100测温函数

    PT100电阻值计算过程如下: 理论电压关系为:V3-V1=11(V2-V1).由于电阻等的误差原因,采用实际测量求平均值的方法得出实际放大倍数. 放大电路测量几组数据如下:其中V3-V1=Av(V2 ...

  7. android模拟器访问localhost或127.0.0.1报错

    在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正 ...

  8. C#程序实现动态调用DLL的研究

    摘 要:在<csdn开发高手>2004年第03期中的<化功大法——将DLL嵌入EXE>一文,介绍了如何把一个动态链接库作为一个资源嵌入到可执行文件,在可执行文件运行时,自动从资 ...

  9. iOS动画详解(一)

    Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的绘图.变换.颜色管理.脱屏渲 ...

  10. hadoop安装与WordCount例子

    1.JDK安装 下载网址: http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u29-download-513648.html  ...