c#获取UG NX 文件类型是零件、装配体还是工程图
时间: 2023-12-30 11:02:13 浏览: 188
UG NX二次开发(C#)-工程图框选对象并输出框选对象的信息
5星 · 资源好评率100%
要获取UG NX文件的类型,可以使用以下代码示例:
```csharp
using System;
using NXOpen;
class Program
{
static void Main(string[] args)
{
string filePath = "C:\\example.prt"; //UG NX文件路径
PartLoadStatus status;
BasePart basePart = null;
try
{
basePart = Session.GetSession().Parts.OpenBaseDisplay(filePath, out status);
if (status.NumberOfFailures > 0)
{
throw new Exception("Failed to open the file: " + filePath);
}
if (basePart is Part)
{
Part part = (Part)basePart;
if (part.ComponentAssembly.RootComponent == null)
{
Console.WriteLine("The file is a part.");
}
else
{
Console.WriteLine("The file is an assembly.");
}
}
else if (basePart is DrawingSheet)
{
Console.WriteLine("The file is a drawing.");
}
else
{
Console.WriteLine("The file type is unknown.");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (basePart != null)
{
basePart.Close(BasePart.CloseWholeTree.False, BasePart.CloseModified.CloseModified, null);
}
}
Console.ReadLine();
}
}
```
以上代码会打开UG NX文件并判断其类型,输出结果为:
- 如果文件是零件,输出 "The file is a part."
- 如果文件是装配体,输出 "The file is an assembly."
- 如果文件是工程图,输出 "The file is a drawing."
- 如果文件类型未知,输出 "The file type is unknown."
阅读全文