public SchemaEntityOpr(Element ele)
 {
     if (false == GetEntityFrom(ele))
     {
         m_sch    = SchemaOpr.Instance().schema;
         m_entity = new Entity(m_sch);
     }
 }
 public SchemaEntityOpr(Entity ent)
 {
     if (null != ent && ent.Schema.GUID == SchemaOpr.Instance().schema.GUID)
     {
         m_sch    = ent.Schema;
         m_entity = ent;
     }
 }
        /// <summary>
        /// 从element获取扩展存储
        /// </summary>
        /// <param name="ele"></param>
        /// <returns></returns>
        public bool GetEntityFrom(Element ele)
        {
            Entity ent = ele.GetEntity(SchemaOpr.Instance().schema);

            if (ent.IsValid())
            {
                m_sch    = ent.Schema;
                m_entity = ent;
                return(true);
            }

            //
            return(false);
        }
        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();
                        }
                    }
                }
            }
        }
        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 { }
                    }
                }
            }
        }