Opening the Lines of Communication Between Revit® and Third-Party Applications David Rushforth, PE, LEED AP BD+C Senior Electrical Engineer, R.G. Vanderweil Engineers firstname.lastname@example.org Presentation Expectations Have questions? Please save them for after the presentation or send me an e-mail Experience Level Topics are advanced Explanations are geared toward non-programmers Database communication only Goal Revit API experience is (partially) assumed Share “the power of the possible” WARNING The following content was prepared by an engineer, NOT a programmer. The samples of code shown may contain disturbing examples of poor programming practices and represent only one of many ways to accomplish the desired objective. About the Presenter Electrical Engineer at R.G. Vanderweil Engineers Native to Las Vegas BS Electrical Engineering, BYU MS Electrical Engineering, UNLV Presentation Outline Communication Overview Lines of Communications Text Based (Csv, Txt, XML, Ini, Dat) Microsoft Access® SQL Microsoft Excel® Autodesk AutoCAD® Other Applications Autodesk Revit® Another Approach: Microsoft Access® As Hub Ideas For Further Development Communication Overview Microsoft Access ® SQL Microsoft Excel ® Text Autodesk AutoCAD ® SKM Power Tools Autodesk Revit® Lines of Communication: Text Based Lines of Communication: Text Based Write New File (Example formats: Csv, Txt, XML, Ini, Dat) Dim writefilename As String writefilename = "C:\[your path here]" Dim fs As New FileStream(writefilename, FileMode.Create, FileAccess.Write) Dim s As New StreamWriter(fs) s.WriteLine("Stuff") s.Close() Read/Write/Edit existing File Data Code Dim FilePath As String FilePath = " C:\[your path here]" Dim text As Array Dim lines As New List(Of String) text = System.IO.File.ReadAllLines(FilePath) lines.AddRange(text) Dim n As Integer For n = 0 To lines.Count - 1 ‘[read, store, insert, or make decisions based on lines(n) ] Next n Color Key Variable declarations Supporting code Read Write Lines of Communication: Microsoft Access® Lines of Communication: Microsoft Access® Connect to database file for direct manipulation Dim strAccessFile As String = "C:\Temp\MyDatabase.accdb" Dim mAccessApplication As New Microsoft.Office.Interop.Access.Application mAccessApplication.OpenCurrentDatabase(strAccessFile) Dim myEmployeesRecords As dao.Recordset Dim TableName As String = "Employees" myEmployeesRecords = mAccessApplication.CurrentDb.OpenRecordset("SELECT * FROM " & TableName) myEmployeesRecords.MoveFirst() Dim readFirstName As String readFirstName = myEmployeesRecords.Fields("FirstName").Value myEmployeesRecords.Edit() myEmployeesRecords.Fields("FirstName").Value = "David" myEmployeesRecords.Fields("LastName").Value = "Rushforth" myEmployeesRecords.Update() mAccessApplication.Quit(Option:=Microsoft.Office.Interop.Access.AcQuitOption.acQuitSaveNone) mAccessApplication = Nothing For 64-bit compatibility Copy dao360.dll to Revit “Program” folder Color Key Variable declarations Supporting code Read Write Lines of Communication: SQL Lines of Communication: SQL Connect to database file for direct manipulation Dim StrConnectionString As String StrConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Temp\TestDatabase.mdf;Integrated Security=True;User Instance=True“ Dim mySqlConnection As New SqlConnection(StrConnectionString) Dim mySqlCommand As New SqlCommand mySqlConnection.Open() mySqlCommand.Connection = mySqlConnection Dim TableName As String = "Employees“ mySqlCommand.CommandText = "SELECT * FROM " & TableName mySqlCommand.CommandText = "UPDATE " & TableName & " SET FirstName='EditedFirstName',LastName='EditedLastName' WHERE (NameID= 1234)“ mySqlCommand.CommandText = "SELECT * FROM " & TableName & " WHERE (FirstName='FirstName1')“ mySqlCommand.ExecuteNonQuery() ‘RUN THIS AFTER EACH .CommandText Dim mySqlDataReader As SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader While mySqlDataReader.Read() 'While Data is Present MsgBox(mySqlDataReader("FirstName") & ", " & mySqlDataReader("LastName")) End While mySqlDataReader.Close() mySqlConnection.Close() Color Key Variable declarations Supporting code Read Write Lines of Communication: Microsoft Excel® Lines of Communication: Microsoft Excel® Connect to Excel file for direct manipulation Dim strXlsFile As String strXlsFile = "C:\Temp\test.xls" Dim mExcelApplication As New Microsoft.Office.Interop.Excel.Application Dim mExcelWorkbook As Microsoft.Office.Interop.Excel.Workbook = mExcelApplication.Workbooks.Open(strXlsFile) Dim mExcelWorksheet As Microsoft.Office.Interop.Excel.Worksheet = mExcelWorkbook.Worksheets(1) Dim readValue As String readValue = mExcelWorksheet.Range("A1").Value mExcelWorksheet.Range("A2").Value = “New Value" mExcelWorkbook.Windows(1).Visible = True mExcelWorkbook.Save() mExcelWorkbook = Nothing mExcelApplication.Quit() mExcelApplication = Nothing Color Key Variable declarations Supporting code Read Write Lines of Communication: Autodesk AutoCAD® Lines of Communication: Autodesk AutoCAD® DXF Files Text based file format Can be written or edited Scripts Auto-run script on open AutoCAD cadpath = myApplicationFilePath & " /b " & ScriptName Dim ACADProcessID As Integer ACADProcessID = Shell(cadpath, vbMaximizedFocus) LISP Load a LISP routine during a script s.WriteLine("(load ""MyLispFilename.lsp"")") Lines of Communication: Other Applications Lines of Communication: Other Applications Identify ways of: Getting Data Out Reports (txt, xls) Export options Getting Data In Import options Proprietary extensions (e.g. .xyz) may be text based Lines of Communication: Autodesk Revit® RibbonCommands.dll Ribbon.dll Set References (RevitAPI.dll, etc.) Create Ribbon Toolbar Create Button1 -When pressed access class in RibbonCommands.dll Create Button2 -… .addins or Revit.ini Load dll on startup References (Revit API) RevitAPI.dll, RevitAPIUI.dll Set References (RevitAPI.dll, etc.) Class PowerSuite -Gather Param info -Open Power Suite -Transfer Info Class SetPowerSuiteParams -Set changed parameters Class ParameterXfmr -Sort Project Elements -Sort Selected Elements -Open form Class … AHK Lines of Communication: Autodesk Revit® Getting Data Out Getting Data In Read Parameters Import from text Connect to Database or Table Excel to DXF Link CAD drawings Set Parameters Load Families Create objects Automating the Interface AutoHotkey (AHK) Scripts Another Approach: Microsoft Access® As Hub Work flow Send data from Revit to Access® database Access® stores and controls the flow of information between Revit and third party applications Benefits Perform advanced lookups and calculations Review and change design outside of Revit Can send changes back to Revit Same interface for Revit and Non-Revit projects Integrated Program Example Text SKM Power Tools Microsoft Excel ® Autodesk Revit® Autodesk AutoCAD ® Microsoft Access® Ideas For Further Development Write custom reports from Revit Write errors or standards deviations to journals Export room dimensions Automate print settings Create sheets, objects, or families from stored data Extract specific family types from project Create a keynote database Automate riser diagrams Create room or space schedules with error reporting THANK YOU! If you liked it… If this will help you in your work… Fill out the online class survey. Send me an email and let me know how it helped. If you have questions… Ask now or send me an e-mail later. email@example.com Autodesk [and other] are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or affiliates in the USA and/or other countries. All other brand names, product names, or trademarks belong to their respective holders. Autodesk reserves the right to alter product and services offerings, and specifications and pricing at any time without notice, and is not responsible for typographical or graphical errors that may appear in this document. © 2010 Autodesk, Inc. All rights reserved.