After Running a Sync Function, Right Clicking a layout crashes AutoCAD
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have built a function that backs up data from SheetSet Manager to a database. When this function is ran, it successfully completes, and the user is able to continue working. However, I have an odd error case.
If the user runs this command, and after it completes, they then right click a layout tab, the whole AutoCAD program crashes. This happens every time this case comes up. I have narrowed the crash down to this one function.
I am using AutoCAD 2023 on Windows 10.
Here is the code to the function. This function calls what I beleive is the culprit: The SheetSet(sheetset) constructor (Line 12) . I have come to this conclusion because I used the base constructor I made with no sheetset path and there was no crash induced.
public Routine_SyncSheetSet(Project project)
{
try
{
if (project == null)
{
return;
}
if (project.InDB)
{
SheetSet sheetSet = new SheetSet(project.SheetSet);
Dictionary <string, Sheet> SheetSetSheets = new Dictionary<string, Sheet>();
if (sheetSet == null)
{
MessageBox.Show($"Error - Detected SheetSet was not found/Error extracting data from sheetset:\n{project.SheetSet}");
return;
}
foreach (Sheet sheet in sheetSet.Sheets)
{
if(!SheetSetSheets.ContainsKey($"{sheet.Number} {sheet.RS}"))
{
SheetSetSheets.Add($"{sheet.Number} {sheet.RS}", sheet);
if (sheet.Check())
{
Sheet sheetcopy = new Sheet(sheet);
sheet.Select();
sheetcopy.IssueDate = sheet.IssueDate;
sheetcopy.Update(); // Update the Sheets that are in the Database
}
sheet.Insert(); // Add the Sheets that are not in the Database
}
}
// Query the all the sheets with the SheetSet from the Database
Dictionary<string, Sheet> DBSheets = new Dictionary<string, Sheet>();
SqlConnection conn = Utils.DBUtils.GetOpenConnection();
SqlCommand cmd = new SqlCommand(SheetsQuery(project.SheetSet), conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// Pull each sheet from the database and add it to the DBSheets dictionary
Sheet sheet = new Sheet(reader);
if (!DBSheets.ContainsKey($"{sheet.Number} {sheet.RS}"))
{
DBSheets.Add($"{sheet.Number} {sheet.RS}", sheet);
}
}
reader.Close();
// If the sheet is in the Database, but not in the SheetSet, delete it
foreach (string fullSheetNumber in DBSheets.Keys)
{
if (!SheetSetSheets.ContainsKey(fullSheetNumber))
{
DBSheets[fullSheetNumber].Delete();
}
}
}
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("\nSheetset Synced");
}
catch (Exception ex)
{
new ExceptionTools(ex).HandleException();
}
}
Looking at the Sheet Set Constructor, there is code here that I believe is causing the Layout Error.
public SheetSet(string fullpath)
{
FullPath = fullpath;
ObjectName = "SheetSet";
Sheets = GetSheets(FullPath);
}
public List<Sheet> GetSheets(string fullpath)
{
List<Sheet> Sheets = new List<Sheet>();
IAcSmSheetSetMgr sheetsetmgr = new AcSmSheetSetMgr();
AcSmDatabase sheetset = sheetsetmgr.OpenDatabase(fullpath, false);
ProcessComponents(sheetset.GetSheetSet(), Sheets);
if (Error)
{
MessageBox.Show(ErrorString, "Error Processing SheetSet", MessageBoxButton.OK, MessageBoxImage.Error);
}
sheetsetmgr.Close(sheetset);
return Sheets;
}
private void ProcessComponents(IAcSmComponent component, List<Sheet> sheets)
{
try
{
// Attempt to cast the component to AcSmSubset
AcSmSubset subset = component as AcSmSubset;
if (subset != null)
{
// Recursively process child components of the subset
IAcSmEnumComponent enumComponent = subset.GetSheetEnumerator();
while (true)
{
IAcSmComponent childComponent = enumComponent.Next();
if (childComponent == null)
{
break;
}
ProcessComponents(childComponent, sheets);
}
}
else
{
// Attempt to cast the component to AcSmSheet
AcSmSheet sheet = component as AcSmSheet;
if (sheet != null)
{
// Process the sheet
IAcSmAcDbObjectReference layout = sheet.GetLayout();
string sheetTitle = sheet.GetName();
string fullSheetNumber = sheet.GetNumber();
if (fullSheetNumber == "" || fullSheetNumber == null)
{
return;
}
string sheetNumber = "";
string rsNumber = "";
string rstype = "";
Utils.Utils.SheetTitleParse(fullSheetNumber, ref sheetNumber, ref rsNumber, ref rstype);
string sheetFullPath = layout.GetFileName();
string delta = "";
string dateTimeString = "";
DateTime dateTime = DateTime.Now;
string ca = "";
string reason = "";
try
{
delta = sheet.GetCustomPropertyBag().GetProperty("**DO_NOT_EDIT**1_DELTA").GetValue();
dateTimeString = sheet.GetCustomPropertyBag().GetProperty("**DO_NOT_EDIT**3_DATE-IS").GetValue();
ca = sheet.GetCustomPropertyBag().GetProperty("**DO_NOT_EDIT**4_RFISUB#").GetValue();
reason = sheet.GetCustomPropertyBag().GetProperty("**DO_NOT_EDIT**5_REASON").GetValue();
}
catch (Exception ex)
{
Error = true;
ErrorString = "SheetSet -> Process Components -> Format of the Sheets in SheetSet is not correct. Error with pulling **DO_NOT_EDIT** data";
return;
}
string RFI = "";
string SUB = "";
if (delta == "Value")
{
delta = "";
}
if (reason == "Value")
{
reason = "";
}
if (dateTimeString == null || dateTimeString == "Value" || dateTimeString.Trim() == "")
{
dateTime = DateTime.Parse("2000-01-01");
}
else
{
if (DateTime.TryParse(dateTimeString, out DateTime dt))
{
dateTime = dt;
}
else
{
dateTime = DateTime.Parse("2000-01-01");
}
}
CAtoRFISUB(ca, ref RFI, ref SUB);
sheets.Add(new Sheet(FullPath, sheetTitle, sheetNumber, rsNumber, sheetFullPath, delta, dateTime, RFI, SUB, reason));
}
// Else, skip the component if it's neither a subset nor a sheet
}
}
catch (Exception ex)
{
new ExceptionTools(ex).HandleException();
}
}
Here is some information that I am given when the Exception is raised:
Any thoughts, help, or advice would greatly be appreciated.
Thank you!