UG12使用VB脚本批量输出后处理,如何获取程序组所有工序
我想用脚本在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-2Line 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> ## !
Option Strict Off
Imports System
Imports System.IO
Imports System.Windows.Forms
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Collections.Generic ' 添加此行以引用 List 类型
Module TestRenameCamObjects
Dim theSession As Session = Session.GetSession()
Dim theUFSessionAs UFSession = UFSession.GetUFSession()
Dim theUI As UI = UI.GetUI()
Dim TempPath As String = Environment.GetEnvironmentVariable("TMP")
Dim UGRelease As String = Nothing
Dim UGFullRelease As String = Nothing
Dim postProcessorPath As String = "D:\Program Files\Siemens\NX 12.0\MACH\resource\postprocessor\YIYI_360.tcl"
Dim postProcessorDefPath As String = "D:\Program Files\Siemens\NX 12.0\MACH\resource\postprocessor\YIYI_360.def"
Dim outputBasePath As String = "E:\工作文件\Bln\Bln"
Sub Main(Args As String())
theSession.LogFile.WriteLine("Executing ... " & _
System.Reflection.Assembly.GetExecutingAssembly().Location)
Threading.Thread.CurrentThread.CurrentCulture = New Globalization.CultureInfo("en-US")
Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo("en-US")
UGRelease = theSession.GetEnvironmentVariableValue("UGII_VERSION")
UGFullRelease = theSession.GetEnvironmentVariableValue("UGII_FULL_VERSION")
System.Windows.Forms.Application.EnableVisualStyles()
' 检查是否在CAM环境中
Dim AppID As Integer = UFConstants.UF_APP_NONE
theUFSession.UF.AskApplicationModule(AppID)
If AppID <> UFConstants.UF_APP_CAM Then
theUI.NXMessageBox.Show("Application mismatch", NXMessageBox.DialogType.Error, "No NX CAM session, Exiting!")
Exit Sub
End If
' 获取选中的程序组
Dim selectedGroups As List(Of TaggedObject) = New List(Of TaggedObject)()
For i As Integer = 0 To theUI.SelectionManager.GetNumSelectedObjects() - 1
Dim theSelectedObject As TaggedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If theSession.Parts.Work.CAMSetup.IsGroup(theSelectedObject) Then
selectedGroups.Add(theSelectedObject)
End If
Next
' 对选中的程序组进行处理
For Each theSelectedObject As TaggedObject In selectedGroups
Dim theNcGroup As CAM.NCGroup = CType(theSelectedObject, CAM.NCGroup)
Dim groupName As String = theNcGroup.Name
Dim groupOutputPath As String = Path.Combine(outputBasePath, groupName)
' 创建子文件夹
If Not Directory.Exists(groupOutputPath) Then
Directory.CreateDirectory(groupOutputPath)
End If
' 获取程序组中的所有工序
Dim operations As CAM.Operations = theNcGroup.Operations
For Each operation As CAM.Operation In operations
Dim operationName As String = operation.Name
Dim toolName As String = operation.Tool.Name
Dim newOperationName As String = operationName.Substring(0, 3) & toolName
' 重命名工序
operation.SetName(newOperationName)
' 输出工序
Dim postBuilder As CAM.PostBuilder = theSession.Parts.Work.CAMSetup.PostBuilder
postBuilder.PostOperation(operation, postProcessorPath, postProcessorDefPath, groupOutputPath, False)
Next
Next
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
Return CInt(Session.LibraryUnloadOption.Immediately)
End Function
End Module
Dim operations
For Each operation
62行多了个S,定义和循环的变量名都不一致 这个问题我通过AI编程解决了,对我工作来说方便了一些,以下是代码,附件为文件
Imports System.Collections.Generic
Imports System
Imports NXOpen
Imports System.IO
Module NXJournal
Sub Main(ByVal args() As String)
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim theUI As UI = UI.GetUI()
' 获取选定对象
' 获取程序组和工序
' 获取选定程序组
' 获取选中的程序组
Dim selectedGroups As New List(Of CAM.NCGroup)
For i As Integer = 0 To theUI.SelectionManager.GetNumSelectedObjects() - 1
Dim selectedObject = theUI.SelectionManager.GetSelectedTaggedObject(i)
If TypeOf selectedObject Is CAM.NCGroup Then
selectedGroups.Add(CType(selectedObject, CAM.NCGroup))
End If
Next
' 获取首个程序组及其工序
If selectedGroups.Count = 0 Then
theUI.NXMessageBox.Show("选择错误", NXMessageBox.DialogType.Error, "请至少选择一个程序组")
Exit Sub
End If
Dim ncGroup As CAM.NCGroup = selectedGroups(0)
' 0=首道工序,1=第二道工序,2=第三道工序,3=第四道工序
For i As Integer = 0 To 3
Dim operation As CAM.Operation = CType(ncGroup.GetMembers().GetValue(i), CAM.Operation)
' 构造路径参数
' 获取加工参数
' 参数定义
Dim programGroupName As String = ncGroup.Name
Dim operationName As String = operation.Name
' 创建输出目录
' 创建输出路径
Dim outputDir As String = String.Format("{0}{1}\\", "E:\工作文件\Bln\Bln\\", programGroupName)
If Not IO.Directory.Exists(outputDir) Then
IO.Directory.CreateDirectory(outputDir)
End If
' 后处理设置
workPart.CAMSetup.PostprocessWithPostModeSetting(
{operation},
"YIYI_360",
String.Format("{0}{1}_{2}.nc", outputDir, programGroupName, operationName),
CAM.CAMSetup.OutputUnits.PostDefined,
CAM.CAMSetup.PostprocessSettingsOutputWarning.PostDefined,
CAM.CAMSetup.PostprocessSettingsReviewTool.PostDefined,
CAM.CAMSetup.PostprocessSettingsPostMode.Normal
)
Next
End Sub
End Module
如程序组下有多个工序,可以修改第31行的内容,个数从0开始计数,5个工序就改为 To 4
可以通过百度网盘直接下载文件 push https://pan.baidu.com/s/16VlAJ4RG2-DvhDHcnIn0qQ?pwd=ugnx 提取码: ugnx
页:
[1]