Refactored ExportContext
This commit is contained in:
@@ -26,7 +26,7 @@
|
|||||||
<PublisherName>Rogers Engineering</PublisherName>
|
<PublisherName>Rogers Engineering</PublisherName>
|
||||||
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
|
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
|
||||||
<WebPage>publish.htm</WebPage>
|
<WebPage>publish.htm</WebPage>
|
||||||
<ApplicationRevision>6</ApplicationRevision>
|
<ApplicationRevision>7</ApplicationRevision>
|
||||||
<ApplicationVersion>1.6.0.%2a</ApplicationVersion>
|
<ApplicationVersion>1.6.0.%2a</ApplicationVersion>
|
||||||
<UseApplicationTrust>false</UseApplicationTrust>
|
<UseApplicationTrust>false</UseApplicationTrust>
|
||||||
<PublishWizardCompleted>true</PublishWizardCompleted>
|
<PublishWizardCompleted>true</PublishWizardCompleted>
|
||||||
@@ -99,6 +99,12 @@
|
|||||||
<Compile Include="Extensions\TimeSpanExtensions.cs" />
|
<Compile Include="Extensions\TimeSpanExtensions.cs" />
|
||||||
<Compile Include="Extensions\UIExtensions.cs" />
|
<Compile Include="Extensions\UIExtensions.cs" />
|
||||||
<Compile Include="Extensions\UnitConversionExtensions.cs" />
|
<Compile Include="Extensions\UnitConversionExtensions.cs" />
|
||||||
|
<Compile Include="Forms\MainForm.cs">
|
||||||
|
<SubType>Form</SubType>
|
||||||
|
</Compile>
|
||||||
|
<Compile Include="Forms\MainForm.Designer.cs">
|
||||||
|
<DependentUpon>MainForm.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="ItemExtractors\AssemblyItemExtractor.cs" />
|
<Compile Include="ItemExtractors\AssemblyItemExtractor.cs" />
|
||||||
<Compile Include="ItemExtractors\BomColumnIndices.cs" />
|
<Compile Include="ItemExtractors\BomColumnIndices.cs" />
|
||||||
<Compile Include="ItemExtractors\BomItemExtractor.cs" />
|
<Compile Include="ItemExtractors\BomItemExtractor.cs" />
|
||||||
@@ -122,12 +128,6 @@
|
|||||||
<Compile Include="ViewFlipDeciders\AskViewFlipDecider.cs" />
|
<Compile Include="ViewFlipDeciders\AskViewFlipDecider.cs" />
|
||||||
<Compile Include="ViewFlipDeciders\AutoViewFlipDecider.cs" />
|
<Compile Include="ViewFlipDeciders\AutoViewFlipDecider.cs" />
|
||||||
<Compile Include="ViewFlipDeciders\IViewFlipDecider.cs" />
|
<Compile Include="ViewFlipDeciders\IViewFlipDecider.cs" />
|
||||||
<Compile Include="Forms\MainForm.cs">
|
|
||||||
<SubType>Form</SubType>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="Forms\MainForm.Designer.cs">
|
|
||||||
<DependentUpon>MainForm.cs</DependentUpon>
|
|
||||||
</Compile>
|
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ViewFlipDeciders\PreferUpViewFlipDecider.cs" />
|
<Compile Include="ViewFlipDeciders\PreferUpViewFlipDecider.cs" />
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
using ExportDXF.ViewFlipDeciders;
|
using ExportDXF.ViewFlipDeciders;
|
||||||
|
using SolidWorks.Interop.sldworks;
|
||||||
|
using SolidWorks.Interop.swconst;
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace ExportDXF.Services
|
namespace ExportDXF.Services
|
||||||
{
|
{
|
||||||
@@ -10,6 +14,9 @@ namespace ExportDXF.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class ExportContext
|
public class ExportContext
|
||||||
{
|
{
|
||||||
|
private const string DRAWING_TEMPLATE_FOLDER = "Templates";
|
||||||
|
private const string DRAWING_TEMPLATE_FILE = "Blank.drwdot";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The document to be exported.
|
/// The document to be exported.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -34,5 +41,90 @@ namespace ExportDXF.Services
|
|||||||
/// Callback for reporting progress and status messages.
|
/// Callback for reporting progress and status messages.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Action<string, Color?> ProgressCallback { get; set; }
|
public Action<string, Color?> ProgressCallback { get; set; }
|
||||||
|
|
||||||
|
public void LogProgress(string message, Color? color = null)
|
||||||
|
{
|
||||||
|
ProgressCallback?.Invoke(message, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SldWorks SolidWorksApp { get; set; }
|
||||||
|
|
||||||
|
public DrawingDoc TemplateDrawing { get; set; }
|
||||||
|
|
||||||
|
private string DrawingTemplatePath
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return Path.Combine(
|
||||||
|
Application.StartupPath,
|
||||||
|
DRAWING_TEMPLATE_FOLDER,
|
||||||
|
DRAWING_TEMPLATE_FILE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public DrawingDoc GetOrCreateTemplateDrawing()
|
||||||
|
{
|
||||||
|
if (TemplateDrawing != null)
|
||||||
|
return TemplateDrawing;
|
||||||
|
|
||||||
|
TemplateDrawing = SolidWorksApp.NewDocument(
|
||||||
|
DrawingTemplatePath,
|
||||||
|
(int)swDwgPaperSizes_e.swDwgPaperDsize,
|
||||||
|
1,
|
||||||
|
1) as DrawingDoc;
|
||||||
|
|
||||||
|
return TemplateDrawing;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CleanupTemplateDrawing()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (TemplateDrawing == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (SolidWorksApp == null)
|
||||||
|
{
|
||||||
|
ProgressCallback?.Invoke("Warning: Cannot cleanup template drawing - SolidWorks app not available", Color.DarkBlue);
|
||||||
|
TemplateDrawing = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var model = TemplateDrawing as ModelDoc2;
|
||||||
|
if (model != null)
|
||||||
|
{
|
||||||
|
var title = model.GetTitle();
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(title))
|
||||||
|
{
|
||||||
|
// Close the document without saving
|
||||||
|
SolidWorksApp.CloseDoc(title);
|
||||||
|
ProgressCallback?.Invoke("Closed template drawing", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the reference regardless of success/failure
|
||||||
|
TemplateDrawing = null;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ProgressCallback?.Invoke($"Failed to close template drawing: {ex.Message}", Color.Red);
|
||||||
|
|
||||||
|
// Still clear the reference to prevent further issues
|
||||||
|
TemplateDrawing = null;
|
||||||
|
|
||||||
|
// Don't throw here as this is cleanup code - log the error but continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CloseDocument(string title)
|
||||||
|
{
|
||||||
|
SolidWorksApp?.CloseDoc(title);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ModelDoc2 CreateDocument(string templatePath, int paperSize, double width, double height)
|
||||||
|
{
|
||||||
|
return SolidWorksApp?.NewDocument(templatePath, paperSize, width, height) as ModelDoc2;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -16,14 +16,13 @@ namespace ExportDXF.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="drawing">The drawing document to export.</param>
|
/// <param name="drawing">The drawing document to export.</param>
|
||||||
/// <param name="saveDirectory">The directory where the PDF file will be saved.</param>
|
/// <param name="saveDirectory">The directory where the PDF file will be saved.</param>
|
||||||
/// <param name="progressCallback">Optional callback for progress updates.</param>
|
/// <param name="context">The export context containing SolidWorks app and callbacks.</param>
|
||||||
void ExportToPdf(DrawingDoc drawing, string saveDirectory, Action<string, Color?> progressCallback);
|
void ExportToPdf(DrawingDoc drawing, string saveDirectory, ExportContext context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class DrawingExporter : IDrawingExporter
|
public class DrawingExporter : IDrawingExporter
|
||||||
{
|
{
|
||||||
public void ExportToPdf(DrawingDoc drawing, string saveDirectory, Action<string, Color?> progressCallback)
|
public void ExportToPdf(DrawingDoc drawing, string saveDirectory, ExportContext context)
|
||||||
{
|
{
|
||||||
if (drawing == null)
|
if (drawing == null)
|
||||||
throw new ArgumentNullException(nameof(drawing));
|
throw new ArgumentNullException(nameof(drawing));
|
||||||
@@ -31,27 +30,40 @@ namespace ExportDXF.Services
|
|||||||
if (string.IsNullOrWhiteSpace(saveDirectory))
|
if (string.IsNullOrWhiteSpace(saveDirectory))
|
||||||
throw new ArgumentException("Save directory cannot be null or empty.", nameof(saveDirectory));
|
throw new ArgumentException("Save directory cannot be null or empty.", nameof(saveDirectory));
|
||||||
|
|
||||||
|
if (context == null)
|
||||||
|
throw new ArgumentNullException(nameof(context));
|
||||||
|
|
||||||
|
if (context.SolidWorksApp == null)
|
||||||
|
throw new ArgumentException("SolidWorksApp cannot be null in context.", nameof(context));
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var pdfFileName = GetPdfFileName(drawing);
|
var pdfFileName = GetPdfFileName(drawing);
|
||||||
var pdfPath = Path.Combine(saveDirectory, pdfFileName);
|
var pdfPath = Path.Combine(saveDirectory, pdfFileName);
|
||||||
|
|
||||||
var model = drawing as ModelDoc2;
|
var model = drawing as ModelDoc2;
|
||||||
var sldWorks = (SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application"));
|
var sldWorks = context.SolidWorksApp;
|
||||||
|
|
||||||
var exportData = sldWorks.GetExportFileData(
|
var exportData = sldWorks.GetExportFileData(
|
||||||
(int)swExportDataFileType_e.swExportPdfData) as ExportPdfData;
|
(int)swExportDataFileType_e.swExportPdfData) as ExportPdfData;
|
||||||
|
|
||||||
|
if (exportData == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("Failed to get PDF export data from SolidWorks.");
|
||||||
|
}
|
||||||
|
|
||||||
exportData.ViewPdfAfterSaving = false;
|
exportData.ViewPdfAfterSaving = false;
|
||||||
exportData.SetSheets(
|
exportData.SetSheets(
|
||||||
(int)swExportDataSheetsToExport_e.swExportData_ExportAllSheets,
|
(int)swExportDataSheetsToExport_e.swExportData_ExportAllSheets,
|
||||||
drawing);
|
drawing);
|
||||||
|
|
||||||
|
context.ProgressCallback?.Invoke($"Exporting drawing to PDF: \"{pdfFileName}\"", null);
|
||||||
|
|
||||||
int errors = 0;
|
int errors = 0;
|
||||||
int warnings = 0;
|
int warnings = 0;
|
||||||
|
|
||||||
var modelExtension = model.Extension;
|
var modelExtension = model.Extension;
|
||||||
modelExtension.SaveAs(
|
var success = modelExtension.SaveAs(
|
||||||
pdfPath,
|
pdfPath,
|
||||||
(int)swSaveAsVersion_e.swSaveAsCurrentVersion,
|
(int)swSaveAsVersion_e.swSaveAsCurrentVersion,
|
||||||
(int)swSaveAsOptions_e.swSaveAsOptions_Silent,
|
(int)swSaveAsOptions_e.swSaveAsOptions_Silent,
|
||||||
@@ -59,19 +71,29 @@ namespace ExportDXF.Services
|
|||||||
ref errors,
|
ref errors,
|
||||||
ref warnings);
|
ref warnings);
|
||||||
|
|
||||||
if (errors == 0)
|
if (success && errors == 0)
|
||||||
{
|
{
|
||||||
progressCallback?.Invoke($"Saved drawing to PDF: \"{pdfFileName}\"", Color.Green);
|
context.ProgressCallback?.Invoke($"Saved drawing to PDF: \"{pdfFileName}\"", Color.Green);
|
||||||
|
}
|
||||||
|
else if (success && warnings > 0)
|
||||||
|
{
|
||||||
|
context.ProgressCallback?.Invoke(
|
||||||
|
$"PDF export completed with warnings: {warnings}",
|
||||||
|
Color.DarkBlue);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
progressCallback?.Invoke($"PDF export completed with errors: {errors}", Color.Yellow);
|
context.ProgressCallback?.Invoke(
|
||||||
|
$"PDF export failed. Errors: {errors}, Warnings: {warnings}",
|
||||||
|
Color.Red);
|
||||||
|
throw new InvalidOperationException($"PDF export failed with {errors} errors and {warnings} warnings.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
progressCallback?.Invoke($"Failed to export PDF: {ex.Message}", Color.Red);
|
var errorMessage = $"Failed to export PDF: {ex.Message}";
|
||||||
throw;
|
context.ProgressCallback?.Invoke(errorMessage, Color.Red);
|
||||||
|
throw new InvalidOperationException(errorMessage, ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +101,14 @@ namespace ExportDXF.Services
|
|||||||
{
|
{
|
||||||
var model = drawing as ModelDoc2;
|
var model = drawing as ModelDoc2;
|
||||||
var modelFilePath = model.GetPathName();
|
var modelFilePath = model.GetPathName();
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(modelFilePath))
|
||||||
|
{
|
||||||
|
// Handle unsaved documents
|
||||||
|
var title = model.GetTitle();
|
||||||
|
return string.IsNullOrEmpty(title) ? "Untitled.pdf" : Path.GetFileNameWithoutExtension(title) + ".pdf";
|
||||||
|
}
|
||||||
|
|
||||||
return Path.GetFileNameWithoutExtension(modelFilePath) + ".pdf";
|
return Path.GetFileNameWithoutExtension(modelFilePath) + ".pdf";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,9 +25,6 @@ namespace ExportDXF.Services
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class DxfExportService : IDxfExportService
|
public class DxfExportService : IDxfExportService
|
||||||
{
|
{
|
||||||
private const string DRAWING_TEMPLATE_FOLDER = "Templates";
|
|
||||||
private const string DRAWING_TEMPLATE_FILE = "Blank.drwdot";
|
|
||||||
|
|
||||||
private readonly ISolidWorksService _solidWorksService;
|
private readonly ISolidWorksService _solidWorksService;
|
||||||
private readonly IBomExtractor _bomExtractor;
|
private readonly IBomExtractor _bomExtractor;
|
||||||
private readonly IPartExporter _partExporter;
|
private readonly IPartExporter _partExporter;
|
||||||
@@ -57,6 +54,7 @@ namespace ExportDXF.Services
|
|||||||
throw new ArgumentNullException(nameof(context));
|
throw new ArgumentNullException(nameof(context));
|
||||||
|
|
||||||
ValidateContext(context);
|
ValidateContext(context);
|
||||||
|
SetupExportContext(context);
|
||||||
|
|
||||||
var startTime = DateTime.Now;
|
var startTime = DateTime.Now;
|
||||||
|
|
||||||
@@ -85,6 +83,7 @@ namespace ExportDXF.Services
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
CleanupExportContext(context);
|
||||||
_solidWorksService.EnableUserControl(true);
|
_solidWorksService.EnableUserControl(true);
|
||||||
|
|
||||||
var duration = DateTime.Now - startTime;
|
var duration = DateTime.Now - startTime;
|
||||||
@@ -131,7 +130,7 @@ namespace ExportDXF.Services
|
|||||||
|
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
{
|
{
|
||||||
LogProgress(context, "No items found in assembly.", Color.Yellow);
|
LogProgress(context, "No items found in assembly.", Color.DarkBlue);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +176,7 @@ namespace ExportDXF.Services
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Export drawing to PDF first
|
// Export drawing to PDF first
|
||||||
_drawingExporter.ExportToPdf(drawing, saveDirectory, context.ProgressCallback);
|
_drawingExporter.ExportToPdf(drawing, saveDirectory, context);
|
||||||
|
|
||||||
// Then export parts to DXF
|
// Then export parts to DXF
|
||||||
ExportItems(items, saveDirectory, context);
|
ExportItems(items, saveDirectory, context);
|
||||||
@@ -185,6 +184,40 @@ namespace ExportDXF.Services
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Context Management
|
||||||
|
|
||||||
|
private void SetupExportContext(ExportContext context)
|
||||||
|
{
|
||||||
|
// Set up SolidWorks application reference
|
||||||
|
context.SolidWorksApp = _solidWorksService.GetNativeSldWorks();
|
||||||
|
|
||||||
|
if (context.SolidWorksApp == null)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException("SolidWorks service is not connected.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up drawing template path
|
||||||
|
context.TemplateDrawing = null;
|
||||||
|
|
||||||
|
LogProgress(context, "Export context initialized", null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CleanupExportContext(ExportContext context)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Clean up template drawing if it was created
|
||||||
|
context.CleanupTemplateDrawing();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogProgress(context, $"Warning: Failed to cleanup template drawing: {ex.Message}", Color.DarkBlue);
|
||||||
|
// Don't throw - this is cleanup code
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region Item Processing
|
#region Item Processing
|
||||||
|
|
||||||
private List<Item> ExtractItemsFromAssembly(AssemblyDoc assembly, ExportContext context)
|
private List<Item> ExtractItemsFromAssembly(AssemblyDoc assembly, ExportContext context)
|
||||||
@@ -207,117 +240,43 @@ namespace ExportDXF.Services
|
|||||||
|
|
||||||
private void ExportItems(List<Item> items, string saveDirectory, ExportContext context)
|
private void ExportItems(List<Item> items, string saveDirectory, ExportContext context)
|
||||||
{
|
{
|
||||||
DrawingDoc templateDrawing = null;
|
LogProgress(context, "", null);
|
||||||
|
|
||||||
try
|
int successCount = 0;
|
||||||
|
int failureCount = 0;
|
||||||
|
|
||||||
|
foreach (var item in items)
|
||||||
{
|
{
|
||||||
templateDrawing = CreateTemplateDrawing();
|
if (context.CancellationToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
LogProgress(context, "Export canceled by user.", Color.DarkBlue);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// PartExporter will handle template drawing creation through context
|
||||||
|
_partExporter.ExportItem(item, saveDirectory, context);
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(item.FileName))
|
||||||
|
successCount++;
|
||||||
|
else
|
||||||
|
failureCount++;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
LogProgress(context, $"Error exporting item {item.ItemNo}: {ex.Message}", Color.Red);
|
||||||
|
failureCount++;
|
||||||
|
}
|
||||||
|
|
||||||
LogProgress(context, "", null);
|
LogProgress(context, "", null);
|
||||||
|
|
||||||
int successCount = 0;
|
|
||||||
int failureCount = 0;
|
|
||||||
|
|
||||||
foreach (var item in items)
|
|
||||||
{
|
|
||||||
if (context.CancellationToken.IsCancellationRequested)
|
|
||||||
{
|
|
||||||
LogProgress(context, "Export canceled by user.", Color.Yellow);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
_partExporter.ExportItem(item, templateDrawing, saveDirectory, context);
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(item.FileName))
|
|
||||||
successCount++;
|
|
||||||
else
|
|
||||||
failureCount++;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
LogProgress(context, $"Error exporting item {item.ItemNo}: {ex.Message}", Color.Red);
|
|
||||||
failureCount++;
|
|
||||||
}
|
|
||||||
|
|
||||||
LogProgress(context, "", null);
|
|
||||||
}
|
|
||||||
|
|
||||||
LogProgress(context, $"Export complete: {successCount} succeeded, {failureCount} failed",
|
|
||||||
failureCount > 0 ? Color.Yellow : Color.Green);
|
|
||||||
|
|
||||||
// Create BOM Excel file
|
|
||||||
CreateBomExcelFile(items, saveDirectory, context);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
CloseTemplateDrawing(templateDrawing);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Template Drawing Management
|
|
||||||
|
|
||||||
private DrawingDoc CreateTemplateDrawing()
|
|
||||||
{
|
|
||||||
var sldWorks = _solidWorksService.GetNativeSldWorks();
|
|
||||||
|
|
||||||
if (sldWorks == null)
|
|
||||||
throw new InvalidOperationException("SolidWorks service is not connected.");
|
|
||||||
|
|
||||||
var templatePath = GetDrawingTemplatePath();
|
|
||||||
|
|
||||||
if (!File.Exists(templatePath))
|
|
||||||
{
|
|
||||||
throw new FileNotFoundException(
|
|
||||||
$"Drawing template not found at: {templatePath}",
|
|
||||||
templatePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var drawing = sldWorks.NewDocument(
|
LogProgress(context, $"Export complete: {successCount} succeeded, {failureCount} failed",
|
||||||
templatePath,
|
failureCount > 0 ? Color.DarkBlue : Color.Green);
|
||||||
(int)swDwgPaperSizes_e.swDwgPaperDsize,
|
|
||||||
1,
|
|
||||||
1) as DrawingDoc;
|
|
||||||
|
|
||||||
if (drawing == null)
|
// Create BOM Excel file
|
||||||
{
|
CreateBomExcelFile(items, saveDirectory, context);
|
||||||
throw new InvalidOperationException(
|
|
||||||
"Failed to create drawing from template. Please check the template file.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return drawing;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CloseTemplateDrawing(DrawingDoc drawing)
|
|
||||||
{
|
|
||||||
if (drawing == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var model = drawing as ModelDoc2;
|
|
||||||
var title = model?.GetTitle();
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(title))
|
|
||||||
{
|
|
||||||
_solidWorksService.CloseDocument(title);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Ignore errors during cleanup
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetDrawingTemplatePath()
|
|
||||||
{
|
|
||||||
return Path.Combine(
|
|
||||||
Application.StartupPath,
|
|
||||||
DRAWING_TEMPLATE_FOLDER,
|
|
||||||
DRAWING_TEMPLATE_FILE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -408,6 +367,8 @@ namespace ExportDXF.Services
|
|||||||
context.ProgressCallback?.Invoke(message, color);
|
context.ProgressCallback?.Invoke(message, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,10 +25,9 @@ namespace ExportDXF.Services
|
|||||||
/// Exports an item (component from BOM or assembly) to DXF.
|
/// Exports an item (component from BOM or assembly) to DXF.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="item">The item to export.</param>
|
/// <param name="item">The item to export.</param>
|
||||||
/// <param name="templateDrawing">The template drawing to use for creating flat patterns.</param>
|
|
||||||
/// <param name="saveDirectory">The directory where the DXF file will be saved.</param>
|
/// <param name="saveDirectory">The directory where the DXF file will be saved.</param>
|
||||||
/// <param name="context">The export context.</param>
|
/// <param name="context">The export context.</param>
|
||||||
void ExportItem(Item item, DrawingDoc templateDrawing, string saveDirectory, ExportContext context);
|
void ExportItem(Item item, string saveDirectory, ExportContext context);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class PartExporter : IPartExporter
|
public class PartExporter : IPartExporter
|
||||||
@@ -38,6 +37,12 @@ namespace ExportDXF.Services
|
|||||||
if (part == null)
|
if (part == null)
|
||||||
throw new ArgumentNullException(nameof(part));
|
throw new ArgumentNullException(nameof(part));
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(saveDirectory))
|
||||||
|
throw new ArgumentException("Save directory cannot be null or empty.", nameof(saveDirectory));
|
||||||
|
|
||||||
|
if (context == null)
|
||||||
|
throw new ArgumentNullException(nameof(context));
|
||||||
|
|
||||||
var model = part as ModelDoc2;
|
var model = part as ModelDoc2;
|
||||||
var activeConfig = model.GetActiveConfiguration() as SolidWorks.Interop.sldworks.Configuration;
|
var activeConfig = model.GetActiveConfiguration() as SolidWorks.Interop.sldworks.Configuration;
|
||||||
var originalConfigName = activeConfig?.Name;
|
var originalConfigName = activeConfig?.Name;
|
||||||
@@ -47,16 +52,9 @@ namespace ExportDXF.Services
|
|||||||
var fileName = GetSinglePartFileName(model, context.FilePrefix);
|
var fileName = GetSinglePartFileName(model, context.FilePrefix);
|
||||||
var savePath = Path.Combine(saveDirectory, fileName + ".dxf");
|
var savePath = Path.Combine(saveDirectory, fileName + ".dxf");
|
||||||
|
|
||||||
var templateDrawing = CreateTemplateDrawing(context);
|
context.GetOrCreateTemplateDrawing();
|
||||||
|
|
||||||
try
|
ExportPartToDxf(part, originalConfigName, savePath, context);
|
||||||
{
|
|
||||||
ExportPartToDxf(part, originalConfigName, templateDrawing, savePath, context);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
CloseTemplateDrawing(templateDrawing, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@@ -67,8 +65,14 @@ namespace ExportDXF.Services
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ExportItem(Item item, DrawingDoc templateDrawing, string saveDirectory, ExportContext context)
|
public void ExportItem(Item item, string saveDirectory, ExportContext context)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(saveDirectory))
|
||||||
|
throw new ArgumentException("Save directory cannot be null or empty.", nameof(saveDirectory));
|
||||||
|
|
||||||
|
if (context == null)
|
||||||
|
throw new ArgumentNullException(nameof(context));
|
||||||
|
|
||||||
if (item?.Component == null)
|
if (item?.Component == null)
|
||||||
{
|
{
|
||||||
context.ProgressCallback?.Invoke($"Item {item?.ItemNo} - skipped, no component", Color.Yellow);
|
context.ProgressCallback?.Invoke($"Item {item?.ItemNo} - skipped, no component", Color.Yellow);
|
||||||
@@ -93,7 +97,9 @@ namespace ExportDXF.Services
|
|||||||
var fileName = GetItemFileName(item, context.FilePrefix);
|
var fileName = GetItemFileName(item, context.FilePrefix);
|
||||||
var savePath = Path.Combine(saveDirectory, fileName + ".dxf");
|
var savePath = Path.Combine(saveDirectory, fileName + ".dxf");
|
||||||
|
|
||||||
if (ExportPartToDxf(part, item.Component.ReferencedConfiguration, templateDrawing, savePath, context))
|
var templateDrawing = context.GetOrCreateTemplateDrawing();
|
||||||
|
|
||||||
|
if (ExportPartToDxf(part, item.Component.ReferencedConfiguration, savePath, context))
|
||||||
{
|
{
|
||||||
item.FileName = Path.GetFileNameWithoutExtension(savePath);
|
item.FileName = Path.GetFileNameWithoutExtension(savePath);
|
||||||
}
|
}
|
||||||
@@ -116,8 +122,18 @@ namespace ExportDXF.Services
|
|||||||
|
|
||||||
// Get description from custom properties
|
// Get description from custom properties
|
||||||
var config = item.Component.ReferencedConfiguration;
|
var config = item.Component.ReferencedConfiguration;
|
||||||
item.Description = model.Extension.CustomPropertyManager[config].Get("Description");
|
|
||||||
item.Description = model.Extension.CustomPropertyManager[""].Get("Description");
|
// Try configuration-specific properties first
|
||||||
|
var configPropertyManager = model.Extension.CustomPropertyManager[config];
|
||||||
|
item.Description = configPropertyManager?.Get("Description");
|
||||||
|
|
||||||
|
// Fall back to document-level properties if no config-specific description
|
||||||
|
if (string.IsNullOrEmpty(item.Description))
|
||||||
|
{
|
||||||
|
var docPropertyManager = model.Extension.CustomPropertyManager[""];
|
||||||
|
item.Description = docPropertyManager?.Get("Description");
|
||||||
|
}
|
||||||
|
|
||||||
item.Description = TextHelper.RemoveXmlTags(item.Description);
|
item.Description = TextHelper.RemoveXmlTags(item.Description);
|
||||||
|
|
||||||
// Get material
|
// Get material
|
||||||
@@ -127,7 +143,6 @@ namespace ExportDXF.Services
|
|||||||
private bool ExportPartToDxf(
|
private bool ExportPartToDxf(
|
||||||
PartDoc part,
|
PartDoc part,
|
||||||
string configName,
|
string configName,
|
||||||
DrawingDoc templateDrawing,
|
|
||||||
string savePath,
|
string savePath,
|
||||||
ExportContext context)
|
ExportContext context)
|
||||||
{
|
{
|
||||||
@@ -141,6 +156,8 @@ namespace ExportDXF.Services
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var templateDrawing = context.GetOrCreateTemplateDrawing();
|
||||||
|
|
||||||
SolidWorksHelper.ConfigureFlatPatternSettings(model);
|
SolidWorksHelper.ConfigureFlatPatternSettings(model);
|
||||||
|
|
||||||
var sheet = templateDrawing.IGetCurrentSheet();
|
var sheet = templateDrawing.IGetCurrentSheet();
|
||||||
@@ -286,15 +303,5 @@ namespace ExportDXF.Services
|
|||||||
Color.Red);
|
Color.Red);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DrawingDoc CreateTemplateDrawing(ExportContext context)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException("Template drawing creation not implemented.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CloseTemplateDrawing(DrawingDoc drawing, ExportContext context)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException("Template drawing creation not implemented.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user