How to get parameters and mass of a part with Python

How to get parameters and mass of a part with Python

Anonymous
Not applicable
15,631 Views
21 Replies
Message 1 of 22

How to get parameters and mass of a part with Python

Anonymous
Not applicable

Hi all,

 

I am trying to get the list of parameters and mass properties from a .ipt, but it´s not working. Retrieving description and designer from the iproperties is in contrast no problem.

 

I´m a mechanical engineer and I´m using Python because it´s the only programming language I know.

 

This is my code:

 

import win32com.client
from win32com.client import gencache


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
prop = oApp.ActiveDocument.PropertySets.Item("Design Tracking Properties")

# getting description and designer from iproperties (works)
Descrip = prop('Description').Value
Designer = prop('Designer').Value
print(Descrip)
print(Designer)

# getting mass and parameters (doesn´t work)
MassProps = oDoc.ComponentDefinition.MassProperties
partDef = oDoc.ComponentDefinition.Parameters

 

and this is the error message I get:

 

File "M:/Python/Idas_Arbeit/inventor_7.py", line 21, in <module>
    oMassProps = oDoc.ComponentDefinitions.MassProperties
  File "M:\zzz_Anaconda\lib\site-packages\win32com\client\__init__.py", line 473, in __getattr__
    raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))
AttributeError: '<win32com.gen_py.Autodesk Inventor Object Library.Document instance at 0x2937542647032>' object has no attribute 'ComponentDefinitions'

 

I use Inventor 2018 and Python 3.6.2. on Windows 10.

 

 

Any ideas? Your help would be very much appreciated!

 

0 Likes
Accepted solutions (2)
15,632 Views
21 Replies
Replies (21)
Message 21 of 22

bradeneuropeArthur
Mentor
Mentor
I use vb.net!

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 22 of 22

Anonymous
Not applicable
Accepted solution

Hi All,

sorry for my late answer. I wanted to share the final solution for getting the actual parameters of a part. It was sent to me by chandra.shekar.g via email.

 

 

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)