Friday, November 12, 2010

Listing Level Elevations with RevitPythonShell

I recently stumbled apon the Revit APP blog and spotted a simple code listing for getting all level elevations. This should be a no-brainer using the RevitPythonShell (RPS), so I fired up a shell and started typing. This is a session transcript in the shell:

>>>import clr

>>>clr.AddReference('RevitAPI')

>>>import Autodesk.Revit.DB

>>>collector = Autodesk.Revit.DB.FilteredElementCollector(__revit__.ActiveUIDocument.Document)

>>>elems = collector.WhereElementIsNotElementType().OfClass(Autodesk.Revit.DB.Level).ToElements()

>>>for lv in elems:
...    print '%s: %.2f' % (lv.Name, lv.Elevation)
...
Level 1: 0.00
Level 2: 9.84
>>>

I am getting a bit fed up of typing import clr and referencing RevitAPI whenever I use RPS so my next step will be to try and add that by default. I'll keep you posted about that.

Wednesday, November 10, 2010

Unit Testing Revit Plugins

Yesterday, Jeremy Tammik brought an interesting thread on augi (Unit testing with Revit API) to my attention, suggesting that I might like to add something on the RevitPythonShell (RPS).

I couldn't register as a member on augi (some mail thing I don't want to digg into), so I thought I'd add my thoughts here.

First a disclaimer: I'm not test infected. I don't floss my teeth every day either - unit testing is just one of those things you should be doing, but nothing really bad happens if you don't. Especially if you use tooth picks etc.

Unit tests (as I understand the term) cannot be dependent on an environment (in this case: Revit). So running a test as a Revit plugin with a Revit document is not a unit test. It is an integration test. I do plenty of these. Here is how:

Using the RPS, I create a "driver" script that loads the assembly to be tested (see the loadplugin module) and executes its public methods, much the same way as the IExternalCommand implementation would. Note here, that I tend to keep my IExternalCommand implementations "dumb", refactoring the logic to other parts of the assembly, so that I don't have to test the IExternalCommand itself (it should just be evident that it works, e.g. instantiate plugin object, call public method on it passing some command data, return).

The "driver" script can be registered as an RPS command and will then show up in the Revit Ribbon. You can even assign a keyboard shortcut to it using the standard Revit procedure for shortcuts (you will have to restart Revit after registering the RPS command first, as it only then gets promoted to an IExternalCommand). Run it often (after every compile)!

I generally don't register plugins that are under development with Revit, so that Revit doesn't lock the assembly on startup - this way, I can keep the source open in Visual Studio and build to my hearts content. The "driver" script will just pick up the newest build and test it.

Anything written to "Debug.WriteLine" will be output in the RPS window. This is handy for testing stuff!

Once you are at the "driver" script level, you can employ pythons unit testing libraries: unittest and also use the doc tests. This also keeps your tests separate from your production code, especially since you are using another language to write the tests!

If you really want to do unit testing, you will have to go the full monty - this is the reason for unit testing anyway:

  • Split up your code into Revit dependent functionality and modules.
  • Make sure your plugin abstracts from the Revit stuff into a logical model that you can then mock and write tests to.

  • Regard the Revit API as an I/O interface or a database connection that is not part of unit testing.

Tuesday, November 9, 2010

Revit Room Boundaries

This came up today at work: For a given room, what are the room bounding objects?

Questions like these can easily be solved with RevitPythonShell. So I fired up an RPS shell and came up with the following script:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB.Architecture import Room
from Autodesk.Revit.DB import Wall

sel = __revit__.ActiveUIDocument.Selection.Elements
rooms = [e for e in sel if type(e) is Room]
for room in rooms:
    for bsa in room.Boundary:     # bsa = BoundarySegmentArray
        for bs in bsa:            # bs = BoundarySegment
            print bs.Element

This gets you quite close. I added a featured script to the RPS site that can be used as an RPS command and prints the element id for boundary segments that are model elements.

Wednesday, October 6, 2010

Obfuscating an Autodesk Revit Plugin

Jeremy Tammik mentions obfuscation for Revit plugins in his post Obfuscation Tools. I would like to mention two free obfusctators I have used:

Both of these can be hooked up to your build script so that they obfuscate automatically. Please read their documentation on how to do this.

I liked Eazfuscator a lot and used it for all dependant assemblies of my project, but sadly, it chokes on references to the assembly RevitAPI. So for developing Autodesk Revit Plugins, it will not work.

Babel .NET can obfuscate assemblies that reference RevitAPI.dll. I tied it into my build process in Visual Studio 2010 as a Post-build event command line:

  1. select your project in the Solution Explorer

  2. right-click and choose Properties from the context menu

  3. in the tab Build Events, enter the following in the field Post-build event command line:

    if /I "$(ConfigurationName)" NEQ "Debug" "C:\Program Files\Babel\Babel.exe" "$(TargetPath)" --stringencrypt --output "$(TargetPath)"
    

This assumes, of course, that you have installed Babel .NET to C:\Program Files\Babel. Also note, that the build configuration "Debug" is excluded from obfuscation.

Thursday, August 19, 2010

Deleting a bunch of Elements By ElementId in RevitPythonShell

I know I promised examples for RevitPythonShell and never delivered. Well, here is an example I came across this morning:

I had to delete a bunch of elements from a document. I new the ids of the elements and was about to manually delete them using the "Select by ID" tool in Revit and deleting them one by one. I know this works, because I did the exact same thing yesterday on the exact same document, but forgot to save the changes. I'd just have to do all that tedious work again.

But then I thought: Wait a minute... aren't we supposed to be scripting this stuff?

So, here is a session in the shell part of RevitPythonShell

>>>ids = [136116, 136119, 136120, 136121, 136122, 136131, 136132, 136133, 136134, 136135, 136136, 136137, 136138, 136139, 136140, 136141, 136142, 136143, 136144, 136145, 136155, 136165, 136206, 136306, 136309, 136419, 136422]

>>>import clr

>>>clr.AddReference("RevitAPI")

>>>from Autodesk.Revit.DB import *

>>>transaction = Transaction(doc, "find all the mutants!")

>>>transaction.Start()

>>>for id in ids:
...    doc.Delete(doc.get_Element(ElementId(id)))
...
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002D [136116]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002E [136119]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000002F [136120]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000030 [136121]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000031 [136122]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000032 [136131]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000033 [170627]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000034 [136132]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000035 [136133]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000036 [136134]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000037 [170628]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000038 [136135]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000039 [136136]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003A [136137]>, <Autodesk.Revit.DB.ElementId object at 0x000000000000003B [170629]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003C [136138]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003D [136139]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000003E [136140]>, <Autodesk.Revit.DB.ElementId object at 0x000000000000003F [170630]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000040 [136141]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000041 [136142]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000042 [136143]>, <Autodesk.Revit.DB.ElementId object at 0x0000000000000043 [170631]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000044 [136144]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000045 [136145]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000046 [136155]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000047 [136165]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000048 [136206]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x0000000000000049 [136306]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004A [136309]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004B [136419]>])
List[ElementId]([<Autodesk.Revit.DB.ElementId object at 0x000000000000004C [136422]>])
>>>transaction.Commit()

<Autodesk.Revit.DB.TransactionStatus object at 0x000000000000004D [Committed]>
>>>

Here a few notes that might illuminate some parts of working with RevitPythonShell:

  • access to the Revit API is added by the following lines
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
  • the document is provided via the __revit__ variable
doc = __revit__.ActiveUIDocument.Document
  • you need to start a transaction before changing the document
transaction = Transaction(doc, "call this whatever you like")
transaction.Start()
# do stuff
transaction.Commit()
  • to get an element given an integer, use this technique
element = doc.get_Element(ElementId(myinteger))
  • Document.Delete() returns a list of deleted elements on each call. That is what the clutter (List[ElementId]([Autodesk.Revit.DB.ElementID object at...) is all about in the session transcript above.

Thursday, July 15, 2010

New release of RevitPythonShell for x64 and Revit 2011 only

I upgraded my development machine a couple of weeks ago to Windows 7 64-bit. At the same time I migrated all my Revit Addin projects to Autodesk Revit 2011. This includes RevitPythonShell. I finally got round to building a new installer using the RevitAddInUtility. This was as good a project as any to try out building x64 applications and installers for Revit 2011.

Most of the port was done with the help of code I recieved from Jason Schaeffer (joespiff) via the RevitPythonShell google group

I ran into some snares:

  • Visual Studio 2010 has some restrictions with x64 managed applications
    • The Designer will not show your forms unless you set the build platform to "Any CPU" and rebuild.
    • The Setup projects bundle the wrong "InstallUtilLib.dll" shim when building x64 .msi packages
  • Windows 7 doesn't like it when programs modify stuff in the "Program Files" folder, so I moved the RevitPythonShell.xml file to the AppData folder.

Wednesday, June 9, 2010

My .vimrc

This is another post for my future self: At the time of writing, my .vimrc looks like this:

" --------------------------------------------------------------------------------
" call system vimrc
" --------------------------------------------------------------------------------
source $VIM/_vimrc

" --------------------------------------------------------------------------------
" startup
" --------------------------------------------------------------------------------
autocmd GUIEnter * simalt ~X    " maximize window

" --------------------------------------------------------------------------------
" opening files
" --------------------------------------------------------------------------------

" change working directory to path of file
autocmd BufEnter * lcd %:p:h    

" reformat xml files (pretty print)
autocmd FileType xml exe ":silent 1,$!xmllint --format --encode UTF-8 --recover - 2>/dev/null"

" --------------------------------------------------------------------------------
" saving files
" --------------------------------------------------------------------------------
set backupdir=./_Backup,$TEMP,.

" --------------------------------------------------------------------------------
" searching
" --------------------------------------------------------------------------------
set hlsearch        " highlight matches to search pattern
set incsearch        " incremental search
set smartcase       " case sensitive if search string contains uppercase letters
set ignorecase        " case insensitive searching

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=0         " do not break lines when line length increases
set tabstop=4            " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start 

set ruler               " show line and column number


" --------------------------------------------------------------------------------
" encoding
" --------------------------------------------------------------------------------
set encoding=utf-8
"set printencoding=utf-8

" --------------------------------------------------------------------------------
" keyboard mappings
" --------------------------------------------------------------------------------
set langmap=¨]


Future Daren, you can find the location of the .vimrc by editing the file $MYVIMRC in vim itself! Unless it hasn't been created yet ($MYVIMRC will then point to the system-wide version). In Windows, just plop a _vimrc file in your %userprofile%.