青华模具培训学校

 找回密码
 注册

QQ登录

只需一步,快速开始

青华模具培训学院
查看: 7047|回复: 12

[分享] 使用VB.NET【快速出车间文档】

[复制链接]
发表于 2013-6-20 14:51 | 显示全部楼层 |阅读模式
本帖最后由 dwlucien 于 2013-6-29 17:42 编辑   G5 D5 Y. q4 u9 A% c9 E
* ^  }% z! F  N9 n0 s. k
ug附带的出车间文档需要进行运算,这样一来,出车间文档在程序刀路形成很慢的情况下这运算速度让人接受不了;下图是做的一个简易车间文档示例:; U4 N- Q( q5 u2 a6 i
2013-6-20 14-27-28.jpg
# s( V: z) S" m$ U& q% _( D" H/ Y  M6 h) x4 c
3 g! u: r, z- _0 Q: B- F7 B1 A
OptionStrict Off
Imports System
Imports System.IO
Imports NXOpen
Imports NXOpen.CAM
Imports NXOpen.UF
Imports NXOpen.Utilities
Imports System.Drawing
Imports System.Windows.Forms
Module Module1
    DimtheSession As Session= Session.GetSession()
    Dim myUI As UI = UI.GetUI()
    DimtheUFSession As UFSession= UFSession.GetUFSession()
    DimtheWorkPart As Part= theSession.Parts.Work
    Dim mytag()As NXOpen.Tag'选择的加工操作标记数组
    Dim mcount As Integer '选择的加工操作数量
    SubMain()      
        Shell(Environment.GetEnvironmentVariable("UGII_USER_DIR") + "\UDO\Post\SnapPic.exe", AppWinStyle.NormalFocus, True)
        Try
            '获取选择的操作数
           theUFSession.UiOnt.AskSelectedNodes(mcount, mytag)
            Ifmcount = 0 Then
                MsgBox("没有选择加工操作!", MsgBoxStyle.OkOnly, "提示信息")
                ExitSub
            EndIf
            '设置车间文档EXCEL模板路径
            Dimfp As String = Environment.GetEnvironmentVariable("UGII_USER_DIR") + "\UDO\Post\WorkShop.xls"
            '打开与写出车间文档
            If String.IsNullOrEmpty(fp) = FalseThen
                DimmyExcel As New MY_EXCEL
                myExcel.Create()
                myExcel.Open(fp)
                myExcel.Write("M4", AskDisplatyPartName())
                myExcel.Write("S4", Format(Now, "yyyy/MM/dd"))
                myExcel.InsertPicture("b4", Environment.GetEnvironmentVariable("UGII_USER_DIR") + "\UDO\Post\jietu.jpg")
                Fori = 0 To mcount - 1
                    '输出结果
                    myExcel.Write("B" + (i + 17).ToString, (i +1).ToString)
                    myExcel.Write("D" + (i + 17).ToString,GetGemoGroup_Name(mytag(i)))
                    myExcel.Write("E" + (i + 17).ToString,GetToolPathName(mytag(i)))
                    myExcel.Write("F"+ (i + 17).ToString, GetToolName(mytag(i)))
                    myExcel.Write("G" + (i + 17).ToString,GetToolNumber(mytag(i)).ToString)
                    myExcel.Write("H" + (i + 17).ToString,GetToolDiameter(mytag(i)).ToString)
                    myExcel.Write("I" + (i + 17).ToString,GetToolCornerRadius(mytag(i)).ToString)
                    myExcel.Write("J" + (i + 17).ToString,GetToolHeight(mytag(i)).ToString)
                    myExcel.Write("K" + (i + 17).ToString, GetToolFluteLength(mytag(i)).ToString)
                    myExcel.Write("L" + (i + 17).ToString,GetStockPart(mytag(i)).ToString)
                    myExcel.Write("M" + (i + 17).ToString,GetStockFloor(mytag(i)).ToString)
                    myExcel.Write("N" + (i + 17).ToString,GetSpeedValue(mytag(i)).ToString)
                    myExcel.Write("O" + (i + 17).ToString,GetFeedValue(mytag(i)).ToString)
                    myExcel.Write("Q" + (i + 17).ToString, CInt(GetCutTime(mytag(i))).ToString)
                Next
                myExcel.SaveAs()
                'myExcel.Quit()
            EndIf
            '出错处理:
        Catchex As Exception
            MsgBox(ex)
        End Try
    End Sub
    '当前文档文件名
    FunctionAskDisplatyPartName() As String
        Dim UFSAs UFSession= UFSession.GetUFSession()
        Dimpart_name As String= ""
        Dimpart_tag As NXOpen.Tag= UFS.Part.AskDisplayPart
        UFS.Part.AskPartName(part_tag,part_name)
        Dimaa() As String
        Dimbb() As String
        aa = Split(part_name, ".prt")
        part_name = aa(0)
        bb = Split(part_name, "\")
        part_name = bb(bb.Length - 1)
        Returnpart_name
    End Function
    '取加工几何试图程序组
    FunctionGetGemoGroup_Name(ByVal camObjectTag As NXOpen.Tag) As String
        DimtheGemoGroup_Tag As NXOpen.Tag
        DimGemoGroupName As String= ""
       theUFSession.Oper.AskGeomGroup(camObjectTag, theGemoGroup_Tag)
       theUFSession.Obj.AskName(theGemoGroup_Tag, GemoGroupName)
        ReturnGemoGroupName
    End Function
    '取操作名称
    FunctionGetToolPathName(ByVal camObjectTag As NXOpen.Tag) As String
        DimToolPathName As String= ""
       theUFSession.Oper.AskNameFromTag(camObjectTag, ToolPathName)
        ReturnToolPathName
    End Function
  S+ e6 w. |6 w' }- ?1 V4 p. H) t
3 f6 ^) k, x7 [; ~
$ V, b1 Z6 M, Z' j  D. C# J4 O
    '获取刀具名称
    Function GetToolName(ByVal camObjectTag As NXOpen.Tag) As String
        Dim ToolName As String = ""
        Dim ToolTag As NXOpen.Tag
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Obj.AskName(ToolTag, ToolName)
        Return ToolName
    End Function
    '获取刀具号码
    Function GetToolNumber(ByVal camObjectTag As NXOpen.Tag) As Integer
        Dim ToolTag As NXOpen.Tag
        Dim ToolNumber As Integer
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Param.AskIntValue(ToolTag, UFConstants.UF_PARAM_TL_NUMBER, ToolNumber)
        Return ToolNumber
    End Function
    '获取刀具长度
    Function GetToolHeight(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim ToolTag As NXOpen.Tag
        Dim ToolHeight As Double
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Param.AskDoubleValue(ToolTag, UFConstants.UF_PARAM_TL_HEIGHT, ToolHeight)
        Return ToolHeight
    End Function
    '获取刀具直径
    Function GetToolDiameter(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim ToolTag As NXOpen.Tag
        Dim ToolDiameter As Double
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Param.AskDoubleValue(ToolTag, UFConstants.UF_PARAM_TL_DIAMETER, ToolDiameter)
        Return ToolDiameter
    End Function
    '获取刀具刃长
    Function GetToolFluteLength(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim ToolTag As NXOpen.Tag
        Dim ToolFluteLength As Double
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Param.AskDoubleValue(ToolTag, UFConstants.UF_PARAM_TL_FLUTE_LN, ToolFluteLength)
        Return ToolFluteLength
    End Function
    '获取刀具R角
    Function GetToolCornerRadius(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim ToolTag As NXOpen.Tag
        Dim ToolCornerRadius As Double
        theUFSession.Oper.AskCutterGroup(camObjectTag, ToolTag)
        theUFSession.Param.AskDoubleValue(ToolTag, UFConstants.UF_PARAM_TL_COR1_RAD, ToolCornerRadius)
        Return ToolCornerRadius
    End Function
    '取部件侧部余量
    Function GetStockPart(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim StockPart As Double
        theUFSession.Param.AskDoubleValue(camObjectTag, UFConstants.UF_PARAM_STOCK_PART, StockPart)
        Return StockPart
    End Function
    '取部件底部余量
    Function GetStockFloor(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim Stockfloor As Double
        theUFSession.Param.AskDoubleValue(camObjectTag, UFConstants.UF_PARAM_STOCK_FLOOR, Stockfloor)
        Return Stockfloor
    End Function
    '取主轴转速
    Function GetSpeedValue(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim SpeedVale As Double
        theUFSession.Param.AskDoubleValue(camObjectTag, UFConstants.UF_PARAM_SPINDLE_RPM, SpeedVale)
        Return SpeedVale
    End Function
    '取进给速度
    Function GetFeedValue(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim FeedValue As Double
        Dim _camObject As NXObject = NXObjectManager.Get(camObjectTag)
        Dim params(0) As CAM.Operation
        params(0) = CType(_camObject, Operation)
        Dim FeedsBuilder1 As ObjectsFeedsBuilder = theSession.Parts.Work.CAMSetup.CreateFeedsBuilder(params)
        FeedValue = FeedsBuilder1.FeedsBuilder.FeedCutBuilder.Value
        FeedsBuilder1.Destroy()
        Return FeedValue
    End Function
    '取切削时间
    Function GetCutTime(ByVal camObjectTag As NXOpen.Tag) As Double
        Dim CutTime As Double
        theUFSession.Param.AskDoubleValue(camObjectTag, 142, CutTime)
        Return CutTime
    End Function
    Public Function GetUnloadOption(ByVal dummy As String) As Integer
        'Unloads the image immediately after execution within NX
        GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately
        '----Other unload options-------
        'Unloads the image when the NX session terminates
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination
        'Unloads the image explicitly, via an unload dialog
        'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
        '-------------------------------
    End Function
End Module
Public Class MY_EXCEL
    Private app As Object
    Private book As Object
    Private sheet As Object
    '表格名称
    Public Property xlSheetName() As String
        Get
            Return sheet.Name
        End Get
        Set(ByVal value As String)
            sheet.Name = value
        End Set
    End Property
    '新建程序
    Public Function Create() As Boolean
        app = CreateObject("Excel.Application")
        If app Is Nothing Then
            Return False
        Else
            app.Visible = True
            Return True
        End If
    End Function
    '打开文件
    Public Sub Open(ByVal xlFileName As String)
        book = app.Workbooks.Open(xlFileName)
        sheet = book.ActiveSheet
    End Sub
    '写单元格
    Public Sub Write(ByVal _Range As String, ByVal value As Object)
        If _Range <> "" Then
            sheet.Range(_Range).Value = value
        End If
    End Sub
    '插入图片
    Public Sub InsertPicture(ByVal _Range As String, ByVal imageFile As String)
        If _Range <> "" Then
            Dim ExcelRange As Object = sheet.Range(_Range)
            Dim Ins_image As Image
            Ins_image = Image.FromFile(imageFile)
            Dim HeightScale As Double = Math.Round((ExcelRange.Height * 12 - 6) / Ins_image.Height, 2)
            Dim WidthScale As Double = Math.Round((ExcelRange.Width * 15 - 6) / Ins_image.Width, 2)
            Dim ScaleHW As Double = Math.Min(HeightScale, WidthScale)
            sheet.Shapes.AddPicture(imageFile, 0, 1, ExcelRange.Left + 2, ExcelRange.Top + 2, Ins_image.Width * ScaleHW, Ins_image.Height * ScaleHW)
            Ins_image.Dispose()
        End If
    End Sub
    '取指定单元格值
    Public Function GetValue(ByVal _table As Integer, ByVal _Range As String) As String
        Dim sheet As Object = book.Sheets.Item(_table)
        Return sheet.Range(_Range).value
    End Function
    Public Function Save() As Boolean
        book.Save()
    End Function
    Public Function SaveAs() As Boolean
        Dim Sdlg As SaveFileDialog = New SaveFileDialog '定义一个保存对话框
        Sdlg.FileName = AskDisplatyPartName() + "加工工序单.xls" '保存对话框的默认文件名
        Sdlg.Filter = "EXCEL档案(*.xls)|*.xls" '过滤器设置
        If Sdlg.ShowDialog = Windows.Forms.DialogResult.OK Then '如果保存对话框的确认按钮被按下
            book.SaveAs(Sdlg.FileName) '保存文件
        End If
    End Function
    '结束EXCEL对象
    Public Function Quit() As Boolean
        book.close()
        app.Quit()
        app = Nothing
        GC.Collect()
    End Function
    '取数组
    Public Function GetArryValue(ByRef _table As Integer, ByVal _Row As String, ByVal _Cell As String, ByRef ArryString() As String) As Integer
        Dim sheet As Object = book.Sheets.Item(_table)
        Dim rowvalue As String = sheet.Range(_Row).value
        Dim cellvalue As String = sheet.Range(_Cell).value
        Dim k As Integer = 0
        If rowvalue <> "" And cellvalue <> "" Then
            Dim rowstr() As String = Split(rowvalue, "/")
            Dim cellstring() As String = Split(cellvalue, "/")
            Dim a, b, c, d As Integer
            If rowstr.Length > 1 And cellstring.Length > 0 Then
                a = Convert.ToInt32(rowstr(0))
                b = Convert.ToInt32(rowstr(1))
                If a > 0 And b > a Then
                    ReDim Preserve ArryString(b - a)
                    For i As Integer = 0 To b - a
                        ArryString(i) = cellstring(0) & Convert.ToString(a + i)
                        k += 1
                    Next
                    If rowstr.Length > 3 And cellstring.Length > 1 Then
                        c = Convert.ToInt32(rowstr(2))
                        d = Convert.ToInt32(rowstr(3))
                        If c > 0 And d > c Then
                            ReDim Preserve ArryString(b - a + d - c + 1)
                            For j As Integer = 0 To d - c
                                ArryString(b - a + j + 1) = cellstring(1) & Convert.ToString(c + j)
                                k += 1
                            Next
                        End If
                    End If
                End If
            End If
        End If
        Return k
    End Function
End Class
[全部放开了]

4 ?. Q% |# y& r2 ~4 j2 e- m- M1 x
, W4 e) L* U! E+ n. l% v

( {) t/ d+ j5 I5 X. Y" A7 E& O

7 j, _* @; E6 d5 d7 R! m4 I3 j1 i# ]8 ?. p/ i4 o( t; m7 p" u6 A
发表于 2013-7-3 12:35 | 显示全部楼层
楼主怎么做出来的,能否分享一下!
回复 支持 反对

使用道具 举报

发表于 2013-7-6 08:16 | 显示全部楼层
这个开发对我来说挺难,我需要学习,先谢谢了!有整好的先提供一个试用可以吧!呵呵
回复 支持 反对

使用道具 举报

发表于 2013-8-30 21:26 | 显示全部楼层
真的非常感谢学习了很有用
5 S+ g: J0 e1 b  a' R
回复 支持 反对

使用道具 举报

发表于 2013-10-24 06:22 | 显示全部楼层
怎么用的  不会用 / Y4 [( g& ^4 n1 |- Z. C" ~4 ^
回复 支持 反对

使用道具 举报

发表于 2013-10-26 16:16 | 显示全部楼层
怎么弄的呀
) j3 f7 J9 D- b- _3 L0 k
回复 支持 反对

使用道具 举报

发表于 2013-10-29 20:30 | 显示全部楼层
加工时间,只有在高版本才可以在part文件中取出,低版本还是要通过后处理的  l  J$ `& {! d$ \8 \1 v6 I) U8 J
话说,这个程序单比较眼熟
回复 支持 反对

使用道具 举报

发表于 2013-10-31 11:26 | 显示全部楼层
楼主,我根据你的方法为什么选择程序组,获取不了程序组名字。, a( `- r$ {0 A3 d; W1 C
Imports NXOpen
+ b  r! z6 r0 C7 a# }Imports NXOpen.UF/ q; M. W: q- C- _- v& X
1 F/ @2 I) w# Y" ^
Module Module1) W: v6 n8 F/ D& z
    Dim theSession As Session = Session.GetSession()- [. t, n2 K7 E# i4 a4 ]
    Dim theUI As UI = UI.GetUI(); \- c; J1 j9 X" r% Y: p: _; n
    Dim theUfSession As UFSession = UFSession.GetUFSession()+ E( q# C6 n8 o/ ^
    Dim theWorkPart As Part = theSession.Parts.Work) y4 ^; ]8 r* A) P: L; Y0 c
    Dim mytag() As NXOpen.Tag '选择的加工操作标记数组+ G  a) ~' {/ s- _
    Dim a1 As NXOpen.Tag
& f) t4 f' }4 N7 g4 H* s5 ^8 S    Dim mcount As Integer '选择的加工操作数量
) W6 K7 Z/ m5 q& {0 j5 R2 W: n' Q, E; u
2 h& l) T$ |0 i! R  Y; v. H    Sub Main()
* R. k5 C: c/ a0 m+ \9 v$ Y1 c# T8 [+ h, j/ `* C# ~5 ]: \" h: t
        theUfSession.UiOnt.AskSelectedNodes(mcount, mytag)/ b  q3 q5 {6 l: a0 b7 t) T
        # N5 h7 C+ k. k4 H+ K
        Dim a As String$ `& z5 S' _; T, }3 b2 A' @
        For i = 0 To (mcount - 1)9 T" K0 a+ a4 m9 S) I5 M- g0 H& ^
            a = ""
# s; v% O, ?5 m' W' x# ~+ I            theUfSession.Oper.AskGeomGroup(mytag(i), a1), W8 C) |0 C7 T3 B* i
            theUfSession.Obj.AskName(a1, a)
7 j& {1 i! }7 S, d! Y4 \2 R* A            MsgBox(a)' w$ L, c6 O! p: o0 l# Q5 U
        Next i5 J. i' s# D: g+ {3 N
3 x  @+ t( F9 A
; l' K( p0 {9 _3 z0 _' k
    End Sub
回复 支持 反对

使用道具 举报

发表于 2013-10-31 14:50 | 显示全部楼层
不错,支持一下啊
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|关于我们|sitemap|小黑屋|Archiver|手机版|UG网-UG技术论坛-青华数控模具培训学校 ( 粤ICP备15108561号 )

GMT+8, 2024-12-4 16:47 , Processed in 0.059019 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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