Search code examples
python-3.xautodesk-inventor

Extracting parameters from Autodesk Inventor with Python


I am trying to extract the parameters of an Inventor part (.ipt) with Python, using the following code:

#Open Inventor
invApp = win32com.client.Dispatch("Inventor.Application")

#Make inventor visible
invApp.Visible = True

#Set file names of template
Part_FileName_BaseIPT = 'C:\\Base.ipt'

#Open the base model
oPartDoc=invApp.Documents.Open(Part_FileName_BaseIPT)

#Collect parameters
oParams = oPartDoc.ComponentDefinition.Parameters

(It is part of a code snippet I found here: Using python to automate Autodesk Inventor)

I get the following error message: …' object has no attribute 'ComponentDefinition'

Any ideas what´s wrong?

Could it be that I have to tell Python somehow that oPartDoc is related to a Part Document (and not to an Assembly Document). In VBA retrieving the paramters of a part would look like this:

Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument

Dim param As Parameter
Set param = partDoc.ComponentDefinition.Parameters

I suppose that the information given in the first line of VBA is missing so far in the Python code.

This is part of the Inventor API Object model table, which might be helpful for the solution: API Object model

Unfortunately the use of the Inventor API with Python is documented very poorly, also a post in the Autodesk forum did not bring any solution. But since Python is the only programming language I know, I have to rely on it.

I have been trying to solve this for quite a while now, any help would be highly appreciated!

(I use Inventor 2018, Python 3.6.2 (Anaconda) and Windows 10.)


Solution

  • I finally found the solution, the Inventor helpdesk sent it to me:

    import win32com.client
    from win32com.client import gencache, Dispatch, constants, DispatchEx
    
    oApp = win32com.client.Dispatch('Inventor.Application')
    oApp.Visible = True
    mod = gencache.EnsureModule('{D98A091D-3A0F-4C3E-B36E-61F62068D488}', 0, 1, 0)
    oApp = mod.Application(oApp)
    # oApp.SilentOperation = True
    oDoc = oApp.ActiveDocument
    oDoc = mod.PartDocument(oDoc)
    #in case of an assembly use the following line instead
    #oDoc = mod.AssemblyDocument(oDoc)
    prop = oApp.ActiveDocument.PropertySets.Item("Design Tracking Properties")
    
    # getting description and designer from iproperties
    Descrip = prop('Description').Value
    Designer = prop('Designer').Value
    print("Description: ",Descrip)
    print("Designer: ",Designer)
    
    # getting mass and area
    MassProps = oDoc.ComponentDefinition.MassProperties
    #area of part
    dArea = MassProps.Area
    print("area: ",dArea)
    #mass
    mass = MassProps.Mass
    print("mass: ",mass)
    
    #getting  parameters
    oParams = oDoc.ComponentDefinition.Parameters
    lNum = oParams.Count
    print("number of Parameters: ",lNum)
    # make sure the parameter names exist in the Inventor model
    param_d0 = oParams.Item("d0").Value
    print("Parameter d0: ",param_d0)
    param_d1 = oParams.Item("d1").Value
    print("Parameter d1: ",param_d1)
    param_d2 = oParams.Item("d2").Value
    print("Parameter d2: ",param_d2)
    param_d0_exp = oParams.Item("d0").Expression
    print("Parameter d0_exp: ",param_d0_exp)
    param_d1_exp = oParams.Item("d1").Expression
    print("Parameter d1_exp: ",param_d1_exp)
    param_d2_exp = oParams.Item("d2").Expression
    print("Parameter d2_exp: ",param_d2_exp)
    

    original post at the autodesk community web page:

    https://forums.autodesk.com/t5/inventor-customization/how-to-get-parameters-and-mass-of-a-part-with-python/td-p/7553056