VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 不得不明白的vb.net Delegate-只供入门

首先从UG5下面的UGOPENvs_files文件夹中拷贝VB文件夹到 Microsoft Visual Studio 9.0(我用的是vs2008)文件夹下覆盖原来文件

开启VS点击新建会找到UG5的visual basic模板,取名为Project,打开这个模板会给自动给你创建初始代码如下:

------------------------------------

Option Strict Off 

Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

'------------------------------------------------------------
'
'  Module MyProject
'
'      (Add description here)
'------------------------------------------------------------
Module MyProject


'------------------------------------------------------------
'
'  Main()
'
'      (Add description here)
'
'------------------------------------------------------------
Sub Main()

    'Add your code here


End Sub

'------------------------------------------------------------
'
'  GetUnloadOption()
'
'     Used to tell NX when to unload this library
'
'     Available options include:
'       Session.LibraryUnloadOption.Immediately
'       Session.LibraryUnloadOption.Explicitly
'       Session.LibraryUnloadOption.AtTermination
'
'     Any programs that register callbacks must use
'     AtTermination as the unload option.
'------------------------------------------------------------
Public Function GetUnloadOption(ByVal dummy As String) As Integer
    GetUnloadOption = Session.LibraryUnloadOption.Immediately
End Function

 

End Module

 

------------------------------------

其中Sub main()是UG入口函数

此时我们从右边解决资源管理器中右键点击MyProject选择添加新项,并添加一个windows窗体组件默认名称为Form1.vb,这样我们就生成了一个窗口类,类名为Form1

然后在sub main过程中添加如下代码显示窗口:

 Dim f As New Form1'定义f为Form1类

  f.ShowDialog()'显示f窗口

此时代码完成.选择解决资源管理器,双击Project选择调试,选择启动外部调试,浏览UG主文件ugraf.exe

然后调试运行,等UG加载完成后新建或打开一个UGpart文件,按Ctrl+U调出对话框,到VS工程文件夹(默认在你的文档下)打开当前工程文件夹下的bin文件夹勾选VS生成的Project.DLL文件,这样就显示一个窗口了,当然我们可以在窗口上添加其它控件运用UGopen函数完成其它功能。

 

第一个例子:怎样用VB.NET在UG中创建一个点?
Option Strict Off

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module CreatePoint

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()

    Sub Main()

        Dim sp As New Point3d(0, 0, 0)

        Dim thePoint As Point = s.Parts.Work.Points.CreatePoint(sp)

    End Sub


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function

End Module

第二个例子:怎样用VB.NET在UG中创建一个条线?

Option Strict Off  

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module template_code

  Dim s As Session = Session.GetSession()

Sub Main()

  Dim sp As New Point3d(0, 0, 0)
  Dim ep As New Point3d(10, 10, 0)

  Dim theLine As Line = s.Parts.Work.Curves.CreateLine(sp, ep)

        
End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module

 

第三个例子:怎样用VB.NET在UG中创建一个圆柱,然后更改它的颜色?

Option Strict Off  
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module create_a_cylinder_and_set_color

    Sub Main()

        Dim s As Session = Session.GetSession()
        Dim ufs As UFSession = UFSession.GetUFSession()

        Dim wp As Part = s.Parts.Work()

        Dim cyl_feat_tag As NXOpen.Tag
        Dim orig() As Double = {1, 1, 0}
        Dim dir() As Double = {1, 1, 1}

        ufs.Modl.CreateCylinder(FeatureSigns.Nullsign, Nothing, orig, "50", _
            "25", dir, cyl_feat_tag)

        Dim cyl_body_tag As NXOpen.Tag

        ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)
        Dim cyl_body As Body = CType(NXObjectManager.Get(cyl_body_tag), Body)

        MsgBox("Color change", MsgBoxStyle.Information, "Current Operation:")
        cyl_body.Color = 3
        cyl_body.RedisplayObject()

        s.Preferences.ScreenVisualization.FitPercentage = 95
        wp.Views.WorkView.Fit()


  end Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function

End Module

第四个例子:怎样用VB.NET在UG中创建注释?

Option Strict Off  

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module create_note

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()

    Dim theNote As NXOpen.Tag
Try
    Dim workPart As Part = s.Parts.Work
    Dim workPartTag As NXOpen.Tag = workPart.Tag

Catch ex As Exception
    ufs.Ui.OpenListingWindow()
    ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())
    ufs.Ui.WriteListingWindow(vbCrLf & "+++ Work Part Required" & vbCrLf)
    Return
End Try


    Dim num_lines As Integer = 2
    Dim textString As String() = {"This is the first line.", _
                                  "This is the second line."}
    Dim origin_3d() As Double = {6, 6, 0}
    Dim orientation As Integer = 0 ' zero is Horizontal, 1 is Vertical

Try

    ufs.Drf.CreateNote(num_lines, textString, origin_3d, _
                    orientation, theNote)

Catch ex As Exception
    ufs.Ui.OpenListingWindow()
    ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())
    ufs.Ui.WriteListingWindow(vbCrLf & "+++ Note not created" & vbCrLf)
End Try

End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module

第五个例子:怎样用VB.NET在UG中创建两个体然后做布尔加操作?

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module template_code

Sub Main()

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

  '
  ' ------------------------------------------------- make sure we have a part
  Dim this_part As NXOpen.Tag
Try
  this_part = s.Parts.Work.Tag
Catch ex As Exception

  If this_part = NXOpen.Tag.Null Then
    MsgBox("You need an open part to run this program.", MsgBoxStyle.OKOnly)
    ' no part, so exit program gracefully
    Exit Sub
  End If

End Try

  '
  ' ------------------------------------------------- first solid: a block

  Dim corner_pt(2) As Double
  Dim block_feat_tag As NXOpen.Tag
  Dim edge_lengths(2) As String

  ' This is an alternate way to set the string value:
  '
  'Dim lengths(2) As Double
  'lengths(0) = 150.1234
  'edge_lengths(0) = lengths(0).ToString
  '
  ' but setting it this way, we get the expression to boot:

  edge_lengths(0) = "xlen=150.1234"
  edge_lengths(1) = "ylen=65.4321"
  edge_lengths(2) = "thickness=25."

  Dim sign As FeatureSigns

  sign = FeatureSigns.Nullsign

  corner_pt(0) = 20
  corner_pt(1) = 30
  corner_pt(2) = -10

  ufs.Modl.CreateBlock1(sign, corner_pt, edge_lengths, block_feat_tag)

  If block_feat_tag <> NXOpen.Tag.Null Then
    ufs.View.FitView(NXOpen.Tag.Null, 1.0)
    MsgBox("First Solid Body tag is: " & block_feat_tag.ToString)
  End If

  '
  ' ------------------------------------------------- second solid: a cylinder

  Dim height As String
  Dim diameter As String
  Dim direction(2) As Double
  Dim cyl_feat_tag As NXOpen.Tag

  height = "cyl_height=90"
  diameter = "cyl_dia=40"

  direction(0) = 0.707
  direction(1) = 0.707
  direction(2) = 0.707

  ufs.Modl.CreateCyl1(sign, corner_pt, height, diameter, direction, cyl_feat_tag)

  If cyl_feat_tag <> NXOpen.Tag.Null Then
    ufs.View.FitView(NXOpen.Tag.Null, 1.0)
    MsgBox("Second Solid Body tag is: " & cyl_feat_tag.ToString)
  End If

  '
  ' ------------------------------------------------- unite the two solids

  Dim block_body_tag As NXOpen.Tag
  Dim cyl_body_tag As NXOpen.Tag

  ufs.Modl.AskFeatBody(block_feat_tag, block_body_tag)
  ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)

  ufs.Modl.UniteBodies(block_body_tag, cyl_body_tag)

  '
  ' ------------------------------------------------- report count of solids

  Dim all_bodies() As Body = s.Parts.Work.Bodies.ToArray()
  Dim body_count As Integer

  body_count = all_bodies.Length

  MsgBox("Count of Bodies now in Work Part: " & body_count)

End Sub

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module

第六个例子:怎样用VB.NET在UG中选择一个体?
Option Strict Off

Imports System

Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF

Module select_a_body_demo

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()
    Dim body As NXOpen.Tag

    While select_a_body(body) = Selection.Response.Ok

        MsgBox("Body Tag:" & body.ToString())

        ufs.Disp.SetHighlight(body, 0)

    End While

    End Sub

Function select_a_body(ByRef body As NXOpen.Tag) As Selection.Response

    Dim message As String
    Dim title As String = "Select a body"
    Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
    Dim response As Integer
    Dim obj As NXOpen.Tag
    Dim view As NXOpen.Tag
    Dim cursor(2) As Double
    Dim ip As UFUi.SelInitFnT = AddressOf init_proc

    ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

    Try
        ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, _
                     Nothing, response, body, cursor, view)
    Finally
         ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
    End Try

    If response <> UFConstants.UF_UI_OBJECT_SELECTED And _
       response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
        Return Selection.Response.Cancel
    Else
        Return Selection.Response.Ok
    End If

    End Function

Function init_proc(ByVal select_ As IntPtr, _
                       ByVal userdata As IntPtr) As Integer

    Dim num_triples As Integer = 1
    Dim mask_triples(0) As UFUi.Mask
    mask_triples(0).object_type = UFConstants.UF_solid_type
    mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype
    mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY

    ufs.Ui.SetSelMask(select_, _
                       UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
                       num_triples, mask_triples)
    Return UFConstants.UF_UI_SEL_SUCCESS

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

    GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

End Function

End Module

第七个例子:怎样用VB.NET在UG中选择一个面?

Option Strict Off

Imports System

Imports NXOpen
Imports NXOpen.UI
Imports NXOpen.Utilities
Imports NXOpen.UF

Module select_a_face_demo

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()
    Dim face As NXOpen.Tag

    While select_a_face(face) = Selection.Response.Ok

        MsgBox("Face Tag:" & face.ToString())

        ufs.Disp.SetHighlight(face, 0)

    End While

    End Sub

Function select_a_face(ByRef face As NXOpen.Tag) As Selection.Response

    Dim message As String
    Dim title As String = "Select a FACE"
    Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
    Dim response As Integer
    Dim obj As NXOpen.Tag
    Dim view As NXOpen.Tag
    Dim cursor(2) As Double
    Dim mask_face As UFUi.SelInitFnT = AddressOf mask_for_faces

    ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

    Try
        ufs.Ui.SelectWithSingleDialog(message, title, scope, mask_face, _
                     Nothing, response, face, cursor, view)
    Finally
         ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
    End Try

    If response <> UFConstants.UF_UI_OBJECT_SELECTED And _
       response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
        Return Selection.Response.Cancel
    Else
        Return Selection.Response.Ok
    End If

    End Function

Function mask_for_faces(ByVal select_ As IntPtr, _
                       ByVal userdata As IntPtr) As Integer

    Dim num_triples As Integer = 1
    Dim mask_triples(0) As UFUi.Mask
    mask_triples(0).object_type = UFConstants.UF_solid_type
    mask_triples(0).object_subtype = UFConstants.UF_solid_face_subtype
    mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE

    ufs.Ui.SetSelMask(select_, _
                       UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
                       num_triples, mask_triples)
    Return UFConstants.UF_UI_SEL_SUCCESS

End Function

Public Function GetUnloadOption(ByVal dummy As String) As Integer

    GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

End Function

End Module

第八个例子:怎样用VB.NET在UG中选择曲线和边?

Option Strict Off  

Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpen.UI
Imports NXOpen.Utilities

Module select_curves_or_edges

  Dim s As Session = Session.GetSession()
  Dim ufs As UFSession = UFSession.GetUFSession()

Sub Main()

Dim curves() As NXOpen.Tag
Dim num_curves As Integer
Dim n As String = vbCrLf

    num_curves = select_curves_or_edges("Select Curves or Edges:", curves)

    If (num_curves) > 0 Then
        ufs.Ui.OpenListingWindow()
        ufs.Ui.WriteListingWindow("Selected count: " & num_curves.ToString & n)
    End If

End Sub

Function select_curves_or_edges(ByVal prompt As String, _
                                ByRef curves() As NXOpen.Tag) As Integer

    Dim cnt As Integer = 0
    Dim response As Integer
    Dim inx As Integer = 0
    Dim mask_crvs As UFUi.SelInitFnT = AddressOf mask_for_curves

    ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

    Try
        ufs.Ui.SelectWithClassDialog(prompt, "Curves:", _
            UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, _
            mask_crvs, Nothing, response, cnt, curves)
    Finally
        ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
    End Try

    For inx = 0 To curves.Length - 1
        ufs.Disp.SetHighlight(curves(inx), 0)
    Next

    Return cnt

End Function

Function mask_for_curves(ByVal select_ As IntPtr, _
                       ByVal userdata As IntPtr) As Integer

    Dim num_triples As Integer = 6
    Dim mask_triples(5) As UFUi.Mask

    mask_triples(0).object_type = UFConstants.UF_line_type
    mask_triples(0).object_subtype = 0
    mask_triples(0).solid_type = 0

    mask_triples(1).object_type = UFConstants.UF_circle_type
    mask_triples(1).object_subtype = 0
    mask_triples(1).solid_type = 0

    mask_triples(2).object_type = UFConstants.UF_conic_type
    mask_triples(2).object_subtype = 0
    mask_triples(2).solid_type = 0

    mask_triples(3).object_type = UFConstants.UF_spline_type
    mask_triples(3).object_subtype = 0
    mask_triples(3).solid_type = 0

    mask_triples(4).object_type = UFConstants.UF_point_type
    mask_triples(4).object_subtype = 0
    mask_triples(4).solid_type = 0

    mask_triples(5).object_type = UFConstants.UF_solid_type
    mask_triples(5).object_subtype = 0
    mask_triples(5).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE

    ufs.Ui.SetSelMask(select_, _
                       UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _
                       num_triples, mask_triples)

    Return UFConstants.UF_UI_SEL_SUCCESS

End Function

  Public Function GetUnloadOption(ByVal dummy As String) As Integer

      GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

  End Function

End Module



出处:https://www.cnblogs.com/yunbo/archive/2010/08/02/1790866.html


相关教程