private void ConvertButton_Click(object sender, RoutedEventArgs e)
        {
            //System.Windows.MessageBox.Show("Selection: " + linkDataGrid.SelectedItems[0].GetType().FullName);
            // Strip this schema object from the entity.
            //DataRowView selectedRow = (DataRowView)linkDataGrid.SelectedItems[0];
            if (linkDataGrid.SelectedItems[0] is LinkData selectedLinkData)
            {
                //string cell = selectedRow.Row.ItemArray[3].ToString();

                // Read the schema information
                Autodesk.Revit.DB.ExtensibleStorage.Schema schema = Autodesk.Revit.DB.ExtensibleStorage.Schema.Lookup(schemaGuid);
                if (schema != null)
                {
                    Autodesk.Revit.DB.ExtensibleStorage.Entity entity = null;
                    DataStorage ds = SchemaManager.GetDataStorage(doc);
                    try
                    {
                        entity = ds.GetEntity(schema);
                    }
                    catch { }

                    if (entity != null)
                    {
                        elementIds = entity.Get <IList <ElementId> >("ScheduleId");
                        paths      = entity.Get <IList <string> >("ExcelFilePath");
                        worksheets = entity.Get <IList <string> >("WorksheetName");
                        dateTimes  = entity.Get <IList <string> >("DateTime");
                        pathTypes  = entity.Get <IList <int> >("PathType");

                        int index = -1;
                        for (int i = 0; i < elementIds.Count; i++)
                        {
                            try
                            {
                                //int intValue = Convert.ToInt32(cell);
                                if (elementIds[i].IntegerValue == selectedLinkData.ElementId)
                                {
                                    index = i;
                                }
                            }
                            catch { }
                        }

                        if (index >= 0)
                        {
                            elementIds.RemoveAt(index);
                            paths.RemoveAt(index);
                            worksheets.RemoveAt(index);
                            dateTimes.RemoveAt(index);
                            pathTypes.RemoveAt(index);

                            Transaction trans = new Transaction(doc, "Convert Excel Document to Import");
                            trans.Start();

                            if (elementIds.Count > 0)
                            {
                                entity.Set <IList <ElementId> >("ScheduleId", elementIds);
                                entity.Set <IList <string> >("ExcelFilePath", paths);
                                entity.Set <IList <string> >("WorksheetName", worksheets);
                                entity.Set <IList <string> >("DateTime", dateTimes);
                                entity.Set <IList <int> >("PathType", pathTypes);
                                ds.SetEntity(entity);
                            }
                            else
                            {
                                // Delete the entity data
                                ds.DeleteEntity(schema);
                                doc.Delete(ds.Id);
                            }

                            trans.Commit();

                            BuildTable();
                        }
                    }
                }
            }
        }
예제 #2
0
        public void ModifySchedule(Document doc, ElementId scheduleId, string file, string worksheet, string transactionMsg, int pathType, bool contentOnly)
        {
            // Set the Pathtype
            PathType pt = (PathType)pathType;

            // Clear the current schedule of data/formatting and then rebuild it.
            ViewSchedule sched = null;

            _doc = doc;
            try
            {
                Element schedElem = doc.GetElement(scheduleId);
                sched = schedElem as ViewSchedule;
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", ex.Message);
            }

            if (sched != null)
            {
                Transaction trans = new Transaction(doc, transactionMsg);
                trans.Start();
                if (!contentOnly)
                {
                    // Get the header body to create the necessary rows and columns
                    TableSectionData headerData = sched.GetTableData().GetSectionData(SectionType.Header);
                    int rowCount    = headerData.NumberOfRows;
                    int columnCount = headerData.NumberOfColumns;
                    for (int i = 1; i < columnCount; i++)
                    {
                        try
                        {
                            headerData.RemoveColumn(1);
                        }
                        catch { }
                    }
                    for (int i = 1; i < rowCount; i++)
                    {
                        try
                        {
                            headerData.RemoveRow(1);
                        }
                        catch { }
                    }

                    // Make sure the name is up to date
                    sched.Name = worksheet;
                }
                // Add the new schedule data in
                AddScheduleData(file, sched, doc, pt, contentOnly);
                trans.Commit();
            }
            else
            {
                TaskDialog errorDialog = new TaskDialog("Error")
                {
                    MainInstruction = $"Schedule ({worksheet}) could not be found. Remove from update list?",
                    CommonButtons   = TaskDialogCommonButtons.No | TaskDialogCommonButtons.Yes,
                    DefaultButton   = TaskDialogResult.Yes
                };
                TaskDialogResult result = errorDialog.Show();

                if (result == TaskDialogResult.Yes)
                {
                    try
                    {
                        // Find an Excel File
                        // Get the schema
                        Schema schema = Schema.Lookup(schemaGUID);
                        //Entity entity = null;
                        DataStorage         ds          = SchemaManager.GetDataStorage(_doc);
                        ExcelScheduleEntity schedEntity = ds.GetEntity <ExcelScheduleEntity>();

                        if (schedEntity != null)
                        {
                            int index = -1;
                            for (int i = 0; i < schedEntity.ScheduleId.Count; i++)
                            {
                                if (schedEntity.ScheduleId[i].IntegerValue == scheduleId.IntegerValue)
                                {
                                    index = i;
                                    break;
                                }
                            }

                            if (index != -1)
                            {
                                using (Transaction trans = new Transaction(doc, "Remove Schedule Excel Link Data"))
                                {
                                    trans.Start();

                                    // Check if there are more linked items than the one we found
                                    if (schedEntity.ScheduleId.Count > 1)
                                    {
                                        // Cull the index
                                        schedEntity.ScheduleId.RemoveAt(index);
                                        schedEntity.DateTime.RemoveAt(index);
                                        schedEntity.ExcelFilePath.RemoveAt(index);
                                        schedEntity.PathType.RemoveAt(index);
                                        schedEntity.WorksheetName.RemoveAt(index);

                                        // Set the entity back to the DS
                                        ds.SetEntity(schedEntity);
                                    }
                                    // If we only have one item and we're removing it, just delete the DataStorage and entity
                                    else
                                    {
                                        ds.DeleteEntity <ExcelScheduleEntity>();
                                        doc.Delete(ds.Id);
                                    }

                                    trans.Commit();
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskDialog.Show("Error", ex.Message);
                    }
                }
            }
        }
        private void ReloadFromButton_Click(object sender, RoutedEventArgs e)
        {
            //DataRowView selectedRow = (DataRowView)linkDataGrid.SelectedItems[0];
            LinkData selectedRow = (LinkData)linkDataGrid.SelectedItems[0];

            if (selectedRow != null)
            {
                // Find an Excel File
                System.Windows.Forms.OpenFileDialog openDlg = new System.Windows.Forms.OpenFileDialog()
                {
                    Title            = "Reload From an Excel File",
                    Filter           = "Excel Files (*.xls; *.xlsx)|*.xls;*.xlsx",
                    RestoreDirectory = true
                };


                System.Windows.Forms.DialogResult result = openDlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    string excelFilePath = openDlg.FileName;

                    if (System.IO.File.Exists(excelFilePath))
                    {
                        // read the Excel file and create the schedule
                        Excel.Application excelApp     = new Excel.Application();
                        Excel.Workbook    workbook     = excelApp.Workbooks.Open(excelFilePath);
                        Excel.Sheets      wbWorksheets = workbook.Worksheets;

                        List <WorksheetObject> worksheetObjs = new List <WorksheetObject>();
                        foreach (Excel.Worksheet ws in wbWorksheets)
                        {
                            WorksheetObject wo   = new WorksheetObject();
                            string          name = ws.Name;
                            wo.Name = name;
                            Excel.Range range = ws.UsedRange;
                            try
                            {
                                range.CopyPicture(Excel.XlPictureAppearance.xlPrinter, Excel.XlCopyPictureFormat.xlBitmap);
                                if (Clipboard.GetDataObject() != null)
                                {
                                    IDataObject data = Clipboard.GetDataObject();
                                    if (data.GetDataPresent(DataFormats.Bitmap))
                                    {
                                        System.Drawing.Image img = (System.Drawing.Image)data.GetData(DataFormats.Bitmap, true);
                                        wo.Image = img;
                                    }
                                }
                            }
                            catch { }
                            worksheetObjs.Add(wo);
                        }

                        // Pop up the worksheet form
                        WorksheetSelectForm wsForm = new WorksheetSelectForm(worksheetObjs, this, doc);
                        wsForm.ShowDialog();

                        if (wsForm.DialogResult.HasValue && wsForm.DialogResult.Value)
                        {
                            for (int i = 0; i < elementIds.Count; i++)
                            {
                                try
                                {
                                    int intValue = selectedRow.ElementId;
                                    if (elementIds[i].IntegerValue == intValue)
                                    {
                                        // read and reload the file.
                                        Scheduler creator = new Scheduler();
                                        creator.ModifySchedule(doc, elementIds[i], excelFilePath, worksheetObj.Name, "Reload Excel Schedule", pathTypes[i], contentOnly);
                                        string docPath;
                                        if (doc.IsWorkshared)
                                        {
                                            docPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(doc.GetWorksharingCentralModelPath());
                                        }
                                        else
                                        {
                                            docPath = doc.PathName;
                                        }

                                        if ((PathType)pathTypes[i] == PathType.Relative)
                                        {
                                            paths[i] = PathExchange.GetRelativePath(excelFilePath, docPath);
                                        }
                                        else
                                        {
                                            paths[i] = excelFilePath;
                                        }

                                        worksheets[i] = worksheetObj.Name;
                                        System.IO.FileInfo fi = new System.IO.FileInfo(excelFilePath);
                                        dateTimes[i] = fi.LastWriteTimeUtc.ToString();

                                        // Read the schema information
                                        Autodesk.Revit.DB.ExtensibleStorage.Schema schema = Autodesk.Revit.DB.ExtensibleStorage.Schema.Lookup(schemaGuid);
                                        if (schema != null)
                                        {
                                            Autodesk.Revit.DB.ExtensibleStorage.Entity entity = null;
                                            DataStorage ds = SchemaManager.GetDataStorage(doc);
                                            try
                                            {
                                                entity = ds.GetEntity(schema);
                                            }
                                            catch { }

                                            if (entity != null)
                                            {
                                                Transaction trans = new Transaction(doc, "Update Excel Document");
                                                trans.Start();
                                                entity.Set <IList <string> >("ExcelFilePath", paths);
                                                entity.Set <IList <string> >("WorksheetName", worksheets);
                                                entity.Set <IList <string> >("DateTime", dateTimes);
                                                entity.Set <IList <int> >("PathType", pathTypes);
                                                ds.SetEntity(entity);
                                                trans.Commit();

                                                BuildTable();
                                            }
                                        }
                                    }
                                }
                                catch { }
                            }
                        }
                        try
                        {
                            workbook.Close();
                            Marshal.ReleaseComObject(worksheets);
                            //Marshal.ReleaseComObject(worksheet);
                            Marshal.ReleaseComObject(workbook);
                            excelApp.Quit();
                            Marshal.ReleaseComObject(excelApp);
                        }
                        catch { }
                    }
                }
            }
        }