- 第一个例子:怎样用VB.NET在UG中创建一个点?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UF 5 Imports NXOpen.UI 6 Imports NXOpen.Utilities 7 Module CreatePoint 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 Sub Main() 11 Dim sp As New Point3d(0, 0, 0) 12 Dim thePoint As Point = s.Parts.Work.Points.CreatePoint(sp) 13 End Sub 14 Public Function GetUnloadOption(ByVal dummy As String) As Integer 15 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 16 End Function
2.第二个例子:怎样用VB.NET在UG中创建一个条线?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UF 5 Imports NXOpen.UI 6 Imports NXOpen.Utilities 7 Module template_code 8 Dim s As Session = Session.GetSession() 9 Sub Main() 10 Dim sp As New Point3d(0, 0, 0) 11 Dim ep As New Point3d(10, 10, 0) 12 Dim theLine As Line = s.Parts.Work.Curves.CreateLine(sp, ep) 13 End Sub 14 Public Function GetUnloadOption(ByVal dummy As String) As Integer 15 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 16 End Function 17 End Module
3.第三个例子:怎样用VB.NET在UG中创建一个圆柱,然后更改它的颜色?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UF 5 Imports NXOpen.UI 6 Imports NXOpen.Utilities 7 Module create_a_cylinder_and_set_color 8 Sub Main() 9 Dim s As Session = Session.GetSession() 10 Dim ufs As UFSession = UFSession.GetUFSession() 11 Dim wp As Part = s.Parts.Work() 12 Dim cyl_feat_tag As NXOpen.Tag 13 Dim orig() As Double = {1, 1, 0} 14 Dim dir() As Double = {1, 1, 1} 15 ufs.Modl.CreateCylinder(FeatureSigns.Nullsign, Nothing, orig, "50", _ 16 "25", dir, cyl_feat_tag) 17 Dim cyl_body_tag As NXOpen.Tag 18 ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag) 19 Dim cyl_body As Body = CType(NXObjectManager.Get(cyl_body_tag), Body) 20 MsgBox("Color change", MsgBoxStyle.Information, "Current Operation:") 21 cyl_body.Color = 3 22 cyl_body.RedisplayObject() 23 s.Preferences.ScreenVisualization.FitPercentage = 95 24 wp.Views.WorkView.Fit() 25 end Sub 26 Public Function GetUnloadOption(ByVal dummy As String) As Integer 27 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 28 End Function 29 End Module
4.第四个例子:怎样用VB.NET在UG中创建注释?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UF 5 Imports NXOpen.UI 6 Imports NXOpen.Utilities 7 Module create_note 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 Sub Main() 11 Dim theNote As NXOpen.Tag 12 Try 13 Dim workPart As Part = s.Parts.Work 14 Dim workPartTag As NXOpen.Tag = workPart.Tag 15 Catch ex As Exception 16 ufs.Ui.OpenListingWindow() 17 ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString()) 18 ufs.Ui.WriteListingWindow(vbCrLf & "+++ Work Part Required" & vbCrLf) 19 Return 20 End Try 21 Dim num_lines As Integer = 2 22 Dim textString As String() = {"This is the first line.", _ 23 "This is the second line."} 24 Dim origin_3d() As Double = {6, 6, 0} 25 Dim orientation As Integer = 0 ' zero is Horizontal, 1 is Vertical 26 Try 27 ufs.Drf.CreateNote(num_lines, textString, origin_3d, _ 28 orientation, theNote) 29 Catch ex As Exception 30 ufs.Ui.OpenListingWindow() 31 ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString()) 32 ufs.Ui.WriteListingWindow(vbCrLf & "+++ Note not created" & vbCrLf) 33 End Try 34 End Sub 35 Public Function GetUnloadOption(ByVal dummy As String) As Integer 36 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 37 End Function 38 End Module
5.第五个例子:怎样用VB.NET在UG中创建两个体然后做布尔加操作?
1 Imports System 2 Imports NXOpen 3 Imports NXOpen.UF 4 Imports NXOpen.UI 5 Imports NXOpen.Utilities 6 Module template_code 7 Sub Main() 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 ' 11 ' ------------------------------------------------- make sure we have a part 12 Dim this_part As NXOpen.Tag 13 Try 14 this_part = s.Parts.Work.Tag 15 Catch ex As Exception 16 If this_part = NXOpen.Tag.Null Then 17 MsgBox("You need an open part to run this program.", MsgBoxStyle.OKOnly) 18 ' no part, so exit program gracefully 19 Exit Sub 20 End If 21 End Try 22 ' 23 ' ------------------------------------------------- first solid: a block 24 Dim corner_pt(2) As Double 25 Dim block_feat_tag As NXOpen.Tag 26 Dim edge_lengths(2) As String 27 ' This is an alternate way to set the string value: 28 ' 29 'Dim lengths(2) As Double 30 'lengths(0) = 150.1234 31 'edge_lengths(0) = lengths(0).ToString 32 ' 33 ' but setting it this way, we get the expression to boot: 34 edge_lengths(0) = "xlen=150.1234" 35 edge_lengths(1) = "ylen=65.4321" 36 edge_lengths(2) = "thickness=25." 37 Dim sign As FeatureSigns 38 sign = FeatureSigns.Nullsign 39 corner_pt(0) = 20 40 corner_pt(1) = 30 41 corner_pt(2) = -10 42 ufs.Modl.CreateBlock1(sign, corner_pt, edge_lengths, block_feat_tag) 43 If block_feat_tag <> NXOpen.Tag.Null Then 44 ufs.View.FitView(NXOpen.Tag.Null, 1.0) 45 MsgBox("First Solid Body tag is: " & block_feat_tag.ToString) 46 End If 47 ' 48 ' ------------------------------------------------- second solid: a cylinder 49 Dim height As String 50 Dim diameter As String 51 Dim direction(2) As Double 52 Dim cyl_feat_tag As NXOpen.Tag 53 height = "cyl_height=90" 54 diameter = "cyl_dia=40" 55 direction(0) = 0.707 56 direction(1) = 0.707 57 direction(2) = 0.707 58 ufs.Modl.CreateCyl1(sign, corner_pt, height, diameter, direction, cyl_feat_tag) 59 If cyl_feat_tag <> NXOpen.Tag.Null Then 60 ufs.View.FitView(NXOpen.Tag.Null, 1.0) 61 MsgBox("Second Solid Body tag is: " & cyl_feat_tag.ToString) 62 End If 63 ' 64 ' ------------------------------------------------- unite the two solids 65 Dim block_body_tag As NXOpen.Tag 66 Dim cyl_body_tag As NXOpen.Tag 67 ufs.Modl.AskFeatBody(block_feat_tag, block_body_tag) 68 ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag) 69 ufs.Modl.UniteBodies(block_body_tag, cyl_body_tag) 70 ' 71 ' ------------------------------------------------- report count of solids 72 Dim all_bodies() As Body = s.Parts.Work.Bodies.ToArray() 73 Dim body_count As Integer 74 body_count = all_bodies.Length 75 MsgBox("Count of Bodies now in Work Part: " & body_count) 76 End Sub 77 Public Function GetUnloadOption(ByVal dummy As String) As Integer 78 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 79 End Function 80 End Module
6.第六个例子:怎样用VB.NET在UG中选择一个体?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UI 5 Imports NXOpen.Utilities 6 Imports NXOpen.UF 7 Module select_a_body_demo 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 Sub Main() 11 Dim body As NXOpen.Tag 12 While select_a_body(body) = Selection.Response.Ok 13 MsgBox("Body Tag:" & body.ToString()) 14 ufs.Disp.SetHighlight(body, 0) 15 End While 16 End Sub 17 Function select_a_body(ByRef body As NXOpen.Tag) As Selection.Response 18 Dim message As String 19 Dim title As String = "Select a body" 20 Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY 21 Dim response As Integer 22 Dim obj As NXOpen.Tag 23 Dim view As NXOpen.Tag 24 Dim cursor(2) As Double 25 Dim ip As UFUi.SelInitFnT = AddressOf init_proc 26 ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 27 Try 28 ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, _ 29 Nothing, response, body, cursor, view) 30 Finally 31 ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 32 End Try 33 If response <> UFConstants.UF_UI_OBJECT_SELECTED And _ 34 response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then 35 Return Selection.Response.Cancel 36 Else 37 Return Selection.Response.Ok 38 End If 39 End Function 40 Function init_proc(ByVal select_ As IntPtr, _ 41 ByVal userdata As IntPtr) As Integer 42 Dim num_triples As Integer = 1 43 Dim mask_triples(0) As UFUi.Mask 44 mask_triples(0).object_type = UFConstants.UF_solid_type 45 mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype 46 mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY 47 ufs.Ui.SetSelMask(select_, _ 48 UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _ 49 num_triples, mask_triples) 50 Return UFConstants.UF_UI_SEL_SUCCESS 51 End Function 52 Public Function GetUnloadOption(ByVal dummy As String) As Integer 53 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 54 End Function 55 End Module
7.第七个例子:怎样用VB.NET在UG中选择一个面?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UI 5 Imports NXOpen.Utilities 6 Imports NXOpen.UF 7 Module select_a_face_demo 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 Sub Main() 11 Dim face As NXOpen.Tag 12 While select_a_face(face) = Selection.Response.Ok 13 MsgBox("Face Tag:" & face.ToString()) 14 ufs.Disp.SetHighlight(face, 0) 15 End While 16 End Sub 17 Function select_a_face(ByRef face As NXOpen.Tag) As Selection.Response 18 Dim message As String 19 Dim title As String = "Select a FACE" 20 Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY 21 Dim response As Integer 22 Dim obj As NXOpen.Tag 23 Dim view As NXOpen.Tag 24 Dim cursor(2) As Double 25 Dim mask_face As UFUi.SelInitFnT = AddressOf mask_for_faces 26 ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 27 Try 28 ufs.Ui.SelectWithSingleDialog(message, title, scope, mask_face, _ 29 Nothing, response, face, cursor, view) 30 Finally 31 ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 32 End Try 33 If response <> UFConstants.UF_UI_OBJECT_SELECTED And _ 34 response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then 35 Return Selection.Response.Cancel 36 Else 37 Return Selection.Response.Ok 38 End If 39 End Function 40 Function mask_for_faces(ByVal select_ As IntPtr, _ 41 ByVal userdata As IntPtr) As Integer 42 Dim num_triples As Integer = 1 43 Dim mask_triples(0) As UFUi.Mask 44 mask_triples(0).object_type = UFConstants.UF_solid_type 45 mask_triples(0).object_subtype = UFConstants.UF_solid_face_subtype 46 mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE 47 ufs.Ui.SetSelMask(select_, _ 48 UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _ 49 num_triples, mask_triples) 50 Return UFConstants.UF_UI_SEL_SUCCESS 51 End Function 52 Public Function GetUnloadOption(ByVal dummy As String) As Integer 53 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 54 End Function 55 End Module 56 Option Strict Off 57 Imports System 58 Imports NXOpen 59 Imports NXOpen.UF 60 Imports NXOpen.UI 61 Imports NXOpen.Utilities 62 Module select_curves_or_edges 63 Dim s As Session = Session.GetSession() 64 Dim ufs As UFSession = UFSession.GetUFSession() 65 Sub Main() 66 Dim curves() As NXOpen.Tag 67 Dim num_curves As Integer 68 Dim n As String = vbCrLf 69 num_curves = select_curves_or_edges("Select Curves or Edges:", curves) 70 If (num_curves) > 0 Then 71 ufs.Ui.OpenListingWindow() 72 ufs.Ui.WriteListingWindow("Selected count: " & num_curves.ToString & n) 73 End If 74 End Sub 75 Function select_curves_or_edges(ByVal prompt As String, _ 76 ByRef curves() As NXOpen.Tag) As Integer 77 Dim cnt As Integer = 0 78 Dim response As Integer 79 Dim inx As Integer = 0 80 Dim mask_crvs As UFUi.SelInitFnT = AddressOf mask_for_curves 81 ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 82 Try 83 ufs.Ui.SelectWithClassDialog(prompt, "Curves:", _ 84 UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, _ 85 mask_crvs, Nothing, response, cnt, curves) 86 Finally 87 ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 88 End Try 89 For inx = 0 To curves.Length - 1 90 ufs.Disp.SetHighlight(curves(inx), 0) 91 Next 92 Return cnt 93 End Function 94 Function mask_for_curves(ByVal select_ As IntPtr, _ 95 ByVal userdata As IntPtr) As Integer 96 Dim num_triples As Integer = 6 97 Dim mask_triples(5) As UFUi.Mask 98 mask_triples(0).object_type = UFConstants.UF_line_type 99 mask_triples(0).object_subtype = 0 100 mask_triples(0).solid_type = 0 101 mask_triples(1).object_type = UFConstants.UF_circle_type 102 mask_triples(1).object_subtype = 0 103 mask_triples(1).solid_type = 0 104 mask_triples(2).object_type = UFConstants.UF_conic_type 105 mask_triples(2).object_subtype = 0 106 mask_triples(2).solid_type = 0 107 mask_triples(3).object_type = UFConstants.UF_spline_type 108 mask_triples(3).object_subtype = 0 109 mask_triples(3).solid_type = 0 110 mask_triples(4).object_type = UFConstants.UF_point_type 111 mask_triples(4).object_subtype = 0 112 mask_triples(4).solid_type = 0 113 mask_triples(5).object_type = UFConstants.UF_solid_type 114 mask_triples(5).object_subtype = 0 115 mask_triples(5).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE 116 ufs.Ui.SetSelMask(select_, _ 117 UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _ 118 num_triples, mask_triples) 119 Return UFConstants.UF_UI_SEL_SUCCESS 120 End Function 121 Public Function GetUnloadOption(ByVal dummy As String) As Integer 122 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 123 End Function 124 End Module
8.第八个例子:怎样用VB.NET在UG中选择曲线和边?
1 Option Strict Off 2 Imports System 3 Imports NXOpen 4 Imports NXOpen.UF 5 Imports NXOpen.UI 6 Imports NXOpen.Utilities 7 Module select_curves_or_edges 8 Dim s As Session = Session.GetSession() 9 Dim ufs As UFSession = UFSession.GetUFSession() 10 Sub Main() 11 Dim curves() As NXOpen.Tag 12 Dim num_curves As Integer 13 Dim n As String = vbCrLf 14 num_curves = select_curves_or_edges("Select Curves or Edges:", curves) 15 If (num_curves) > 0 Then 16 ufs.Ui.OpenListingWindow() 17 ufs.Ui.WriteListingWindow("Selected count: " & num_curves.ToString & n) 18 End If 19 End Sub 20 Function select_curves_or_edges(ByVal prompt As String, _ 21 ByRef curves() As NXOpen.Tag) As Integer 22 Dim cnt As Integer = 0 23 Dim response As Integer 24 Dim inx As Integer = 0 25 Dim mask_crvs As UFUi.SelInitFnT = AddressOf mask_for_curves 26 ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 27 Try 28 ufs.Ui.SelectWithClassDialog(prompt, "Curves:", _ 29 UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, _ 30 mask_crvs, Nothing, response, cnt, curves) 31 Finally 32 ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 33 End Try 34 For inx = 0 To curves.Length - 1 35 ufs.Disp.SetHighlight(curves(inx), 0) 36 Next 37 Return cnt 38 End Function 39 Function mask_for_curves(ByVal select_ As IntPtr, _ 40 ByVal userdata As IntPtr) As Integer 41 Dim num_triples As Integer = 6 42 Dim mask_triples(5) As UFUi.Mask 43 mask_triples(0).object_type = UFConstants.UF_line_type 44 mask_triples(0).object_subtype = 0 45 mask_triples(0).solid_type = 0 46 mask_triples(1).object_type = UFConstants.UF_circle_type 47 mask_triples(1).object_subtype = 0 48 mask_triples(1).solid_type = 0 49 mask_triples(2).object_type = UFConstants.UF_conic_type 50 mask_triples(2).object_subtype = 0 51 mask_triples(2).solid_type = 0 52 mask_triples(3).object_type = UFConstants.UF_spline_type 53 mask_triples(3).object_subtype = 0 54 mask_triples(3).solid_type = 0 55 mask_triples(4).object_type = UFConstants.UF_point_type 56 mask_triples(4).object_subtype = 0 57 mask_triples(4).solid_type = 0 58 mask_triples(5).object_type = UFConstants.UF_solid_type 59 mask_triples(5).object_subtype = 0 60 mask_triples(5).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE 61 ufs.Ui.SetSelMask(select_, _ 62 UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _ 63 num_triples, mask_triples) 64 Return UFConstants.UF_UI_SEL_SUCCESS 65 End Function 66 Public Function GetUnloadOption(ByVal dummy As String) As Integer 67 GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY 68 End Function 69 End Module
转自:.net做UG NX二次开发(VB.net) / NX Secondry Dev. with .net using VB.net Code - 白途思 - 博客园 (cnblogs.com)