Hallo CAD.DE Community,
mein Name ist Florian, männlich   , Freizeitprogrammierer und verzweifelt.
, Freizeitprogrammierer und verzweifelt.
Das Thema quält mich nun schon seit beinahe drei Jahren immer wieder. Daher schreibe ich nun diesen Beitrag.
Ich habe gefühlt alle englischen und deutschen Foren nach einer Lösung durchforstet und einiges selbst versucht.
Über ein Plug-in habe ich die Schnittstelle zu Inventor herstellen können (Visual Basic 2010 Express und Inventor 2015 Pro).
Nach dieser Anleitung bin ich vorgegangen: http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=17324828
Nun möchte ich die WithEvents mit einbinden, im wesentlichen "OnSave".
Dabei möchte ich nicht über ein AddIn gehen, da mein Ziel ist: Eine Windows Form Anwendung über eine Exe zu starten in der ich schon verschiedene, eigene kleine Programme untergebracht habe. In diese Windows Form möchte ich jetzt das OnSave Event einbinden.
Ausgangssituation:
- Visual Basic 2010 Express Project
- Autodesk.Inventor.Interop Verweis gesetzt
- Nur 1 Windows Form mit folgendem Code
Imports System
Imports System.Type
Imports System.Activator
Imports System.Runtime.InteropServices
Imports Inventor
Public Class Form1
    Dim _invApp As Inventor.Application
    Dim _started As Boolean = False
    Private m_inventorApplication As Inventor.Application
    Private WithEvents m_ApplicationEvents As Inventor.ApplicationEvents
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        'Überprüfung ob Inventor gestartet ist
        ' Add any initialization after the InitializeComponent() call.
        Try
            _invApp = Marshal.GetActiveObject("Inventor.Application")
        Catch ex As Exception
            Try
                Dim invAppType As Type = _
                GetTypeFromProgID("Inventor.Application")
                _invApp = CreateInstance(invAppType)
                _invApp.Visible = True
                'Note: if you shut down the Inventor session that was started
                'this(way) there is still an Inventor.exe running. We will use
                'this Boolean to test whether or not the Inventor App  will
                'need to be shut down.
                _started = True
            Catch ex2 As Exception
                MsgBox(ex2.ToString())
                MsgBox("Unable to get or start Inventor")
            End Try
        End Try
        'Überprüfung ob Inventor gestartet ist
    End Sub
    'Bei Click auf Button1 wird das selektierte Bauteil ausgeblendet
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        If _invApp.Documents.Count = 0 Then
            MsgBox("Need to open an Assembly document")
            Return
        End If
        If _invApp.ActiveDocument.DocumentType <> _
              DocumentTypeEnum.kAssemblyDocumentObject Then
            MsgBox("Need to have an Assembly document active")
            Return
        End If
        Dim asmDoc As AssemblyDocument
        asmDoc = _invApp.ActiveDocument
        If asmDoc.SelectSet.Count = 0 Then
            MsgBox("Need to select a Part or Sub Assembly")
            Return
        End If
        Dim selSet As SelectSet
        selSet = asmDoc.SelectSet
        Try
            Dim compOcc As ComponentOccurrence
            Dim obj As Object
            For Each obj In selSet
                compOcc = obj
                Debug.Print(compOcc.Name)
                compOcc.Visible = False
            Next
        Catch ex As Exception
            MsgBox("Is the selected item a Component?")
            MsgBox(ex.ToString())
            Return
        End Try
    End Sub
    'Bei Click auf Button1 wird das selektierte Bauteil ausgeblendet
    Private Sub m_ApplicationEvents_OnSaveDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles m_ApplicationEvents.OnSaveDocument
        MsgBox("test")
        If BeforeOrAfter <>EventTimingEnum.kAfter Then
            MsgBox("test")
            Exit Sub
        End If
        MsgBox("test")
    End Sub
End Class
Die "Private Sub m_ApplicationEvents_OnSaveDocument" kann ich über VB Express 2010 einfügen.
Wenn ich nun das ganze ausführe passiert beim speichern allerdings nichts.
Ist das zu einfach gedacht?
Was fehlt noch?
Lässt sich das so überhaupt realisieren.
Bin um jeden Tipp dankbar  
Grüße Flo
Eine Antwort auf diesen Beitrag verfassen (mit Zitat/Zitat des Beitrags) IP