乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 59|回复: 4

AddHandler命令WillStart/RemveHandler命令WillStart不工作

[复制链接]

15

主题

44

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
104
发表于 2015-10-9 21:09:23 | 显示全部楼层 |阅读模式
我试图在用户启动特定命令时更改当前层。我正在将处理程序与命令WillStart/CommandEnded/CommandCancelled等一起使用……我知道Autocad帮助告诉我;我使用命令WillStart更改AutoCAD变量“CLAYER”&在发出命令Ended时返回到原始图层。我微小的大脑(一旦确认…)在想,既然我没有操纵对象本身(我只是在改变“CLAYER”变量&检查/插入层),我仍然在遵守规则……我错了吗?因为我的代码不工作&我的“RemoveHandler命令WillStart”似乎不工作(即使解释器正在调用它)
这篇文章让我毛骨悚然……&年龄越来越大……
现在,我在这个网站上查找了以前的文章,发现了一篇关于对象否决的有趣文章。我在autodesk中找不到任何关于否决的文档,除了“创建我的第一个.net项目”在internet上的内容…看起来很有希望,因为我需要掌握…

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

4

主题

219

帖子

4

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
238
发表于 2015-10-10 10:26:30 | 显示全部楼层
如果看不到您在代码中做了什么,就很难进行评论。但根据您有限的描述,在命令启动时设置当前层,并在命令结束/取消时将其恢复到上一层是非常容易的,您不需要Overrule的帮助(如果它可以帮助您的需要)
例如,我希望始终在给定层上绘制线,但不希望用户在每次绘制线之前都麻烦正确设置当前层,以下代码用于此目的:
  1. using Autodesk.AutoCAD.ApplicationServices;
  2. using Autodesk.AutoCAD.EditorInput;
  3. using Autodesk.AutoCAD.Runtime;
  4. using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  5. [assembly: CommandClass(typeof(CommandWillStartHandler.Commands))]
  6. [assembly: ExtensionApplication(typeof(CommandWillStartHandler.Commands))]
  7. namespace CommandWillStartHandler
  8. {
  9.     public class Commands : IExtensionApplication
  10.     {
  11.         private static string _savedLayer = null;
  12.         private const string LINE_LAYER = "MyLine";
  13.         #region IExtensionApplication Interface
  14.         public void Initialize()
  15.         {
  16.             Document dwg = CadApp.DocumentManager.MdiActiveDocument;
  17.             Editor ed = dwg.Editor;
  18.             try
  19.             {
  20.                 ed.WriteMessage("\nInitialising custom add-in...");
  21.                 AddCommandHandler();
  22.                 ed.WriteMessage("\nInitializing custom add-in completed.");
  23.             }
  24.             catch
  25.             {
  26.                 ed.WriteMessage("\nInitializing custom add-in failed");
  27.             }
  28.             finally
  29.             {
  30.                 Autodesk.AutoCAD.Internal.Utils.PostCommandPrompt();
  31.             }
  32.         }
  33.         public void Terminate()
  34.         {
  35.         }
  36.         private void AddCommandHandler()
  37.         {
  38.             CadApp.DocumentManager.DocumentCreated += DocumentManager_DocumentCreated;
  39.             foreach (Document d in CadApp.DocumentManager)
  40.             {
  41.                 d.CommandWillStart += Document_CommandWillStart;
  42.                 d.CommandEnded += Document_CommandEnded;
  43.                 d.CommandCancelled += Document_CommandCancelled;
  44.             }
  45.         }
  46.         private void DocumentManager_DocumentCreated(object sender, DocumentCollectionEventArgs e)
  47.         {
  48.             e.Document.CommandWillStart += Document_CommandWillStart;
  49.             e.Document.CommandEnded += Document_CommandEnded;
  50.             e.Document.CommandCancelled += Document_CommandCancelled;
  51.         }
  52.         private void Document_CommandEnded(object sender, CommandEventArgs e)
  53.         {
  54.             Document doc = CadApp.DocumentManager.MdiActiveDocument;
  55.             Editor ed = doc.Editor;
  56.             ed.WriteMessage("\nCommand {0} ended.", e.GlobalCommandName);
  57.             if (!string.IsNullOrEmpty(_savedLayer))
  58.             {
  59.                 CadApp.SetSystemVariable("CLAYER", _savedLayer);
  60.                 _savedLayer = null;
  61.             }
  62.         }
  63.         private void Document_CommandCancelled(object sender, CommandEventArgs e)
  64.         {
  65.             Document doc = CadApp.DocumentManager.MdiActiveDocument;
  66.             Editor ed = doc.Editor;
  67.             ed.WriteMessage("\nCommand {0} cancelled.", e.GlobalCommandName);
  68.             if (!string.IsNullOrEmpty(_savedLayer))
  69.             {
  70.                 CadApp.SetSystemVariable("CLAYER", _savedLayer);
  71.                 _savedLayer = null;
  72.             }
  73.         }
  74.         private void Document_CommandWillStart(object sender, CommandEventArgs e)
  75.         {
  76.             Document doc = CadApp.DocumentManager.MdiActiveDocument;
  77.             Editor ed = doc.Editor;
  78.             ed.WriteMessage("\nCommand {0} will start.", e.GlobalCommandName);
  79.             //Whenever "LINE" command starts, the active layer is set to
  80.             //LINE_LAYER so that the line is drawn on that layer
  81.             if (e.GlobalCommandName.ToUpper() == "LINE")
  82.             {
  83.                 _savedLayer = CadApp.GetSystemVariable("CLAYER").ToString();
  84.                 CadApp.SetSystemVariable("CLAYER", LINE_LAYER);
  85.             }
  86.         }
  87.         #endregion
  88.     }
  89. }

HTH
回复

使用道具 举报

15

主题

44

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
104
发表于 2015-10-13 21:17:18 | 显示全部楼层
您的代码运行良好,但我的RemoveHandler部分不起作用...在我的窗体中,我有一个“关闭”按钮,它关闭了“RemoveHandler”部分。即使在触发“RemoveHandler”之后,程序仍然发出“CommandWillStart”事件。这是我到目前为止写的(相当简单的)有问题的代码。我砍掉了大部分与事件处理程序无关的代码。关于“RemoveHandler”函数有什么不一致的地方吗?非常感谢你的帮助...谢谢!
  1. Imports Autodesk.AutoCAD.ApplicationServices
  2. Imports Autodesk.AutoCAD.ApplicationServices.Core.Application
  3. Imports Autodesk.AutoCAD.DatabaseServices
  4. Imports Autodesk.AutoCAD.Geometry
  5. Imports Autodesk.AutoCAD.EditorInput
  6. Imports Autodesk.AutoCAD.ApplicationServices.Application
  7. Imports System.Windows.Forms
  8. Imports Autodesk.AutoCAD.Colors
  9. Imports System.IO
  10. Imports Autodesk.AutoCAD.Runtime
  11. Public Class Form1
  12.     Implements IExtensionApplication
  13.     Private CurrentLayerName As String
  14.     Private _SelectedLayer As LayerData
  15.     Private Shared acDoc As Document
  16.     Public Sub Initialize() Implements IExtensionApplication.Initialize
  17.             Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Initialize")
  18.         acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  19.     End Sub
  20.        
  21.     Public Sub Terminate() Implements IExtensionApplication.Terminate
  22.     End Sub
  23.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  24.         _SelectedLayer = New LayerData
  25.       End Sub
  26.     Private Sub myOnButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles myOnButton.Click
  27.          AddHandler acDoc.CommandWillStart, AddressOf SwapTheLayer
  28.     End Sub
  29.        
  30.     Private Sub OFF_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OFF_Button.Click
  31.         RemoveHandler acDoc.CommandWillStart, AddressOf SwapTheLayer
  32.     End Sub
  33.     Private Sub SwapTheLayer(ByVal senderObj As Object, ByVal e As CommandEventArgs)
  34.         CurrentLayerName = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CLAYER")
  35.         Dim importedLayer As Array = _SelectedLayer.ToArray
  36.         Select Case e.GlobalCommandName
  37.             Case "MTEXT"
  38.                 If checkIfLayerExistInDrawing(MText_TextBox.Text) = True Then
  39.                     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("layer exist")
  40.                     Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", MText_TextBox.Text.ToString)
  41.                 Else
  42.                     Dim myCreateLay As New CreateLayer(importedLayer(0).MyLayerName,
  43.                                                        importedLayer(0).MyLayerDescription,
  44.                                                        importedLayer(0).MyLayerColor,
  45.                                                        importedLayer(0).MyLayerPlot,
  46.                                                        importedLayer(0).MyLayerLinetypeName,
  47.                                                        importedLayer(0).MyLayerLineweight)
  48.                     Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("CLAYER", MText_TextBox.Text)
  49.                     Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("layer do not exist in drawing")
  50.                 End If
  51.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("AddHandler CommandEnded")
  52.                 AddHandler acDoc.CommandEnded, AddressOf BackToOriginalLayer
  53.                 AddHandler acDoc.CommandCancelled, AddressOf BackToOriginalLayer
  54.                 AddHandler acDoc.CommandFailed, AddressOf BackToOriginalLayer
  55.             End Select
  56.     End Sub
  57.     Private Sub BackToOriginalLayer(ByVal senderObj As Object, ByVal e As CommandEventArgs)
  58.         Select Case e.GlobalCommandName
  59.             Case Is = "MTEXT"
  60.                 Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", CurrentLayerName)
  61.                 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("RemoveHandler CommandEnded")
  62.                 RemoveHandler acDoc.CommandEnded, AddressOf BackToOriginalLayer
  63.                 RemoveHandler acDoc.CommandCancelled, AddressOf BackToOriginalLayer
  64.                 RemoveHandler acDoc.CommandFailed, AddressOf BackToOriginalLayer
  65.         End Select
  66.     End Sub
  67.     Private Function checkIfLayerExistInDrawing(ByVal myLayerName As String)
  68.         Dim LayerExist As Boolean
  69.         '' Get the current document and database
  70.         Dim acCurDb As Database = acDoc.Database
  71.         '' Start a transaction
  72.         Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  73.             '' Open the Layer table for read
  74.             Dim acLyrTbl As LayerTable
  75.             acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
  76.             'layer exist in drawing...
  77.             If acLyrTbl.Has(myLayerName) = False Then
  78.                 LayerExist = False
  79.             Else
  80.                 LayerExist = True
  81.             End If
  82.         End Using
  83.         Return LayerExist
  84.     End Function
  85. End Class

回复

使用道具 举报

4

主题

219

帖子

4

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
238
发表于 2015-10-14 09:45:49 | 显示全部楼层
我不明白这个表格是怎么显示的。我猜你有另一个CommandClass,用CommandMethod来定义你的命令,其中窗体被调用。
在没有看到其他代码(您可能认为这些代码与问题无关,但我认为它们是相关的)的情况下,我可以看到您的代码有几个问题:
1。UI设计(在本例中为表单)的良好实践是尽可能将业务逻辑(在本例中为AutoCAD相关操作、处理文档的命令事件)与UI分离。在UI模块中实现IExtensionApplication接口就更差了。您应该将添加/移除事件处理程序放在单独的类中。例如,在your command类中可以有两个CommandMethods。也就是说,你可以有两个独立的custo命令“AddHanler”和“RemoveHandler”,它们不仅可以由用户在命令行作为命令输入,也可以由你的窗体调用(看起来就像你通过调用MdiDocument将窗体显示为无模式(浮动窗体))。SendStringToExecute()。
2。您的代码没有删除事件处理程序的直接原因可能是由于您在Initialize()方法中为MdiActiveDocument设置了一个静态/共享变量aDoc,并且如果MdiActiveDocument发生更改,则从不重新指向它:AutoCAD是多文档应用程序,其活动文档可以更改。您的代码需要确保所有打开的文档都添加/删除了处理程序。或者,如果您只针对特定的文档(不是所有打开的文档),您的代码需要记住哪个文档添加了处理程序,这样您就可以相应地删除它们。还要记住,用户可以打开新文档,关闭现有文档。如果您只针对特定文档(不是所有文档),您可能还希望了解如何将CommandClass与每个文档或所有文档相关联(使用共享或非共享CommandMethod调用)。
回复

使用道具 举报

15

主题

44

帖子

1

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
104
发表于 2015-10-14 21:26:29 | 显示全部楼层
在未来的项目中,我会记住这一点……或者在我有时间的时候修改这一点……
在我的编码中,我只是想让它在一个特定的文档中工作。一旦开始工作,我将处理多文档接口。这正是我在初始化部分使用共享/静态文档变量的原因。来自MSDN的共享主题:为了让我的处理程序工作,我认为通过将同一个MdiActiveDocument共享给我的类中的addhandler和removehandler,可以降低出错的风险……正如你所说;如果不使用共享变量,有没有关于如何实现这一点的建议<再次感谢您的时间和建议!非常感谢。。。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 22:02 , Processed in 0.150981 second(s), 62 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表