明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2823|回复: 3

程序结束,CAD命令行不能返回Command提示,为什么?

[复制链接]
发表于 2011-9-18 14:48 | 显示全部楼层 |阅读模式
1明经币
问题如图和题目

程序代码:

  1. namespace Xgtool
  2. {
  3.     public class ZbbzMain
  4.     {
  5.         [CommandMethod("clzbbz")]      
  6.         static public void clzbbz()
  7.         {
  8.             #region 检查是否已经设置了比例尺并取得比例尺
  9.             double Blc;
  10.             if (System.Convert.ToInt32(Application.GetSystemVariable("Userr1")) == 0.0000)
  11.             {
  12.                 tongyong.Srblc();
  13.                 Blc = Convert.ToDouble(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Userr1"));
  14.             }
  15.             else
  16.             {
  17.                 Blc = Convert.ToDouble(Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("Userr1"));
  18.             }
  19.             #endregion

  20.             #region 获得当前数据库并启动事务管理器
  21.             Document acDoc = Application.DocumentManager.MdiActiveDocument;
  22.             Database acCurDb = acDoc.Database;
  23.             DocumentLock dl = Application.DocumentManager.MdiActiveDocument.LockDocument();
  24.             #endregion

  25.             #region 创建文字样式并取得文字样式Id
  26.             double textHeight = 3 * Blc / 1000;
  27.             string textName = "宋体";
  28.             double textXScale = 0.85;
  29.             tongyong.setTextStyle(textName, textHeight, textXScale);
  30.             string layerName = "点坐标标注";
  31.             short layerColor = 2;
  32.             ObjectId clzbbzBlockLayerId = Xgtool.tongyong.CreateLayer(layerName, layerColor);
  33.             #endregion

  34.             #region 设置关键字
  35.             PromptPointResult pPtRes1;
  36.             PromptPointResult pPtRes2;
  37.             PromptIntegerOptions pIntOpts = new PromptIntegerOptions("\n按空格或回车键继续/:");
  38.             pIntOpts.Keywords.Add("E", "E", "退出E", true, true);
  39.             pIntOpts.AllowNone = true;//允许空输入
  40.             PromptPointOptions pPtOpts1 = new PromptPointOptions("\n请选择标注点:");
  41.             PromptPointOptions pPtOpts2 = new PromptPointOptions("\n请选择文字位置点:");
  42.             #endregion

  43.             #region 开始循环操作
  44.             do
  45.             {
  46.                 #region 提示开始点,并检查用户的输入状况
  47.                 pPtRes1 = acDoc.Editor.GetPoint(pPtOpts1);
  48.                 Point3d ptStart = pPtRes1.Value;//读取开始点ptStart
  49.                 //如果用户按了 ESC 键或取消了命令就退出,并在命令行输出信息。
  50.                 if (pPtRes1.Status == PromptStatus.Cancel)
  51.                 {
  52.                     acDoc.Editor.WriteMessage("\n用户终止了程序!");
  53.                     return;
  54.                 }
  55.                 pPtOpts2.UseBasePoint = true;//显示橡皮筋线
  56.                 pPtOpts2.BasePoint = ptStart;//橡皮筋线的基准点是ptStart
  57.                 pPtRes2 = acDoc.Editor.GetPoint(pPtOpts2);
  58.                 Point3d ptEnd = pPtRes2.Value;
  59.                 if (pPtRes2.Status == PromptStatus.Cancel)
  60.                 {
  61.                     acDoc.Editor.WriteMessage("\n用户终止了程序!");
  62.                     return;
  63.                 }
  64.                 #endregion

  65.                 #region 测试点XY值的最大长度
  66.                 string ptStartX = ptStart.X.ToString("f3");//取X
  67.                 string ptStartY = ptStart.Y.ToString("f3");//取Y
  68.                 DBText MMM = new DBText();
  69.                 MMM.SetDatabaseDefaults();

  70.                 double textS;
  71.                 if (ptStartX.Length > ptStartY.Length)
  72.                 {
  73.                     MMM.TextString = ptStartX;
  74.                     textS = MMM.GeometricExtents.MaxPoint.X - MMM.GeometricExtents.MinPoint.X;
  75.                 }
  76.                 else
  77.                 {
  78.                     MMM.TextString = ptStartY;
  79.                     textS = MMM.GeometricExtents.MaxPoint.X - MMM.GeometricExtents.MinPoint.X;
  80.                 }
  81.                 #endregion

  82.                 #region 定义一个事件,在数据库里创建一个名称为"*U"属性块
  83.                 using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
  84.                 {
  85.                     ObjectId tstId = ObjectId.Null;
  86.                     TextStyleTable tst = acTrans.GetObject(acCurDb.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
  87.                     tstId = acCurDb.Textstyle = tst["宋体"];

  88.                     BlockTable bt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite) as BlockTable;
  89.                     BlockTableRecord newBtr = new BlockTableRecord();
  90.                     newBtr.Name = "*U";
  91.                     bt.Add(newBtr);
  92.                     acTrans.AddNewlyCreatedDBObject(newBtr, true);

  93.                     //定义块插入点
  94.                     Point3d pt1 = new Point3d(0, 0, 0);
  95.                     Point3d pt2 = new Point3d(ptEnd.X - ptStart.X, ptEnd.Y - ptStart.Y, 0);
  96.                     Point3d pt3, pt4, pt5;

  97.                     if ((ptEnd.X - ptStart.X) >= 0)
  98.                     {
  99.                         pt3 = new Point3d(pt2.X + textS + 3 * (Blc / 1000), pt2.Y, 0);
  100.                         pt4 = new Point3d(pt2.X + (textS + 3 * (Blc / 1000)) / 2, pt2.Y + 2 * (Blc / 1000), 0);
  101.                         pt5 = new Point3d(pt2.X + (textS + 3 * (Blc / 1000)) / 2, pt2.Y - 2 * (Blc / 1000), 0);
  102.                     }
  103.                     else
  104.                     {
  105.                         pt3 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)), pt2.Y, 0);
  106.                         pt4 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)) / 2, pt2.Y + 2 * (Blc / 1000), 0);
  107.                         pt5 = new Point3d(pt2.X - (textS + 3 * (Blc / 1000)) / 2, pt2.Y - 2 * (Blc / 1000), 0);
  108.                     }

  109.                     //指引线
  110.                     Line acLine = new Line(pt1, pt2);
  111.                     acLine.LayerId = clzbbzBlockLayerId;
  112.                     newBtr.AppendEntity(acLine);//线添加到块里
  113.                     acTrans.AddNewlyCreatedDBObject(acLine, true);

  114.                     //水平线
  115.                     Line horizontalGridLine = new Line(pt2, pt3);
  116.                     horizontalGridLine.LayerId = clzbbzBlockLayerId;
  117.                     newBtr.AppendEntity(horizontalGridLine);//线添加到块里
  118.                     acTrans.AddNewlyCreatedDBObject(horizontalGridLine, true);

  119.                     //定义属性X
  120.                     AttributeDefinition attdef = new AttributeDefinition(pt4, ptStartX, "X(E)", "测量点的X值", tstId);
  121.                     attdef.Justify = AttachmentPoint.MiddleCenter;
  122.                     attdef.AlignmentPoint = pt4;
  123.                     attdef.LayerId = clzbbzBlockLayerId;
  124.                     newBtr.AppendEntity(attdef);//属性值添加到块里
  125.                     acTrans.AddNewlyCreatedDBObject(attdef, true);

  126.                     //定义属性Y
  127.                     AttributeDefinition attdef1 = new AttributeDefinition(pt5, ptStartY, "Y(N)", "测量点的Y值", tstId);
  128.                     attdef1.Justify = AttachmentPoint.MiddleCenter;
  129.                     attdef1.AlignmentPoint = pt5;
  130.                     attdef1.LayerId = clzbbzBlockLayerId;
  131.                     newBtr.AppendEntity(attdef1);//属性值添加到块里
  132.                     acTrans.AddNewlyCreatedDBObject(attdef1, true);

  133.                     //在模型空间插入块
  134.                     BlockReference br = new BlockReference(ptStart, newBtr.ObjectId);
  135.                     br.LayerId = clzbbzBlockLayerId;
  136.                     BlockTableRecord modelSpaceBtr = acTrans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  137.                     ObjectId brId = modelSpaceBtr.AppendEntity(br);
  138.                     acTrans.AddNewlyCreatedDBObject(br, true);

  139.                     //在块表记录插入属性X
  140.                     AttributeReference ar = new AttributeReference();
  141.                     ar.SetAttributeFromBlock(attdef, br.BlockTransform);
  142.                     ar.TextString = attdef.TextString;
  143.                     br.AttributeCollection.AppendAttribute(ar);
  144.                     acTrans.AddNewlyCreatedDBObject(ar, true);

  145.                     //在块表记录插入属性Y
  146.                     AttributeReference ar1 = new AttributeReference();
  147.                     ar1.SetAttributeFromBlock(attdef1, br.BlockTransform);
  148.                     ar1.TextString = attdef1.TextString;
  149.                     br.AttributeCollection.AppendAttribute(ar1);
  150.                     acTrans.AddNewlyCreatedDBObject(ar1, true);
  151.                     acTrans.Commit();
  152.                 }
  153.                 #endregion
  154.             }
  155.             while (acDoc.Editor.GetInteger(pIntOpts).Status != PromptStatus.Keyword);
  156.             dl.Dispose();
  157.             #endregion
  158.         }
  159.     }
  160. }

这个问题到底出在哪里?
附件: 您需要 登录 才可以下载或查看,没有账号?注册

最佳答案

查看完整内容

在程序结束前执行: acDoc.SendStringToExecute("CommandLine ", false, false, false);
发表于 2011-9-18 14:48 | 显示全部楼层
在程序结束前执行:
acDoc.SendStringToExecute("CommandLine ", false, false, false);
回复

使用道具 举报

发表于 2011-9-20 14:00 | 显示全部楼层
在程序结束前执行:
acDoc.SendStringToExecute("CommandLine ", false, false, false);
回复

使用道具 举报

 楼主| 发表于 2011-9-20 15:32 | 显示全部楼层
问题是解决了,但还是没有找到症结所在,还是可以作为最佳答案了,谢谢!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-4 15:17 , Processed in 0.308398 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表