C# AutoCAD 利用Editor.CommandAsync 同步监测自带命令的执行情况

发布时间 2023-09-24 08:59:05作者: 南胜NanSheng

#1官方文档并无相关解释:AutoCAD 2023 Developer and ObjectARX Help | Editor.CommandAsync Method | Autodesk

#2 上例子,我用自带的命令画一个圆,画完后我要修改它的颜色,此时该如何操作呢,下面是可用的代码

[CommandMethod(nameof(tt_CommandAsync))]
        public void tt_CommandAsync()
        {
            try
            {
                var ed = Application.DocumentManager.MdiActiveDocument.Editor;
                var db = Application.DocumentManager.MdiActiveDocument.Database;
                Application.SetSystemVariable("filedia", 0);
                var cr = ed.CommandAsync("circle", "0,0,0", 500);
                cr.OnCompleted(() =>
                        {
                            var psr = ed.SelectLast();
                            ed.WriteMessage($"" + Environment.NewLine);
                            Application.SetSystemVariable("filedia", 1);
                            if (psr.Status == PromptStatus.OK)
                            {
                                using (var tr = db.TransactionManager.StartTransaction())
                                {
                                    var ent = tr.GetObject(psr.Value[0].ObjectId, OpenMode.ForWrite) as Entity;
                                    if (ent != null)
                                    {
                                        ent.Color = Color.FromColorIndex(ColorMethod.ByAci, 150);
                                        tr.Commit();
                                    }
                                    else tr.Abort();
                                }
                            }
                        }
                    );
            }
            catch (System.Exception ex)
            {
                Application.ShowAlertDialog(ex.StackTrace);
            }
        }

测试命令是可以执行的,达到了我们要求。