Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Visual studio program. withevent documentchange

9 REPLIES 9
SOLVED
Reply
Message 1 of 10
TechInventor20
472 Views, 9 Replies

Visual studio program. withevent documentchange

TechInventor20
Advocate
Advocate

Goodmorning,

 

We got a standalone program at work for inventor. In the program we can do different things with inventor. But the one thing we need is to get the name of the active document without having to click at a button or something. I can't get it to work. The current code is this:

 

Public Class Form5




    Private WithEvents m_onDocumentChangeEvent As Inventor.ApplicationEvents
    Private WithEvents m_appEvents As Inventor.ApplicationEvents

    Private g_inventorApplication As Inventor.Application
    Private DocumentObject As Inventor.Document

    Public Sub ActiveEvents()

        g_inventorApplication = Marshal.GetActiveObject("Inventor.Application")

        DocumentObject = g_inventorApplication.ActiveDocument

        m_onDocumentChangeEvent = g_inventorApplication.ApplicationEvents

        m_appEvents = g_inventorApplication.ApplicationEvents
        
    End Sub

    Private Sub m_onDocumentChangeEvent_OnDocumentChange(ByVal DocumentObject As Inventor.Document,
                                                     ByVal BeforeOrAfter As EventTimingEnum,
                                                     ByVal ReasonsForChange As CommandTypesEnum,
                                                     ByVal Context As NameValueMap,
                                                     ByRef HandlingCode As HandlingCodeEnum) Handles m_onDocumentChangeEvent.OnDocumentChange


        If BeforeOrAfter = EventTimingEnum.kAfter Then

            MsgBox("OnActivateDocument " & DocumentObject.DisplayName)

        End If
        
        Label1.Text = "Inventor document name"
    End Sub
End Class

 

Is it possible to change the label name when I change the the document"?? 

0 Likes

Visual studio program. withevent documentchange

Goodmorning,

 

We got a standalone program at work for inventor. In the program we can do different things with inventor. But the one thing we need is to get the name of the active document without having to click at a button or something. I can't get it to work. The current code is this:

 

Public Class Form5




    Private WithEvents m_onDocumentChangeEvent As Inventor.ApplicationEvents
    Private WithEvents m_appEvents As Inventor.ApplicationEvents

    Private g_inventorApplication As Inventor.Application
    Private DocumentObject As Inventor.Document

    Public Sub ActiveEvents()

        g_inventorApplication = Marshal.GetActiveObject("Inventor.Application")

        DocumentObject = g_inventorApplication.ActiveDocument

        m_onDocumentChangeEvent = g_inventorApplication.ApplicationEvents

        m_appEvents = g_inventorApplication.ApplicationEvents
        
    End Sub

    Private Sub m_onDocumentChangeEvent_OnDocumentChange(ByVal DocumentObject As Inventor.Document,
                                                     ByVal BeforeOrAfter As EventTimingEnum,
                                                     ByVal ReasonsForChange As CommandTypesEnum,
                                                     ByVal Context As NameValueMap,
                                                     ByRef HandlingCode As HandlingCodeEnum) Handles m_onDocumentChangeEvent.OnDocumentChange


        If BeforeOrAfter = EventTimingEnum.kAfter Then

            MsgBox("OnActivateDocument " & DocumentObject.DisplayName)

        End If
        
        Label1.Text = "Inventor document name"
    End Sub
End Class

 

Is it possible to change the label name when I change the the document"?? 

9 REPLIES 9
Message 2 of 10

CattabianiI
Collaborator
Collaborator
Accepted solution

Have you debugged this code? Does it stop in the event handler? 

OnDocumentChange fires when a document has a change, which means a parameter modification, a feature modification and so on.

 

Maybe what you need is OnActivateDocument?

Have you debugged this code? Does it stop in the event handler? 

OnDocumentChange fires when a document has a change, which means a parameter modification, a feature modification and so on.

 

Maybe what you need is OnActivateDocument?

Message 3 of 10

TechInventor20
Advocate
Advocate

Thank you for the reply. 


I did what you suggested but now I cant even debug the program. I get this error message at

OnDocumentChange

TechInventor20_0-1652791996075.png

 

Do you know what i do wrong?

 

0 Likes

Thank you for the reply. 


I did what you suggested but now I cant even debug the program. I get this error message at

OnDocumentChange

TechInventor20_0-1652791996075.png

 

Do you know what i do wrong?

 

Message 4 of 10

CattabianiI
Collaborator
Collaborator
Accepted solution

OnDocumentChange and OnActivateDocument have two different signatures (number and type of arguments of the event handler), check the sintax in the manual I linked in the previous message.

OnDocumentChange and OnActivateDocument have two different signatures (number and type of arguments of the event handler), check the sintax in the manual I linked in the previous message.

Message 5 of 10

TechInventor20
Advocate
Advocate

I can debug the program, but when I'm in the Form and I change files inside inventor it doesn't work. It doesn't trigger the code. What am I doing wrong? Or is this not the code for the job?

0 Likes

I can debug the program, but when I'm in the Form and I change files inside inventor it doesn't work. It doesn't trigger the code. What am I doing wrong? Or is this not the code for the job?

Message 6 of 10

CattabianiI
Collaborator
Collaborator

Who is calling ActiveEvents method? can you debug that method?

Changing files means switch the active document or make a modification on a document?


0 Likes

Who is calling ActiveEvents method? can you debug that method?

Changing files means switch the active document or make a modification on a document?


Message 7 of 10

TechInventor20
Advocate
Advocate

Yes, I got it to work.


Just need to call the ActiveEvents...

I need to know when it changes the document. then I get some properties from the part, assembly or drawing.

 

Thank you for helping!

0 Likes

Yes, I got it to work.


Just need to call the ActiveEvents...

I need to know when it changes the document. then I get some properties from the part, assembly or drawing.

 

Thank you for helping!

Message 8 of 10

TechInventor20
Advocate
Advocate

Got another question. The following code isn't working...

 Public Sub ActiveEvents()

        g_inventorApplication = Marshal.GetActiveObject("Inventor.Application")

        DocumentObject = g_inventorApplication.ActiveDocument

        m_onDocumentChangeEvent = g_inventorApplication.ApplicationEvents

        m_appEvents = g_inventorApplication.ApplicationEvents


    End Sub


    Private Sub m_onDocumentChangeEvent_OnActivateDocument(ByVal DocumentObject As Inventor.Document,
                                                     ByVal BeforeOrAfter As EventTimingEnum,
                                                     ByVal Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles m_onDocumentChangeEvent.OnActivateDocument


        If BeforeOrAfter = EventTimingEnum.kAfter Then
            If _invApp.ActiveDocument.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
                If BackgroundWorker3.IsBusy <> True Then
                    BackgroundWorker3.RunWorkerAsync()
                End If
            End If
        End If
    End Sub


    Private Sub BackgroundWorker3_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker3.DoWork


'some code





    End Sub

    Private Sub backgroundWorker3_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted
        Dim drawingdoc As DrawingDocument
        drawingdoc = _invApp.ActiveDocument
        Dim labeltext As String
        labeltext = drawingdoc.DisplayName
        'MsgBox(labeltext)
        Label24.Text = labeltext
end sub

 

the part where it go's wrong is when i give Label24.text a new name. I get this error message.

 

TechInventor20_0-1652968105399.png

 

0 Likes

Got another question. The following code isn't working...

 Public Sub ActiveEvents()

        g_inventorApplication = Marshal.GetActiveObject("Inventor.Application")

        DocumentObject = g_inventorApplication.ActiveDocument

        m_onDocumentChangeEvent = g_inventorApplication.ApplicationEvents

        m_appEvents = g_inventorApplication.ApplicationEvents


    End Sub


    Private Sub m_onDocumentChangeEvent_OnActivateDocument(ByVal DocumentObject As Inventor.Document,
                                                     ByVal BeforeOrAfter As EventTimingEnum,
                                                     ByVal Context As NameValueMap, ByRef HandlingCode As HandlingCodeEnum) Handles m_onDocumentChangeEvent.OnActivateDocument


        If BeforeOrAfter = EventTimingEnum.kAfter Then
            If _invApp.ActiveDocument.DocumentType = DocumentTypeEnum.kDrawingDocumentObject Then
                If BackgroundWorker3.IsBusy <> True Then
                    BackgroundWorker3.RunWorkerAsync()
                End If
            End If
        End If
    End Sub


    Private Sub BackgroundWorker3_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker3.DoWork


'some code





    End Sub

    Private Sub backgroundWorker3_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorker3.RunWorkerCompleted
        Dim drawingdoc As DrawingDocument
        drawingdoc = _invApp.ActiveDocument
        Dim labeltext As String
        labeltext = drawingdoc.DisplayName
        'MsgBox(labeltext)
        Label24.Text = labeltext
end sub

 

the part where it go's wrong is when i give Label24.text a new name. I get this error message.

 

TechInventor20_0-1652968105399.png

 

Message 9 of 10

CattabianiI
Collaborator
Collaborator

RunWorkerAsync in that event handler without await, updating somthing in the UI thread; this could be really painful: is it really necessary? The exception seems to talk about threads, right?

I think here is more about threads in .NET and the web has plenty of Q&A about this matter, than Inventor related things.



0 Likes

RunWorkerAsync in that event handler without await, updating somthing in the UI thread; this could be really painful: is it really necessary? The exception seems to talk about threads, right?

I think here is more about threads in .NET and the web has plenty of Q&A about this matter, than Inventor related things.



Message 10 of 10

TechInventor20
Advocate
Advocate

I fixed it with the 

Control.CheckForIllegalCrossThreadCalls = False

code

0 Likes

I fixed it with the 

Control.CheckForIllegalCrossThreadCalls = False

code

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report