refactor: consolidate output folder resolution and prefix handling

Move ParseDrawingNumber + GetDrawingOutputFolder into Export() before
the document-type switch so folder resolution happens once. Extract
PrependPrefix helper in PartExporter to deduplicate the prefix guard.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-16 08:45:53 -05:00
parent 32e8379e9b
commit a17d8cac49
2 changed files with 42 additions and 20 deletions
+11 -16
View File
@@ -63,6 +63,9 @@ namespace ExportDXF.Services
var startTime = DateTime.Now; var startTime = DateTime.Now;
var drawingNumber = ParseDrawingNumber(context);
var outputFolder = _fileExportService.GetDrawingOutputFolder(drawingNumber);
try try
{ {
_solidWorksService.EnableUserControl(false); _solidWorksService.EnableUserControl(false);
@@ -70,15 +73,15 @@ namespace ExportDXF.Services
switch (context.ActiveDocument.DocumentType) switch (context.ActiveDocument.DocumentType)
{ {
case DocumentType.Part: case DocumentType.Part:
ExportPart(context); ExportPart(context, outputFolder);
break; break;
case DocumentType.Assembly: case DocumentType.Assembly:
ExportAssembly(context); ExportAssembly(context, outputFolder);
break; break;
case DocumentType.Drawing: case DocumentType.Drawing:
ExportDrawing(context); ExportDrawing(context, drawingNumber, outputFolder);
break; break;
default: default:
@@ -98,7 +101,7 @@ namespace ExportDXF.Services
#region Export Methods by Document Type #region Export Methods by Document Type
private void ExportPart(ExportContext context) private void ExportPart(ExportContext context, string outputFolder)
{ {
LogProgress(context, "Active document is a Part"); LogProgress(context, "Active document is a Part");
@@ -109,11 +112,10 @@ namespace ExportDXF.Services
return; return;
} }
// Export directly to the output folder _partExporter.ExportSinglePart(part, outputFolder, context);
_partExporter.ExportSinglePart(part, _fileExportService.OutputFolder, context);
} }
private void ExportAssembly(ExportContext context) private void ExportAssembly(ExportContext context, string outputFolder)
{ {
LogProgress(context, "Active document is an Assembly"); LogProgress(context, "Active document is an Assembly");
LogProgress(context, "Fetching components..."); LogProgress(context, "Fetching components...");
@@ -135,11 +137,10 @@ namespace ExportDXF.Services
LogProgress(context, $"Found {items.Count} item(s)."); LogProgress(context, $"Found {items.Count} item(s).");
// Export directly to the output folder ExportItems(items, outputFolder, context);
ExportItems(items, _fileExportService.OutputFolder, context);
} }
private void ExportDrawing(ExportContext context) private void ExportDrawing(ExportContext context, string drawingNumber, string drawingOutputFolder)
{ {
LogProgress(context, "Active document is a Drawing"); LogProgress(context, "Active document is a Drawing");
LogProgress(context, "Finding BOM tables..."); LogProgress(context, "Finding BOM tables...");
@@ -161,12 +162,6 @@ namespace ExportDXF.Services
LogProgress(context, $"Found {items.Count} component(s)"); LogProgress(context, $"Found {items.Count} component(s)");
// Determine drawing number for file naming
var drawingNumber = ParseDrawingNumber(context);
// Resolve output folder: /{outputDir}/{equipmentNo}/{drawingNo}/ or flat fallback
var drawingOutputFolder = _fileExportService.GetDrawingOutputFolder(drawingNumber);
// Export drawing to PDF // Export drawing to PDF
var tempDir = CreateTempWorkDir(); var tempDir = CreateTempWorkDir();
_drawingExporter.ExportToPdf(drawing, tempDir, context); _drawingExporter.ExportToPdf(drawing, tempDir, context);
+31 -4
View File
@@ -32,6 +32,13 @@ namespace ExportDXF.Services
public class PartExporter : IPartExporter public class PartExporter : IPartExporter
{ {
private readonly IFileExportService _fileExportService;
public PartExporter(IFileExportService fileExportService)
{
_fileExportService = fileExportService ?? throw new ArgumentNullException(nameof(fileExportService));
}
public void ExportSinglePart(PartDoc part, string saveDirectory, ExportContext context) public void ExportSinglePart(PartDoc part, string saveDirectory, ExportContext context)
{ {
if (part == null) if (part == null)
@@ -99,12 +106,22 @@ namespace ExportDXF.Services
var templateDrawing = context.GetOrCreateTemplateDrawing(); var templateDrawing = context.GetOrCreateTemplateDrawing();
// Stash existing file before overwriting
item.StashedFilePath = _fileExportService.StashFile(savePath);
if (ExportPartToDxf(part, item.Component.ReferencedConfiguration, savePath, context)) if (ExportPartToDxf(part, item.Component.ReferencedConfiguration, savePath, context))
{ {
item.FileName = Path.GetFileNameWithoutExtension(savePath); item.FileName = Path.GetFileNameWithoutExtension(savePath);
item.ContentHash = Utilities.ContentHasher.ComputeDxfContentHash(savePath);
} }
else else
{ {
// Export failed - restore stashed file if we have one
if (item.StashedFilePath != null && File.Exists(item.StashedFilePath))
{
File.Move(item.StashedFilePath, savePath, overwrite: true);
item.StashedFilePath = null;
}
LogExportFailure(item, context); LogExportFailure(item, context);
} }
} }
@@ -274,7 +291,8 @@ namespace ExportDXF.Services
var isDefaultConfig = string.Equals(config, "default", StringComparison.OrdinalIgnoreCase); var isDefaultConfig = string.Equals(config, "default", StringComparison.OrdinalIgnoreCase);
var name = isDefaultConfig ? title : $"{title} [{config}]"; var name = isDefaultConfig ? title : $"{title} [{config}]";
return prefix + name;
return PrependPrefix(name, prefix);
} }
private string GetItemFileName(Item item, string prefix) private string GetItemFileName(Item item, string prefix)
@@ -282,9 +300,7 @@ namespace ExportDXF.Services
prefix = prefix?.Replace("\"", "''") ?? string.Empty; prefix = prefix?.Replace("\"", "''") ?? string.Empty;
if (string.IsNullOrWhiteSpace(item.ItemNo)) if (string.IsNullOrWhiteSpace(item.ItemNo))
{ return PrependPrefix(item.PartName, prefix);
return prefix + item.PartName;
}
var num = item.ItemNo.PadLeft(2, '0'); var num = item.ItemNo.PadLeft(2, '0');
// Expected format: {DrawingNo} PT{ItemNo} // Expected format: {DrawingNo} PT{ItemNo}
@@ -293,6 +309,17 @@ namespace ExportDXF.Services
: $"{prefix} PT{num}"; : $"{prefix} PT{num}";
} }
private string PrependPrefix(string name, string prefix)
{
if (string.IsNullOrEmpty(prefix))
return name;
if (name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
return name;
return prefix + name;
}
private void LogExportFailure(Item item, ExportContext context) private void LogExportFailure(Item item, ExportContext context)
{ {
var desc = item.Description?.ToLower() ?? string.Empty; var desc = item.Description?.ToLower() ?? string.Empty;