Importing an assembly document into python

Importing an assembly document into python

nj21997
Participant Participant
1,408 Views
8 Replies
Message 1 of 9

Importing an assembly document into python

nj21997
Participant
Participant

Hi,

 

I'm having difficulty importing an assembly file into python. There seems to be a conflict between the output when I check Document.Type and Document.DocumentType. The second recognises that the file is an an assembly. But the first returns the ‘Document’ ObjectType which needs to also be an assembly one so that I can manipulate it properly.

 

Does anyone know how I can force the object type to change?

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

JelteDeJong
Mentor
Mentor

Are you using python for Inventor or fusion? 

Jelte de Jong
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


Blog: hjalte.nl - github.com

0 Likes
Message 3 of 9

nj21997
Participant
Participant

Currently inventor, but it does look like restarting with fusion might save a lot of hassle

0 Likes
Message 4 of 9

JelteDeJong
Mentor
Mentor

I was not aware that it was possible to automate Inventory with python. Usually you would vb.net or C# (or C++). just out of curiosity how did you manage to get the inventor objects in python?

 

The .Type is a general property that you will find on almost all Inventor object. 

The .DocumentType is more specific.

And if you need to fine more specific information you should look at the Document.SubType. For example if you want to know if you have a sheetmetal part.

https://adndevblog.typepad.com/manufacturing/2013/01/inventor-document-sub-types.html 

Jelte de Jong
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


Blog: hjalte.nl - github.com

0 Likes
Message 5 of 9

nj21997
Participant
Participant

It works to some extent. I think the issue must be that you cannot define the variable type before setting its type.

 

Here is the code I have:

import win32com.client
from win32com.client import gencache, Dispatch, constants, DispatchEx, GetActiveObject

# tries to open inventor or creates a new instance
try:
invApp = GetActiveObject('Inventor.Application')
except:
invApp = Dispatch('Inventor.Application')
invApp.Visible = True

# No idea what this does but was recommended by the guy who wrote it originally
mod = gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
invApp = mod.Application(invApp)

# Defines the active doc as a variable
oPartDoc = invApp.ActiveDocument


print(oPartDoc.Type)
print(oPartDoc.DocumentType)

 

Both the type and document type return different values. Being:

oPartDoc.Type - 50332160 which is the ObjectTypeEnum for Document

oPartDoc.DocumentType - 12291 which measn Assembly Document

oParts = oPartDoc.ComponentDefinition.BOM

 When I try to return the BOM I get an error stating:

'<win32com.gen_py.Autodesk Inventor Object Library.Document instance at 0x2791884160736>' object has no attribute 'ComponentDefinition'

 

I hope this clarifies where the issue comes from. I hope I'm just doing something simple wrong.

 

Many thanks,

Rob

0 Likes
Message 6 of 9

JelteDeJong
Mentor
Mentor

the property "invApp.ActiveDocument" will always return a object of the type "Document". the "Document" type does not have a property "ComponentDefinition". The type "PartDocument" has this property. Therefore you need to tell the compilere that it is save to treat the "invApp.ActiveDocument"as a "PartDocument". In VB.net you would do it like this:

Dim oPartDoc As PartDocument = invApp.ActiveDocument

 or in C# (which is a bit more strict)

PartDocument oPartDoc = (PartDocument)invApp.ActiveDocument

But I dont know how you would cast in Python.

Jelte de Jong
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


Blog: hjalte.nl - github.com

0 Likes
Message 7 of 9

nj21997
Participant
Participant

You hero, I found the casting thing.

For reference that is what the mod thing is for:

oAssemblyPartDoc = mod.AssemblyDocument(oPartDoc)

 This turns the regular document:

Autodesk Inventor Object Library.Document instance

 

Into an assembly document:

Autodesk Inventor Object Library.AssemblyDocument instance

0 Likes
Message 8 of 9

Michael.Navara
Advisor
Advisor

The variable 'mod' contains reference to  "Autodesk Inventor Object Library" which describes the Inventor API objects.

In my opinion you can rename this variable to something like "inventorApi". 

see registry for more info

HKEY_CLASSES_ROOT\TypeLib\{D98A091D-3A0F-4C3E-B36E-61F62068D488}\1.0

Message 9 of 9

JelteDeJong
Mentor
Mentor
Accepted solution

After some googling I found this 

https://stackoverflow.com/questions/47443621/extracting-parameters-from-autodesk-inventor-with-pytho... 

It looks like a nice example

Jelte de Jong
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


Blog: hjalte.nl - github.com