找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1644|回复: 1

[分享] How to combine a .Net Jig and MessageFilter to catch user input

[复制链接]

已领礼包: 593个

财富等级: 财运亨通

发表于 2013-5-27 22:07:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
How to combine a .Net Jig and MessageFilter to catch user input such as Right Click?                                                        By Philippe Leefsma
  Here is a common question developers were asking to ADN support:
  I use a EntityJig derived class to let the user place a block reference in the dwg, it works fine but I would like to let the user get a null angle just giving a right click when prompted for the angle. Using "UserInputControls.NullResponseAccepted" doesn't seem to work as expected.
  Do you have a solution to handle that request?
  
  Solution
  Here is a complete VB.Net sample that illustrates ho to handle a MessageFilter within a Jig. The sample could be extended to monitor more user actions if needed.
  1. Imports Autodesk.AutoCAD.ApplicationServices
  2. Imports Autodesk.AutoCAD.DatabaseServices
  3. Imports Autodesk.AutoCAD.EditorInput
  4. Imports Autodesk.AutoCAD.Geometry
  5. Imports Autodesk.AutoCAD.Runtime
  6. Imports System.Runtime.InteropServices

  7. '////////////////////////////////////////////////////////////////////
  8. '// Use: Jig a newly created block reference
  9. '// Author: Philippe Leefsma, September 2011
  10. '////////////////////////////////////////////////////////////////////

  11. Public Class JigMsgFilter
  12.     Inherits EntityJig
  13.     Private _position As Point3d
  14.     Private _filter As JigFilter

  15.     Public Sub New(ByVal btrId As ObjectId)
  16.         MyBase.New(New BlockReference(Point3d.Origin, btrId))
  17.         _position = Point3d.Origin
  18.         _filter = New JigFilter
  19.         System.Windows.Forms.Application.AddMessageFilter(_filter)
  20.     End Sub

  21.     Public Overloads ReadOnly Property Entity() As BlockReference
  22.         Get
  23.             Return MyBase.Entity
  24.         End Get
  25.     End Property

  26.     Protected Overloads Overrides Function Update() As Boolean
  27.         Entity.Position = _position
  28.         Return True
  29.     End Function

  30.     Protected Overloads Overrides Function Sampler( _
  31.         ByVal prompts As JigPrompts) _
  32.             As SamplerStatus

  33.         Dim jigOpts As JigPromptPointOptions = _
  34.           New JigPromptPointOptions(vbCrLf + "Specify position: "))
  35.         jigOpts.UserInputControls = _
  36.             UserInputControls.NullResponseAccepted

  37.         Dim res As PromptPointResult = prompts.AcquirePoint(
  38.             jigOpts)
  39.         If (res.Status <> PromptStatus.OK) Then
  40.             Return SamplerStatus.Cancel
  41.         End If
  42.         If (_position = res.Value) Then
  43.             Return SamplerStatus.NoChange
  44.         End If

  45.         _position = res.Value
  46.         Return SamplerStatus.OK

  47.     End Function


  48.     Public Function Run() As PromptStatus
  49.         Dim promptResult As PromptResult = _
  50.         Application.DocumentManager.MdiActiveDocument.Editor.Drag(Me)
  51.         Return promptResult.Status
  52.     End Function

  53.     '////////////////////////////////////////////////////////////
  54.     '// Use: Start Jig command
  55.     '//
  56.     '////////////////////////////////////////////////////////////

  57.     Public Shared Sub JigMsgFilter()
  58.         Dim doc As Document = _
  59.             Application.DocumentManager.MdiActiveDocument
  60.         Dim db As Database = doc.Database
  61.         Dim ed As Editor = doc.Editor
  62.         Dim res As PromptResult = _
  63.             ed.GetString(vbCrLf + "Enter block name to insert: ")
  64.         If res.Status <> PromptStatus.OK Then
  65.             Exit Sub
  66.         End If

  67.         Using Tx As Transaction = _
  68.             db.TransactionManager.StartTransaction()
  69.             Dim bT As BlockTable = Tx.GetObject( _
  70.                 db.BlockTableId, _
  71.                 OpenMode.ForRead)

  72.             If (Not bT.Has(res.StringResult)) Then
  73.                 ed.WriteMessage(vbCrLf + "Block do not exist :(")
  74.                 Exit Sub
  75.             End If

  76.             Dim btrId As ObjectId = bT(res.StringResult)
  77.             Dim jig As New JigMsgFilter(btrId)
  78.             If (jig.Run() <> PromptStatus.OK) Then
  79.                 jig.Entity.Dispose()
  80.                 Exit Sub
  81.             End If
  82.             Dim model As BlockTableRecord = Tx.GetObject( _
  83.                 bT(BlockTableRecord.ModelSpace), _
  84.                 OpenMode.ForWrite)
  85.             model.AppendEntity(jig.Entity)
  86.             Tx.AddNewlyCreatedDBObject(jig.Entity, True)
  87.             Tx.Commit()
  88.         End Using
  89.     End Sub

  90.     '////////////////////////////////////////////////////////////
  91.     '// Use: Implementation of message filter
  92.     '//
  93.     '////////////////////////////////////////////////////////////

  94.     Private Class JigFilter
  95.         Implements System.Windows.Forms.IMessageFilter
  96.         Private WM_RIGHTCLICK As Integer = &H204
  97.         Public Function PreFilterMessage( _
  98.             ByRef m As System.Windows.Forms.Message) _
  99.                 As Boolean _
  100.             Implements _
  101.                 System.Windows.Forms.IMessageFilter.PreFilterMessage
  102.             If m.Msg = WM_RIGHTCLICK Then
  103.                 Dim doc As Document = _
  104.                     Application.DocumentManager.MdiActiveDocument
  105.                 Dim ed As Editor = doc.Editor()
  106.                 ed.WriteMessage(vbCrLf + "Right Button pressed!!")

  107.                 'Block message for AutoCAD

  108.                 Return True
  109.             End If
  110.             Return False
  111.         End Function
  112.     End Class
  113. End Class
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 1632个

财富等级: 堆金积玉

发表于 2013-6-2 21:00:54 | 显示全部楼层
好久没有用VB了,看着代码有点吃力{:soso_e140:}
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:37 , Processed in 0.160433 second(s), 29 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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