public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; Application app = uiapp.Application; Document doc = uiapp.ActiveUIDocument.Document; //BindingMap bindingMap = doc.ParameterBindings; // slow, fixed in 2009 wu 3, cf. case 1247995 Guid paramGuid = LabUtils.SharedParamGUID(app, LabConstants.SharedParamsGroupAPI, LabConstants.SharedParamsDefFireRating); // Let user select the Excel file. WinForms.OpenFileDialog dlg = new WinForms.OpenFileDialog(); dlg.Title = "Select source Excel file from which to update Revit shared parameters"; dlg.Filter = "Excel spreadsheet files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*)|*"; if (WinForms.DialogResult.OK != dlg.ShowDialog()) { return(Result.Cancelled); } // Launch/Get Excel via COM Interop: X.Application excel = new X.Application(); if (null == excel) { LabUtils.ErrorMsg("Failed to get or start Excel."); return(Result.Failed); } excel.Visible = true; X.Workbook workbook = excel.Workbooks.Open(dlg.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); X.Worksheet worksheet = workbook.ActiveSheet as X.Worksheet; using (Transaction t = new Transaction(doc)) { t.Start("Import Fire Rating Values from Excel"); // Starting from row 2, loop the rows and extract Id and FireRating param. int id; double fireRatingValue; int row = 2; while (true) { try { // Extract relevant XLS values. X.Range r = worksheet.Cells[row, 1] as X.Range; if (null == r.Value2) { break; } double d = (double)r.Value2; id = (int)d; if (0 >= id) { break; } r = worksheet.Cells[row, 4] as X.Range; fireRatingValue = (double)r.Value2; // Get document's door element via Id ElementId elementId = new ElementId(id); Element door = doc.GetElement(elementId); // Set the param if (null != door) { //Parameter parameter = door.get_Parameter( LabConstants.SharedParamsDefFireRating ); Parameter parameter = door.get_Parameter(paramGuid); parameter.Set(fireRatingValue); } } catch (Exception) { break; } ++row; } t.Commit(); } #if ACTIVATE_REVIT // // Set focus back to Revit (there may be a better way, but this works :-) // #if USE_PROCESS_GET_PROCESSES foreach (Process p in Process.GetProcesses()) { try { if ("REVIT" == p.ProcessName.ToUpper().Substring(0, 5)) { // In VB, we can use AppActivate( p.Id ); // Pre-3.0, I think you may need to use p/invoke and call the native Windows // SetForegroundWindow() function directly. // http://www.codeproject.com/csharp/windowhider.asp?df=100 break; } } catch (Exception) { } } #endif // USE_PROCESS_GET_PROCESSES JtRevitWindow w = new JtRevitWindow(); w.Activate(); #endif // ACTIVATE_REVIT return(Result.Succeeded); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; Application app = uiapp.Application; Document doc = uiapp.ActiveUIDocument.Document; //BindingMap bindingMap = doc.ParameterBindings; // slow, fixed in 2009 wu 3, cf. case 1247995 Guid paramGuid = LabUtils.SharedParamGUID( app, LabConstants.SharedParamsGroupAPI, LabConstants.SharedParamsDefFireRating ); // Let user select the Excel file. WinForms.OpenFileDialog dlg = new WinForms.OpenFileDialog(); dlg.Title = "Select source Excel file from which to update Revit shared parameters"; dlg.Filter = "Excel spreadsheet files (*.xls;*.xlsx)|*.xls;*.xlsx|All files (*)|*"; if( WinForms.DialogResult.OK != dlg.ShowDialog() ) { return Result.Cancelled; } // Launch/Get Excel via COM Interop: X.Application excel = new X.Application(); if( null == excel ) { LabUtils.ErrorMsg( "Failed to get or start Excel." ); return Result.Failed; } excel.Visible = true; X.Workbook workbook = excel.Workbooks.Open( dlg.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value ); X.Worksheet worksheet = workbook.ActiveSheet as X.Worksheet; using( Transaction t = new Transaction( doc ) ) { t.Start( "Import Fire Rating Values from Excel" ); // Starting from row 2, loop the rows and extract Id and FireRating param. int id; double fireRatingValue; int row = 2; while( true ) { try { // Extract relevant XLS values. X.Range r = worksheet.Cells[row, 1] as X.Range; if( null == r.Value2 ) { break; } double d = (double) r.Value2; id = (int) d; if( 0 >= id ) { break; } r = worksheet.Cells[row, 4] as X.Range; fireRatingValue = (double) r.Value2; // Get document's door element via Id ElementId elementId = new ElementId( id ); Element door = doc.GetElement( elementId ); // Set the param if( null != door ) { //Parameter parameter = door.get_Parameter( LabConstants.SharedParamsDefFireRating ); Parameter parameter = door.get_Parameter( paramGuid ); parameter.Set( fireRatingValue ); } } catch( Exception ) { break; } ++row; } t.Commit(); } #if ACTIVATE_REVIT // // Set focus back to Revit (there may be a better way, but this works :-) // #if USE_PROCESS_GET_PROCESSES foreach( Process p in Process.GetProcesses() ) { try { if( "REVIT" == p.ProcessName.ToUpper().Substring( 0, 5 ) ) { // In VB, we can use AppActivate( p.Id ); // Pre-3.0, I think you may need to use p/invoke and call the native Windows // SetForegroundWindow() function directly. // http://www.codeproject.com/csharp/windowhider.asp?df=100 break; } } catch( Exception ) { } } #endif // USE_PROCESS_GET_PROCESSES JtRevitWindow w = new JtRevitWindow(); w.Activate(); #endif // ACTIVATE_REVIT return Result.Succeeded; }