2021/08/23 CAD二次开发程序在自定义UCS运行注意事项
部分转载至此文~感谢作者!
注意事项
- AutoCAD 界面中,通过手动操作创建的图元都是基于 UCS 的
- 通过程序点选获取的坐标点是是基于UCS
- 通过程序程序创建图元都是基于WCS(即自动生成基于WCS)
- 通过程序解析图元的坐标点都是基于WCS
代码示例
提取CAD地形线(多段线)标高,并以单行文字在原位置标注该标高
// 修改前的代码
public static void MarkElevation()
{
Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
Editor acEd = acDoc.Editor;
PromptEntityOptions pCurveOpt = new PromptEntityOptions("请选择要标注的曲线");
PromptEntityResult pCurveRes = acEd.GetEntity(pCurveOpt);
if (pCurveRes.Status == PromptStatus.OK)
{
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
Polyline acCurve = acTrans.GetObject(pCurveRes.ObjectId, OpenMode.ForRead) as Polyline;
if (acCurve != null)
{
DBText acText = new DBText();
acText.TextString = acCurve.Elevation.ToString();
acText.DtRotateToCurve(acCurve, pCurveRes.PickedPoint);
acText.Justify = AttachmentPoint.MiddleCenter;
acText.AlignmentPoint = acCurve.GetClosestPointTo(pCurveRes.PickedPoint, true);
acText.ColorIndex = 2;
acText.WidthFactor = 0.7;
BlockTable blkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blkTblRec = acTrans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
blkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
acTrans.Commit();
}
}
}
// 修改后的代码
public static void MarkElevation()
{
Document acDoc = AcadApp.DocumentManager.MdiActiveDocument;
Editor acEd = acDoc.Editor;
PromptEntityOptions pCurveOpt = new PromptEntityOptions("请选择要标注的曲线");
PromptEntityResult pCurveRes = acEd.GetEntity(pCurveOpt);
if (pCurveRes.Status == PromptStatus.OK)
{
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
Polyline acCurve = acTrans.GetObject(pCurveRes.ObjectId, OpenMode.ForRead) as Polyline;
if (acCurve != null)
{
Point3d transPoi =pCurveRes.PickedPoint.TransformBy(acEd.CurrentUserCoordinateSystem);//将程序点选坐标转换到Wcs坐标
DBText acText = new DBText();
acText.TextString = acCurve.Elevation.ToString();
acText.DtRotateToCurve(acCurve, transPoi);
acText.Justify = AttachmentPoint.MiddleCenter;
acText.AlignmentPoint = acCurve.GetClosestPointTo(transPoi, true);
acText.ColorIndex = 2;
acText.WidthFactor = 0.7;
BlockTable blkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord blkTblRec = acTrans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
blkTblRec.AppendEntity(acText);
acTrans.AddNewlyCreatedDBObject(acText, true);
acTrans.Commit();
}
}
}

浙公网安备 33010602011771号