My first Addon (HELP!!!!)

My first Addon (HELP!!!!)

Anonymous
Not applicable
1,097 Views
8 Replies
Message 1 of 9

My first Addon (HELP!!!!)

Anonymous
Not applicable

I have lots of custom tools as VB & macros, I'm trying to improve on them and change them into Addons.

The problem I am having (have been hitting my head against a wall trying to solve this) What I'm trying to do is have an addon that fires when the user creates a new part document. I can have a form open up fine, but I can't get the form to talk to the active inventor part document.

 

So far I have added this into the StandardAddinServer.Vb section:

 

Public Sub oAppEvents_OnNewDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) Handles oAppEvents.OnNewDocument

Dim oApp As Application = m_inventorApplication

Dim Doc As Document = oApp.ActiveDocument

If Doc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

Dim DwgForm As New Form1

DwgForm.Show()

End If

End Sub

 

 

And this code in my form:

 

Imports Inventor
Imports System.Drawing
Imports System.Windows.Forms
Imports Microsoft.Win32
Imports System.Runtime.InteropServices


Public Class Form1

    'Inherits System.Windows.Forms.Form
    'Dim form2 As New DimChange
#Region "Data Members"
    Private Shared m_inventorApplication As Inventor.Application
    'Private Shared form2 As DimChange
#End Region

#Region "Properties"
    Public Shared Property InventorApplication() As Inventor.Application
        Get
            InventorApplication = m_inventorApplication
        End Get
        Set(ByVal Value As Inventor.Application)
            m_inventorApplication = Value
        End Set
    End Property
  
#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim oDoc As Document = InventorApplication.ActiveDocument



    End Sub
End Class

 

But I keep getting the error "Object reference not set to an instance of an object" (Attached)

 

If anyone can help with this that would be great, like I said it's my first time trying to write an addon, I have gone through the inventor samples and tried to work it out for myself but with no luck.

0 Likes
Accepted solutions (1)
1,098 Views
8 Replies
Replies (8)
Message 2 of 9

Owner2229
Advisor
Advisor
Accepted solution

Hi,

paste this to new empty document (eg. Globals.vb)

Module Globals
	
	Public InventorApplication As Inventor.Application
    
End Module

 

Then in your StandardAddinServer.vb: (replace XXX with new Guid, look for a generator in VB)

Imports Inventor
Imports System.Runtime.InteropServices
Imports System.Configuration

Namespace MyAwesomeAddin
    <ProgId("MyAwesomeAddin.StandardAddInServer"), _
    GuidAttribute("XXXXXXXX-XXXX-XXXX-XXX-XXXXXXXXXXXX")> _
    Public Class StandardAddInServer
        Implements Inventor.ApplicationAddInServer

   Private m_AppEvents As ApplicationEvents Public Sub Activate(ByVal AddInSiteObject As Inventor.ApplicationAddInSite, ByVal FirstTime As Boolean) Implements Inventor.ApplicationAddInServer.Activate ' Initialize AddIn members InventorApplication = AddInSiteObject.Application ' Add event handlers m_AppEvents = InventorApplication.ApplicationEvents AddHandler m_AppEvents.OnSaveDocument, AddressOf Me.m_Events_OnSaveDocument AddHandler m_AppEvents.OnOpenDocument, AddressOf Me.m_Events_OnOpenDocument
AddHandler m_AppEvents.OnNewDocument, AddresOf Me.m_Events_OnNewDocument End Sub
Public Sub Deactivate() Implements Inventor.ApplicationAddInServer.Deactivate ' Remove event handlers RemoveHandler m_AppEvents.OnSaveDocument, AddressOf Me.m_Events_OnSaveDocument RemoveHandler m_AppEvents.OnOpenDocument, AddressOf Me.m_Events_OnOpenDocument
RemoveHandler m_AppEvents.OnNewDocument, AddressOf Me.m_Events_OnNewDocument m_AppEvents = Nothing Marshal.ReleaseComObject(InventorApplication) InventorApplication = Nothing System.GC.WaitForPendingFinalizers() System.GC.Collect() End Sub Private Sub m_Events_OnSaveDocument(ByVal DocumentObject As Inventor._Document, ByVal BeforeOrAfter As Inventor.EventTimingEnum, _ ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum) If BeforeOrAfter = EventTimingEnum.kBefore Then ' Something to do before document saves Else If BeforeOrAfter = EventTimingEnum.kAfter Then ' Something to do after document saves End If End Sub

Private Sub m_Events_OnNewDocument(ByVal DocumentObject As Inventor._Document)
If DocumentObject.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
Dim DwgForm As New Form1()
' .ShowDialog for modal form, .Show for non-modal form
DwgForm.ShowDialog()
End If
End Sub

   End Class
End Namespace

 

And in you Form code:

Imports Inventor

Public Class Form1

Private oDoc As Inventor.Document

Public Sub New()
Me.InitializeComponent()
oDoc = InventorApplication.ActiveDocument
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' It'll show you the document's display name on Button1 click
' It's just to test it...

MsgBox(oDoc.DisplayName) End Sub
End Class

 

It's all written from head, so there might be some errors, but I believe the debuger will let you know about them.

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 9

Anonymous
Not applicable

Hi!

 

Thanks for the quick reply!

 

I'm getting a couple of errors, which I don;t' understand

for this line (specifically the  Me.m-Events_OnsOpenDocument  part.)

 

AddHandler m_AppEvents.OnOpenDocument, AddressOf Me.m_Events_OnOpenDocument

I get this error: 'm_Events_OnOpenDocument' is not a member of 'HowdenNewObject.HowdenNewObject.StandardAddInServer'.

 

 and the same for

 

AddHandler m_AppEvents.OnNewDocument, AddressOf Me.m_Events_OnNewDocument

Error 3 'm_Events_OnOpenDocument' is not a member of 'HowdenNewObject.HowdenNewObject.StandardAddInServer'. 

 

 

if there's anything you can suggest that would be great.

 

Thanks.

0 Likes
Message 4 of 9

Owner2229
Advisor
Advisor

Oh yea, well, there's missing one Sub_Routine, you can eighter delete the lines with the error (one for AddHandler and one for RemoveHandler) or add this Sub below the others in the Class (it muss be above "End Class").

I have added the "m_Events_OnSaveDocument" and "m_Events_OnOpenDocument" only because they might be handy for your future use.

E.g. Im ussing the Before and After document open to count how long it takes to open realy big assys.

 

Private Sub m_Events_OnOpenDocument(ByVal DocumentObject As Inventor._Document, ByVal FullDocumentName As String, _
    ByVal BeforeOrAfter As Inventor.EventTimingEnum, ByVal Context As Inventor.NameValueMap, ByRef HandlingCode As Inventor.HandlingCodeEnum)
    If BeforeOrAfter = EventTimingEnum.kBefore Then
        ' Something to do before the document opens
    Else If BeforeOrAfter = EventTimingEnum.kAfter Then
        ' Something to do after the document opens
    End If
End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 5 of 9

Anonymous
Not applicable
Mike,
Thanks again for the help thus far, am almost there!
When I try debugging the code I get right up to the public sub new routine in the form, but no matter what I try I can't get past the line
"oDoc = m_inventorApp.ActiveDocument"

I get the error message:
NullReferenceException was unhanded by user
Object reference not set to an instance of an object
Use the “new” keyword to create an object instance.

the finish line is in sight, just need one last push to get over it.

Thanks,

Neil
0 Likes
Message 6 of 9

Owner2229
Advisor
Advisor

Hi, if you set up all as i suggested, than you should use this line in your form code:

 

oDoc = InventorApplication.ActiveDocument

 

Because it is declared in "Globals.vb".

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 7 of 9

Anonymous
Not applicable

Got it!

 

I had to put the line into the Globals module, that's what did the trick.

 

Public oDoc As Inventor.Document

 

Thanks again for all you help! Smiley Very Happy

 

0 Likes
Message 8 of 9

Jef_E
Collaborator
Collaborator

Hi,

 

 

I'm also trying to create ann addon. I followed this thread but I get the following error..

 

The command "call "%VS140COMNTOOLS%vsvars32"
			mt.exe -manifest "C:\Users\jeee\AppData\Local\Temporary Projects\InventorAddIn1\InventorAddIn1.X.manifest" -outputresource:"C:\Users\jeee\AppData\Local\Temporary Projects\InventorAddIn1\bin\Debug\InventorAddIn1.dll";#2" exited with code 9009.		

Any ideas? What version of VS are you using?



Please kudo if this post was helpfull
Please accept as solution if your problem was solved

Inventor 2014 SP2
0 Likes
Message 9 of 9

Owner2229
Advisor
Advisor

Hi,

first, you should've have started your own thread.

 

Second your problem is not code related. You have an issue with the compiler. I had the same.

Look for "Post-Build Event" in your Project Options (I'm ussing SharpDevelop, so I cant tell you where exactly in VS it is).

Try to play with the "Post-Build Event" command line. I'm ussing this below, so try it and if it won't work then try something betwee it and your original command.

call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" mt.exe -manifest "$(ProjectDir)$(AssemblyName).X.manifest" -outputresource:"$(TargetPath)";#2

Look in the folder and check if the file is there:

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat".

 

If not, try to look around, maybe "\Microsoft Visual Studio 9.0\" or smtg.

 

Or the issue might be in the manifest file itself. Does your's look like this, when you open it in text editor?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity name="My Assembly Name" version="1.0.0.0" />
  <clrClass clsid="{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX}" 
            progid="MyAssembly.StandardAddInServer" 
            threadingModel="Both" 
            name="MyAssembly.MyAssembly.StandardAddInServer" 
            runtimeVersion="" />
  <file name="MyAssembly.dll" hashalg="SHA1" />

Instead of XXX...XXX you should generate new GUID. Look for GUID generator in VS. In SharpDevelop it is in "Edit > Insert > Insert New GUID"

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes