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,611 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,612 Views
21 Replies
Replies (21)
Message 2 of 22

bradeneuropeArthur
Mentor
Mentor

Hi,

 

For the massprop you need to do the following:

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

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 3 of 22

bradeneuropeArthur
Mentor
Mentor

Hoi need another declaration

Make the declaration to PartComponentDefinition for parts.

Make the declaration to assemblyComponentDefinition for assemblies.

This should do it!

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 4 of 22

Anonymous
Not applicable

Hi bradeneurope,

 

thanks for your support. Unfortunately this does not help, I still get the same error: ...object has no attribute 'ComponentDefinition'

0 Likes
Message 5 of 22

Anonymous
Not applicable

how do I do this:

 

 

"Make the declaration to PartComponentDefinition for parts.

Make the declaration to assemblyComponentDefinition for assemblies."

 

 

Could you please provide the Python code for it?

0 Likes
Message 6 of 22

bradeneuropeArthur
Mentor
Mentor

Take a look at this:

You need to declare your component definitions as first as part and assembly.

 

 

Dim bA As AssemblyDocument
Set bA = a.ActiveDocument

Dim cA As AssemblyComponentDefinition Set cA = bA.ComponentDefinition Dim dA As MassProperties Set dA = cA.MassProperties
-----------------------------

Dim bP As PartDocument
Set bP = a.ActiveDocument

Dim cP As PartComponentDefinition
Set cP = bP.ComponentDefinition
Dim dP As MassProperties
Set dP = cP.MassProperties


So do not use:

 

.. oDoc.ComponentDefinitions.Massproperties

 

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 7 of 22

dgreatice
Collaborator
Collaborator

"""declaration"""

oPartDoc = PartDocument()
oPropset = PropertySets()
oPartCompdef = PartComponentDefinition()
MassProp = MassProperties()
MassofPart = Double()
oDescrip = Property()
oDesigner = Property()
oParams = Parameters()
oModelParams = ModelParameters()
oModelParam_1 = ModelParameter()

 

"""Set the Object"""

oPartDoc = ThisApplication.ActiveDocument
PropertySets = opartdoc.PropertySets.item('Design Tracking Properties')
oDescrip = propertysets.item('Description')
oDesigner = propertysets.item('Designer ')
oPartCompdef = oPartDoc.ComponentDefinition
MassProp = oPartCompdef.MassProperties
MassofPart = MassProps.Mass
oParams = oPartCompDef.Parameters
oModelParams = oParams.ModelParameters
oModelParam_1 = oModelParams.Item(1)

 

"""Get the Data"""

print(oDescrip.value)
print(oDesigner.value)
print(massofpart)
print(oModelParam_1.Value + ' cm')

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 8 of 22

Anonymous
Not applicable

Hi dgreatice,

 

thank you for your idea!

Unfortunately it is not working yet. I get the following error: NameError: name 'PartDocument' is not defined.

I think the declaration you did is necessary, but somehow Python is expecting it in a different way?!

0 Likes
Message 9 of 22

dgreatice
Collaborator
Collaborator

are python can add reference library?

 

at Visual Studio, I need to add reference library to read class of API Inventor. Like:

 

C:\Program Files\Autodesk\Inventor 2014\Bin\Public Assemblies\Autodesk.Inventor.Interop.dll

 

 

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 10 of 22

bradeneuropeArthur
Mentor
Mentor
Take a look at my last post. This should solved it Maybe.

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 11 of 22

Anonymous
Not applicable

Hi dgreatice,

 

adding the library sounds like the right thing to do. I found out that the module for this is called "ctypes" in Python, however I didn´t manage to install and use it properly so far.

I keep trying ...

0 Likes
Message 12 of 22

dgreatice
Collaborator
Collaborator

hi,

 

try to find any clue from youtube.

 

https://www.youtube.com/watch?v=VHxqGJc8hsM&t=62s

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 13 of 22

Anonymous
Not applicable

Hello,

 

I find myself in the same situation, I only know Python and I have a few scripts that used ComponentDefinition method. They used to work well, on Inventor 2015, but since the company updated to Inventor 2015 SP2 Update 6, a few days ago, my scripts stopped functioning. Autodesk must have modified something in their COM code... Have you found any solution for your problem yet?

 

Best regards,

Liviu

0 Likes
Message 14 of 22

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

Hi @Anonymous,

 

Try the following python code to retrieve mass properties

 

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)

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

dArea = MassProps.Area
print(dArea)

lNum = partDef.Count
print(lNum)

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



Message 15 of 22

Anonymous
Not applicable
Hi chandra.shekar.g,
 
I tried your solution, but I got stuck at:
 
oApp = mod.Application(oApp)
 
 
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: 'module' object is not callable
 
 
I am using Pyhton 2.7.3 32bit on win32 in a PyScriper console.
 
What I find curious is that I have another application written in Python, which uses ComponentDefinition.Parameters, which I 'froze' with py2exe. That one still works fine, but if I copy the source code in the Python interpreter, I get the error posted by our friend ma23UNC:
 
object has no attribute 'ComponentDefinitions'
 
 
It is such a pity, I really enjoyed automating Inventor in Python....
 
Thank you,
Liviu
0 Likes
Message 16 of 22

bradeneuropeArthur
Mentor
Mentor

try this

 

import win64com.client
from win64com.client import gencache, Dispatch, constants, DispatchEx


oApp = win64com.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)

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

dArea = MassProps.Area
print(dArea)

lNum = partDef.Count
print(lNum)

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 17 of 22

bradeneuropeArthur
Mentor
Mentor
Remarks:
Please put your Software versions in your heading so we can see the software version you use.

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 18 of 22

Anonymous
Not applicable

To the best of my knowledge, 'win64com' does not exist (yet?)....

0 Likes
Message 19 of 22

bradeneuropeArthur
Mentor
Mentor

Does this work then:

 

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)

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

dArea = MassProps.Area
print(dArea)

lNum = partDef.Count
print(lNum)

I have used "######" instead of '######'

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 20 of 22

Anonymous
Not applicable

Hi,

 

No, it still does not work. Does it work for you?

 

 




*** Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32. *** *** Remote Python engine is active *** >>> import win32com.client >>> win32com.client <module 'win32com.client' from 'C:\Users\________\Documents\PORTABLE PROGRAM FILES\Portable Python 2.7.3.1\App\lib\site-packages\win32com\client\__init__.pyc'> >>> from win32com.client import gencache, Dispatch, constants, DispatchEx >>> oApp = win32com.client.Dispatch("Inventor.Application") >>> oApp <win32com.gen_py.Autodesk Inventor Object Library.Application instance at 0x45696624> >>> oApp.Visible = True >>> mod = gencache.EnsureModule("{D98A091D-3A0F-4C3E-B36E-61F62068D488}", 0, 1, 0) >>> mod <module 'win32com.gen_py.D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0' from 'C:\Users\______\AppData\Local\Temp\gen_py\2.7\D98A091D-3A0F-4C3E-B36E-61F62068D488x0x1x0\__init__.pyc'> >>> oApp = mod.Application(oApp) Traceback (most recent call last): File "<interactive input>", line 1, in <module> TypeError: 'module' object is not callable

 

Inventor 2015

64-Bit Edition

Build 223, Release 2015 SP2 Update 6

 

Windows 7 Entreprise SP1

0 Likes