.net做UG NX二次开发(VB.net) / NX Secondry Dev. with .net using VB.net Code

 用.net语言(C#、VB等)开发UG NX二次开发,一定要弄清楚NXOpen和NXOpen.UF的区别。可以看看我以前发的帖子。

 

以下内容来自与http://bbs.icax.cn/491598p1p1

真的要特别感谢名叫“苏州人”的网友贴了这么多代码。不过这些代码的也是属于NXOpen.UF的使用,不是真正的NXOpen。操作录制的才是真正的NXOpen形式(参见我以前的帖子)

不过,广告部分我就不贴了,呵呵。

 

 第一个例子:怎样用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 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

第八个例子:怎样用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

你可能感兴趣的:(.net,String,function,assembly,Integer,VB.NET)