002集—— CAD划线并模拟向命令窗口发送命令(CAD—C#二次开发入门)
模拟向命令窗口发送全图居中的命令: Application.DocumentManager.MdiActiveDocument.SendStringToExecute("z\ne\n",true,false,false);
弹窗命令:
Application.ShowAlertDialog("Erase the newly added polyline.");
本例在cad画一条线并全图显示:
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary4
{public class Class1{[CommandMethod("xx")]public static void AngleFromXAxis(){//// 获得当前文档和数据库 Get the current document and databaseDocument acDoc = Application.DocumentManager.MdiActiveDocument;Database acCurDb = acDoc.Database;// 启动一个事务 Start a transactionusing (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()){// 以只读方式打开块表 Open the Block table for readBlockTable acBlkTbl;acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;// 以写方式打开模型空间块表记录 Open the Block table record Model space for writeBlockTableRecord acBlkTblRec;acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite) as BlockTableRecord;// 创建一条轻量多段线 Create a lightweight polylinePolyline acPoly = new Polyline();acPoly.SetDatabaseDefaults();acPoly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);acPoly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);acPoly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);// 添加新对象到块表记录和事务中 Add the new object to the block table record and the transactionacBlkTblRec.AppendEntity(acPoly);acTrans.AddNewlyCreatedDBObject(acPoly, true);// 更新显示并显示一条提醒消息 Update the display and display an alert messageacDoc.Editor.Regen();//acDoc.Editor.WriteMessage("z\n");//acDoc.Editor.Regen();//acDoc.Editor.WriteMessage("e\n");Application.DocumentManager.MdiActiveDocument.SendStringToExecute("z\ne\n",true,false,false); acDoc.Editor.Regen();Application.DocumentManager.MdiActiveDocument.SendStringToExecute("regen", true, false, false);Application.ShowAlertDialog("pl线已画好");// 从图形中删除多段线 Erase the polyline from the drawing//acPoly.Erase(true);// Application.ShowAlertDialog("pl线已删除");// 保存新对象到数据库中 Save the new object to the databaseacTrans.Commit();}////}}
}