Exemple #1
0
        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);
        }
Exemple #2
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Application   app   = uiapp.Application;
            Document      doc   = uiapp.ActiveUIDocument.Document;

            Category cat = doc.Settings.Categories.get_Item(
                Lab4_3_1_CreateAndBindSharedParam.Target);

            // Launch Excel (same as in Lab 4_2, so we really
            // should have better created some utils...)

            X.Application excel = new X.ApplicationClass();
            if (null == excel)
            {
                LabUtils.ErrorMsg("Failed to get or start Excel.");
                return(Result.Failed);
            }
            excel.Visible = true;
            X.Workbook  workbook = excel.Workbooks.Add(Missing.Value);
            X.Worksheet worksheet;
            //while( 1 < workbook.Sheets.Count )
            //{
            //  worksheet = workbook.Sheets.get_Item( 0 ) as X.Worksheet;
            //  worksheet.Delete();
            //}
            worksheet             = excel.ActiveSheet as X.Worksheet;
            worksheet.Name        = "Revit " + cat.Name;
            worksheet.Cells[1, 1] = "ID";
            worksheet.Cells[1, 2] = "Level";
            worksheet.Cells[1, 3] = "Tag";
            worksheet.Cells[1, 4] = LabConstants.SharedParamsDefFireRating;
            worksheet.get_Range("A1", "Z1").Font.Bold = true;

            List <Element> elems = LabUtils.GetTargetInstances(doc,
                                                               Lab4_3_1_CreateAndBindSharedParam.Target);

            // Get Shared param Guid

            Guid paramGuid = LabUtils.SharedParamGUID(app,
                                                      LabConstants.SharedParamsGroupAPI,
                                                      LabConstants.SharedParamsDefFireRating);

            if (paramGuid.Equals(Guid.Empty))
            {
                LabUtils.ErrorMsg("No Shared param found in the file - aborting...");
                return(Result.Failed);
            }

            // Loop through all elements and export each to an Excel row

            int row = 2;

            foreach (Element e in elems)
            {
                worksheet.Cells[row, 1] = e.Id.IntegerValue; // ID

                // Level:

                //worksheet.Cells[row, 2] = e.Level.Name; // 2013
                //worksheet.Cells[row, 2] = doc.GetElement( e.LevelId ).Name; // 2014

                // When attaching a shared parameter to Material
                // elements, no valid level is defined, of course:

                ElementId levelId = e.LevelId;
                string    levelName
                    = ElementId.InvalidElementId == levelId
            ? "N/A"
            : doc.GetElement(levelId).Name;
                worksheet.Cells[row, 2] = levelName;

                // Tag:

                Parameter tagParameter = e.get_Parameter(
                    BuiltInParameter.ALL_MODEL_MARK);
                if (null != tagParameter)
                {
                    worksheet.Cells[row, 3] = tagParameter.AsString();
                }

                // FireRating:

                Parameter parameter = e.get_Parameter(paramGuid);
                if (null != parameter)
                {
                    worksheet.Cells[row, 4] = parameter.AsDouble();
                }
                ++row;
            }
            return(Result.Succeeded);
        }