Thursday, October 01, 2009

Retrieving currently open documents using Application.Documents

In the code below (which I recommend you don't use until you've read this whole post!) I'm passing a function a known filename and am asking Revit to find it and return it:

Public Shared Function GetFileByName(ByVal name As String) As Document

  Dim docIterator As DocumentSetIterator
        docIterator = revitApp.Documents.GetEnumerator
        Dim doc As Document = Nothing

        While docIterator.MoveNext()
            doc = docIterator.Current
            If doc.Title.ToString = name Then
                Return doc
            End If


        End While

Return (Nothing)

End Function

I recently had a live release failing on a user and after a bit of poking around I found that this code was returning nothing, but we knew that the document was open. It was failing because Revit wasn't returning the file extensions. It only happened on a couple of machines on this particular site, whereas others were fine. To my surprise it was because of the 'Hide extensions for known file types' user setting in Windows Explorer:



So, when you're doing something similar, be sure to allow for this setting being both on and off. I modified my code thus:
If (doc.Title.ToString = name) Or (doc.Title.ToString & ".rvt" = name) Then

Monday, September 14, 2009

I'll tell you where I'm at

I've gone a bit quiet on the Revit front recently. This is because I'm working on a large .Net web project at the moment, with little direct relevance to Revit.

Because of this I started a 'sister' blog with a broader programming remit:


This isn't the end of my Revit programming, it's just an interesting interlude. I'll be back!

Tuesday, August 11, 2009

Revit SDK VB.NET Samples

VB programmers might be disappointed that the wealth of useful code provided in the Revit 2010 SDK samples seems largely C# focussed. On top of this, finding the VB ones seems a little hit and miss.

Well, here's a list of the samples that have a VB.NET version:

AnalyticalViewer, ArchSample, BrowseBindings, CreateBeamsColumnsBraces, CreateShared, DeleteObject, DesignOptionReader, ElementViewer, FireRating, HelloRevit, HelloWorld, InplaceFamilyAnalyticalModel3D, MaterialProperties, PhaseSample, RevitCommands, RoomViewer, RotateFramingObjects, SlabProperties, StructuralLayerFunction, TestFloorThickness, TestWallThickness, TypeSelector.

And don't forget, you can always convert C# to VB using this little tool. :)

Thursday, August 06, 2009

Changing fonts

I had a query about how to change fonts programmatically, so I dropped a textnote into a new model and had a poke. I did the usual quickie looping through selected elements:

While selectionIterator.MoveNext
If TypeOf selectionIterator.Current Is Elements.TextNote Then
Dim note As Elements.TextNote = selectionIterator.Current
'do something here
End If
End While

So what do we do to the textnote? Well, it's the note's TextNoteType we're after, and more specifically the 'TEXT_FONT' parameter.

Dim fontParam As Parameter = note.TextNoteType.Parameter(BuiltInParameter.TEXT_FONT)
If fontParam IsNot Nothing Then
fontParam.Set("Impact")
End If

It's not too hard to see how this could be applied to a whole open project rather than a selection set, and then to all textual elements. So if those in-house standards that you drew up specify Comic Sans* as the font of choice then changing them throughout a project shouldn't be too onerous a task.

* then you should be shot

Tuesday, July 28, 2009

Trimming text notes

It's been 20 days since my last post, in which time the Pitt family has grown by one and I've done very little programming, choosing sleep in my down-time rather than looking at a screen. But here's an issue I made a draft entry of a while back but never posted it for some reason.

I had some files that were showing unexpected characters at the end of inserted text notes. I say unexpected, as I wasn't expecting them, so neither was our software. Closer inspection revealed these to be linefeeds or carriage returns, unprintable in your everyday text editors but nonetheless there when handling the data and making database comparisons. They got there because of the tendency to hit 'return' when completing a text note; something unpreventable, so I have to clean them up.

I looked into the .net String.Trim method, but this didn't seem to work. Then I had a look at String.Replace, to replace the offending character with nothing. The problem then was identifying what the character actually was. I tried \n, VbCrlf, etc etc to no avail.

Then after a coffee I tried Trim(String), which I had imagined did the same thing as String.Trim. This time it worked.

So what's the difference between String.Trim and Trim(String)?

Public Function Trim(ByVal str As String) As String
Member of Microsoft.VisualBasic.Strings
Returns a string containing a copy of a specified string with no leading spaces (LTrim), no trailing spaces (RTrim), or no leading or trailing spaces (Trim).

Public Function Trim() As String
Member of System.String
Removes all leading and trailing white-space characters from the current System.String object.

Subtle, and not entirely clear, but it works for me :)

Wednesday, July 08, 2009

Get googling for Revit API help

Fellow blogger and all-round clever bloke Rod Howarth has founded a Revit API search engine using Google's custom search feature.



My blog is one of five blogs which the engine currently searches, and no doubt this will grow. I'm honoured to be recognised as a useful and reliable source of information, and to be there right from the start.

Perhaps I should get insurance?

DoEvents() with Revit

Just a quickie as a follow-up to my last post. DoEvents isn't recommended for .NET, but given that Revit doesn't support multi-threading we have to use it sometimes to keep our UI responsive whilst we crunch some data.

It's commonly referred to as Application.DoEvents() but in your Revit plugin Application will probably refer to the Revit app and DoEvents won't be recognised as a method. So you need to use this:

System.Windows.Forms.Application.DoEvents()