Exemplo n.º 1
0
        private void ModifySchemaData(Schema schema, ElementId scheduleId)
        {
            DataStorage ds = SchemaManager.GetDataStorage(_doc);

            if (ds == null || !ds.IsValidObject)// Build data storage if necessary.
            {
                return;
            }

            ExcelScheduleEntity schedEntity = ds.GetEntity <ExcelScheduleEntity>();

            if (schedEntity == null)
            {
                return;
            }

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

            schedEntity.DateTime[index] = new FileInfo(schedEntity.ExcelFilePath[index]).LastWriteTimeUtc.ToString();
            ds.SetEntity(schedEntity);
        }
        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            // Change the ExtensibleStorage if the path was changed
            if (pathChanged)
            {
                paths.Clear();
                pathTypes.Clear();

                foreach (LinkData ld in LinkedData)
                {
                    paths.Add(ld.Path);
                    pathTypes.Add((int)ld.PathType);
                }

                // Get the Schema and entity
                Schema schema = Schema.Lookup(schemaGuid);
                if (schema != null && schema.IsValidObject)
                {
                    //Autodesk.Revit.DB.ExtensibleStorage.Entity entity = null;
                    DataStorage ds = SchemaManager.GetDataStorage(doc);
                    if (ds != null)
                    {
                        ExcelScheduleEntity schedEntity = ds.GetEntity <ExcelScheduleEntity>();
                        if (schedEntity != null)
                        {
                            using (Transaction trans = new Transaction(doc, "Manage Excel Links"))
                            {
                                trans.Start();

                                // Change the Path and PathType Parameters
                                schedEntity.ExcelFilePath = paths.ToList();
                                schedEntity.PathType      = pathTypes.ToList();
                                ds.SetEntity(schedEntity);

                                trans.Commit();
                            }
                        }
                    }
                    //try
                    //{
                    //    entity = ds.GetEntity(schema);
                    //}
                    //catch { }

                    //if (entity != null)
                    //{
                    //    Transaction trans = new Transaction(doc, "Manage Excel Links");
                    //    trans.Start();
                    //    entity.Set<IList<string>>("ExcelFilePath", paths);
                    //    entity.Set<IList<int>>("PathType", pathTypes);
                    //    ds.SetEntity(entity);
                    //    trans.Commit();
                    //}
                }
            }

            Close();
        }
Exemplo n.º 3
0
        private void RebuildSchema(Document doc, Schema schema, List <ElementId> elementIds, List <string> paths, List <string> worksheets, List <string> dateTimes, List <int> pathTypes)
        {
            using (Transaction trans = new Transaction(doc, "Updating Excel Link Information"))
            {
                trans.Start();
                try
                {
                    SubTransaction deleteTrans = new SubTransaction(doc);
                    deleteTrans.Start();
                    // Delete the schema/entity from the ProjectInformation
                    doc.ProjectInformation.DeleteEntity(schema);
                    Schema.EraseSchemaAndAllEntities(schema, false);
                    deleteTrans.Commit();


                    // Start a subtransaction for create the datastorage and entity
                    SubTransaction createTrans = new SubTransaction(doc);
                    createTrans.Start();

                    // Build the schedule Entity
                    ExcelScheduleEntity schedEntity = new ExcelScheduleEntity()
                    {
                        DateTime      = dateTimes,
                        ExcelFilePath = paths,
                        PathType      = pathTypes,
                        ScheduleId    = elementIds,
                        WorksheetName = worksheets
                    };

                    // Create a DataStorage for the entity
                    DataStorage ds = DataStorage.Create(doc);
                    ds.Name = Properties.Settings.Default.DataStorageName;
                    ds.SetEntity(schedEntity);

                    // complete the Create subtransaction
                    createTrans.Commit();
                }
                catch (Exception ex)
                {
                    TaskDialog.Show("Test", ex.ToString());
                }
                trans.Commit();
            }
        }
Exemplo n.º 4
0
        public void DocumentOpened(object sender, DocumentOpenedEventArgs e)
        {
            Document doc = e.Document;

            if (doc.IsFamilyDocument)
            {
                return;
            }

            Schema schema = Schema.Lookup(schemaGUID);

            if (schema == null || !schema.IsValidObject)
            {
                return;
            }

            // Check to see if there is out-dated data stored in ProjectInformation
            Entity entity = null;

            entity = doc.ProjectInformation.GetEntity(schema);
            if (entity != null && entity.IsValid())
            {
                // Need to transition the data to a datastorage object.
                // First make sure this isn't a workshared document with the ProjectInfo already checked out by another user
                // If it's checked out by another person, we'll just skip this since we can't fix it now.
                if (doc.IsWorkshared && WorksharingUtils.GetCheckoutStatus(doc, doc.ProjectInformation.Id) == CheckoutStatus.OwnedByOtherUser)
                {
                    return;
                }

                // Otherwise, lets transition the data from the old to the new.
                if (entity.Get <IList <ElementId> >("ScheduleId") != null)
                {
                    // Get the information from the ProjectInformation entity
                    var schedIds = entity.Get <IList <ElementId> >("ScheduleId").ToList();
                    var paths    = entity.Get <IList <string> >("ExcelFilePath").ToList();
                    var wsNames  = entity.Get <IList <string> >("WorksheetName").ToList();
                    var dts      = entity.Get <IList <string> >("DateTime").ToList();
                    var pTypes   = entity.Get <IList <int> >("PathType")?.ToList() ?? new List <int>();

                    // Purge the old Schema and Entity, then assign the data to a new Schema and DataStorage element
                    RebuildSchema(doc, schema, schedIds, paths, wsNames, dts, pTypes);
                }
            }

            // Find if a datstorage element exists now and update as needed.
            DataStorage ds = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).Where(x => x.Name.Equals(dsName)).Cast <DataStorage>().FirstOrDefault();

            // Get the ExcelScheduleEntity from the data storage and verify its valid
            ExcelScheduleEntity ent = ds?.GetEntity <ExcelScheduleEntity>();

            if (ent == null)
            {
                return;
            }

            // Check if any schedules need to be updated
            List <int>      modifyIndices = new List <int>();
            List <DateTime> modDateTimes  = new List <DateTime>();

            for (int i = 0; i < ent.ScheduleId.Count; i++)
            {
                string currentFilePath;
                string docPath;
                if (doc.IsWorkshared)
                {
                    docPath = ModelPathUtils.ConvertModelPathToUserVisiblePath(doc.GetWorksharingCentralModelPath());
                }
                else
                {
                    docPath = doc.PathName;
                }

                if ((PathType)ent.PathType[i] == PathType.Absolute)
                {
                    currentFilePath = ent.ExcelFilePath[i];
                }
                else
                {
                    currentFilePath = PathExchange.GetFullPath(ent.ExcelFilePath[i], docPath);
                }

                // Get the file write time as UTC
                DateTime modTime    = new FileInfo(currentFilePath).LastWriteTimeUtc;
                DateTime storedTime = Convert.ToDateTime(ent.DateTime[i]);

                // Make sure the save time isn't more or less the same as stored.
                if ((modTime - storedTime).Seconds > 1)
                {
                    modifyIndices.Add(i);
                    modDateTimes.Add(modTime);
                }
            }

            if (modifyIndices.Count == modDateTimes.Count && modifyIndices.Count > 0)
            {
                IntPtr statusBar = FindWindowEx(RevitHandle, IntPtr.Zero, "msctls_statusbar32", "");
                foreach (int i in modifyIndices)
                {
                    if (statusBar != IntPtr.Zero)
                    {
                        SetWindowText(statusBar, string.Format("Updating Excel Schedule {0}.", ent.WorksheetName[modifyIndices[i]]));
                    }
                    Scheduler scheduler = new Scheduler();
                    scheduler.ModifySchedule(doc, ent.ScheduleId[modifyIndices[i]], ent.ExcelFilePath[modifyIndices[i]],
                                             ent.WorksheetName[modifyIndices[i]], "Update Excel Schedule", ent.PathType[modifyIndices[i]],
                                             Properties.Settings.Default.reloadValuesOnly);

                    ent.DateTime[modifyIndices[i]] = modDateTimes[i].ToString();
                }
                if (statusBar != IntPtr.Zero)
                {
                    SetWindowText(statusBar, "");
                }

                // change the dateTimes
                using (Transaction t = new Transaction(doc, "Update schedule date"))
                {
                    t.Start();
                    ds.SetEntity(ent);
                    t.Commit();
                }

                // Write to home
                RevitCommon.FileUtils.WriteToHome("Excel Import - Document Open Reload", doc.Application.VersionName, doc.Application.Username);
            }
        }
Exemplo n.º 5
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);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private bool AssignSchemaData(ElementId scheduleId, string worksheetName, Document doc)
        {
            // TODO: At some point it may be good to adjust this section. It's currently trying to read the Entity from two different objects
            // TODO: the original ProjectInformation Entity and the new one attached to a DataStorage object which should be more reliable and safe.


            Entity entity = null;
            ExcelScheduleEntity schedEntity = null;
            DataStorage         ds          = null;

            List <ElementId> elementIds = new List <ElementId>();
            List <string>    filePaths  = new List <string>();
            List <string>    worksheets = new List <string>();
            List <string>    dateTimes  = new List <string>();
            List <int>       pathTypes  = new List <int>();


            // Check to see if the ProjectInfo has an Entity stored in it.
            Schema schema = Schema.Lookup(schemaGUID);

            if (null != schema && schema.IsValidObject)
            {
                entity = _doc.ProjectInformation.GetEntity(schema);
                bool purgeFromProjInfo = false;
                if (entity.IsValid())
                {
                    purgeFromProjInfo = true;
                    // Retrieve the data from it
                    elementIds = entity.Get <IList <ElementId> >("ScheduleId").ToList();
                    filePaths  = entity.Get <IList <string> >("ExcelFilePath").ToList();
                    worksheets = entity.Get <IList <string> >("WorksheetName").ToList();
                    dateTimes  = entity.Get <IList <string> >("DateTime").ToList();
                    pathTypes  = entity.Get <IList <int> >("PathType").ToList();

                    // Delete the entity, we should be transitioning over to the DataStorage object.
                    _doc.ProjectInformation.DeleteEntity(schema);
                }

                // See if a datastorage object already exists for this.
                ICollection <ElementId> dsCollector = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).ToElementIds();
                foreach (ElementId eid in dsCollector)
                {
                    DataStorage dStor = doc.GetElement(eid) as DataStorage;
                    if (dStor.Name == dsName)
                    {
                        ds = dStor;
                        break;
                    }
                }

                if (purgeFromProjInfo)
                {
                    doc.ProjectInformation.DeleteEntity(schema);
                }
            }

            // Create the dataStorage if necessary
            if (ds == null)
            {
                ds      = DataStorage.Create(doc);
                ds.Name = dsName;
            }


            schedEntity = ds.GetEntity <ExcelScheduleEntity>();

            if (schedEntity == null)
            {
                // build out the entity and give it default empty lists.
                schedEntity = new ExcelScheduleEntity()
                {
                    ExcelFilePath = filePaths,
                    DateTime      = dateTimes,
                    PathType      = pathTypes,
                    ScheduleId    = elementIds,
                    WorksheetName = worksheets
                };
            }

            schedEntity.ScheduleId.Add(scheduleId);
            schedEntity.ExcelFilePath.Add(excelFilePath);
            schedEntity.WorksheetName.Add(worksheetName);
            FileInfo fi = new FileInfo(excelFilePath);

            schedEntity.DateTime.Add(fi.LastWriteTimeUtc.ToString());
            schedEntity.PathType.Add(0);
            ds.SetEntity(schedEntity);

            return(true);
        }
        public ManageExcelLinksForm(Document _doc, Guid _schemaGuid)
        {
            doc        = _doc;
            schemaGuid = _schemaGuid;
            InitializeComponent();


            // Read the schema information
            Schema schema = Schema.Lookup(schemaGuid);

            if (schema != null)
            {
                Autodesk.Revit.DB.ExtensibleStorage.DataStorage ds = null;
                ICollection <ElementId> dsCollector = new FilteredElementCollector(doc).OfClass(typeof(DataStorage)).ToElementIds();
                foreach (ElementId eid in dsCollector)
                {
                    DataStorage dStor = doc.GetElement(eid) as DataStorage;
                    if (dStor.Name == Properties.Settings.Default.DataStorageName)
                    {
                        ds = dStor;
                        break;
                    }
                }

                if (ds != null)
                {
                    Autodesk.Revit.DB.ExtensibleStorage.Entity entity = null;
                    entity = ds.GetEntity(schema);
                    ExcelScheduleEntity schedEntity = ds.GetEntity <ExcelScheduleEntity>();
                    if (schedEntity != null)
                    {
                        elementIds = schedEntity.ScheduleId;
                        paths      = schedEntity.ExcelFilePath;
                        worksheets = schedEntity.WorksheetName;
                        dateTimes  = schedEntity.DateTime;
                        pathTypes  = schedEntity.PathType;
                    }

                    //if (entity.IsValid())
                    //{
                    //    elementIds = entity.Get<IList<ElementId>>("ScheduleId");
                    //    paths = entity.Get<IList<string>>("ExcelFilePath");
                    //    worksheets = entity.Get<IList<string>>("WorksheetName");
                    //    dateTimes = entity.Get<IList<string>>("DateTime");
                    //    try
                    //    {
                    //        pathTypes = entity.Get<IList<int>>("PathType");
                    //    }
                    //    catch
                    //    {
                    //        List<int> tempPaths = new List<int>();
                    //        for (int i = 0; i < elementIds.Count; i++)
                    //        {
                    //            tempPaths.Add(0);
                    //        }
                    //        pathTypes = tempPaths;
                    //    }
                    //}
                }


                ////Autodesk.Revit.DB.ExtensibleStorage.Entity entity = null;
                //try
                //{
                //    entity = doc.ProjectInformation.GetEntity(schema);
                //}
                //catch { }

                //if (entity != null)
                //{
                //    try
                //    {
                //        elementIds = entity.Get<IList<ElementId>>("ScheduleId");
                //        paths = entity.Get<IList<string>>("ExcelFilePath");
                //        worksheets = entity.Get<IList<string>>("WorksheetName");
                //        dateTimes = entity.Get<IList<string>>("DateTime");
                //        try
                //        {
                //            pathTypes = entity.Get<IList<int>>("PathType");
                //        }
                //        catch
                //        {
                //            List<int> tempPaths = new List<int>();
                //            for (int i = 0; i < elementIds.Count; i++)
                //            {
                //                tempPaths.Add(0);
                //            }
                //            pathTypes = tempPaths;
                //        }
                //    }
                //    catch { }
                //    if (elementIds == null)
                //    {
                //        elementIds = new List<ElementId>();
                //        paths = new List<string>();
                //        worksheets = new List<string>();
                //        dateTimes = new List<string>();
                //        pathTypes = new List<int>();
                //    }
                //}
            }
            contentOnlyCheckBox.IsChecked = contentOnly;
            contentOnlyCheckBox.ToolTip   = "When reloading, only get new content and not style information or modify rows/columns.\nChanging this setting is only remembered while this Manage Excel Links window is open.";
            //linkDataGrid.ItemsSource = LinkedData;
            // Build the table
            BuildTable();
        }