Python?

Python?

Anonymous
Not applicable
3,536 Views
32 Replies
Message 1 of 33

Python?

Anonymous
Not applicable
Just wondering if there had been any talk (unofficial) of using Python as the scripting interface to Inventor now that VBA is being phased out by Microsoft. PyIV has a nice ring to it!
0 Likes
3,537 Views
32 Replies
Replies (32)
Message 21 of 33

Anonymous
Not applicable
>how to run the scripts without a python
console window popping up momentarily.

for windows rename your extension to .pyw
0 Likes
Message 22 of 33

Anonymous
Not applicable
I've been busy trying to keep ahead of the developers for the next release
of Inventor and haven't had time do much on the newsgroups.

What you've been playing with is interesting. I know developers that have
written applications using different languages with Inventor. As you've
found, as long as the language supports COM you can use it with Inventor.
Inventor doesn't care what language is used. Once the call gets to Inventor
it's all the same. The problem comes when you try to do some of the more
difficult tasks, which each language is usually quite different in how they
have you do it. For example, how do you set up event handlers? Can you
create a COM component, (which is what you need to do to create an Add-In)?

With limited resources we've chosen to supply samples using the most
commonly used languages, but there's nothing to stop you from using any
language. The biggest issue will be getting support since probably most of
the issues you'll run into will be language specific. For anything I've
done I didn't see any advantage in using any of these "fringe" languages.
Personally, they're fun to experiment with but there are too many potential
issues to risk a project on.

Earlier in this thread there were some questions about the future of VBA. I
can't comment on what the future plans are for Inventor because of legal
reasons but I can tell you the current state of affairs with respect to VBA
and Microsoft. VBA is only supported on 32 bit Windows. Inventor 2009
supports both 32 and 64 bit Windows. For Inventor 2009 64 bit we came up
with a "solution" to this by running VBA as a seperate 32 bit process. This
allows VBA to work but there is a performance hit and a few quirks.
Microsoft is pushing VSTA (Visual Studio Tools for Applications) as a VBA
replacement, although it is different (in both good and bad ways).
--
Brian Ekins
Autodesk Inventor API
0 Likes
Message 23 of 33

Anonymous
Not applicable
Brian,

You can do COM components with python, but that is not relevent to me,
I look at python more as an alternative to VBA, which if it had been
mainlined by AD, many would not now be in the predicament that has
presented itself regarding 64bit, ms dumping VBA, AD not implementing
VSTA already like they did in Revit2009, etc, etc.

It also seems to support event handling of sorts, but I have just
jumped into python, so I'm pretty much a newbie to it. The fact it
can manipulate Inventor in an interactive shell is even better than
the VBA editor, IMHO.

AD's current "solution" is making many people pull their hair out as I
can see from the postings on the ng's.

Bob

Brian Ekins (Autodesk) wrote:
0 Likes
Message 24 of 33

Anonymous
Not applicable
Brian, first thanks for your response, I always enjoy reading your thoughts on the programming side of IV.

>The fact it can manipulate Inventor in an interactive shell is even better than the VBA editor

yes, this is cool. It puts the command line (on steriods) back in IV.

I also liked the fact that it runs on so many OS's, it is more powerful than VBA (supports class inheritance) and is very quick to write code with (no Dim statements!)

I haven't tried VSTA yet so I can't really comment on that.
0 Likes
Message 25 of 33

Anonymous
Not applicable
Python has just crept in to this thread?????

http://discussion.autodesk.com/thread.jspa?messageID=6018804훴
0 Likes
Message 26 of 33

Anonymous
Not applicable
>Python has just crept in to this thread?????
or slithered!
0 Likes
Message 27 of 33

Anonymous
Not applicable
:-))
0 Likes
Message 28 of 33

rschader
Advocate
Advocate
At the time, I was working on my "next" python script conversion, and was digging into the gen_py file created from the Inventor COM tlb file. Attached is my latest python script, a "translation" of the AddOccurrence sample from the Inventor API docs (with a few additions to it). This python sample shows how to "get" an AssemblyDocument object (as opposed to the generic Inventor Document type), and I added some sample messagebox calls to show the activedocument type. Below is the text of the script (possibly wrapped wrong?)

# Inventor test script for pythonwin COM access
# AddOccurrence API Docs Sample
# converted from VBA code by Bob Schader
import win32con
import win32ui
import win32com.client
oApp=win32com.client.Dispatch("Inventor.Application")
doctype = oApp.ActiveDocumentType
if doctype == win32com.client.constants.kPartDocumentObject :
win32ui.MessageBox("Active Document Type: Part Document","Add Occurence")
elif doctype == win32com.client.constants.kAssemblyDocumentObject :
win32ui.MessageBox("Active Document Type: Assembly Document","Add Occurence")
elif doctype == win32com.client.constants.kDrawingDocumentObject :
win32ui.MessageBox("Active Document Type: Drawing Document","Add Occurence")
elif doctype == win32com.client.constants.kPresentationDocumentObject :
win32ui.MessageBox("Active Document Type: Presentation Document","Add Occurence")
elif doctype == win32com.client.constants.kDesignElementDocumentObject :
win32ui.MessageBox("Active Document Type: Design Element Document","Add Occurence")
elif doctype == win32com.client.constants.kForeignModelDocumentObject :
win32ui.MessageBox("Active Document Type: Foriegn Model Document","Add Occurence")
elif doctype == win32com.client.constants.kSATFileDocumentObject :
win32ui.MessageBox("Active Document Type: SAT File Document","Add Occurence")
else:
win32ui.MessageBox("Active Document Type: Error","Add Occurence")

if doctype == win32com.client.constants.kAssemblyDocumentObject :
oDoc=oApp.ActiveDocument
oAsmDoc = win32com.client.CastTo(oDoc, "AssemblyDocument")
oAsmCompDef = oAsmDoc.ComponentDefinition
oTG = oApp.TransientGeometry
oMatrix = oTG.CreateMatrix()
oMatrix.SetToRotation(3.14159265358979/4, oTG.CreateVector(0, 0, 1), oTG.CreatePoint(0, 0, 0))
oMatrix.SetTranslation(oTG.CreateVector(3, 2, 1))
oOcc = oAsmCompDef.Occurrences.Add("D:\\Temp\\Part1.ipt", oMatrix)
win32ui.MessageBox("Occurence Added Complete","Add Occurence")


This sucks! Anyone know how to force the web version of the discussion group to insert preformatted or indented text? I tried the preformat tags but they just disappear.

BTW, don't forget to edit the location of your part to add (near the end of the code).
0 Likes
Message 29 of 33

Anonymous
Not applicable
well, apparently the web preview option doesn't work properly,
as the indents appear correct on the nntp side after posting,
and the preformat tags also appear!
0 Likes
Message 30 of 33

Anonymous
Not applicable
>This sucks! Anyone know how to force the web version of the discussion group to insert preformatted or indented text?

I know what you mean. The Customization newsgroup really needs a [ code ] block that preserves <   >s.
0 Likes
Message 31 of 33

Anonymous
Not applicable
It would be nice. I think it's especially a problem for new programmers
that don't have enough experience to easily see where a line wrapped.
--
Brian Ekins
Autodesk Inventor API
0 Likes
Message 32 of 33

Anonymous
Not applicable
>It would be nice.

yes, and for Python (since we are on the topic 😉 ) because the indention denotes code level. Cutting and pasting Bob's code would not compile because the indention isn't correct.
0 Likes
Message 33 of 33

Anonymous
Not applicable
Well that's also why I added it as a zipfile 😉
Bob S.

Josh_Petitt wrote:
>> It would be nice.
>
> yes, and for Python (since we are on the topic 😉 ) because the indention denotes code level. Cutting and pasting Bob's code would not compile because the indention isn't correct.
0 Likes