赞
踩
有时候,我们需要处理给定装配体中的所有零件。本教程中以递归方式逐步完成装配体结构,允许处理每个零件。如果代码按照提供的方式运行,它将打开一个信息窗口,并打印每个零部件的名称和当前活动排列(如果是子装配体)。代码可在操作记录中直接运行(NX9.0测试)。
- using System;
- using NXOpen;
- using NXOpen.UF;
- using NXOpen.Assemblies;
-
- sealed class NXJournal
- {
- public static Session theSession = Session.GetSession();
- public static UFSession ufs = UFSession.GetUFSession();
- public static ListingWindow lw = theSession.ListingWindow;
-
- public static void Main()
- {
- Part workPart = theSession.Parts.Work;
- Part dispPart = theSession.Parts.Display;
- lw.Open();
- try
- {
- ComponentAssembly c = dispPart.ComponentAssembly;//返回装配体的组件集
- //若处理工作部分而非显示部分,注释上一行,取消注释下一行
- //ComponentAssembly c = workPart.ComponentAssembly
-
- if (c.RootComponent != null )
- {
- //插入代码以处理“根组件”(装配文件) (assembly file)
- lw.WriteLine("Assembly: " + c.RootComponent.DisplayName);
- lw.WriteLine(c.RootComponent.GetChildren().Length + " components");
- lw.WriteLine(" + Active Arrangement: " + c.ActiveArrangement.Name);
-
- reportComponentChildren(c.RootComponent, 0);
- }
- else
- {
- lw.WriteLine("Part has no components");
- }
- }
- catch (Exception e)
- {
- theSession.ListingWindow.WriteLine("Failed: " + e.ToString());
- }
- lw.Close();
-
- }
- public static void reportComponentChildren(Component comp, int indent)
- {
- foreach (Component child in comp.GetChildren())
- {
- //插入代码以处理零部件或子部件
- lw.WriteLine("");
- lw.WriteLine(new string(' ', indent * 2) + child.DisplayName);
- lw.WriteLine("child.Tag: " + child.Tag.ToString());
-
- if (child.GetChildren().Length != 0)
-
- {
- //这是一个子部件,请添加特定于部件的代码
- lw.WriteLine(new string(' ', indent * 2) + child.GetChildren().Length + " components");
- lw.WriteLine(new string(' ', indent * 2) + " + Active Arrangement: " + child.OwningPart.ComponentAssembly.ActiveArrangement.Name);
- }
- else
- {
-
- }
- reportComponentChildren(child, indent + 1);
- }
- }
- public static int GetUnloadOption(string arg)
- {
- return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。