Hello, I created a program for solid edge 2022 that makes scans of sheet parts in the assembly, saves these scans in .psm format and .dxf format, in .psm format everything opens and the scan is shown, and in .dxf format it is just a black square without the geometry of the scan. If you manually scan and save manually in .dxf format, then everything is shown. Please tell me what the problem might be. I am attaching the program code.
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using SolidEdgeCommunity;
using SolidEdgeFramework;
using SolidEdgePart;
using SolidEdgeAssembly;
using SolidEdgeGeometry;
namespace DXFExporter
{
public partial class MainForm : Form
{
private SolidEdgeFramework.Application _seApplication;
private AssemblyDocument _seAssemblyDoc;
private ListBox listBoxDetails;
private TextBox txtSelectedAssembly;
private TextBox txtExportFolder;
private string exportFolderPath;
private HashSet<string> processedSheetMetals;
public MainForm()
{
InitializeComponent();
InitializeCustomComponents();
processedSheetMetals = new HashSet<string>();
exportFolderPath = @"C:DXFExport";
txtExportFolder.Text = exportFolderPath;
}
private void InitializeCustomComponents()
{
this.BackColor = Color.Honeydew;
this.Text = "Сохранение развёртки листовых деталей сборки";
this.ShowIcon = false;
var lblSelectedAssembly = new Label
{
Text = "Выбранная сборка:",
Location = new Point(20, 20),
AutoSize = true,
ForeColor = Color.DarkGreen
};
this.Controls.Add(lblSelectedAssembly);
txtSelectedAssembly = new TextBox
{
Location = new Point(150, 20),
Width = 400,
ReadOnly = true,
BackColor = Color.PaleGreen,
ForeColor = Color.DarkGreen,
BorderStyle = BorderStyle.None
};
this.Controls.Add(txtSelectedAssembly);
var btnSelectAssembly = new Button
{
Text = "Выбрать сборку",
Location = new Point(570, 15),
Width = 150,
BackColor = ColorTranslator.FromHtml("#0E5C42"),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat,
};
btnSelectAssembly.FlatAppearance.BorderSize = 0;
btnSelectAssembly.Click += BtnSelectAssembly_Click;
this.Controls.Add(btnSelectAssembly);
var lblExportFolder = new Label
{
Text = "Папка для экспорта:",
Location = new Point(20, 60),
AutoSize = true,
ForeColor = Color.DarkGreen
};
this.Controls.Add(lblExportFolder);
txtExportFolder = new TextBox
{
Location = new Point(150, 60),
Width = 400,
ReadOnly = true,
BackColor = Color.PaleGreen,
ForeColor = Color.DarkGreen,
BorderStyle = BorderStyle.None
};
this.Controls.Add(txtExportFolder);
var btnSelectFolder = new Button
{
Text = "Выбрать папку",
Location = new Point(570, 55),
Width = 150,
BackColor = ColorTranslator.FromHtml("#0E5C42"),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat
};
btnSelectFolder.FlatAppearance.BorderSize = 0;
btnSelectFolder.Click += BtnSelectFolder_Click;
this.Controls.Add(btnSelectFolder);
listBoxDetails = new ListBox
{
Location = new Point(20, 100),
Width = 700,
Height = 200,
BackColor = Color.MintCream,
ForeColor = Color.DarkGreen,
};
this.Controls.Add(listBoxDetails);
var btnProcess = new Button
{
Text = "Запустить",
Location = new Point(20, 320),
Width = 700,
Height = 40,
BackColor = ColorTranslator.FromHtml("#0E5C42"),
ForeColor = Color.White,
FlatStyle = FlatStyle.Flat
};
btnProcess.FlatAppearance.BorderSize = 0;
btnProcess.Click += BtnProcess_Click;
this.Controls.Add(btnProcess);
}
private void BtnSelectAssembly_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Solid Edge Assembly (*.asm)|*.asm"
};
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtSelectedAssembly.Text = openFileDialog.FileName;
OpenAssembly(openFileDialog.FileName);
}
}
private void BtnSelectFolder_Click(object sender, EventArgs e)
{
using (var folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == DialogResult.OK)
{
exportFolderPath = folderDialog.SelectedPath;
txtExportFolder.Text = exportFolderPath;
}
}
}
private void OpenAssembly(string filePath)
{
try
{
OleMessageFilter.Register();
_seApplication = SolidEdgeCommunity.SolidEdgeUtils.Connect(true);
_seApplication.Documents.Open(filePath);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при открытии сборки: {ex.Message}");
}
finally
{
OleMessageFilter.Unregister();
}
}
private void BtnProcess_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(exportFolderPath))
{
MessageBox.Show("Выберите папку для экспорта.");
return;
}
try
{
OleMessageFilter.Register();
if (_seApplication.ActiveDocumentType != SolidEdgeFramework.DocumentTypeConstants.igAssemblyDocument)
{
MessageBox.Show("Активный документ не является сборкой.");
return;
}
_seAssemblyDoc = (AssemblyDocument)_seApplication.ActiveDocument;
var occurrences = _seAssemblyDoc.Occurrences;
// Создание папки с именем сборки
string assemblyFolderName = System.IO.Path.GetFileNameWithoutExtension(_seAssemblyDoc.FullName);
string assemblyExportPath = System.IO.Path.Combine(exportFolderPath, assemblyFolderName);
if (!Directory.Exists(assemblyExportPath))
{
Directory.CreateDirectory(assemblyExportPath);
}
foreach (Occurrence occurrence in occurrences.OfType<Occurrence>())
{
ProcessOccurrence(occurrence, assemblyExportPath);
}
MessageBox.Show("Процесс завершён.");
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка: {ex.Message}");
}
finally
{
OleMessageFilter.Unregister();
}
}
private void ProcessOccurrence(Occurrence occurrence, string assemblyExportPath)
{
if (occurrence.OccurrenceDocument is SheetMetalDocument sheetMetalDoc)
{
string fullname = sheetMetalDoc.FullName;
if (!processedSheetMetals.Contains(sheetMetalDoc.FullName))
{
OpenAndProcessSheetMetal(occurrence.OccurrenceFileName, assemblyExportPath);
processedSheetMetals.Add(fullname);
}
}
else if (occurrence.OccurrenceDocument is AssemblyDocument subAssemblyDoc)
{
foreach (Occurrence subOccurrence in subAssemblyDoc.Occurrences.OfType<Occurrence>())
{
ProcessOccurrence(subOccurrence, assemblyExportPath);
}
}
}
private void OpenAndProcessSheetMetal(string filePath, string assemblyExportPath)
{
try
{
_seApplication.Documents.Open(filePath, DocRelationAutoServer: 8);
var sheetMetalDoc = (SheetMetalDocument)_seApplication.ActiveDocument;
SaveSheetMetalAsDXF(sheetMetalDoc, assemblyExportPath);
sheetMetalDoc.Close(false);
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при обработке листовой детали: {ex.Message}");
}
}
private void SaveSheetMetalAsDXF(SheetMetalDocument sheetMetalDoc, string assemblyExportPath)
{
try
{
// Check if a flat pattern model exists
FlatPatternModel flatPatternModel;
if (sheetMetalDoc.FlatPatternModels.Count == 0)
{
flatPatternModel = sheetMetalDoc.FlatPatternModels.Add(sheetMetalDoc.Models.Item(1));
}
else
{
flatPatternModel = (FlatPatternModel)sheetMetalDoc.FlatPatternModels.Item(1);
}
SolidEdgeGeometry.Body body = (SolidEdgeGeometry.Body)flatPatternModel.Body;
SolidEdgeGeometry.Face face = null;
SolidEdgeGeometry.Edge edge = null;
SolidEdgeGeometry.Vertex vertex = null;
var queryTypes = new FeatureTopologyQueryTypeConstants[]
{
FeatureTopologyQueryTypeConstants.igQueryPlane,
FeatureTopologyQueryTypeConstants.igQueryCylinder,
FeatureTopologyQueryTypeConstants.igQueryCone,
FeatureTopologyQueryTypeConstants.igQueryAll
};
foreach (var queryType in queryTypes)
{
var faces = (SolidEdgeGeometry.Faces)body.Faces[queryType];
if (faces.Count > 0)
{
face = (SolidEdgeGeometry.Face)faces.Item(1);
var edges = (SolidEdgeGeometry.Edges)face.Edges;
if (edges.Count > 0)
{
edge = (SolidEdgeGeometry.Edge)edges.Item(1);
vertex = (SolidEdgeGeometry.Vertex)edge.StartVertex;
break;
}
}
}
if (face == null || edge == null || vertex == null)
{
listBoxDetails.Items.Add($"{sheetMetalDoc.Name}: Не удалось найти подходящие грани, ребра или вершины для создания развёртки.");
return;
}
SolidEdgePart.FlatPatterns flatPatterns = flatPatternModel.FlatPatterns;
flatPatterns.Add(
ReferenceEdge: edge,
ReferenceFace: face,
ReferenceVertex: vertex,
ModelType: SolidEdgePart.FlattenPatternModelTypeConstants.igFlattenPatternModelTypeFlattenAnything
);
string file_name_without_ext = System.IO.Path.GetFileNameWithoutExtension(sheetMetalDoc.Name);
string FileNameDXF = System.IO.Path.ChangeExtension(file_name_without_ext, ".dxf");
string dxfPath = System.IO.Path.Combine(assemblyExportPath, $"{file_name_without_ext}.psm");
sheetMetalDoc.Models.SaveAsFlatDXF(dxfPath, face, edge, vertex);
dxfPath = System.IO.Path.Combine(assemblyExportPath, $"{FileNameDXF}");
sheetMetalDoc.Models.SaveAsFlatDXFEx(dxfPath, face, edge, vertex, true);
listBoxDetails.Items.Add($"{sheetMetalDoc.Name} сохранён как {dxfPath} Успешно");
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка при сохранении DXF: {ex.Message}");
}
}
}
}
I don’t understand where there might be a mistake
New contributor
darety is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.