基于VB.NET和NX8的刀具导入工具开发源码
本帖最后由 hsy 于 2016-2-22 13:11 编辑开发语言:VB.NET
NX版本:NX8.0
开发目的:快速调入library中的指定刀具
定义变量
Public NXToolName As New ArrayList
Public PreName As New ArrayList
Public BckName As New ArrayList
Structure ToolObj
Dim ToolName As String
Dim ToolDima As Double
Dim ToolLength As Double
Dim PreName As String
Dim BackName As String
End Structure
Public NXToolName_Library As New ArrayList
程序入口
Sub Main()
Dim NewForm As New Frmmain
Dim DllPath As String = System.Reflection.Assembly.GetExecutingAssembly().Location.ToString()
Dim Posi As Integer = InStrRev(DllPath, "\")
DllPath = Mid(DllPath, 1, Posi - 1)
Posi = InStrRev(DllPath, "\")
APPPath = Mid(DllPath, 1, Posi)
NXToolName.Clear()
GetToolList("GENERIC_MACHINE")
GetToolListFromLibrary()
Try
If GetRight() = True Then
NewForm.ShowDialog()
Else
MessageBox.Show("服务器没有开启或当前机器没有权限!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
End Try
End Sub
Sub GetToolList(ByRef String_Pass As String)
Dim TheSession As Session = Session.GetSession()
Dim ThePart As NXOpen.Part = TheSession.Parts.Work
Dim NCGroup_Cycle As CAM.NCGroup
NCGroup_Cycle = ThePart.CAMSetup.CAMGroupCollection.FindObject(String_Pass)
Dim NCGroup_Cycle_Members() As CAM.CAMObject = NCGroup_Cycle.GetMembers()
For i = 0 To NCGroup_Cycle_Members.Length - 1
If ThePart.CAMSetup.IsGroup(NCGroup_Cycle_Members(i)) Then
If NCGroup_Cycle_Members(i).Name <> "NONE" Then
NXToolName.Add(NCGroup_Cycle_Members(i).Name)
End If
GetToolList(NCGroup_Cycle_Members(i).Name)
End If
Next
End Sub
Sub GetToolListFromLibrary()
NXToolName_Library.Clear()
Dim NX As String = Application.StartupPath
Dim Num As Integer = InStrRev(NX, "\")
NX = Mid(NX, 1, Num)
Dim ReadFile As StreamReader = New StreamReader(NX + "MACH\resource\library\tool\metric\tool_database.dat", System.Text.Encoding.Default)
Dim StringLine As String = ""
Dim StringSplit() As String
If ReadFile IsNot Nothing Then
Do Until ReadFile.EndOfStream
StringLine = ReadFile.ReadLine
If InStr(StringLine, "DATA ") = 1 Then ‘分析刀具名称
Try
StringSplit = StringLine.Trim.Split("|")
Dim ToolName As String = StringSplit(1)
Dim NewTool As New ToolObj
NewTool.ToolName = ToolName
NewTool.ToolLength = 0
Dim ToolData() As String = ToolName.Trim.Split("_")
If ToolData.Length > 3 Then
For j = 1 To ToolData.Length - 1
Try
If InStr(ToolData(j), "L") > 0 Then
If InStr(ToolData(j), "-") Then
NewTool.ToolLength = CDbl(Mid(ToolData(j), 2, InStr(ToolData(j), "-") - 2))
Else
NewTool.ToolLength = CDbl(Mid(ToolData(j), 2, ToolData(j).Length - 1))
End If
Exit For
End If
Catch ex As Exceptionm
End Try
Next
NewTool.PreName = ToolData(0)
NewTool.BackName = ToolData(ToolData.Length - 1)
Dim PreNameIn As Boolean = False
For i = 0 To PreName.Count - 1
If NewTool.PreName = PreName(i) Then
PreNameIn = True
Exit For
End If
Next
If PreNameIn = False Then
PreName.Add(NewTool.PreName)
End If
Dim BckNameIn As Boolean = False
For i = 0 To BckName.Count - 1
If NewTool.BackName = BckName(i) Then
BckNameIn = True
Exit For
End If
Next
If BckNameIn = False Then
BckName.Add(NewTool.BackName)
End If
NewTool.ToolDima = Trim(StringSplit(10))
If NewTool.ToolDima = 0 Then
NewTool.ToolDima = Trim(StringSplit(14))
End If
NXToolName_Library.Add(NewTool)
End If
Catch ex As Exception
End Try
End If
Loop
PreName.Sort()
BckName.Sort()
End If
End Sub
Public Function GetUnloadOption(ByVal dummy As String) As Integer
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
End Function
导入刀具
Private Sub Btn_Input_Click(sender As System.Object, e As System.EventArgs) Handles Btn_Input.Click
'如果没有选择,则全部导入,否则导入选择的刀具
NXToolName.Clear()
GetToolList("GENERIC_MACHINE")
Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim displayPart As Part = theSession.Parts.Display
Dim tufs As UFSession = UFSession.GetUFSession()
Dim tool1 As CAM.Tool
Dim success1 As Boolean
Dim SumInPut As Integer = 0
Dim SumInPut_Ori As Integer = 0
Dim SumInPut_No As Integer = 0
For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Selected = True Then
Try
Dim CheckIn As Boolean = False
For j = 0 To NXToolName.Count - 1
If DataGridView1.Rows(i).Cells(0).Value.ToString.Trim = NXToolName(j) Then
CheckIn = True
SumInPut_Ori = SumInPut_Ori + 1
Exit For
End If
Next
If CheckIn = False Then
tool1 = workPart.CAMSetup.RetrieveTool(DataGridView1.Rows(i).Cells(0).Value.ToString.Trim, success1)
If success1 = True Then
SumInPut = SumInPut + 1
Else
SumInPut_No = SumInPut_No + 1
End If
End If
Catch ex As Exception
End Try
DataGridView1.Rows(i).Selected = False
End If
Next
tufs.UiOnt.Refresh()
MessageBox.Show(StringA & StringB & StringC, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub 窗体显示
Sub GriviewUpdate(ByRef List As ArrayList)
DataGridView1.Rows.Clear()
For i = 0 To List.Count - 1
DataGridView1.Rows.Add(List(i).ToolName, List(i).PreName, List(i).ToolDima, List(i).ToolLength, List(i).BackName)
Next
End Sub
Private Sub Frmmain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
GriviewUpdate(NXToolName_Library)
For i = 0 To PreName.Count - 1
Dim PreNameCheck As New CheckBox
PreNameCheck.Left = 5
PreNameCheck.Top = i * 30
PreNameCheck.Text = PreName(i)
Panelpre.Controls.Add(PreNameCheck)
PreList.Add(PreNameCheck)
AddHandler PreNameCheck.Click, AddressOf Select_Click
Next
For i = 0 To BckName.Count - 1
Dim BckNameCheck As New CheckBox
BckNameCheck.Left = 5
BckNameCheck.Top = i * 30
BckNameCheck.Text = BckName(i)
Panelbck.Controls.Add(BckNameCheck)
BckList.Add(BckNameCheck)
AddHandler BckNameCheck.Click, AddressOf Select_Click
Next
End Sub
导出到excel
Private Sub OutPutExcel_Click(sender As System.Object, e As System.EventArgs) Handles OutPutExcel.Click
Try
Dim ExlApp As Excel.Application
Dim ExlBook As Excel.Workbook
Dim ExlSheet As Excel.Worksheet
ExlApp = CreateObject("Excel.Application") '创建EXCEL对象
ExlBook = ExlApp.Workbooks.Add()
ExlSheet = ExlBook.Worksheets("sheet1")
ExlSheet.Name = "刀具统计"
ExlApp.Visible = False
ExlSheet.Cells(1, 1) = "刀具名称"
ExlSheet.Cells(1, 2) = "刀具前缀"
ExlSheet.Cells(1, 3) = "刀具直径"
ExlSheet.Cells(1, 4) = "刀具长度"
ExlSheet.Cells(1, 5) = "刀具后缀"
'输出数据
For i = 0 To DataGridView1.Rows.Count - 1
If DataGridView1.Rows(i).Selected = True Then
ExlSheet.Cells(i + 2, 1) = DataGridView1.Rows(i).Cells(0).Value
ExlSheet.Cells(i + 2, 2) = DataGridView1.Rows(i).Cells(1).Value
ExlSheet.Cells(i + 2, 3) = DataGridView1.Rows(i).Cells(2).Value
ExlSheet.Cells(i + 2, 4) = DataGridView1.Rows(i).Cells(3).Value
ExlSheet.Cells(i + 2, 5) = DataGridView1.Rows(i).Cells(4).Value
End If
Next
ExlApp.Cells.EntireColumn.AutoFit()
With SaveFileDialog1
.FileName = "刀具输出" & Today.Date
.InitialDirectory = "D:\"
.DefaultExt = "xls"
.Filter = "(*.xls)|*.xls"
.FilterIndex = 1
.Title = "刀具数据输出"
End With
SaveFileDialog1.ShowDialog()
ExlBook.SaveAs(SaveFileDialog1.FileName)
ExlApp.Visible = True
ExlApp = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub Private Sub Select_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
Dim NXToolName_Library_Update As New ArrayList '导入刀具
NXToolName_Library_Update.Clear()
For i = 0 To NXToolName_Library.Count - 1
Dim CheckRull As Boolean = False
For j = 0 To PreList.Count - 1
If PreList(j).checked = True Then
If NXToolName_Library(i).PreName = PreList(j).text Then
For k = 0 To BckList.Count - 1
If BckList(k).checked = True Then
If NXToolName_Library(i).BackName = BckList(k).text Then
If CheckBoxToolDiam.Checked = True Then
If NXToolName_Library(i).ToolDima = CDbl(TextBox1.Text) Then
CheckRull = True
End If
Exit For
Else
CheckRull = True
Exit For
End If
End If
End If
If CheckRull = True Then
Exit For
End If
Next
End If
End If
If CheckRull = True Then
Dim NewTool As New ToolObj
NewTool.ToolName = NXToolName_Library(i).ToolName
NewTool.ToolDima = NXToolName_Library(i).ToolDima
NewTool.ToolLength = NXToolName_Library(i).ToolLength
NewTool.PreName = NXToolName_Library(i).PreName
NewTool.BackName = NXToolName_Library(i).BackName
NXToolName_Library_Update.Add(NewTool)
Exit For
End If
Next
Next
GriviewUpdate(NXToolName_Library_Update)
Catch ex As Exception
End Try
End Sub
Private Sub CheckBoxToolDiam_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxToolDiam.CheckedChanged
If CheckBoxToolDiam.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
Select_Click(sender, e)
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
Select_Click(sender, e)
End Sub
Private Sub CheckBoxPre_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxPre.CheckedChanged
For i = 0 To PreList.Count - 1
PreList(i).Checked = CheckBoxPre.Checked
Next
Select_Click(sender, e)
End Sub
Private Sub CheckBoxBck_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxBck.CheckedChanged
For i = 0 To BckList.Count - 1
BckList(i).Checked = CheckBoxBck.Checked
Next
Select_Click(sender, e)
End Sub
页:
[1]