2

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 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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()

2 Comments

thanks. I tried like this, but there is the same mistake: object has no attribute 'ComponentDefinition'.
I think it also can be incorrect work of module "win32com.client"
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.