AutoCAD 2016开发: 使用.NET监听外部事件调用命令

2015-04-10 11:50:00
zengde
来源:
翻译 638

先看下效果。


在autocad中调用命令启动外部事件监听例如 Files y s t e mWatcher后,当文件夹(d:/temp/files)中的文件数目发生变化后,就会响应画出正方形,以下为代码

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using s y s t e m;
using s y s t e m.IO;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in1.Commands))]
namespace AutoCAD_CSharp_plug_in1
{
    public class Commands
    {
        const int columns = 10;
        const string path = "d:\\temp\\files";

        private Files y s t e mWatcher _fsw = null;
        private int _fileNum = 0;
        private int _filesTotal = 0;
        private bool _drawing = false;

        [CommandMethod("EC")]
        public void EventCommand()
        {
            var dm = Application.DocumentManager;
            var doc = dm.MdiActiveDocument;
            if (doc == null)
                return;

            var ed = doc.Editor;

            // We'll start by creating one square for each file in the
            // specified location. The nSquares() function uses some
            // global state for the index and the total count

            _fileNum = 0;
            _filesTotal = Directory.GetFiles(path).Length;

            nSquares(ed);

            // Create a Files y s t e mWatcher for the path, looking for
            // write changes and drawing more squares as needed

            if (_fsw == null)
            {
                _fsw = new Files y s t e mWatcher(path, "*.*");
                _fsw.Changed += (o, s) => nSquaresInContext(dm, ed, path);
                _fsw.NotifyFilter = NotifyFilters.LastWrite;
                _fsw.EnableRaisingEvents = true;

                ed.WriteMessage("\nWatching \"{0}\" for changes.", path);
            }
        }

        [CommandMethod("ECX")]
        public void StopEventCommand()
        {
            var dm = Application.DocumentManager;
            var doc = dm.MdiActiveDocument;
            if (doc == null)
                return;

            var ed = doc.Editor;

            if (_fsw != null)
            {
                _fsw.Dispose();
                _fsw = null;
            }

            ed.WriteMessage("\nNo longer watching folder.");
        }

#pragma warning disable 1998

        private async void nSquaresInContext(
          DocumentCollection dc, Editor ed, string path
        )
        {
            // We'll set the total as it may well have changed (hence the
            // need for global state rather than using an argument)

            _filesTotal = Directory.GetFiles(path).Length;

            // Protect the command-calling function with a flag to avoid
            // eInvalidInput failures

            if (!_drawing)
            {
                _drawing = true;

                // Call our square creation function asynchronously

                await dc.e x e cuteInCommandContextAsync(
                  async (o) => nSquares(ed),
                  null
                );

                _drawing = false;
            }
        }

#pragma warning restore 1998

        private void nSquares(Editor ed)
        {
            // Draw squares until we have enough (the total might
            // change, hence the need for global state)

            for (; _fileNum < _filesTotal; _fileNum++)
            {
                // Determine the position in our grid

                int xoff = _fileNum % columns,
                    yoff = _fileNum / columns;

                // Create our polyline via the RECTANG command

                ed.Command(
                  "._rectang",
                  String.Format("{0},{1}", xoff, yoff),
                  String.Format("{0},{1}", xoff + 1, yoff + 1),
                  "_regen"
                );
            }
        }
    }
}
vb项目名称为AutoCAD_CSharp_plug_in1,保存为Command.cs后build项目,启动调试后打开cad程序,输入netload,加载build的AutoCAD CSharp plug-in1.dll文件。
博客分类