How to add a WPF window to an Add-In
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I'm trying to convert my plugins to proper add-ins with limited success. I have problems trying to add a WPF window as a UI and attaching some code to it.
For example: I have a simple code snippet that creates a work point in the center of mass. It works while the code is executed by a click event of a button I've added to my ribbon, which is fine for a simple feature like this.
For more complex add-ins I want the click event of a button on the ribbon to open a WPF window which contains its own set of buttons and controls which will execute my code.
Here is a code snippet that shows what happens when a MyButton (that's the button added to the ribbon) is clicked. This implementation works
Private Sub MyButton_OnExecute(Context As NameValueMap)
Try
Dim oPartDoc As PartDocument
Dim oPartCompDef As PartComponentDefinition
oPartDoc = _inventor.ActiveDocument
oPartCompDef = oPartDoc.ComponentDefinition
Dim oCenterOfMass As Point
Dim oWorkPoint As WorkPoint
oCenterOfMass = oPartCompDef.MassProperties.CenterOfMass
oWorkPoint = oPartCompDef.WorkPoints.AddFixed(oCenterOfMass)
Catch ex As Exception
MsgBox("Center of mass property could not be found")
End Try
End Sub
Now I've changed MyButton so that clicking it shows a WPF window called Window1:
Private Sub MyButton_OnExecute(Context As NameValueMap)
Dim window As New Window1
window.Show()
End Sub
That window contains just one button which should put a work point in the center of mass. The Window1 code looks like this:
Imports System.Runtime.InteropServices
Imports Inventor
Public Class Window1
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Try
Dim _inventor As Inventor.Application
Dim oPartDoc As PartDocument
Dim oPartCompDef As PartComponentDefinition
oPartDoc = _inventor.ActiveDocument
oPartCompDef = oPartDoc.ComponentDefinition
Dim oCenterOfMass As Point
Dim oWorkPoint As WorkPoint
oCenterOfMass = oPartCompDef.MassProperties.CenterOfMass
oWorkPoint = oPartCompDef.WorkPoints.AddFixed(oCenterOfMass)
Catch ex As Exception
MsgBox("Center of mass property could not be found")
End Try
End Sub
End Class
Now there is some bug on line 11. I can't get a reference to active document but I don't know how to fix it. I'm no developer and I already find add-in creation pretty arcane.
I've created my add-in following a tutorial at: Jelte de Jong (hjalte.nl).
The entire visual studio project in its broken state is in the attachments ("CenterOfMass - project files.zip")
I've also added a compiled, working version that doesn't use a WPF window. It should appear in Part environment in 3d model tab in work features panel ("CenterOfMass - compiled and working.zip").
The project contains:
- a CenterOfMass