找回密码
 立即注册

QQ登录

只需一步,快速开始

微信扫码登录

搜索
查看: 1066|回复: 1

[求助] UG12使用VB脚本批量输出后处理,如何获取程序组所有工序

[复制链接]

2

主题

4

回帖

101

积分

上等兵

积分
101
发表于 2024-12-27 13:25:06 | 显示全部楼层 |阅读模式
我想用脚本在UG12中实现
1、当选中程序组的时候执行脚本,将逐个输出程序组中的工序并按照工序名称前三个字符+刀具名称对输出的工序进行重命名
2、输出路径为E:\工作文件\Bln\Bln,输出时在该文件夹内建立以选中程序组名称的子文件夹,并将程序组内工序逐个输出至该子文件夹
3、后处理器为YIYI_360,${UGII_CAM_POST_DIR}YIYI_360.tcl,${UGII_CAM_POST_DIR}YIYI_360.def,后处理器所在文件夹为D:\Program Files\Siemens\NX 12.0\MACH\resource\postprocessor
已编写以下代码,但是执行以下.vb程序是报错,求助如何处理


通过网盘分享的文件:push0.vb 链接: push0.vb 提取码: 6pti

语法错误:
第 62 行:未定义类型“CAM.Operations”。

&MACRO MESSAGE_BOX  -2  Line 62: 未定义类型“CAM.Operations”。
&MACRO MESSAGE_TEXT  
&MACRO MENU, 0, UG_HELP_SYSTEM_LOG UG_GATEWAY_MAIN_MENUBAR <RibbonFileBar->BackStageBar->LeftBackStageGroup->rbn_file_tab_help_cascade.csb> ## !

  1. Option Strict Off
  2. Imports System
  3. Imports System.IO
  4. Imports System.Windows.Forms
  5. Imports NXOpen
  6. Imports NXOpen.UF
  7. Imports NXOpen.Utilities
  8. Imports System.Collections.Generic ' 添加此行以引用 List 类型

  9. Module TestRenameCamObjects
  10.     Dim theSession    As Session   = Session.GetSession()
  11.     Dim theUFSession  As UFSession = UFSession.GetUFSession()
  12.     Dim theUI         As UI        = UI.GetUI()
  13.     Dim TempPath      As String    = Environment.GetEnvironmentVariable("TMP")
  14.     Dim UGRelease     As String    = Nothing
  15.     Dim UGFullRelease As String    = Nothing
  16.     Dim postProcessorPath As String = "D:\Program Files\Siemens\NX 12.0\MACH\resource\postprocessor\YIYI_360.tcl"
  17.     Dim postProcessorDefPath As String = "D:\Program Files\Siemens\NX 12.0\MACH\resource\postprocessor\YIYI_360.def"
  18.     Dim outputBasePath As String = "E:\工作文件\Bln\Bln"

  19.     Sub Main(Args As String())
  20.         theSession.LogFile.WriteLine("Executing ... " & _
  21.             System.Reflection.Assembly.GetExecutingAssembly().Location)
  22.         
  23.         Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-US")
  24.         Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US")
  25.         
  26.         UGRelease = theSession.GetEnvironmentVariableValue("UGII_VERSION")
  27.         UGFullRelease = theSession.GetEnvironmentVariableValue("UGII_FULL_VERSION")
  28.         
  29.         System.Windows.Forms.Application.EnableVisualStyles()
  30.         
  31.         ' 检查是否在CAM环境中
  32.         Dim AppID As Integer = UFConstants.UF_APP_NONE
  33.         theUFSession.UF.AskApplicationModule(AppID)
  34.         If AppID <> UFConstants.UF_APP_CAM Then
  35.             theUI.NXMessageBox.Show("Application mismatch", NXMessageBox.DialogType.Error, "No NX CAM session, Exiting!")
  36.             Exit Sub
  37.         End If
  38.         
  39.         ' 获取选中的程序组
  40.         Dim selectedGroups As List(Of TaggedObject) = New List(Of TaggedObject)()
  41.         For i As Integer = 0 To theUI.SelectionManager.GetNumSelectedObjects() - 1
  42.             Dim theSelectedObject As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
  43.             If theSession.Parts.Work.CAMSetup.IsGroup(theSelectedObject) Then
  44.                 selectedGroups.Add(theSelectedObject)
  45.             End If
  46.         Next
  47.         
  48.         ' 对选中的程序组进行处理
  49.         For Each theSelectedObject As TaggedObject In selectedGroups
  50.             Dim theNcGroup As CAM.NCGroup = CType(theSelectedObject, CAM.NCGroup)
  51.             Dim groupName As String = theNcGroup.Name
  52.             Dim groupOutputPath As String = Path.Combine(outputBasePath, groupName)
  53.             
  54.             ' 创建子文件夹
  55.             If Not Directory.Exists(groupOutputPath) Then
  56.                 Directory.CreateDirectory(groupOutputPath)
  57.             End If
  58.             
  59.             ' 获取程序组中的所有工序
  60.             Dim operations As CAM.Operations = theNcGroup.Operations
  61.             For Each operation As CAM.Operation In operations
  62.                 Dim operationName As String = operation.Name
  63.                 Dim toolName As String = operation.Tool.Name
  64.                 Dim newOperationName As String = operationName.Substring(0, 3) & toolName
  65.                
  66.                 ' 重命名工序
  67.                 operation.SetName(newOperationName)
  68.                
  69.                 ' 输出工序
  70.                 Dim postBuilder As CAM.PostBuilder = theSession.Parts.Work.CAMSetup.PostBuilder
  71.                 postBuilder.PostOperation(operation, postProcessorPath, postProcessorDefPath, groupOutputPath, False)
  72.             Next
  73.         Next
  74.     End Sub
  75.    
  76.     Public Function GetUnloadOption(ByVal dummy As String) As Integer
  77.         Return CInt(Session.LibraryUnloadOption.Immediately)
  78.     End Function
  79. End Module
复制代码


2

主题

315

回帖

1万

积分

中校

积分
11601
发表于 2024-12-27 21:33:13 | 显示全部楼层
Dim operations  
For Each operation
62行多了个S,定义和循环的变量名都不一致

2

主题

4

回帖

101

积分

上等兵

积分
101
 楼主| 发表于 前天 15:05 | 显示全部楼层
这个问题我通过AI编程解决了,对我工作来说方便了一些,以下是代码,附件为文件
  1. Imports System.Collections.Generic
  2. Imports System
  3. Imports NXOpen
  4. Imports System.IO

  5. Module NXJournal
  6.     Sub Main(ByVal args() As String)
  7.         Dim theSession As Session = Session.GetSession()
  8.         Dim workPart As Part = theSession.Parts.Work
  9.         Dim theUI As UI = UI.GetUI()

  10.         ' 获取选定对象
  11.         ' 获取程序组和工序
  12.         ' 获取选定程序组
  13.         ' 获取选中的程序组
  14.         Dim selectedGroups As New List(Of CAM.NCGroup)
  15.         For i As Integer = 0 To theUI.SelectionManager.GetNumSelectedObjects() - 1
  16.             Dim selectedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
  17.             If TypeOf selectedObject Is CAM.NCGroup Then
  18.                 selectedGroups.Add(CType(selectedObject, CAM.NCGroup))
  19.             End If
  20.         Next

  21.         ' 获取首个程序组及其工序
  22.         If selectedGroups.Count = 0 Then
  23.             theUI.NXMessageBox.Show("选择错误", NXMessageBox.DialogType.Error, "请至少选择一个程序组")
  24.             Exit Sub
  25.         End If
  26.         Dim ncGroup As CAM.NCGroup = selectedGroups(0)
  27.         ' 0=首道工序,1=第二道工序,2=第三道工序,3=第四道工序
  28.         For i As Integer = 0 To 3
  29.             Dim operation As CAM.Operation = CType(ncGroup.GetMembers().GetValue(i), CAM.Operation)
  30.             ' 构造路径参数
  31.             ' 获取加工参数
  32.             ' 参数定义

  33.             Dim programGroupName As String = ncGroup.Name
  34.             Dim operationName As String = operation.Name
  35.         
  36.             ' 创建输出目录
  37.             ' 创建输出路径
  38.             Dim outputDir As String = String.Format("{0}{1}\", "E:\工作文件\Bln\Bln\", programGroupName)
  39.             If Not IO.Directory.Exists(outputDir) Then
  40.                 IO.Directory.CreateDirectory(outputDir)
  41.             End If

  42.             ' 后处理设置
  43.             workPart.CAMSetup.PostprocessWithPostModeSetting(
  44.                 {operation},
  45.                 "YIYI_360",
  46.                 String.Format("{0}{1}_{2}.nc", outputDir, programGroupName, operationName),
  47.                 CAM.CAMSetup.OutputUnits.PostDefined,
  48.                 CAM.CAMSetup.PostprocessSettingsOutputWarning.PostDefined,
  49.                 CAM.CAMSetup.PostprocessSettingsReviewTool.PostDefined,
  50.                 CAM.CAMSetup.PostprocessSettingsPostMode.Normal
  51.         )
  52.         Next
  53.     End Sub
  54. End Module
复制代码


如程序组下有多个工序,可以修改第31行的内容,个数从0开始计数,5个工序就改为 To 4

可以通过百度网盘直接下载文件 push https://pan.baidu.com/s/16VlAJ4RG2-DvhDHcnIn0qQ?pwd=ugnx 提取码: ugnx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

咨询QQ:1359218528|发帖须知!|Archiver|手机版|小黑屋|UG爱好者论坛 ( 京ICP备10217105号-2 )

GMT+8, 2025-11-6 12:23

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表