I'm seeking code for a BeginPlot event hanlder written in vb.net for AutoCAD 2018.
Solved! Go to Solution.
I'm seeking code for a BeginPlot event hanlder written in vb.net for AutoCAD 2018.
Solved! Go to Solution.
Solved by norman.yuan. Go to Solution.
Let me assume you are talking about handling PlotReactorManager.BeginPlot event. So, in VB.NET, yo just need to create an PlotReactorManager instance and add your event handler to the event, then you can do whatever ytou want to do in your event handler, something like:
Dim plotMng As NewPlotReactorManager()
AddHandler plotMng.BeginPlot, AddressOf BeginPlotHandler
... ...
Private Sub BeginPlotHandler(sender As Object, e As BeginPlotEventArgs)
'' Do whatever you intend to do here
End Sub
I have an old article on handling PlotReactorManager's events that might be helpful (the code in it is in C#, but it should be easily read-understandable for VB.NETers, or it can be easily converted to VB.NET code with various online .NET language converters):
https://drive-cad-with-code.blogspot.com/2011/02/log-autocads-plotting.html
HTH
Norman Yuan
Let me assume you are talking about handling PlotReactorManager.BeginPlot event. So, in VB.NET, yo just need to create an PlotReactorManager instance and add your event handler to the event, then you can do whatever ytou want to do in your event handler, something like:
Dim plotMng As NewPlotReactorManager()
AddHandler plotMng.BeginPlot, AddressOf BeginPlotHandler
... ...
Private Sub BeginPlotHandler(sender As Object, e As BeginPlotEventArgs)
'' Do whatever you intend to do here
End Sub
I have an old article on handling PlotReactorManager's events that might be helpful (the code in it is in C#, but it should be easily read-understandable for VB.NETers, or it can be easily converted to VB.NET code with various online .NET language converters):
https://drive-cad-with-code.blogspot.com/2011/02/log-autocads-plotting.html
HTH
Norman Yuan
Thanks for the solution/post. It worked just like you said and how I wanted. However, I now have a "follow-up" question which I believe I know the answer to but am unsure. Therefore, I'll ask the question and see what you say.
Why do I need to ". . . create an PlotReactorManager instance . . . " as you mention below when other event handlers can be written as:
AddHandler Core.Application.DocumentManager.MdiActiveDocument.BeginDocumentClose, AddressOf subDocBeginClose
without specifically "creating an instance" first?
Sincerely,
Christmas May
Thanks for the solution/post. It worked just like you said and how I wanted. However, I now have a "follow-up" question which I believe I know the answer to but am unsure. Therefore, I'll ask the question and see what you say.
Why do I need to ". . . create an PlotReactorManager instance . . . " as you mention below when other event handlers can be written as:
AddHandler Core.Application.DocumentManager.MdiActiveDocument.BeginDocumentClose, AddressOf subDocBeginClose
without specifically "creating an instance" first?
Sincerely,
Christmas May
Usually, I do not directly use Autodesk.AutoCAD.ApplicationServices.Core.Application, instead, I always use Autodesk.Autodesk.ApplicationServices.Application.
Now to your question:
Application is a static instance of class Autodesk.Autodesk.ApplicationServices.Application, which represent the running AutoCAD instance, and is created by AutoCAD .NET APIs automatically and always available (as long as AutoCAD runs). That is why you do not need to instantiate an "Application" class.
As for the line of your code to add event handler to MdiActiveDocument, where MdiActiveDocument is a property of DocumentManager, which is a Document class instance that is created by AutoCAD .NET API automatically and refers to the current active document, so, you DO NOT need to "instantiate it". However, there is possibility, at certain moment during AutoCAD session, that MdiActiveDocument could be null, such as all documents are closed (not document is open in AutoCAD), or during active document switching... So, sometimes you may need to test whether MdiActiveDocument is null.
Also take your code as example, when you want to handle BeginDocumentClose event, it is very likely you not only want to something with currently active document, but also to all opening documents, so probably you would need to do:
Dim doc As Document
For Each doc In Application.DocumentManager
AddHandler doc.BeginDocumentClose, AddressOf .....
Next
Furthermore, since new document can be added/opened in AutoCAD during the same AutoCAD session, you may also want to make sure all newly added/opened document would also have its BeginDocumentClose event handled. So, you need to add the event handler whenever a new document is created:
First, make sure this is run at beginning of your code (probably in IExtensionApplication.Initialize():
Dim doc As Document
For Each doc In Application.DocumentManager
AddHandler doc.BeginDocumentClose, AddressOf .....
Next
AddHandler Application.DocumentManager.DocumentCreated, AddressOf DocumentCreatedHandler
... ...
Private Sub DocumentCreatedHandler(sender as Object, e As DocumentCollectionEventArgs)
AddHandler e.Document.BeginDocumentClose, AddressOf ........
End Sub
This, once your code is loaded and executed the first time, not only all existing documents have their BeginDocumentClose event handler hooked, but also any new document added.opened.
HTH
Norman Yuan
Usually, I do not directly use Autodesk.AutoCAD.ApplicationServices.Core.Application, instead, I always use Autodesk.Autodesk.ApplicationServices.Application.
Now to your question:
Application is a static instance of class Autodesk.Autodesk.ApplicationServices.Application, which represent the running AutoCAD instance, and is created by AutoCAD .NET APIs automatically and always available (as long as AutoCAD runs). That is why you do not need to instantiate an "Application" class.
As for the line of your code to add event handler to MdiActiveDocument, where MdiActiveDocument is a property of DocumentManager, which is a Document class instance that is created by AutoCAD .NET API automatically and refers to the current active document, so, you DO NOT need to "instantiate it". However, there is possibility, at certain moment during AutoCAD session, that MdiActiveDocument could be null, such as all documents are closed (not document is open in AutoCAD), or during active document switching... So, sometimes you may need to test whether MdiActiveDocument is null.
Also take your code as example, when you want to handle BeginDocumentClose event, it is very likely you not only want to something with currently active document, but also to all opening documents, so probably you would need to do:
Dim doc As Document
For Each doc In Application.DocumentManager
AddHandler doc.BeginDocumentClose, AddressOf .....
Next
Furthermore, since new document can be added/opened in AutoCAD during the same AutoCAD session, you may also want to make sure all newly added/opened document would also have its BeginDocumentClose event handled. So, you need to add the event handler whenever a new document is created:
First, make sure this is run at beginning of your code (probably in IExtensionApplication.Initialize():
Dim doc As Document
For Each doc In Application.DocumentManager
AddHandler doc.BeginDocumentClose, AddressOf .....
Next
AddHandler Application.DocumentManager.DocumentCreated, AddressOf DocumentCreatedHandler
... ...
Private Sub DocumentCreatedHandler(sender as Object, e As DocumentCollectionEventArgs)
AddHandler e.Document.BeginDocumentClose, AddressOf ........
End Sub
This, once your code is loaded and executed the first time, not only all existing documents have their BeginDocumentClose event handler hooked, but also any new document added.opened.
HTH
Norman Yuan
Can't find what you're looking for? Ask the community or share your knowledge.