Author Topic: Need help with event Handler  (Read 1497 times)

0 Members and 1 Guest are viewing this topic.

mgreven

  • Guest
Need help with event Handler
« on: November 21, 2012, 04:25:40 PM »
Hello,

I have a drawing with 2 blockreferences (BlockA and BlockB), BlockB has an Atribute (Ref-Opn) which contains de Handle of BlockA.

I created an "Modified" eventhandler which is attached to BlockA.
When BlockA is moved, all the blockreferences of BlockB should be moved also.

The event i created is working. But i am wondering if it is possible to retrieve the old position from which BlockA is moved from?


Code - vb.net: [Select]
  1.  
  2.  
  3.        Public Sub Event_AccessoireMod(ByVal senderObj As Object, ByVal evtArgs As EventArgs)
  4.             ' Link to general Class...
  5.             Dim Cmd As New Algemeen
  6.             ' Get some Properties of the MvBlock...
  7.             Dim MyAccBlock As MultiViewBlockReference = senderObj
  8.             Dim MyAccBlockHandle As String = MyAccBlock.Handle.ToString
  9.             ' Look for blockreferences with a reference to the object...
  10.             Dim BlockIDs As AcadDb.ObjectIdCollection = Cmd.SelectAllBlockReferences()
  11.  
  12.             '' Get the current document and database, and start a transaction
  13.             Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  14.             Dim acCurDb As Database = acDoc.Database
  15.  
  16.             Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  17.                 '' Open the Block table record for read
  18.                 For Each ObjID As ObjectId In BlockIDs
  19.                     Dim MyBlock As AcadDb.BlockReference = acTrans.GetObject(ObjID, OpenMode.ForWrite)
  20.                     If MyBlock.Name.StartsWith("BlockB") Then
  21.                         Dim attCol As AttributeCollection = MyBlock.AttributeCollection
  22.                         For Each attId As ObjectId In attCol
  23.                             Dim attRef As AttributeReference = DirectCast(acTrans.GetObject(attId, OpenMode.ForRead), AttributeReference)
  24.                             If attRef.Tag = "REF-OPN" Then
  25.                                 ' Zet de string om in een list...
  26.                                 Dim AttList() As String = Cmd.StrToList(attRef.TextString, ";")
  27.                                 For Each StrItem As String In AttList
  28.                                     If UCase(StrItem) = UCase(MyAccBlockHandle) Then
  29.                                         MyBlock.Position = MyAccBlock.Location
  30.                                     End If
  31.                                 Next
  32.                             End If
  33.                         Next
  34.                     End If
  35.                 Next
  36.                 acTrans.Commit()
  37.             End Using
  38.         End Sub
  39.  
  40.  
  41.  


BlackBox

  • King Gator
  • Posts: 3770
Re: Need help with event Handler
« Reply #1 on: November 21, 2012, 05:40:40 PM »
Could the start position you're after not be stored to a private field or property via the OpenedForModify event?
« Last Edit: November 22, 2012, 03:23:15 AM by RenderMan »
"How we think determines what we do, and what we do determines what we get."

mgreven

  • Guest
Re: Need help with event Handler
« Reply #2 on: November 22, 2012, 01:46:30 AM »
That could be, but i have no idea how to to do this. I am new at Events and there is not much documentation i could find.

BlackBox

  • King Gator
  • Posts: 3770
Re: Need help with event Handler
« Reply #3 on: November 22, 2012, 03:26:01 AM »
You'd register an OpenedForModify event the same way you did the Modified event, and add an appropriate Method as your callback.

More on Object events:

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS1a9193826455f5ff2566ffd511ff6f8c7ca-3626.htm
"How we think determines what we do, and what we do determines what we get."

mgreven

  • Guest
Re: Need help with event Handler
« Reply #4 on: November 22, 2012, 09:22:58 AM »
Based on the following link i created the following code.
http://through-the-interface.typepad.com/through_the_interface/2008/08/rolling-back-th.html

When i run the code it seems to work (there is no error), but nothing happens...

What am i doing wrong in te code?

Code - vb.net: [Select]
  1.  
  2.         Private Shared _doc As Document
  3.         Private Shared _ids As New AcadDb.ObjectIdCollection()
  4.         Private Shared _pts As New Point3dCollection()
  5.  
  6.  
  7.         <CommandMethod("AddMOVEREACTOR")> _
  8.         Public Shared Sub AddReactor()
  9.             Dim _doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  10.             AddHandler _doc.CommandWillStart, AddressOf doc_CommandWillStart
  11.         End Sub
  12.  
  13.         <CommandMethod("DelMOVEREACTOR")> _
  14.         Public Shared Sub delReactor()
  15.             Dim _doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  16.             RemoveHandler _doc.CommandWillStart, AddressOf doc_CommandWillStart
  17.         End Sub
  18.  
  19.  
  20.         Private Shared Sub doc_CommandWillStart(sender As Object, e As CommandEventArgs)
  21.             If e.GlobalCommandName = "MOVE" Then
  22.                 _ids.Clear()
  23.                 _pts.Clear()
  24.                 AddHandler _doc.Database.ObjectOpenedForModify, AddressOf _db_ObjectOpenedForModify
  25.                 AddHandler _doc.CommandCancelled, AddressOf _doc_CommandEnded
  26.                 AddHandler _doc.CommandEnded, AddressOf _doc_CommandEnded
  27.                 AddHandler _doc.CommandFailed, AddressOf _doc_CommandEnded
  28.             End If
  29.  
  30.         End Sub
  31.  
  32.  
  33.         Private Shared Sub removeEventHandlers()
  34.             RemoveHandler _doc.CommandCancelled, AddressOf _doc_CommandEnded
  35.             RemoveHandler _doc.CommandEnded, AddressOf _doc_CommandEnded
  36.             RemoveHandler _doc.CommandFailed, AddressOf _doc_CommandEnded
  37.             RemoveHandler _doc.Database.ObjectOpenedForModify, AddressOf _db_ObjectOpenedForModify
  38.  
  39.         End Sub
  40.  
  41.         Private Shared Sub _doc_CommandEnded(sender As Object, e As CommandEventArgs)
  42.             ' Remove database reactor before restoring positions
  43.             removeEventHandlers()
  44.             rollbackLocations()
  45.         End Sub
  46.  
  47.         Private Shared Sub _db_ObjectOpenedForModify(sender As Object, e As ObjectEventArgs)
  48.             Dim AcBlock As AcadDb.BlockReference = TryCast(e.DBObject, AcadDb.BlockReference)
  49.             If AcBlock IsNot Nothing Then
  50.                 ' In AutoCAD 2007, OpenedForModify is called only
  51.                 ' once by MOVE.
  52.                 ' In 2008, OpenedForModify is called multiple
  53.                 ' times by the MOVE command ... we are only
  54.                 ' interested in the first call, because
  55.                 ' in the second one, the object location
  56.                 ' has already been changed:
  57.                 If Not _ids.Contains(AcBlock.ObjectId) Then
  58.                     _ids.Add(AcBlock.ObjectId)
  59.                     _pts.Add(AcBlock.Position)
  60.                 End If
  61.             End If
  62.         End Sub
  63.  
  64.         Private Shared Sub rollbackLocations()
  65.             Debug.Assert(_ids.Count = _pts.Count, "Expected same number of ids and locations")
  66.             Dim t As Transaction = _doc.Database.TransactionManager.StartTransaction()
  67.             Dim AlgCmd As New Algemeen
  68.             Dim MyEd As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
  69.  
  70.             Using t
  71.                 Dim i As Integer = 0
  72.                 For Each id As ObjectId In _ids
  73.                     Dim AcBlock As AcadDb.BlockReference = TryCast(t.GetObject(id, OpenMode.ForWrite), AcadDb.BlockReference)
  74.                     Dim OldInspnt As Point3d = _pts(System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1))
  75.                     ' Calculate the horizontal distance between the points...
  76.                     Dim Dist As Double = AlgCmd.DistanceBetween(New Point3d(OldInspnt.X, OldInspnt.Y, 0), New Point3d(AcBlock.Position.X, AcBlock.Position.Y, 0))
  77.                     ' Calculate the horizontal Angle betweeen the points...
  78.                     Dim pt1 As Point2d = New Point2d(OldInspnt.X, OldInspnt.Y)
  79.                     Dim pt2 As Point2d = New Point2d(AcBlock.Position.X, AcBlock.Position.Y)
  80.                     Dim Angle As Double = pt1.GetVectorTo(pt2).Angle
  81.                     Dim MyAccBlockHandle As String = AcBlock.Handle.ToString
  82.                     ' Kijk of er een Block is met een refentie naar het object...
  83.                     Dim BlockIDs As AcadDb.ObjectIdCollection = AlgCmd.SelectAllBlockReferences()
  84.  
  85.                     '' Get the current document and database, and start a transaction
  86.                     Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  87.                     Dim acCurDb As Database = acDoc.Database
  88.  
  89.                     Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  90.                         '' Open the Block table record for read
  91.                         For Each ObjID As ObjectId In BlockIDs
  92.                             Dim MyBlock As AcadDb.BlockReference = acTrans.GetObject(ObjID, OpenMode.ForWrite)
  93.                             If MyBlock.Name.StartsWith("KST-") Then
  94.                                 Dim attCol As AttributeCollection = MyBlock.AttributeCollection
  95.                                 For Each attId As ObjectId In attCol
  96.                                     Dim attRef As AttributeReference = DirectCast(t.GetObject(attId, OpenMode.ForRead), AttributeReference)
  97.                                     If attRef.Tag = "REF-KST" Then
  98.                                         ' Zet de string om in een list...
  99.                                         Dim AttList() As String = AlgCmd.StrToList(attRef.TextString, ";")
  100.                                         For Each StrItem As String In AttList
  101.                                             If UCase(StrItem) = UCase(MyAccBlockHandle) Then
  102.                                                 ' Block Found... Calculate new position...
  103.                                                 Dim NewBlkPosition As Point3d = AlgCmd.PolarPoints(MyBlock.Position, Angle, Dist)
  104.                                                 MyBlock.Position = NewBlkPosition
  105.                                             End If
  106.                                         Next
  107.                                     End If
  108.                                 Next
  109.                             End If
  110.                         Next
  111.                         acTrans.Commit()
  112.                     End Using
  113.  
  114.                 Next
  115.             t.Commit()
  116.             End Using
  117.  
  118.         End Sub
  119.  
  120.  
  121.