Inventor API Object Model Questions

Inventor API Object Model Questions

btillman
Advocate Advocate
364 Views
1 Reply
Message 1 of 2

Inventor API Object Model Questions

btillman
Advocate
Advocate

I hope I ask this question correctly because I'm still very confused about using the Inventor API Object Model correctly. Lets say I setup a class to launch Inventor like this:

 

class LaunchInventor
    {
        public Inventor.Application vInvApp { get; set; }
        public Inventor.Document vInvDoc { get; set; }

        public LaunchInventor()
        {
            try
            {
                // Check for instance of AutoCAD
                vInvApp = (Inventor.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Inventor.Application");
            }
            catch (Exception) // if none found start a new instance of Inventor
            {
                vInvApp = (Inventor.Application)System.Activator.CreateInstance(System.Type.GetTypeFromProgID("Inventor.Application"));
            }
            if (vInvApp != null)
            {
                vInvApp.Visible = true;
                vInvApp.WindowState = Inventor.WindowsSizeEnum.kMaximize;
            }
        }
}

So if I understand it correctly, and apparently I don't, I should now be able to use the vInvApp object to create a new PartDocument. As I look at the API Object Model I see that PartDocument is under Documents which is under Application. But to create a new PartDocument I have to do this:

 

PartDocument partDoc = (PartDocument)vInvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, vInvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject));

So my thinking that I could do something like:

 

PartDocument pDoc1 = vInvApp.Documents.PartDocument.Add(xxxxxxx)

Is nothing like the required syntax. I'm still learning this API but what am I doing wrong in thinking that I'd just use "." to access the properties deeper down in the model. Suddenly there is this thing with parenthesis which I'm not quite following yet. As when do I need to use parenthesis vs the period?

0 Likes
365 Views
1 Reply
Reply (1)
Message 2 of 2

Owner2229
Advisor
Advisor

Hi, as for me the first sample should work:

PartDocument partDoc = (PartDocument)vInvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, vInvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject));

If it doesn't you can try it like this:

PartDocument partDoc = default(PartDocument);
partDoc = (PartDocument)vInvApp.Documents.Add(DocumentTypeEnum.kPartDocumentObject, vInvApp.FileManager.GetTemplateFile(DocumentTypeEnum.kPartDocumentObject));

The dot is used to access sub routines / functions / types.

The parenthesis is used to pass the values to the routine / function.

 

E.g. Like in this sample for the creation of a new part:

You have Inventor Application: vInvApp

First you need to access it's documents: vInvApp.Documents

Then you want to call the function to add the new document: vInvApp.Documents.Add()

Since the function needs to know what type of document you want to add and what template it should use, you need to pass these values to it.

Some of the document types are:

DocumentTypeEnum.kPartDocumentObject
DocumentTypeEnum.kAssemblyDocumentObject
DocumentTypeEnum.kDrawingDocumentObject

To get the template file you need to first access the file manager: vInvApp.FileManager

And then use the function to get the template: vInvApp.FileManager.GetTemplateFile()

Again you need to pass it the type of the document, so it knows which template to use.

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