VB.NET embed viewport into XAML for copy part/assembly

VB.NET embed viewport into XAML for copy part/assembly

w_heuvelmanXKEG7
Explorer Explorer
132 Views
3 Replies
Message 1 of 4

VB.NET embed viewport into XAML for copy part/assembly

w_heuvelmanXKEG7
Explorer
Explorer

Dear form members,

 

I am looking for a solution to get a viewport to embed into XAML UI for the following usecase:

Currently I have a code that copies an part or assembly and re-creates the existing contraints to the new copied part, now I want to add functionality to select the remaining created constraints and with that have a view inside my xaml to create a highlightset with the constraint face of the copied part to then create a promt for a new face etc. 

However if I try to embed the HWND in my xaml it will just crash inventor.

 

Please share your idea's if you have some to solve this and any workaround is appreciated also skipping the embed in xaml window but the focus is to clearly show the highlightset in the copied part to create a new / change the existing constraint.

0 Likes
133 Views
3 Replies
Replies (3)
Message 2 of 4

lauri_barnhart
Autodesk
Autodesk

Welcome to the community, @w_heuvelmanXKEG7.

 

Thank you for posting your question. Could you provide some additional information such as product version and possibly a screen recording showing the crash in real time? Any additional information you can provide would be greatly appreciated.

 

Best,


Lauri | Community Manager
Message 3 of 4

w_heuvelmanXKEG7
Explorer
Explorer

I'm using Autodesk Inventor Profesional 2026 with a VB.NET 4.8 project inside visual studio. The sourcecode is below and should make it clear what I am trying to do/create but is with a bit of copy paste from AI's because I was just trying to make stuff work when changing the HWND of the viewframe to the xaml window did not work.

 

After this I tried also making my program to be always on top but that still hides the inventor viewport/viewframe after this.

 

But basically I'm trying to make a preview window that I can control that will always be on top / docked to the side in inventor like my screenshot below

screenshot.png

 

then while working on a assembly where I can show a changing highlightset of the part I'm copying based on the existing constraints while working in the assemblydocument to re-constraint a copy of the original based on the user input and constraints that were available on the original part. However currently after selecting anything in the assembly it puts that window on top (which is expected for any normal usecase) but I would love to be able to set that particular window to be on top within my code until I don't need it anymore.

This is to give more movability/freedom than to just create a screenshot of the part with the highlightset and display that, I prefer a seperate viewport so I can actually be able to move the part around when the highlightset is not created a clear view of what the user should try to re-constrain.

 

the WH_solutions.Fnc is used for the inventor connection but only has 2 references InvApp (which basically gets the running inventor application).

 

Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Integration
Imports System.Windows.Interop
Imports Inventor
Imports WH_solutions.Fnc

Imports WH_solutions.MainWindow

Public Class TESTshit

    'Old code

    Private PreviewPanel As System.Windows.Forms.Panel
    Private PreviewView As Inventor.View
    Private XamlWindowHandle As IntPtr
    Private UsedAssemblyDocument As Document

    'latest gatgpt addition: 
    Private DockWindow As Inventor.DockableWindow
    Private DockPanel As System.Windows.Forms.Panel
    Private PreviewHost As System.Windows.Forms.Panel

    'Private PreviewRootHandle As IntPtr

    '    <DllImport("user32.dll", SetLastError:=True)>
    '    Private Shared Function SetParent(
    '    hWndChild As IntPtr,
    '    hWndNewParent As IntPtr
    ') As IntPtr
    '    End Function

    <DllImport("user32.dll", SetLastError:=True)>
    Private Shared Function GetWindow(
    hWnd As IntPtr,
    uCmd As UInteger
) As IntPtr
    End Function

    Private Const GW_OWNER As UInteger = 4

    '    <DllImport("user32.dll", SetLastError:=True)>
    '    Private Shared Function SetWindowPos(
    '    hWnd As IntPtr,
    '    hWndInsertAfter As IntPtr,
    '    X As Integer,
    '    Y As Integer,
    '    cx As Integer,
    '    cy As Integer,
    '    uFlags As UInteger
    ') As Boolean
    '    End Function

    <DllImport("user32.dll")>
    Private Shared Function GetAncestor(
    hWnd As IntPtr,
    gaFlags As UInteger
) As IntPtr
    End Function

    <DllImport("user32.dll", EntryPoint:="SetWindowLongPtrW", SetLastError:=True)>
    Private Shared Function SetWindowLongPtr64(
    hWnd As IntPtr,
    nIndex As Integer,
    dwNewLong As IntPtr
) As IntPtr
    End Function

    Private Const GA_ROOT As UInteger = 2

    Private Const GWLP_HWNDPARENT As Integer = -8

    Public Sub New(PreviewPanel As System.Windows.Forms.Panel, xamlWindowHandle As IntPtr)

        Me.PreviewPanel = PreviewPanel
        Me.XamlWindowHandle = xamlWindowHandle

    End Sub
    Public Sub Testing()

        UsedAssemblyDocument = InvApp.ActiveDocument

        '-----------------------------------------
        ' 1. Get the selected occurrence
        '-----------------------------------------
        Dim picked As Object =
            InvApp.CommandManager.Pick(
                Inventor.SelectionFilterEnum.kAssemblyOccurrenceFilter,
                "Select the component to preview"
            )

        If picked Is Nothing Then Return

        Dim occurrence As Inventor.ComponentOccurrence =
            TryCast(picked, Inventor.ComponentOccurrence)

        If occurrence Is Nothing Then Return

        'Get the source document
        Dim sourceDoc As Inventor.Document =
            occurrence.Definition.Document

        'ORIGINAL CODE WORKING________________________________________________

        'Create another view of that document
        PreviewView = sourceDoc.Views.Add

        'Prepare the view
        PreviewView.GoHome()
        PreviewView.Fit()


        'Move this view into its own ViewFrame
        Dim viewFrame As Inventor.ViewFrame =
            PreviewView.ViewTab.MoveToNewViewFrame(
                100,   'Left
                100,   'Top
                400,   'Width
                300    'Height
            )

        'Dim viewFrameHandle As IntPtr = New IntPtr(CLng(viewFrame.HWND)) 'uncommented

        Dim previewHand As IntPtr = New IntPtr(CLng(PreviewView.HWND))

        Dim previewRootHandle As IntPtr = GetAncestor(previewHand, GA_ROOT)

        Dim originalOwner As IntPtr =
    GetWindow(previewRootHandle, GW_OWNER)

        '        MsgBox(
        '    "Preview HWND: " & previewRootHandle.ToString() &
        '    vbCrLf &
        '    "Original Owner: " & originalOwner.ToString() &
        '    vbCrLf &
        '    "XAML HWND: " & XamlWindowHandle.ToString()
        ')

        previewRootHandle = previewRootHandle

        Dim result As IntPtr =
    SetWindowLongPtr64(
        previewRootHandle,
        GWLP_HWNDPARENT,
        XamlWindowHandle
    )

        Dim newOwner As IntPtr =
    GetWindow(previewRootHandle, GW_OWNER)



        '        MsgBox(
        '    "New Owner: " & newOwner.ToString() &
        '    vbCrLf &
        '    "XAML HWND: " & XamlWindowHandle.ToString()
        ')

    End Sub

    Public Sub ClosePreview()

        If PreviewView Is Nothing Then Return

        Try

            UsedAssemblyDocument.Activate()

            PreviewView.Close()

        Catch ex As Exception

            MsgBox("Could not close preview: " & ex.Message)

        End Try

        PreviewView = Nothing

    End Sub

End Class

 

 

@w_heuvelmanXKEG7 - this post has been edited due to Community Rules & Etiquette violation.  

 

0 Likes
Message 4 of 4

w_heuvelmanXKEG7
Explorer
Explorer

Thank you for the feedback, I have now tried to explain it more clearly and added screenshot and the current sourcecode. Basically the crash itself occurred when I tried to take the viewframe / viewport out of inventor and tried to let it be controlled by my own program. This just crashed/shut down inventor without any notice or warning. Unfortunatly I have no video of it and I am not trying to recreate it just to have a video of it.

0 Likes