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: 

My First Plugin - Lesson 3 C# issue

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
scottmoyse
1827 Views, 7 Replies

My First Plugin - Lesson 3 C# issue

It looks like the Lesson 3 C# code sample is incomplete, there's no private void (equivalent of a private sub from VB?) Form1_FormClosed, as a result Inventor doesn't close correctly when the form is closed. I've written the code like this:

 

private void Form1_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
        {
            if (_started)
                _invApp.Quit();
            _invApp = (Inventor.Application)null;
        }

 

but it doesn't work. It runs/compiles error free, but when I close the form Inventor doesn't close. The VB sample has 'ByVar' in the sub argument (inside the brackets). I wondering if I shoudl be using a C# equivalent here, I just don't know what it is. I appreciate any help that can be given here.


Scott Moyse
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


EESignature


Design & Manufacturing Technical Services Manager at Cadpro New Zealand

Co-founder of the Grumpy Sloth full aluminium billet mechanical keyboard project

7 REPLIES 7
Message 2 of 8
scottmoyse
in reply to: scottmoyse

Ok so I see ByVal isn't the problem because everything is passed as By Value in C#, So somethign else is missing instead..... 


Scott Moyse
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


EESignature


Design & Manufacturing Technical Services Manager at Cadpro New Zealand

Co-founder of the Grumpy Sloth full aluminium billet mechanical keyboard project

Message 3 of 8
scottmoyse
in reply to: scottmoyse

Yippee I got it working. It looks like its a terminology issue:

 

VB = Closed

C# = Closing

 

This is the code that should be in the C# sample code

 

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_started)
                _invApp.Quit();
            _invApp = (Inventor.Application)null;
        }

Note I'm uncertain about the _invApp = (Inventor.Application)null; line of code. I commented it out and it didn't seem to make any difference. Is it just good practice to have it there? in case _invApp is used again in another method later on within this class?

 

 Can someone involved in the My FIrst Plugin program fix this up please?


Scott Moyse
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


EESignature


Design & Manufacturing Technical Services Manager at Cadpro New Zealand

Co-founder of the Grumpy Sloth full aluminium billet mechanical keyboard project

Message 4 of 8
Qube-it
in reply to: scottmoyse

Scott, 

 

Without looking at the rest of the code, I'm assuming that _invApp is already declared as a private variable of type Application.Inventor and that this code is essentially being called when a close event (perhaps on some form that is part of a stand-alone application that controls Inventor?) is being fired.

 

With that said, the setting of the _invApp to null is doing just what you're thinking (i.e. making sure that nothing else in that class can do anything with it). If anything else did happen to call it then there would be a null reference exception thrown and you would most likely know about it. Since your calling the Quit() method inside a method that handles a Form Closing event, I would venture a guess that the entire class will fall out of scope since it's attached to the Form that's closing. Let's put it this way, you probably don't need it but it won't hurt anything to set it to null.

 

As an aside, you shouldn't have to cast the null to Inventor.Application since _invApp is (more than likely) already that type. In other words, if you left that last line of code in there you can remove the casting of Inventor.Application. Like so...

 

private void Form1_FormClosed(object sender, System.Windows.Vorms.FormClosedEventArgs e)
{
    if(_started)
        _invApp.Quit();
    _invApp = null;
}

 

-Brian Hall-
Message 5 of 8
scottmoyse
in reply to: scottmoyse

you assume correctly & confirm my understanding which is awesome. I cast to Inventor.Application cos the sample code did it further up. Even though _invApp had already been assigned to that type. I thought it was unnecessary there as well but since I'm a noob I rolled with it. This makes me feel good 🙂 I'm starting to get it.

Cheers for your time Brian

Scott Moyse
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


EESignature


Design & Manufacturing Technical Services Manager at Cadpro New Zealand

Co-founder of the Grumpy Sloth full aluminium billet mechanical keyboard project

Message 6 of 8
wayne.brill
in reply to: scottmoyse

Hi Scott,

 

I see the _invApp.Quit(); method is called when Form1 is disposed. (in Form1.Designer.cs) If you put a break point on this line does it get hit when you close the Form? (It does in my tests if launching the form started Inventor) 

 

protectedoverridevoid Dispose(bool disposing)

        {

if (disposing && (components != null))

            {

                components.Dispose();

            }

           

base.Dispose(disposing);

           

//Test to see if we started the Inventor Application.

//If Inventor was started by running this form then call the

           

//Quit method.

if (_started)

            {

                _invApp.Quit();

            }

            _invApp =

null;

        }



Wayne Brill
Developer Technical Services
Autodesk Developer Network

Message 7 of 8
scottmoyse
in reply to: wayne.brill

Wayne,

 

thank you for your reply.

 

You had me confused there for a while. I couldn't see where the dispose calls were, then I realised the code you had posted was from two seperate tabs/windows.

 

Yes when I place a stop on that line it gets hit. I see it then moves onto the disposing stuff after that, even though the Inventor process isn't showing up in the task manager anymore.

 

Is it just empyting the memory? How does it know what to clear out? Can you explain a bit more about this dispose method please?

 


Scott Moyse
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.


EESignature


Design & Manufacturing Technical Services Manager at Cadpro New Zealand

Co-founder of the Grumpy Sloth full aluminium billet mechanical keyboard project

Message 8 of 8
wayne.brill
in reply to: scottmoyse

Hi Scott,

 

When I created that Visual Studio 2010 C# Windows Forms Application everything in Form1.Designer.cs  was created automatically exept thie code that I added to Quit Inventor when the Dispose method is called. The dispose method is always called when the form is shut down. This seems like a good place to call the Quit method on the Inventor Application object. (Just to be clear, the Dispose method is not part of the Inventor API).  The Quit method on the Inventor Application object is what removes Inventor from memory.

 

 

 

 

 



Wayne Brill
Developer Technical Services
Autodesk Developer Network

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

Post to forums  

Autodesk Design & Make Report