需将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();
}
}
}
}