public LinkedSheet GetLinkedSheet(string uniqueId, Guid projectGuid) { LinkedSheet lSheet = null; try { if (null != connection && null != command) { using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT * FROM LinkedSheets WHERE LinkedSheet_Element_Id= '" + uniqueId + "' AND LinkedSheet_Project_Id = '" + projectGuid.ToString() + "'"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { lSheet = new LinkedSheet(); lSheet.Id = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Id")); lSheet.SheetId = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Sheet_Id")); Guid projectId = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Project_Id")); lSheet.LinkProject = new LinkedProject(projectGuid); lSheet.LinkedElementId = reader.GetString(reader.GetOrdinal("LinkedSheet_Element_Id")); lSheet.IsSource = reader.GetBoolean(reader.GetOrdinal("LinkedSheet_IsSource")); break; } } } } } catch (Exception ex) { string message = ex.Message; } return(lSheet); }
public static bool ChangeLinkedSheet(LinkedSheet item, CommandType cmdType) { bool result = false; try { if (null != connection && null != command) { switch (cmdType) { case CommandType.INSERT: command.CommandText = @"INSERT INTO LinkedSheets (LinkedSheet_Id, LinkedSheet_Sheet_Id, LinkedSheet_Project_Id, LinkedSheet_Element_Id, LinkedSheet_IsSource)" + "VALUES ('" + item.Id.ToString() + "', '" + item.SheetId.ToString() + "', '" + item.LinkProject.Id.ToString() + "', '" + item.LinkedElementId.ToString() + "', @isSource)"; command.Parameters["@isSource"].Value = item.IsSource; if (command.ExecuteNonQuery() > 0) { result = true; } break; case CommandType.UPDATE: command.CommandText = @"UPDATE LinkedSheets SET LinkedSheet_Sheet_Id = '" + item.SheetId.ToString() + "', LinkedSheet_Project_Id = '" + item.LinkProject.Id.ToString() + "', " + "LinkedSheet_Element_Id ='" + item.LinkedElementId + "', LinkedSheet_IsSource = @isSource" + " WHERE LinkedSheet_Id = '" + item.Id.ToString() + "'"; command.Parameters["@isSource"].Value = item.IsSource; if (command.ExecuteNonQuery() > 0) { result = true; } break; case CommandType.DELETE: command.CommandText = "DELETE FROM LinkedSheets WHERE LinkedSheet_Id = '" + item.Id.ToString() + "'"; if (command.ExecuteNonQuery() > 0) { result = true; } break; } } } catch (Exception ex) { string message = ex.Message; } return(result); }
private bool DeleteSheet(ElementId sheetId) { bool deleted = false; try { if (idMaps.ContainsKey(sheetId)) { string uniqueId = idMaps[sheetId]; LinkedSheet linkedSheet = dataManager.GetLinkedSheet(uniqueId, linkedProjectId); if (null != linkedSheet) { if (linkedSheet.IsSource) { MessageBoxResult msgResult = MessageBox.Show("Would you like to delete the sheet item in the linked database?", "Delete Sheet Source Item", MessageBoxButton.YesNo, MessageBoxImage.Question); if (msgResult == MessageBoxResult.Yes) { RevitSheet rvtSheet = new RevitSheet() { Id = linkedSheet.SheetId }; bool sheetDeleted = SheetDataWriter.ChangeSheetItem(rvtSheet, CommandType.DELETE); bool paramDeleted = SheetDataWriter.DeleteSheetParameterValue(rvtSheet.Id.ToString()); bool revisionDeleted = SheetDataWriter.DeleteRevisionOnSheet("RevisionsOnSheet_Sheet_Id", rvtSheet.Id.ToString()); bool linkDeleted = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.DELETE); deleted = (sheetDeleted && paramDeleted && revisionDeleted && linkDeleted) ? true : false; } else { deleted = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.DELETE); } } else { deleted = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.DELETE); } } } } catch (Exception ex) { string message = ex.Message; } return(deleted); }
private bool UpdateSheet(ViewSheet viewSheet, List <ElementId> paramIds) { bool updated = false; try { LinkedSheet lsheet = dataManager.GetLinkedSheet(viewSheet.UniqueId, linkedProjectId); if (null != lsheet) { foreach (ElementId paramId in paramIds) { string paramName = sheetParameters[paramId]; Parameter param = viewSheet.LookupParameter(paramName); if (null != param) { string paramValue = ""; switch (param.StorageType) { case StorageType.Double: paramValue = param.AsDouble().ToString(); break; case StorageType.ElementId: paramValue = param.AsElementId().IntegerValue.ToString(); break; case StorageType.Integer: paramValue = param.AsInteger().ToString(); break; case StorageType.String: paramValue = param.AsString(); break; } var paramFound = from sheetParam in rvtSheetParameters where sheetParam.ParameterName == paramName select sheetParam; if (paramFound.Count() > 0) { SheetParameter sheetParam = paramFound.First(); SheetParameterValue sheetParamValue = dataManager.GetSheetParameterValue(sheetParam.ParameterId, lsheet.SheetId); sheetParamValue.ParameterValue = paramValue; updated = SheetDataWriter.ChangeSheetParameterValue(sheetParamValue, CommandType.UPDATE); } else if (paramId.IntegerValue == (int)BuiltInParameter.SHEET_NAME) { updated = SheetDataWriter.ChangeSheetItem(lsheet.SheetId.ToString(), "Sheet_Name", paramValue); } else if (paramId.IntegerValue == (int)BuiltInParameter.SHEET_NUMBER) { updated = SheetDataWriter.ChangeSheetItem(lsheet.SheetId.ToString(), "Sheet_Number", paramValue); } } } } } catch (Exception ex) { string message = ex.Message; } return(updated); }
private bool InsertSheet(ViewSheet sheet) { bool sheetInserted = false; try { // insert into the database connected RevitSheet rvtSheet = new RevitSheet(Guid.NewGuid(), sheet.SheetNumber, sheet.ViewName); LinkedSheet linkedSheet = new LinkedSheet(Guid.NewGuid(), rvtSheet.Id, new LinkedProject(linkedProjectId), sheet.UniqueId, true); rvtSheet.LinkedSheets.Add(linkedSheet); bool sheetDBUpdated = SheetDataWriter.ChangeSheetItem(rvtSheet, CommandType.INSERT); bool linkedSheetDBUpdated = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.INSERT); List <SheetParameterValue> paramValues = new List <SheetParameterValue>(); foreach (SheetParameter sheetParam in rvtSheetParameters) { SheetParameterValue paramValue = new SheetParameterValue(); paramValue.ParameterValueId = Guid.NewGuid(); paramValue.Parameter = sheetParam; paramValue.SheetId = rvtSheet.Id; Parameter param = sheet.LookupParameter(sheetParam.ParameterName); if (null != param) { switch (param.StorageType) { case StorageType.Double: paramValue.ParameterValue = param.AsDouble().ToString(); break; case StorageType.ElementId: paramValue.ParameterValue = param.AsElementId().IntegerValue.ToString(); break; case StorageType.Integer: paramValue.ParameterValue = param.AsInteger().ToString(); break; case StorageType.String: paramValue.ParameterValue = param.AsString(); break; } } paramValues.Add(paramValue); } bool sheetParamDBUpdated = SheetDataWriter.InsertMultipleParameterValue(paramValues); List <Guid> revisionIds = dataManager.GetRevisionIds(); List <RevisionOnSheet> rosList = new List <RevisionOnSheet>(); foreach (Guid revisionId in revisionIds) { RevitRevision rvtRevision = new RevitRevision(); rvtRevision.Id = revisionId; RevisionOnSheet ros = new RevisionOnSheet(Guid.NewGuid(), rvtSheet.Id, rvtRevision, false); rvtSheet.SheetRevisions.Add(rvtRevision.Id, ros); rosList.Add(ros); } bool rosDBUpdated = SheetDataWriter.InsertMultipleRevisionOnSheet(rosList); sheetInserted = (sheetDBUpdated && linkedSheetDBUpdated && rosDBUpdated) ? true : false; if (sheetInserted && !idMaps.ContainsKey(sheet.Id)) { idMaps.Add(sheet.Id, sheet.UniqueId); } } catch (Exception ex) { string message = ex.Message; } return(sheetInserted); }
private static void CheckSheetLinks(Document doc, Guid projectId, ref RevitSheetData sheetData) { try { Dictionary <Guid /*revisionId*/, int /*elementId*/> revDictionary = new Dictionary <Guid, int>(); var linkedRevisions = from revision in sheetData.Revisions where revision.LinkStatus.IsLinked select revision; if (linkedRevisions.Count() > 0) { revDictionary = linkedRevisions.ToDictionary(o => o.Id, o => o.LinkStatus.LinkedElementId); } for (int i = 0; i < sheetData.Sheets.Count; i++) { RevitSheet rvtSheet = sheetData.Sheets[i]; var linkedSheetFound = from lsheet in sheetData.Sheets[i].LinkedSheets where lsheet.LinkProject.Id == projectId select lsheet; if (linkedSheetFound.Count() > 0) { LinkedSheet linkedSheet = linkedSheetFound.First(); ViewSheet viewSheet = doc.GetElement(linkedSheet.LinkedElementId) as ViewSheet; if (null != viewSheet) { sheetData.Sheets[i].LinkStatus.IsLinked = true; sheetData.Sheets[i].LinkStatus.CurrentLinkedId = linkedSheet.LinkedElementId; sheetData.Sheets[i].LinkStatus.LinkedElementId = viewSheet.Id.IntegerValue; sheetData.Sheets[i].LinkStatus.ToolTip = "Linked Sheet ElementId: " + viewSheet.Id.IntegerValue; //parameter check string toolTip = ""; if (CompareSheetParameters(viewSheet, sheetData.Sheets[i], sheetData, out toolTip)) { sheetData.Sheets[i].LinkStatus.Modified = true; sheetData.Sheets[i].LinkStatus.ToolTip = toolTip; } var viewRevisionIds = from elementId in viewSheet.GetAllRevisionIds() select elementId.IntegerValue; //revision check foreach (Guid revisionId in rvtSheet.SheetRevisions.Keys) { RevisionOnSheet ros = rvtSheet.SheetRevisions[revisionId]; var revisionFound = from revision in sheetData.Revisions where revision.Id == revisionId select revision; if (revisionFound.Count() > 0) { RevitRevision rvtRevision = revisionFound.First(); sheetData.Sheets[i].SheetRevisions[revisionId].RvtRevision = rvtRevision; if (rvtRevision.LinkStatus.IsLinked) { if (ros.Include && viewRevisionIds.Contains(rvtRevision.LinkStatus.LinkedElementId)) { sheetData.Sheets[i].SheetRevisions[revisionId].LinkStatus.IsLinked = true; } } } } } else { //item deleted sheetData.Sheets[i].LinkedSheets.Remove(linkedSheet); bool linkedSheetDBUpdated = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.DELETE); } } } } catch (Exception ex) { MessageBox.Show("Failed to check link status of sheets.\n" + ex.Message, "Sheet Manager Updater - Check Sheet Links", MessageBoxButton.OK, MessageBoxImage.Warning); } }
public static bool InsertNewSheet(ViewSheet sheet, Guid projectId, ref RevitSheetData sheetData) { bool inserted = false; try { var sheetNumberExisting = from rvtSheet in sheetData.Sheets where rvtSheet.Number == sheet.SheetNumber select rvtSheet; if (sheetNumberExisting.Count() > 0) { RevitSheet rvtSheet = sheetNumberExisting.First(); int sheetIndex = sheetData.Sheets.IndexOf(rvtSheet); LinkedSheet linkedSheet = new LinkedSheet(Guid.NewGuid(), rvtSheet.Id, new LinkedProject(projectId), sheet.UniqueId, false); sheetData.Sheets[sheetIndex].LinkedSheets.Add(linkedSheet); bool linkedSheetDBUpdated = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.INSERT); RevitLinkStatus linkStatus = new RevitLinkStatus(); linkStatus.IsLinked = true; linkStatus.CurrentLinkedId = sheet.UniqueId; linkStatus.LinkedElementId = sheet.Id.IntegerValue; linkStatus.ToolTip = "Linked Sheet ElementId: " + sheet.Id.IntegerValue; string toolTip = ""; if (CompareSheetParameters(sheet, rvtSheet, sheetData, out toolTip)) { linkStatus.Modified = true; linkStatus.ToolTip = toolTip; } sheetData.Sheets[sheetIndex].LinkStatus = linkStatus; inserted = true; } else { RevitSheet rvtSheet = new RevitSheet(Guid.NewGuid(), sheet.SheetNumber, sheet.ViewName); LinkedSheet linkedSheet = new LinkedSheet(Guid.NewGuid(), rvtSheet.Id, new LinkedProject(projectId), sheet.UniqueId, true); rvtSheet.LinkedSheets.Add(linkedSheet); bool sheetDBUpdated = SheetDataWriter.ChangeSheetItem(rvtSheet, CommandType.INSERT); bool linkedSheetDBUpdated = SheetDataWriter.ChangeLinkedSheet(linkedSheet, CommandType.INSERT); RevitLinkStatus linkStatus = new RevitLinkStatus(); linkStatus.IsLinked = true; linkStatus.CurrentLinkedId = sheet.UniqueId; linkStatus.LinkedElementId = sheet.Id.IntegerValue; linkStatus.ToolTip = "Linked Sheet ElementId: " + sheet.Id.IntegerValue; rvtSheet.LinkStatus = linkStatus; foreach (SheetParameter sheetParam in sheetData.SheetParameters) { SheetParameterValue paramValue = new SheetParameterValue(); paramValue.ParameterValueId = Guid.NewGuid(); paramValue.Parameter = sheetParam; paramValue.SheetId = rvtSheet.Id; Parameter param = sheet.LookupParameter(sheetParam.ParameterName); if (null != param) { switch (param.StorageType) { case StorageType.Double: paramValue.ParameterValue = param.AsDouble().ToString(); break; case StorageType.ElementId: paramValue.ParameterValue = param.AsElementId().IntegerValue.ToString(); break; case StorageType.Integer: paramValue.ParameterValue = param.AsInteger().ToString(); break; case StorageType.String: paramValue.ParameterValue = param.AsString(); break; } } rvtSheet.SheetParameters.Add(sheetParam.ParameterId, paramValue); } bool sheetParamDBUpdated = SheetDataWriter.InsertMultipleParameterValue(rvtSheet.SheetParameters.Values.ToList()); List <RevisionOnSheet> rosList = new List <RevisionOnSheet>(); foreach (RevitRevision revision in sheetData.Revisions) { RevisionOnSheet ros = new RevisionOnSheet(Guid.NewGuid(), rvtSheet.Id, revision, false); rvtSheet.SheetRevisions.Add(revision.Id, ros); rosList.Add(ros); } bool rosDBUpdated = SheetDataWriter.InsertMultipleRevisionOnSheet(rosList); sheetData.Sheets.Add(rvtSheet); inserted = (sheetDBUpdated && linkedSheetDBUpdated && rosDBUpdated) ? true : false; } } catch (Exception ex) { MessageBox.Show("Failed to add a new sheet element.\n" + ex.Message, "New Sheet", MessageBoxButton.OK, MessageBoxImage.Warning); } return(inserted); }
private static void ReadRevitSheets(ref RevitSheetData sheetData) { try { ObservableCollection <LinkedSheet> linkedSheets = new ObservableCollection <LinkedSheet>(); using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT * FROM LinkedSheets"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { LinkedSheet lSheet = new LinkedSheet(); lSheet.Id = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Id")); lSheet.SheetId = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Sheet_Id")); Guid projectId = reader.GetGuid(reader.GetOrdinal("LinkedSheet_Project_Id")); var projectFound = from project in sheetData.LinkedProjects where project.Id == projectId select project; if (projectFound.Count() > 0) { lSheet.LinkProject = projectFound.First(); } lSheet.LinkedElementId = reader.GetString(reader.GetOrdinal("LinkedSheet_Element_Id")); lSheet.IsSource = reader.GetBoolean(reader.GetOrdinal("LinkedSheet_IsSource")); linkedSheets.Add(lSheet); } } } ObservableCollection <SheetParameterValue> sheetParameterValues = new ObservableCollection <SheetParameterValue>(); using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT * FROM SheetParameterValues"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { SheetParameterValue paramValue = new SheetParameterValue(); paramValue.ParameterValueId = reader.GetGuid(reader.GetOrdinal("ParameterValue_Id")); paramValue.SheetId = reader.GetGuid(reader.GetOrdinal("ParameterValue_Sheet_Id")); Guid parameterId = reader.GetGuid(reader.GetOrdinal("ParameterValue_Parameter_Id")); var paramFound = from param in sheetData.SheetParameters where param.ParameterId == parameterId select param; if (paramFound.Count() > 0) { paramValue.Parameter = paramFound.First(); } paramValue.ParameterValue = reader.GetString(reader.GetOrdinal("ParameterValue_Parameter_Value")); sheetParameterValues.Add(paramValue); } } } ObservableCollection <RevisionOnSheet> revisionOnSheets = new ObservableCollection <RevisionOnSheet>(); using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT * FROM RevisionsOnSheet"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { RevisionOnSheet ros = new RevisionOnSheet(); ros.MapId = reader.GetGuid(reader.GetOrdinal("RevisionsOnSheet_Id")); ros.SheetId = reader.GetGuid(reader.GetOrdinal("RevisionsOnSheet_Sheet_Id")); Guid revisionId = reader.GetGuid(reader.GetOrdinal("RevisionsOnSheet_Revision_Id")); var revisionFound = from rev in sheetData.Revisions where rev.Id == revisionId select rev; if (revisionFound.Count() > 0) { ros.RvtRevision = revisionFound.First(); } ros.Include = reader.GetBoolean(reader.GetOrdinal("RevisionsOnSheet_Include")); revisionOnSheets.Add(ros); } } } sheetData.Sheets.Clear(); using (SQLiteCommand cmd = new SQLiteCommand(connection)) { cmd.CommandText = "SELECT * FROM Sheets"; using (SQLiteDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { RevitSheet sheet = new RevitSheet(); sheet.Id = reader.GetGuid(reader.GetOrdinal("Sheet_Id")); sheet.Number = reader.GetString(reader.GetOrdinal("Sheet_Number")); sheet.Name = reader.GetString(reader.GetOrdinal("Sheet_Name")); Guid disciplineId = reader.GetGuid(reader.GetOrdinal("Sheet_Discipline_Id")); var disciplineFound = from discipline in sheetData.Disciplines where discipline.Id == disciplineId select discipline; if (disciplineFound.Count() > 0) { sheet.DisciplineObj = disciplineFound.First(); } var linkedSheetFound = from linkedSheet in linkedSheets where linkedSheet.SheetId == sheet.Id select linkedSheet; if (linkedSheetFound.Count() > 0) { sheet.LinkedSheets = new ObservableCollection <LinkedSheet>(linkedSheetFound.ToList()); } foreach (SheetParameter param in sheetData.SheetParameters) { var parameterValueFound = from paramValue in sheetParameterValues where paramValue.SheetId == sheet.Id && paramValue.Parameter.ParameterId == param.ParameterId select paramValue; if (parameterValueFound.Count() > 0) { SheetParameterValue value = parameterValueFound.First(); sheet.SheetParameters.Add(param.ParameterId, value); } } var revisionOnSheetFound = from ros in revisionOnSheets where ros.SheetId == sheet.Id select ros; if (revisionOnSheetFound.Count() > 0) { foreach (RevisionOnSheet ros in revisionOnSheetFound) { if (!sheet.SheetRevisions.ContainsKey(ros.RvtRevision.Id)) { sheet.SheetRevisions.Add(ros.RvtRevision.Id, ros); } } } sheetData.Sheets.Add(sheet); } } } sheetData.Sheets = new ObservableCollection <RevitSheet>(sheetData.Sheets.OrderBy(o => o.Number)); } catch (Exception ex) { string message = ex.Message; } }