VB Samples and Other Questions

I have been very quiet now for a few days.
The reason is that I went on a climbing weekend to Mallorca with my brother and left my laptop in the car while we went for a climb.
The car was broken into, and the laptop and all my valuables were stolen, right down to the bicycle keys.
Since I had actually used the bicycle to get to the airport, the first thing I had to do on arriving back was to borrow a saw to break my own bicycle lock.
Now I am in the painful process of restoring data to a temporary computer.
A valuable lesson in letting go, I suppose. More on this below.

Anyway, here are some queries on different topics, some of which have been touched upon previously, and an updated reiteration may be of general interest:

  1. Programmatically triggering IFC export.
  2. ImportExport SDK sample in VB.
  3. OnStartup sample code in VB.
  4. 64 bit OLE DB access.

Question:
How can I trigger a daily export of a Revit model to an IFC file?

Answer:
I am sorry to say that there is currently no API access to this functionality.
One option that you might explore is to simulate the user input required for this repetitive task programmatically.
You might do this directly using the Win32 API, or make use of some utilities that provide easier access to this functionality.
Here are some of the previous posts dealing with related topics:

Another option that may fit your requirements perfectly but is not officially supported is the use of the Revit journal file.
Every action performed through the Revit user interface is recorded in a journal file, located in a subfolder of the Revit installation folder.
We once discussed

how to obtain the path of the current journal file
.
Journal files can be rerun to reproduce the actions performed in a previous session, for instance by specifying them on the Revit command line during start-up.
Custom applications can define what data their commands register in the journal file, and how to process that data when a journal file is replayed, as demonstrated by the Revit SDK Journaling sample.
In your case, however, you would possibly not need to do any programming or customisation at all, just start up Revit, perform the IFC export, close down Revit again, and save the resulting journal file.
This file might be reusable to automate the IFC export on a daily basis, just as you require.

Question:
The Revit SDK 2010 provides a sample application named ImportExport which is of interest to me.
Unfortunately, it is in C#, I am only familiar with VB, and the example is quite comprehensive.
How can I convert this to VB, please?

Answer:
We already discussed the topics of conversion, disassembly, decompilation, Lutz Roeder’s

Reflector

tool and the related topic of

obfuscation

in previous posts.
This tool can be used perfectly well to decompile the Revit SDK Samples, as well.
The decompiled code can be displayed in a variety of different languages, including Delphi, managed C++, Chrome and Visual Basic.
As said, the ImportExport sample is written in C#.
I opened the compiled assembly ImportExport.dll in Reflector and asked to see the source code of the Command class disassembled into Visual Basic.
Here is the result:


Public Class Command
Implements IExternalCommand
' Methods
Public Function Execute( _
ByVal commandData As ExternalCommandData, _
ByRef message As String, _
ByVal elements As ElementSet _
) As Result
Try
If (Nothing Is commandData.Application.ActiveDocument) Then
message = "Active view is null."
Return Result.Failed
End If
Dim mainData As New MainData(commandData)
Using mainForm As MainForm = New MainForm(mainData)
If (mainForm.ShowDialog = DialogResult.Cancel) Then
Return Result.Cancelled
End If
End Using
Catch ex As Exception
message = ex.ToString
Return Result.Failed
End Try
Return Result.Succeeded
End Function
End Class

I will leave it up to you to decompile the rest of the classes, if you wish.
Alternatively, there are other tools available which decompile an entire application in one go.

Nowadays, Reflector is available from Red Gate.

Question:
I would like my application to run automatically every time a file is opened.
I assume this can be achieved using the OnStartup method.
How is OnStartup used in VB.NET?

Answer:
We already looked at this topic once previously, and provided a short description on registering to

application events in VB
.
I repeated the steps described there in the Visual Studio 2008 environment for Revit Architecture 2010,
and implemented a simple sample external application named OnStartupVb and implementing OnStartup in Visual Basic for you.
The description provided in the previous post for Revit 2009 remains valid for 2010 as well.
Here is the resulting source code of the external application class:


Imports Autodesk.Revit
Public Class App
Implements IExternalApplication
Public Function OnShutdown(ByVal a As ControlledApplication) _
As IExternalApplication.Result _
Implements IExternalApplication.OnShutdown
Return IExternalApplication.Result.Succeeded
End Function
Public Function OnStartup(ByVal a As ControlledApplication) _
As IExternalApplication.Result _
Implements IExternalApplication.OnStartup
System.Windows.Forms.MessageBox.Show( _
"OnStartup says Hello from Jeremy", _
"OnStartup VB")
End Function
End Class

Here is a copy of the full Visual Studio solution
OnStartupVb.

Question:
No OLEDB provider registered …
How can I retrieve a record set from an Access 2007 database using VB.NET in 64-bit Vista?

Answer:
If you install 32-bit Revit 2009 in 64-bit Vista, then this is possible.
This requires compiling your Revit plug-in in a 32-bit OS, then loading it into 32-bit Revit in the 64-bit OS.
On the other hand, if the Revit version is 64-bit, one possible approach is to use the SQL server database accessing driver.
Note that the 32-bit version of Revit 2010 cannot be installed on a 64-bit OS, so in that case using SQL Server or SQL Server Express would be the recommended option.

Thanks to Joe Ye for this answer!

Back to the topic of Mallorca.
Apart from criminality, it is a surprisingly beautiful place with large areas of untouched nature, mountains, and almost inaccessible beaches, in spite of the strong touristic focus in the cities and villages.
Also a huge amount of beautiful climbing opportunities.
Here I am walking along a path heading for both a lonesome beach and a challenging cliff:

Lonesome path on Mallorca

The weather was hot enough to require some protection against the sun:

Jeremy as a colourful Tuareg


Comments

14 responses to “VB Samples and Other Questions”

  1. Chuck Gabriel Avatar
    Chuck Gabriel

    I’m sorry to hear about the theft. It is always so frustrating to hear about bad things happening to good people.

  2. Hi, Jeremy
    Concerning “No OLEDB provider registered” problem.
    Does Joe Ye as an extract code by using the Server or SQL Server Express to connect to an Excel file?
    Cheers!

  3. Joe Ye Avatar
    Joe Ye

    Hi Pnavarra,
    I don’t have the handy code using SQL Several provider to read an Excel file.
    I will pay attention to reading that. While you might find other code in internet to read excel file using SQL Server provider.
    Cheers,
    Joe Ye

  4. Hi Joe,
    I’m trying to do by myself or find this code in net.
    Anyway, I’ve try to open a csv file with the same procedure and it does not work.
    Cheers and thx for answering.

  5. Ok, I done it with a CSV file and a StreamHeader object.
    Cheers!

  6. Hi Pierre,
    Congratulations on solving the issue. Is the solution in any way specific to the Revit API? If so, it might be interesting to publish it for others to make use of as well. Although it sounds to me as if it does not in fact have all that much to do with Revit?
    Cheers, Jeremy.

  7. Hi Jeremy,
    Actually, this solution is not specific to the Revit API. The problem is, in 64 bits environment the OleDBJet is not running. So, no way to open a connection with a Database like Access, Excel. I did not find a simple way to open with SqlServer, so, I’ve resolved the problem with opening a CSV file with StremHeader. The function I made is a simple function wich fill a Revit schedule and create non placed rooms with a specific column. you can see a sample of it here : http://www.steekr.com/n/50-17/share/LNK808149057ab0f2411/
    Concerning your last sentence, I did not understand it….
    “Although it sounds to me as if it does not in fact have all that much to do with Revit?”

  8. Dear Pierre,
    Thank you very much for the description of your solution. Concerning the last sentence, I meant exactly what you said yourself as well, that this solution is not specific to the Revit API.
    Cheers, Jeremy.

  9. All right.
    So, the problem is solved.
    Have a nice day.
    I don’t know if I’ve already congratulate you for your work…anyway : CONGRATULATIONS MAN!
    Cheers!

  10. Dear Pierre,
    Wow, great, it does me so good to hear that! Thank YOU for your appreciation and your many suggestions!
    Cheers, Jeremy.

  11. stefan Avatar
    stefan

    hi there,
    i have excessively used VBA inside our CAD package microstation and autocad. i have done solar access analysis and surface panalization and much more using VBA.
    i was wondering if vb is also the scripting language for revit.
    thanks for your help,
    stefan

  12. Dear Stefan,
    I hope your use of VBA was actually appropriate and not really excessive :-) … maybe extensive, as well.
    The scripting language for Revit is anything that supports the .NET framework. Prime examples of this are C#.NET, VB.NET, and managed C++. The same applies for AutoCAD.NET programming. As Kean Walmsley demonstrates in various posts for AutoCAD, there are a many other languages that can also be used, such as Boo, F#, Python, and Ruby. Some of these could be used for Revit scripting as well. As discussed in previous posts, it is possible to use the .NET functionality to combine and translate between different languages:
    http://thebuildingcoder.typepad.com/blog/2008/10/converting-between-vb-and-c-and-net-decompilation.html
    http://thebuildingcoder.typepad.com/blog/2009/01/hello-f-via-c.html
    http://thebuildingcoder.typepad.com/blog/2009/05/vb-samples-and-other-questions.html#2
    VB.NET is a different language from VBA, although there are many similarities. Personally, I prefer C# over VB, but that is a question of taste.
    I hope this helps.
    Cheers, Jeremy.

  13. Daniel Avatar
    Daniel

    Hi Jeremy,
    I want to implement importexport SDK sample in the closing event of REVIT Document. For instance I can use it as an ExternalCommand of course.
    But if I try to call the mainform of importexport SDK in DocumentClosing event I see that
    The constructor of Maindata need commandData argument as an ExternalCommandata. In the context of DocumentClosing EVENT I don’t known how to get this argument.
    Have you a solution or a runaround.
    I use revit 2014.
    Thanks

  14. Dear Daniel,
    The ExternalCommandData argument is only available in the Execute event handler of an external command and nowhere else.
    If you look at the ImportExport implementation, you will probably note that you do not need all of the ExternalCommandData information anyway.
    You should be able to get everything you need from the current document itself, and that is provided as an argument to the DocumentClosing event handler.
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading