How to add your knowledge

Demonstration: Getting Started

    Table of contents
    No headers

    Possibly the best introduction to Python's usefulness as a scripting language for Inventor is through an interactive command session with the interpreter.

    One aspect of the Python programming language to be aware of is that it is an interpreted language: rather than compiling your code into an executable form to be run directly by the operating system (such as with programs written in C), the Python interpreter is instead run by the operating system, which then acts upon the instructions in your script.

    The discovery of the interpreted nature of Python is sometimes a source of alarm for those encountering the language for the first time, due to an assumption that execution of the code will be slow.  The reality, however, is that a machine capable of running Inventor will almost certainly run all but the most computationally intensive Python scripts faster than human perception-- certainly faster than would be possible for human interaction with Inventor to accomplish tasks that an automation script is intended to handle.

    On the other hand, the fact that Python is interpreted presents a major advantage when coding in an exploratory vein.  Since the interpreter can be run interactively from the command line or through an IDE, you are free to try out code line-by-line, making small corrections or changes as you go, viewing data changes step by step, and so-forth.

    If you haven't already done so, download and install Python and Python for Windows.  Note that there are several versions of Python available, at the time of this writing the latest version is 3.1  It is recommended for the time being to choose version 2.6, however.  Availability of the various extensions tend to slightly lag behind the development of the language, so it is generally advisable to choose the installers listed in the top page of this topic unless you have verified that all extensions you might need are available for the Python version you choose.

    Before starting, create a folder named 'C:\inventor_python'.  Once you've done so, download the zip file listed in the files section of this page.  It contais the Inventor part 'get-started-part.ipt' (created with version 2008).  Unzip this part into the inventor_python folder.

    Note that due to current limited permissions on file extension uploads in the wiki, the file has been uploaded with a bogus ".txt" extension.  Simply remove .txt from the file name to access the zip file.

    Next, start the Python interpreter using the IDE provided with PythonWin.  Assuming you have installed Python v2.6, this is probably located at 'C:\Python26\Lib\site-packages\pythonwin\Pythonwin.exe'

    Import the module containing COM functionality by entering the following

    import win32com.client

    Then create a COM link between Python and Inventor by entering

    oApp=win32com.client.Dispatch("Inventor.Application")
     

     


    You may have noticed one of two things after the Dispatch command: 1.) nothing at all, if Inventor is already running, or 2.) a pause before the IDE prompts for entry of the next command if Inventor was not running.  If the case is 2.), then you might take a look at the process list at this time to note that Inventor is now running in the background:

     


    At this time, make Inventor visible by entering 'oApp.Visible=True'.  When doing this, you will notice a couple more things: 1.) the PythonWin IDE features command completion, which you can take advantage of by pressing the Tab key when the desired method or property is highlighted,

     

    and 2.) the previously invisible instance of Inventor becomes visible.

    Next, open the sample part file by entering 'oPartDoc=oApp.Documents.Open('C:\\inventor_python\\get-started-part.ipt')' (the double-backslash is required because the backslash is a special character in Python).  There will be another pause while the part file opens in Inventor.
     

     


    Now use the IDE for a little exploration: first, create an object containing the part's parameters by typing 'oParams=oPartDoc.ComponentDefinition.Parameters'.  The oParams object is a list of custom objects, each object containing Name and Value properties for the parameters.  The list can be viewed using a For statement:
     

     


    Notice that the first element in the oParams list is the width of the part.  This parameter can be modified, try it by entering 'oParams[0].Value=3.81'.  You will notice no apparent change in your part-- update the part by entering 'oPartDoc.Update()'.

     

    This concludes the introduction to Inventor scripting with Python.  To learn more advanced techniques for accessing Inventor objects from Python, see the next demonstration on early binding.