Teigha(ODA<Open Design Alliance>_开放设计联盟)——cad c# 二次开发

 需将dll库文件与exe文件放同一路径下,运行exe即可执行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using Teigha.Runtime;

namespace WindowsFormsApp1
{
    public partial class Form1: Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Services svc = new Services())
            {
                string desktopPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
                string fname = Path.Combine(desktopPath, "teigha创建的文件.dwg");
                Database db = new Database();
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    for (int i = 0; i < 200; i++)
                    {
                        Point3d pt1 = new Point3d(i, i + 200, 0);
                        string str = "aaa" + i.ToString();
                        DBText txt = new DBText();
                        txt.Position = pt1;
                        txt.TextString = str;

                        btr.AppendEntity(txt);
                        tr.AddNewlyCreatedDBObject(txt, true);

                    }
                    tr.Commit();
                    db.SaveAs(fname, DwgVersion.AC1027);
                    db.Dispose();
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using (Services svc = new Services())
            {
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string fname = Path.Combine(desktopPath, "teigha创建的文件.dwg");
                Database db = new Database(false, false);
                db.ReadDwgFile(fname, System.IO.FileShare.Read, false, null);
                using (var tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    Circle cir = new Circle();
                    for (global::System.Int32 i = 0; i < 20; i++)
                    {
                        Point3d pt1 = new Point3d(i, i * 2, 0);
                        double rad = 100;

                        cir.Center = pt1;
                        cir.Radius = rad;
                        cir.ColorIndex = i;


                    }
                    btr.AppendEntity(cir);
                    tr.AddNewlyCreatedDBObject(cir, true);
                    tr.Commit();
                    db.Save();
                    db.Dispose();
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            using (Services ser = new Services())
            {
                string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                string fname = Path.Combine(desktopPath, "teigha创建的文件.dwg");
                Database db = new Database(false, false);
                db.ReadDwgFile(fname, System.IO.FileShare.Read, false, null);
                using (var trans = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btrec = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
                    foreach (ObjectId objid in btrec)
                    {
                        Entity ent = trans.GetObject(objid, OpenMode.ForWrite) as Entity;
                        if (ent.GetType().Name == "DBText")
                        {
                            DBText txt = (DBText)ent;
                            if (txt.TextString == "aaa2")
                            {
                                txt.TextString = "teigha";
                            }
                        }
                    }
                    trans.Commit();
                }
                db.Save();
                db.Dispose();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

山水CAD插件定制

你的鼓励是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值