想通过另外一个 winform程序发送udp实时新增&修改cad图形位置 最后保存dwg文件 。目前遇到问题:不放线程里,可以在cad上绘制,需要一直等待命令绘制,放在线程里面就报错(试过异步、委托到主线程等方式)。或者可以直接通过修改dwg文件,网上复制了些例子,没成功
[CommandMethod("OpenUI")]
public void OpenUI()
{
Process.Start(User.getInstance.GetProgramPath());
m_udpMgr = new UdpMgr();
m_udpMgr.RecvMsg();
//Thread thr = new Thread(() =>
//{
// Thread.Sleep(1000);
Database db = HostApplicationServices.WorkingDatabase;
db.AddRectToModelSpace(new Point2d(0, 0), new Point2d(100, 100));
db.AddRectToModelSpace(new Point2d(200, 200), new Point2d(100, 100));
db.AddRectToModelSpace(new Point2d(500, 500), new Point2d(1000, 1000));
//});
//thr.IsBackground = true;
//thr.Start();
}
public static ObjectId AddEntityToModelSpace(this Database db, Entity ent)
{
ObjectId entId = ObjectId.Null;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
//打开块表
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForRead);
//打开块表记录
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//添加图形到块表记录
entId = btr.AppendEntity(ent);
//更新数据信息
trans.AddNewlyCreatedDBObject(ent, true);
//提交事务
trans.Commit();
}
return entId;
}
/// <summary>
/// 绘制矩形
/// </summary>
/// <param name="db">图形数据库</param>
/// <param name="point1">第一个点</param>
/// <param name="point2">对角点</param>
/// <returns>ObjectId</returns>
public static ObjectId AddRectToModelSpace(this Database db,Point2d point1,Point2d point2)
{
//申明多段线
Polyline pLine = new Polyline();
//计算矩形4个顶点坐标
Point2d p1 = new Point2d(Math.Min(point1.X, point2.X),Math.Min(point1.Y,point2.Y));
Point2d p2 = new Point2d(Math.Max(point1.X, point2.X),Math.Min(point1.Y,point2.Y));
Point2d p3 = new Point2d(Math.Max(point1.X, point2.X),Math.Max(point1.Y,point2.Y));
Point2d p4 = new Point2d(Math.Min(point1.X, point2.X),Math.Max(point1.Y,point2.Y));
//添加多段线的顶点
pLine.AddVertexAt(0,p1,0,0,0);
pLine.AddVertexAt(0,p2,0,0,0);
pLine.AddVertexAt(0,p3,0,0,0);
pLine.AddVertexAt(0,p4,0,0,0);
//闭合多段线
pLine.Closed = true;
return db.AddEntityToModelSpace(pLine);
}