当前位置:   article > 正文

UG NX二次开发 创建子进程以处理装配体中的所有零部件 C#_ugopen遍历装配体

ugopen遍历装配体

1、简介

有时候,我们需要处理给定装配体中的所有零件。本教程中以递归方式逐步完成装配体结构,允许处理每个零件。如果代码按照提供的方式运行,它将打开一个信息窗口,并打印每个零部件的名称和当前活动排列(如果是子装配体)。代码可在操作记录中直接运行(NX9.0测试)。

2、代码

  1. using System;
  2. using NXOpen;
  3. using NXOpen.UF;
  4. using NXOpen.Assemblies;
  5. sealed class NXJournal
  6. {
  7. public static Session theSession = Session.GetSession();
  8. public static UFSession ufs = UFSession.GetUFSession();
  9. public static ListingWindow lw = theSession.ListingWindow;
  10. public static void Main()
  11. {
  12. Part workPart = theSession.Parts.Work;
  13. Part dispPart = theSession.Parts.Display;
  14. lw.Open();
  15. try
  16. {
  17. ComponentAssembly c = dispPart.ComponentAssembly;//返回装配体的组件集
  18. //若处理工作部分而非显示部分,注释上一行,取消注释下一行
  19. //ComponentAssembly c = workPart.ComponentAssembly
  20. if (c.RootComponent != null )
  21. {
  22. //插入代码以处理“根组件”(装配文件) (assembly file)
  23. lw.WriteLine("Assembly: " + c.RootComponent.DisplayName);
  24. lw.WriteLine(c.RootComponent.GetChildren().Length + " components");
  25. lw.WriteLine(" + Active Arrangement: " + c.ActiveArrangement.Name);
  26. reportComponentChildren(c.RootComponent, 0);
  27. }
  28. else
  29. {
  30. lw.WriteLine("Part has no components");
  31. }
  32. }
  33. catch (Exception e)
  34. {
  35. theSession.ListingWindow.WriteLine("Failed: " + e.ToString());
  36. }
  37. lw.Close();
  38. }
  39. public static void reportComponentChildren(Component comp, int indent)
  40. {
  41. foreach (Component child in comp.GetChildren())
  42. {
  43. //插入代码以处理零部件或子部件
  44. lw.WriteLine("");
  45. lw.WriteLine(new string(' ', indent * 2) + child.DisplayName);
  46. lw.WriteLine("child.Tag: " + child.Tag.ToString());
  47. if (child.GetChildren().Length != 0)
  48. {
  49. //这是一个子部件,请添加特定于部件的代码
  50. lw.WriteLine(new string(' ', indent * 2) + child.GetChildren().Length + " components");
  51. lw.WriteLine(new string(' ', indent * 2) + " + Active Arrangement: " + child.OwningPart.ComponentAssembly.ActiveArrangement.Name);
  52. }
  53. else
  54. {
  55. }
  56. reportComponentChildren(child, indent + 1);
  57. }
  58. }
  59. public static int GetUnloadOption(string arg)
  60. {
  61. return System.Convert.ToInt32(Session.LibraryUnloadOption.Immediately);
  62. }
  63. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/646567
推荐阅读
相关标签
  

闽ICP备14008679号