Example #1
0
        /// <summary>
        /// Program entry point
        /// </summary>
        /// <param name="commandData"></param>
        /// <param name="message"></param>
        /// <param name="elements"></param>
        /// <returns>Result</returns>
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            try
            {
                UIApplication uiapp = commandData.Application;
                UIDocument    uidoc = uiapp.ActiveUIDocument;
                Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
                Document doc = uidoc.Document;

                // Determine elements to export
                FilteredElementCollector collector = null;

                // Access current selection
                SelElementSet set = uidoc.Selection.Elements;

                int n = set.Size;

                if (0 < n)
                {
                    // If any elements were preselected, export those
                    ICollection <ElementId> ids = set
                                                  .Cast <Element>()
                                                  .Select <Element, ElementId>(e => e.Id)
                                                  .ToArray <ElementId>();

                    collector = new FilteredElementCollector(doc, ids);
                }
                else
                {
                    // If nothing was preselected, export everything
                    collector = new FilteredElementCollector(doc);
                }

                collector.WhereElementIsNotElementType()
                .WhereElementIsViewIndependent();

                if (null == _exportFolderName)
                {
                    _exportFolderName = Path.GetTempPath();
                }

                string filename = null;

                if (!FileSelect(_exportFolderName,
                                out filename))
                {
                    return(Result.Cancelled);
                }

                _exportFolderName = Path.GetDirectoryName(filename);
                Command exporter = new Command();
                Options opt      = app.Create.NewGeometryOptions();
                ExportElements(exporter, collector, opt);
                exporter.ExportTo(filename);

                return(Result.Succeeded);
            }
            catch (Exception ex)
            {
                message = ex.Message;
                return(Result.Failed);
            }
        }