基本信息
源码名称:C# CAD插件开发实例源码下载
源码大小:1.21M
文件格式:.rar
开发语言:C#
更新时间:2017-07-14
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

0,
项目效果查看:
a:在CAD2006的命令行输入netload,加载Test.dll
b1:在命令行输入helloworld可以看到命令功能
b2:右键可以看到右键菜单,画一个红色的圆
b3:左边工具面板多了一个工具条,有个界面可以输入各种参数来画一个组合图形


1,建一个xindows窗体程序项目,设置输出为类库
2,引用acdbmgd.dll和acmgd.dll
3,引用如下命名空间
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows.ToolPalette;
using Autodesk.AutoCAD.Windows;


4,在Form1上规划界面和后台代码。重点看代码如何实现

5,计划是CAD上增加一个面板按钮,点击按钮就打开Form1来自动画图。
   这里我们加一个用户控件,拖一个按钮,按钮方法写:
Form1 modalForm = new Form1();            Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modalForm);
注意这里仅仅是打开的窗体的方式选择了AutoCAD的方式。  
想建立一整套工具栏,就多拉几个按钮。按4的方法写好每个按钮执行的代码(可以要个Form1这样的界面,也可以不要,自由选择)

6,把这个项目导出还需要一个类来辅助
先看初始化Initialize()和Terminate()方法。这里给CAD加了一个面板工具栏和右键菜单,以及一些命令行

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows.ToolPalette;
using Autodesk.AutoCAD.Windows;
namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        float C;//长
        float K;//宽
        /// <summary> 画CAD图,根据计算出的坐标
        /// 
        /// </summary>
        void drawCAD()
        {
            Point3d p0 = new Point3d(0,0,0);
            Point3d p1 = new Point3d(C,0,0);
            Point3d p2 = new Point3d(C, K, 0);
            Point3d p3 = new Point3d(0, K, 0);
           
            using (DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
            {
                //
                Database db = HostApplicationServices.WorkingDatabase;
                using (Transaction trans = db.TransactionManager.StartTransaction())
                {
                    BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;


                    //画4条直线的矩形
                    List<Line> lins = new List<Line>();
                    lins.Add(new Line(p0, p1));
                    lins.Add(new Line(p1, p2));
                    lins.Add(new Line(p2, p3));
                    lins.Add(new Line(p3, p0));
                    foreach (Line line in lins)
                    {
                        btr.AppendEntity(line);
                        trans.AddNewlyCreatedDBObject(line, true);
                    }


                    //画圆,这里直接用矩形的四个点来画
                    List<Point3d> points = new List<Point3d>();
                    points.Add(p0);
                    points.Add(p1);
                    points.Add(p2);
                    points.Add(p3);
                    List<Circle> cirs = new List<Circle>();
                    foreach (Point3d p3d in points)
                    {
                        int R = 20;
                        cirs.Add(new Circle(p3d, Vector3d.ZAxis, R));
                    }
                    foreach (Circle cir in cirs)
                    {
                        cir.ColorIndex = 1;
                        btr.AppendEntity(cir);
                        trans.AddNewlyCreatedDBObject(cir, true);
                    }

                    trans.Commit();
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.C = Convert.ToInt32(this.textBox1.Text);//int转成float一定可以,所以可以把int的值直接赋给float
            this.K = Convert.ToInt32(this.textBox2.Text);
            this.drawCAD();
            this.Close();
        }
    }
}