创建图形对象

来源:互联网 发布:数据库设计有几个阶段 编辑:程序博客网 时间:2024/05/16 06:53

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;

[assembly:CommandClass(typeof(Liness.Lines))]
namespace Liness
{
    class Lines
    {
        [CommandMethod("addline")]
        public void AddLine()
        {
            //获取当前的 document 和 database
            Document DocObj = Application.DocumentManager.MdiActiveDocument;
            Database DBObj = DocObj.Database;

            //启动一个事务
            using (Transaction TransObj = DBObj.TransactionManager.StartTransaction())
            {
                //Open the BlockTable for Read
                BlockTable BTObj = TransObj.GetObject(DBObj.BlockTableId, OpenMode.ForRead) as BlockTable;

                //Open the BlockTableRecord Model space for write
                BlockTableRecord BTRObj = TransObj.GetObject(BTObj[BlockTableRecord.ModelSpace],OpenMode.ForWrite)as BlockTableRecord;

                //Create a line that starts at (100,100) and ends at (800,800)
                Line acline = new Line(new Point3d(100, 100, 0), new Point3d(800, 800, 800));
                acline.SetDatabaseDefaults();

                //Add the new object to the block table record and the transaction
                BTRObj.AppendEntity(acline);
                TransObj.AddNewlyCreatedDBObject(acline, true);

                //Save the new Object to the database
                TransObj.Commit();
            }
        }

        [CommandMethod("ployline")]
        public void AddMultithreading()
        {
            //Get the current document and database
            Document DocObj = Application.DocumentManager.MdiActiveDocument;
            Database DBObj = DocObj.Database;

            //start a transaction
            using (Transaction TransObj = DBObj.TransactionManager.StartTransaction())
            {
                //Open the BlockTable for read
                BlockTable BTObj = TransObj.GetObject(DBObj.BlockTableId, OpenMode.ForRead) as BlockTable;

                //Open the BlockTableRecord Model space for write
                BlockTableRecord BTRObj = TransObj.GetObject(BTObj[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                //Create a ployline with two segments(3 points)
                Polyline Pline = new Polyline();
                Pline.SetDatabaseDefaults();
                Pline.AddVertexAt(0, new Point2d(200, 400), 0, 0, 0);
                Pline.AddVertexAt(1, new Point2d(400, 200), 0, 0, 0);
                Pline.AddVertexAt(2, new Point2d(600, 400), 0, 0, 0);

                //Add the new object to the BlockTableRecord and the transaction
                BTRObj.AppendEntity(Pline);
                TransObj.AddNewlyCreatedDBObject(Pline, true);

                //Save the new object to the database
                TransObj.Commit();
            }
        }
    }
}

原创粉丝点击