Friday, February 5, 2010

Where are all the posts I promised?

Why oh why have I not delivered on my promise to post neat stuff you can do with RevitPythonShell? Well, what exactly would you want to do with it? I use this tool to scratch an itch - automate a plugin I am writing. I mainly use the Interactive button and type away at the prompt. I also have some canned scripts I like to use, but I'm not sure if posting them here would make a lot of sense: They are very specific to the plugin. But I'll show a little bit, just for the curious among you:

Python 2.5.0 () on cli
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> apps = [a for a in __revit__.LoadedApplications if 'DpvApplication' in str(a)]
>>> dpv = apps[0]
>>> dir(dpv)
['ActiveDocument', 'Calculators', 'GetHistory', 'OnShutdown', 'OnStartup', 'Result', 'TakeSnapshot', '__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__']
>>> dpv.TakeSnapshot()
>>> snapshot = dpv.GetHistory(dpv.ActiveDocument).CurrentSnapshot
>>> for room in snapshot.Rooms:
...    print room.Area, room.Name
... 
163.14989375 Raum 1
>>> 


Right after the banner, I create a reference to my plugin. This uses the LoadedApplications collection on Autodesk.Revit.Application. As you can see, I then proceed to exercise the plugin, calling its methods (TakeSnapshot, GetHistory, ActiveDocument) etc. You can call any public methods defined in your plugin this way!

But working in the interactive shell for some period has shown some of its weaknesses. I'm busy trying to fix that, but this might still take a while - the last couple of revisions have gone in that direction (but there is no new release yet).

Since this is a tool to make exploring your (C#) plugins easier, it makes sense to customize it a bit. I have configured RevitPythonShell.xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<RevitPythonShell>
  <SearchPaths>
    <!-- a list of paths to add to the engines search path -->
    <SearchPath name="C:\Python25\Lib"/>
    <SearchPath name="C:\RevitPythonShell"/>
  </SearchPaths>
  <Commands>
    <!-- a list of preconfigured commands -->
    <Command name="dumpdpvfolder" src="C:\RevitPythonShell\Commands\dpvdumpfolder.py"/>
    <Command name="read model" src="C:\RevitPythonShell\Commands\readmodel.py"/>
    <Command name="Report" src="C:\RevitPythonShell\Commands\report.py"/>
    <Command name="Interactive" src="C:\RevitPythonShell\Commands\interactive.py"/>
  </Commands>
  <DefaultScript><![CDATA[
# type in a python script to run here and click "Execute"
# you can access the Autodesk.Revit.Application object with the variable __revit__
# the default script (shown below) opens up an interactive interpreter

import code
code.interact(None, None,
  {
    '__name__' : '__console__',
    '__doc__'  : None,
    '__revit__': __revit__
  })]]></DefaultScript>
</RevitPythonShell>


Notice the Commands section: These are shortcuts to stuff I execute often. Conserved scripts that use the technique explained above to exercise the DesignPerformanceViewer plugin.

No comments:

Post a Comment