程序结束,CAD命令行不能返回Command提示,为什么?
问题如图和题目程序代码:
namespace Xgtool
{
public class ZbbzMain
{
static public void clzbbz()
{
#region 检查是否已经设置了比例尺并取得比例尺
double Blc;
if (System.Convert.ToInt32(Application.GetSystemVariable("Userr1")) == 0.0000)
{
tongyong.Srblc();
Blc = Convert.ToDouble(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Userr1"));
}
else
{
Blc = Convert.ToDouble(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Userr1"));
}
#endregion
#region 获得当前数据库并启动事务管理器
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument();
#endregion
#region 创建文字样式并取得文字样式Id
double textHeight = 3 * Blc / 1000;
string textName = "宋体";
double textXScale = 0.85;
tongyong.setTextStyle(textName, textHeight, textXScale);
string layerName = "点坐标标注";
short layerColor = 2;
ObjectId clzbbzBlockLayerId = Xgtool.tongyong.CreateLayer(layerName, layerColor);
#endregion
#region 设置关键字
PromptPointResult pPtRes1;
PromptPointResult pPtRes2;
PromptIntegerOptions pIntOpts = new PromptIntegerOptions("\n按空格或回车键继续/:");
pIntOpts.Keywords.Add("E", "E", "退出E", true, true);
pIntOpts.AllowNone = true;//允许空输入
PromptPointOptions pPtOpts1 = new PromptPointOptions("\n请选择标注点:");
PromptPointOptions pPtOpts2 = new PromptPointOptions("\n请选择文字位置点:");
#endregion
#region 开始循环操作
do
{
#region 提示开始点,并检查用户的输入状况
pPtRes1 = acDoc.Editor.GetPoint(pPtOpts1);
Point3d ptStart = pPtRes1.Value;//读取开始点ptStart
//如果用户按了 ESC 键或取消了命令就退出,并在命令行输出信息。
if (pPtRes1.Status == PromptStatus.Cancel)
{
acDoc.Editor.WriteMessage("\n用户终止了程序!");
return;
}
pPtOpts2.UseBasePoint = true;//显示橡皮筋线
pPtOpts2.BasePoint = ptStart;//橡皮筋线的基准点是ptStart
pPtRes2 = acDoc.Editor.GetPoint(pPtOpts2);
Point3d ptEnd = pPtRes2.Value;
if (pPtRes2.Status == PromptStatus.Cancel)
{
acDoc.Editor.WriteMessage("\n用户终止了程序!");
return;
}
#endregion
#region 测试点XY值的最大长度
string ptStartX = ptStart.X.ToString("f3");//取X
string ptStartY = ptStart.Y.ToString("f3");//取Y
DBText MMM = new DBText();
MMM.SetDatabaseDefaults();
double textS;
if (ptStartX.Length > ptStartY.Length)
{
MMM.TextString = ptStartX;
textS = MMM.GeometricExtents.MaxPoint.X - MMM.GeometricExtents.MinPoint.X;
}
else
{
MMM.TextString = ptStartY;
textS = MMM.GeometricExtents.MaxPoint.X - MMM.GeometricExtents.MinPoint.X;
}
#endregion
#region 定义一个事件,在数据库里创建一个名称为"*U"属性块
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
ObjectId tstId = ObjectId.Null;
TextStyleTable tst = acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
tstId = acCurDb.Textstyle = tst["宋体"];
BlockTable bt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord newBtr = new BlockTableRecord();
newBtr.Name = "*U";
bt.Add(newBtr);
acTrans.AddNewlyCreatedDBObject(newBtr, true);
//定义块插入点
Point3d pt1 = new Point3d(0, 0, 0);
Point3d pt2 = new Point3d(ptEnd.X - ptStart.X, ptEnd.Y - ptStart.Y, 0);
Point3d pt3, pt4, pt5;
if ((ptEnd.X - ptStart.X) >= 0)
{
pt3 = new Point3d(pt2.X + textS + 3 * (Blc / 1000), pt2.Y, 0);
pt4 = new Point3d(pt2.X + (textS + 3 * (Blc / 1000)) / 2, pt2.Y + 2 * (Blc / 1000), 0);
pt5 = new Point3d(pt2.X + (textS + 3 * (Blc / 1000)) / 2, pt2.Y - 2 * (Blc / 1000), 0);
}
else
{
pt3 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)), pt2.Y, 0);
pt4 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)) / 2, pt2.Y + 2 * (Blc / 1000), 0);
pt5 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)) / 2, pt2.Y - 2 * (Blc / 1000), 0);
}
//指引线
Line acLine = new Line(pt1, pt2);
acLine.LayerId = clzbbzBlockLayerId;
newBtr.AppendEntity(acLine);//线添加到块里
acTrans.AddNewlyCreatedDBObject(acLine, true);
//水平线
Line horizontalGridLine = new Line(pt2, pt3);
horizontalGridLine.LayerId = clzbbzBlockLayerId;
newBtr.AppendEntity(horizontalGridLine);//线添加到块里
acTrans.AddNewlyCreatedDBObject(horizontalGridLine, true);
//定义属性X
AttributeDefinition attdef = new AttributeDefinition(pt4, ptStartX, "X(E)", "测量点的X值", tstId);
attdef.Justify = AttachmentPoint.MiddleCenter;
attdef.AlignmentPoint = pt4;
attdef.LayerId = clzbbzBlockLayerId;
newBtr.AppendEntity(attdef);//属性值添加到块里
acTrans.AddNewlyCreatedDBObject(attdef, true);
//定义属性Y
AttributeDefinition attdef1 = new AttributeDefinition(pt5, ptStartY, "Y(N)", "测量点的Y值", tstId);
attdef1.Justify = AttachmentPoint.MiddleCenter;
attdef1.AlignmentPoint = pt5;
attdef1.LayerId = clzbbzBlockLayerId;
newBtr.AppendEntity(attdef1);//属性值添加到块里
acTrans.AddNewlyCreatedDBObject(attdef1, true);
//在模型空间插入块
BlockReference br = new BlockReference(ptStart, newBtr.ObjectId);
br.LayerId = clzbbzBlockLayerId;
BlockTableRecord modelSpaceBtr = acTrans.GetObject(bt, OpenMode.ForWrite) as BlockTableRecord;
ObjectId brId = modelSpaceBtr.AppendEntity(br);
acTrans.AddNewlyCreatedDBObject(br, true);
//在块表记录插入属性X
AttributeReference ar = new AttributeReference();
ar.SetAttributeFromBlock(attdef, br.BlockTransform);
ar.TextString = attdef.TextString;
br.AttributeCollection.AppendAttribute(ar);
acTrans.AddNewlyCreatedDBObject(ar, true);
//在块表记录插入属性Y
AttributeReference ar1 = new AttributeReference();
ar1.SetAttributeFromBlock(attdef1, br.BlockTransform);
ar1.TextString = attdef1.TextString;
br.AttributeCollection.AppendAttribute(ar1);
acTrans.AddNewlyCreatedDBObject(ar1, true);
acTrans.Commit();
}
#endregion
}
while (acDoc.Editor.GetInteger(pIntOpts).Status != PromptStatus.Keyword);
dl.Dispose();
#endregion
}
}
}
这个问题到底出在哪里? 在程序结束前执行:
acDoc.SendStringToExecute("CommandLine ", false, false, false); 在程序结束前执行:
acDoc.SendStringToExecute("CommandLine ", false, false, false); 问题是解决了,但还是没有找到症结所在,还是可以作为最佳答案了,谢谢!
页:
[1]