Autodesk Inventor API with Python

7.2k views Asked by At

I try to use Python for Autodesk Inventor API. For example, I try to make a new sketch in an Inventor part document that I make with Python.

But I have a problem with adding sketches. All of my tries were wrong. Here's an example of code (one of a lot):

import win32com.client

from win32com.client import constants


invApp = win32com.client.Dispatch("Inventor.Application")
invApp.Visible = True

asd = invApp.Documents.Add(constants.kPartDocumentObject,"",True)

nsk = asd.PartDocuments.ComponentDefinition.Sketches.Add()

There is an error:

AttributeError: '<win32com.gen_py.Autodesk Inventor Object Library.Document instance at 0x284773312>' object has no attribute 'PartDocuments'

I tried to find the right way to call the attributes but I have not had any success.

3

There are 3 answers

2
Augusto Goncalves On

I'm not expert on Python... but the Documents.Add method return the new document, therefore the asd should contain the PartDocument type

asd = invApp.Documents.Add(constants.kPartDocumentObject,"",True)
nsk = asd.ComponentDefinition.Sketches.Add()
0
dable On

As others have mentioned. Documents.Add() returns a Document object. We need to cast it to a partDocument like this.

invDoc = invApp.Documents.Add(constants.kPartDocumentObject,"",True)
invPartDoc = win32com.client.CastTo(invDoc, 'PartDocument')
xyPlane = invPartDoc.ComponentDefinition.WorkPlanes.Item(3)
sketch = invPartDoc.ComponentDefinition.Sketches.Add(xyPlane)
0
Brian Ekins On

This is an old question and I'm not sure what the solution is in Python, but I know what the problem is. The Documents.Add method is typed to return a Document object. The Document object does not support the ComponentDefinition property. However, in reality, a specific type of document is being returned. In this case, a PartDocument object is actually being returned and it does support the ComponentDefinition object. I don't know how you would cast a variable in Python so it knows it is really a PartDocument and not the base class Document object.