本帖转载于:AutoCAD.Net圆弧半径标注延长线 - 程序园 (voidcn.com)(略有改动)
#region 注册RegApp
public static void CheckRegApp(string regapptablename)
{
Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
RegAppTable appTbl = trans.GetObject(db.RegAppTableId, OpenMode.ForWrite) as RegAppTable;
if (!appTbl.Has(regapptablename))
{
RegAppTableRecord appTblRcd = new RegAppTableRecord();
appTblRcd.Name = regapptablename;
appTbl.Add(appTblRcd);
trans.AddNewlyCreatedDBObject(appTblRcd, true);
}
trans.Commit();
}
return;
}
#endregion
[CommandMethod("mydra")]
public static void mydra()
{
// 获取当前数据库
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction启动事务
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read以读模式打开块表
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
// 以写模式打开块表记录ModelSpace
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite) as BlockTableRecord;
// Create the radial dimension创建半径标注
RadialDimension acRadDim = new RadialDimension();
acRadDim.Center = new Point3d(0, 0, 0); //圆心
acRadDim.ChordPoint = new Point3d(5, 5, 0); //标注点
acRadDim.LeaderLength = 5; //引线长度
acRadDim.DimensionText= "S<>"; //标注内容
acRadDim.TextPosition = new Point3d(5, 5, 0); //文字放置位置
acRadDim.DimensionStyle = acCurDb.Dimstyle; //标注样式
acRadDim.Dimscale = scal; //全局比例
acRadDim.Dimlfac = sca; //线型比例
// 添加新对象到模型空间和事务
CheckRegApp("ACAD_DSTYLE_DIMRADIAL_EXTENSION");//自定义函数检查RegApp名字是否存在,不存在就添加regApp名字
ResultBuffer resBuf = new ResultBuffer();
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, "ACAD_DSTYLE_DIMRADIAL_EXTENSION"));
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 387));
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 1));
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 388));
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataReal, 6.26953));//圆弧的开始角度
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataInteger16, 390));
resBuf.Add(new TypedValue((int)DxfCode.ExtendedDataReal, 2.67146));//圆弧的结束角度
acRadDim.XData = resBuf;
acBlkTblRec.AppendEntity(acRadDim);
acTrans.AddNewlyCreatedDBObject(acRadDim, true);
// 提交修改,关闭事务
acTrans.Commit();
}
}