- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 csharp 于 2014-6-10 02:47 编辑
- // (C) Copyright 2014 by
- //
- using System;
- using Autodesk.AutoCAD.Runtime;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.EditorInput;
- using Exception = System.Exception;
- [assembly: CommandClass(typeof(AutoCAD_CSharp_plug_in20.MyCommands))]
- namespace AutoCAD_CSharp_plug_in20
- {
- public class MyCommands
- {
-
- #region "send Move command by callback function"
- private static bool MoveExit = false;
- private static Line ln;
- //declare the callback delegation
- delegate void Del();
- private static Del _actionCompletedDelegate;
- // Exit function,check if Zoom command is esc\cancelled
- static void MdiActiveDocument_CommandCancelled(object sender, CommandEventArgs e)
- {
- MoveExit = true;
- try
- {
- Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction();
- using (tr)
- {
- DBObject ent = (DBObject) tr.GetObject(ln.ObjectId, OpenMode.ForWrite);
- ent.Erase();
- tr.Commit();
- }
- }
- catch (Exception)
- {
- throw;
- }
- }
- static void MdiActiveDocument_CommandEnded(object sender, CommandEventArgs e)
- {
- MoveExit = true;
- }
- [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]
- public void MyCommand()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed;
- if (doc != null)
- {
- ed = doc.Editor;
- Database db = doc.Database;
- Transaction tr = db.TransactionManager.StartTransaction();
- using (tr)
- {
- try
- {
- Point3d p1 = new Point3d(0, 0, 0);
- Point3d p2 = new Point3d(100, 100, 0);
- ln = new Line();
- ln.StartPoint = p1;
- ln.EndPoint = p2;
- BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
- btr.AppendEntity(ln);
- tr.AddNewlyCreatedDBObject(ln, true);
- tr.Commit();
- doc.CommandCancelled += MdiActiveDocument_CommandCancelled;
- doc.CommandEnded += MdiActiveDocument_CommandEnded;
- Editor.CommandResult cmdResult =
- ed.CommandAsync(new object[]{"_.Move", "L", "", p1, Editor.PauseToken});
- _actionCompletedDelegate = new Del(CreateMoveAsyncCallback);
- cmdResult.OnCompleted(new Action(_actionCompletedDelegate));
- MoveExit = false;
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
-
- // callback function
- public static void CreateMoveAsyncCallback()
- {
- var ed = Application.DocumentManager.MdiActiveDocument.Editor;
- //if Move command is running
- if (!MoveExit)
- {
- Point3d pt = new Point3d(0, 0, 0);
- // AutoCAD hands over to the callback function
- Editor.CommandResult cmdResult = ed.CommandAsync(new object[]{
- "_.Move", "L", "",pt, Editor.PauseToken});
- // delegate callback function, wait for interaction ends
- _actionCompletedDelegate = new Del(CreateMoveAsyncCallback);
- cmdResult.OnCompleted(new Action(_actionCompletedDelegate));
- }
- else
- {
- ed.WriteMessage("Move Exit");
- return;
- }
- }
- #endregion
- }
- }
使用 Move 命令模拟 Jig,命令方式更简单写起来更快捷,Jig 繁琐强大,各有所长
使用了 Events (事件),Autolisp 中叫反应器(:vlr-commandended, :vlr-CommandCancelled)
仅 AutoCAD 2015 .Net 4.5 及以上支持
|
|