NXOpen入口
class Program
{
static void Main(string[] args)
{
try
{
//Console.ReadKey(true);
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.Clear();
Console.Title = "导出CAD图纸";
string filePath = "";
foreach (string arg in args)
{
filePath = filePath + arg + " ";
}
filePath = filePath.Trim();
CombineSheetManager manager = string.IsNullOrWhiteSpace(filePath)
? new CombineSheetManager()
: new CombineSheetManager(filePath);
manager.Commit();
}
catch (System.Exception ex)
{
if (!string.IsNullOrWhiteSpace(ex.Source))
{
Console.WriteLine(ex.Source);
}
Console.WriteLine(ex);
}
Console.ReadKey();
}
}
主要类:导出 合并
public class CombineSheetManager : BaseCombineManager, IEnumerable<CombineSheet>
{
private readonly List<CombineSheet> mList;
public CombineSheetManager(string filePath)
{
var lines = ReadAllLines(filePath).ToList();
if (lines.Count < 3)
{
throw new System.Exception($"工作包文件格式错误。\r\n{filePath}");
}
this.BaseDirectory = lines[0];
this.InputPart = lines[1];
this.OutputFile = lines[2];
this.mList = lines.Skip(3).Select(p => new CombineSheet(this, p)).ToList();
if (this.mList.Count == 0)
{
throw new System.Exception($"工作包文件没有指定导出图纸。\r\n{filePath}");
}
//赋序号
int i = 0;
foreach (var combineSheet in this.mList)
{
combineSheet.Id = ++i;
}
//查找Root目录
var rootDir = this.BaseDirectory.GetSubFolder("NXBIN");
if (rootDir == null || !rootDir.Exists)
{
rootDir = this.BaseDirectory.GetSubFolder("UGII");
if (rootDir == null || !rootDir.Exists)
{
throw new System.Exception($"当前{this.BaseDirectory}程序安装不完整。");
}
}
this.RootDirectory = rootDir;
//配置表
var ugTo2dDef = this.CurrentDirectory.GetFile("ugto2d.def");
this.SettingTable = new SettingTable(ugTo2dDef);
}
public CombineSheetManager():this(OpenPackage())
{
}
private static string OpenPackage()
{
Console.WriteLine("请输入工作包文件路径:");
return Console.ReadLine();
//var openFile = new System.Windows.Forms.OpenFileDialog();
//openFile.Multiselect = false;
//openFile.Filter = "Package Files(*.package)|*.package|All Files(*.*)|*.*";
//openFile.CheckPathExists = true;
//var result = openFile.ShowDialog();
//string fileName = "";
//if (result == System.Windows.Forms.DialogResult.OK)
//{
// fileName = openFile.FileName;
//}
//openFile.Dispose();
//return fileName;
}
public FolderPathInfo BaseDirectory { get; }
public FilePathInfo InputPart { get; set; }
public FilePathInfo OutputFile { get; set; }
public FolderPathInfo RootDirectory { get; }
public SettingTable SettingTable { get; }
private IEnumerable<string> ReadAllLines(string filePath)
{
bool isDeleteFile = true;
if (strin

该代码示例展示了如何使用NXOpen接口处理工作包文件,实现CAD图纸的自动导出和合并。程序首先读取输入文件,配置转换参数,然后调用UGTO2D和DXFDWG工具将3D模型转换为2DDWG格式,并最终使用AutoCAD进行合并。在过程中,程序会检查和处理可能出现的错误,并清理临时文件。
最低0.47元/天 解锁文章
1783

被折叠的 条评论
为什么被折叠?



