Begtostudy(白途思)'s Professional Technology Blog

欢迎访问begtostudy的专业知识博客!主要是专业技术和算法为主。
  首页  :: 联系 :: 订阅 订阅  :: 管理

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

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

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

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

view plaincopy to clipboardprint?

  1. 第一个例子:怎样用VB.NET在UG中创建一个点?  
  2. Option Strict Off  
  3. Imports System  
  4. Imports NXOpen  
  5. Imports NXOpen.UF  
  6. Imports NXOpen.UI  
  7. Imports NXOpen.Utilities  
  8. Module CreatePoint  
  9. Dim s As Session = Session.GetSession()  
  10. Dim ufs As UFSession = UFSession.GetUFSession()  
  11. Sub Main()  
  12. Dim sp As New Point3d(0, 0, 0)  
  13. Dim thePoint As Point = s.Parts.Work.Points.CreatePoint(sp)  
  14. End Sub
  15. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  16.         GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  17. End Function
  18. End Module第二个例子:怎样用VB.NET在UG中创建一个条线?  
  19. Option Strict Off    
  20. Imports System  
  21. Imports NXOpen  
  22. Imports NXOpen.UF  
  23. Imports NXOpen.UI  
  24. Imports NXOpen.Utilities  
  25. Module template_code  
  26. Dim s As Session = Session.GetSession()  
  27. Sub Main()  
  28. Dim sp As New Point3d(0, 0, 0)  
  29. Dim ep As New Point3d(10, 10, 0)  
  30. Dim theLine As Line = s.Parts.Work.Curves.CreateLine(sp, ep)  
  31. End Sub
  32. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  33.       GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  34. End Function
  35. End Module
  36. 第三个例子:怎样用VB.NET在UG中创建一个圆柱,然后更改它的颜色?  
  37. Option Strict Off    
  38. Imports System  
  39. Imports NXOpen  
  40. Imports NXOpen.UF  
  41. Imports NXOpen.UI  
  42. Imports NXOpen.Utilities  
  43. Module create_a_cylinder_and_set_color  
  44. Sub Main()  
  45. Dim s As Session = Session.GetSession()  
  46. Dim ufs As UFSession = UFSession.GetUFSession()  
  47. Dim wp As Part = s.Parts.Work()  
  48. Dim cyl_feat_tag As NXOpen.Tag  
  49. Dim orig() As Double = {1, 1, 0}  
  50. Dim dir() As Double = {1, 1, 1}  
  51.         ufs.Modl.CreateCylinder(FeatureSigns.Nullsign, Nothing, orig, "50", _  
  52. "25", dir, cyl_feat_tag)  
  53. Dim cyl_body_tag As NXOpen.Tag  
  54.         ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)  
  55. Dim cyl_body As Body = CType(NXObjectManager.Get(cyl_body_tag), Body)  
  56.         MsgBox("Color change", MsgBoxStyle.Information, "Current Operation:")  
  57.         cyl_body.Color = 3  
  58.         cyl_body.RedisplayObject()  
  59.         s.Preferences.ScreenVisualization.FitPercentage = 95  
  60.         wp.Views.WorkView.Fit()  
  61.   end Sub
  62. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  63.         GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  64. End Function
  65. End Module
  66. 第四个例子:怎样用VB.NET在UG中创建注释?  
  67. Option Strict Off    
  68. Imports System  
  69. Imports NXOpen  
  70. Imports NXOpen.UF  
  71. Imports NXOpen.UI  
  72. Imports NXOpen.Utilities  
  73. Module create_note  
  74. Dim s As Session = Session.GetSession()  
  75. Dim ufs As UFSession = UFSession.GetUFSession()  
  76. Sub Main()  
  77. Dim theNote As NXOpen.Tag  
  78. Try
  79. Dim workPart As Part = s.Parts.Work  
  80. Dim workPartTag As NXOpen.Tag = workPart.Tag  
  81. Catch ex As Exception  
  82.     ufs.Ui.OpenListingWindow()  
  83.     ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())  
  84.     ufs.Ui.WriteListingWindow(vbCrLf & "+++ Work Part Required" & vbCrLf)  
  85. Return
  86. End Try
  87. Dim num_lines As Integer = 2  
  88. Dim textString As String() = {"This is the first line.", _  
  89. "This is the second line."}  
  90. Dim origin_3d() As Double = {6, 6, 0}  
  91. Dim orientation As Integer = 0 ' zero is Horizontal, 1 is Vertical
  92. Try
  93.     ufs.Drf.CreateNote(num_lines, textString, origin_3d, _  
  94.                     orientation, theNote)  
  95. Catch ex As Exception  
  96.     ufs.Ui.OpenListingWindow()  
  97.     ufs.Ui.WriteListingWindow(ex.GetBaseException.ToString())  
  98.     ufs.Ui.WriteListingWindow(vbCrLf & "+++ Note not created" & vbCrLf)  
  99. End Try
  100. End Sub
  101. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  102.       GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  103. End Function
  104. End Module
  105. 第五个例子:怎样用VB.NET在UG中创建两个体然后做布尔加操作?  
  106. Imports System  
  107. Imports NXOpen  
  108. Imports NXOpen.UF  
  109. Imports NXOpen.UI  
  110. Imports NXOpen.Utilities  
  111. Module template_code  
  112. Sub Main()  
  113. Dim s As Session = Session.GetSession()  
  114. Dim ufs As UFSession = UFSession.GetUFSession()  
  115. '
  116. ' ------------------------------------------------- make sure we have a part
  117. Dim this_part As NXOpen.Tag  
  118. Try
  119.   this_part = s.Parts.Work.Tag  
  120. Catch ex As Exception  
  121. If this_part = NXOpen.Tag.Null Then
  122.     MsgBox("You need an open part to run this program.", MsgBoxStyle.OKOnly)  
  123. ' no part, so exit program gracefully
  124. Exit Sub
  125. End If
  126. End Try
  127. '
  128. ' ------------------------------------------------- first solid: a block
  129. Dim corner_pt(2) As Double
  130. Dim block_feat_tag As NXOpen.Tag  
  131. Dim edge_lengths(2) As String
  132. ' This is an alternate way to set the string value:
  133. '
  134. 'Dim lengths(2) As Double
  135. 'lengths(0) = 150.1234
  136. 'edge_lengths(0) = lengths(0).ToString
  137. ' but setting it this way, we get the expression to boot:
  138.   edge_lengths(0) = "xlen=150.1234"
  139.   edge_lengths(1) = "ylen=65.4321"
  140.   edge_lengths(2) = "thickness=25."
  141. Dim sign As FeatureSigns  
  142.   sign = FeatureSigns.Nullsign  
  143.   corner_pt(0) = 20  
  144.   corner_pt(1) = 30  
  145.   corner_pt(2) = -10  
  146.   ufs.Modl.CreateBlock1(sign, corner_pt, edge_lengths, block_feat_tag)  
  147. If block_feat_tag <> NXOpen.Tag.Null Then
  148.     ufs.View.FitView(NXOpen.Tag.Null, 1.0)  
  149.     MsgBox("First Solid Body tag is: " & block_feat_tag.ToString)  
  150. End If
  151. '
  152. ' ------------------------------------------------- second solid: a cylinder
  153. Dim height As String
  154. Dim diameter As String
  155. Dim direction(2) As Double
  156. Dim cyl_feat_tag As NXOpen.Tag  
  157.   height = "cyl_height=90"
  158.   diameter = "cyl_dia=40"
  159.   direction(0) = 0.707  
  160.   direction(1) = 0.707  
  161.   direction(2) = 0.707  
  162.   ufs.Modl.CreateCyl1(sign, corner_pt, height, diameter, direction, cyl_feat_tag)  
  163. If cyl_feat_tag <> NXOpen.Tag.Null Then
  164.     ufs.View.FitView(NXOpen.Tag.Null, 1.0)  
  165.     MsgBox("Second Solid Body tag is: " & cyl_feat_tag.ToString)  
  166. End If
  167. '
  168. ' ------------------------------------------------- unite the two solids
  169. Dim block_body_tag As NXOpen.Tag  
  170. Dim cyl_body_tag As NXOpen.Tag  
  171.   ufs.Modl.AskFeatBody(block_feat_tag, block_body_tag)  
  172.   ufs.Modl.AskFeatBody(cyl_feat_tag, cyl_body_tag)  
  173.   ufs.Modl.UniteBodies(block_body_tag, cyl_body_tag)  
  174. '
  175. ' ------------------------------------------------- report count of solids
  176. Dim all_bodies() As Body = s.Parts.Work.Bodies.ToArray()  
  177. Dim body_count As Integer
  178.   body_count = all_bodies.Length  
  179.   MsgBox("Count of Bodies now in Work Part: " & body_count)  
  180. End Sub
  181. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  182.       GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  183. End Function
  184. End Module第六个例子:怎样用VB.NET在UG中选择一个体?  
  185. Option Strict Off  
  186. Imports System  
  187. Imports NXOpen  
  188. Imports NXOpen.UI  
  189. Imports NXOpen.Utilities  
  190. Imports NXOpen.UF  
  191. Module select_a_body_demo  
  192. Dim s As Session = Session.GetSession()  
  193. Dim ufs As UFSession = UFSession.GetUFSession()  
  194. Sub Main()  
  195. Dim body As NXOpen.Tag  
  196. While select_a_body(body) = Selection.Response.Ok  
  197.         MsgBox("Body Tag:" & body.ToString())  
  198.         ufs.Disp.SetHighlight(body, 0)  
  199. End While
  200. End Sub
  201. Function select_a_body(ByRef body As NXOpen.Tag) As Selection.Response  
  202. Dim message As String
  203. Dim title As String = "Select a body"
  204. Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY  
  205. Dim response As Integer
  206. Dim obj As NXOpen.Tag  
  207. Dim view As NXOpen.Tag  
  208. Dim cursor(2) As Double
  209. Dim ip As UFUi.SelInitFnT = AddressOf init_proc  
  210.     ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  211. Try
  212.         ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, _  
  213. Nothing, response, body, cursor, view)  
  214. Finally
  215.          ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  216. End Try
  217. If response <> UFConstants.UF_UI_OBJECT_SELECTED And _  
  218.        response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
  219. Return Selection.Response.Cancel  
  220. Else
  221. Return Selection.Response.Ok  
  222. End If
  223. End Function
  224. Function init_proc(ByVal select_ As IntPtr, _  
  225. ByVal userdata As IntPtr) As Integer
  226. Dim num_triples As Integer = 1  
  227. Dim mask_triples(0) As UFUi.Mask  
  228.     mask_triples(0).object_type = UFConstants.UF_solid_type  
  229.     mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype  
  230.     mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY  
  231.     ufs.Ui.SetSelMask(select_, _  
  232.                        UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _  
  233.                        num_triples, mask_triples)  
  234. Return UFConstants.UF_UI_SEL_SUCCESS  
  235. End Function
  236. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  237.     GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  238. End Function
  239. End Module
  240. 第七个例子:怎样用VB.NET在UG中选择一个面?  
  241. Option Strict Off  
  242. Imports System  
  243. Imports NXOpen  
  244. Imports NXOpen.UI  
  245. Imports NXOpen.Utilities  
  246. Imports NXOpen.UF  
  247. Module select_a_face_demo  
  248. Dim s As Session = Session.GetSession()  
  249. Dim ufs As UFSession = UFSession.GetUFSession()  
  250. Sub Main()  
  251. Dim face As NXOpen.Tag  
  252. While select_a_face(face) = Selection.Response.Ok  
  253.         MsgBox("Face Tag:" & face.ToString())  
  254.         ufs.Disp.SetHighlight(face, 0)  
  255. End While
  256. End Sub
  257. Function select_a_face(ByRef face As NXOpen.Tag) As Selection.Response  
  258. Dim message As String
  259. Dim title As String = "Select a FACE"
  260. Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY  
  261. Dim response As Integer
  262. Dim obj As NXOpen.Tag  
  263. Dim view As NXOpen.Tag  
  264. Dim cursor(2) As Double
  265. Dim mask_face As UFUi.SelInitFnT = AddressOf mask_for_faces  
  266.     ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  267. Try
  268.         ufs.Ui.SelectWithSingleDialog(message, title, scope, mask_face, _  
  269. Nothing, response, face, cursor, view)  
  270. Finally
  271.          ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  272. End Try
  273. If response <> UFConstants.UF_UI_OBJECT_SELECTED And _  
  274.        response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
  275. Return Selection.Response.Cancel  
  276. Else
  277. Return Selection.Response.Ok  
  278. End If
  279. End Function
  280. Function mask_for_faces(ByVal select_ As IntPtr, _  
  281. ByVal userdata As IntPtr) As Integer
  282. Dim num_triples As Integer = 1  
  283. Dim mask_triples(0) As UFUi.Mask  
  284.     mask_triples(0).object_type = UFConstants.UF_solid_type  
  285.     mask_triples(0).object_subtype = UFConstants.UF_solid_face_subtype  
  286.     mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_FACE  
  287.     ufs.Ui.SetSelMask(select_, _  
  288.                        UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _  
  289.                        num_triples, mask_triples)  
  290. Return UFConstants.UF_UI_SEL_SUCCESS  
  291. End Function
  292. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  293.     GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  294. End Function
  295. End Module
  296. Option Strict Off    
  297. Imports System  
  298. Imports NXOpen  
  299. Imports NXOpen.UF  
  300. Imports NXOpen.UI  
  301. Imports NXOpen.Utilities  
  302. Module select_curves_or_edges  
  303. Dim s As Session = Session.GetSession()  
  304. Dim ufs As UFSession = UFSession.GetUFSession()  
  305. Sub Main()  
  306. Dim curves() As NXOpen.Tag  
  307. Dim num_curves As Integer
  308. Dim n As String = vbCrLf  
  309.     num_curves = select_curves_or_edges("Select Curves or Edges:", curves)  
  310. If (num_curves) > 0 Then
  311.         ufs.Ui.OpenListingWindow()  
  312.         ufs.Ui.WriteListingWindow("Selected count: " & num_curves.ToString & n)  
  313. End If
  314. End Sub
  315. Function select_curves_or_edges(ByVal prompt As String, _  
  316. ByRef curves() As NXOpen.Tag) As Integer
  317. Dim cnt As Integer = 0  
  318. Dim response As Integer
  319. Dim inx As Integer = 0  
  320. Dim mask_crvs As UFUi.SelInitFnT = AddressOf mask_for_curves  
  321.     ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  322. Try
  323.         ufs.Ui.SelectWithClassDialog(prompt, "Curves:", _  
  324.             UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, _  
  325.             mask_crvs, Nothing, response, cnt, curves)  
  326. Finally
  327.         ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  328. End Try
  329. For inx = 0 To curves.Length - 1  
  330.         ufs.Disp.SetHighlight(curves(inx), 0)  
  331. Next
  332. Return cnt  
  333. End Function
  334. Function mask_for_curves(ByVal select_ As IntPtr, _  
  335. ByVal userdata As IntPtr) As Integer
  336. Dim num_triples As Integer = 6  
  337. Dim mask_triples(5) As UFUi.Mask  
  338.     mask_triples(0).object_type = UFConstants.UF_line_type  
  339.     mask_triples(0).object_subtype = 0  
  340.     mask_triples(0).solid_type = 0  
  341.     mask_triples(1).object_type = UFConstants.UF_circle_type  
  342.     mask_triples(1).object_subtype = 0  
  343.     mask_triples(1).solid_type = 0  
  344.     mask_triples(2).object_type = UFConstants.UF_conic_type  
  345.     mask_triples(2).object_subtype = 0  
  346.     mask_triples(2).solid_type = 0  
  347.     mask_triples(3).object_type = UFConstants.UF_spline_type  
  348.     mask_triples(3).object_subtype = 0  
  349.     mask_triples(3).solid_type = 0  
  350.     mask_triples(4).object_type = UFConstants.UF_point_type  
  351.     mask_triples(4).object_subtype = 0  
  352.     mask_triples(4).solid_type = 0  
  353.     mask_triples(5).object_type = UFConstants.UF_solid_type  
  354.     mask_triples(5).object_subtype = 0  
  355.     mask_triples(5).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE  
  356.     ufs.Ui.SetSelMask(select_, _  
  357.                        UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _  
  358.                        num_triples, mask_triples)  
  359. Return UFConstants.UF_UI_SEL_SUCCESS  
  360. End Function
  361. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  362.       GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  363. End Function
  364. End Module

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

view plaincopy to clipboardprint?

  1. 第八个例子:怎样用VB.NET在UG中选择曲线和边?  
  2. Option Strict Off    
  3. Imports System  
  4. Imports NXOpen  
  5. Imports NXOpen.UF  
  6. Imports NXOpen.UI  
  7. Imports NXOpen.Utilities  
  8. Module select_curves_or_edges  
  9. Dim s As Session = Session.GetSession()  
  10. Dim ufs As UFSession = UFSession.GetUFSession()  
  11. Sub Main()  
  12. Dim curves() As NXOpen.Tag  
  13. Dim num_curves As Integer
  14. Dim n As String = vbCrLf  
  15.     num_curves = select_curves_or_edges("Select Curves or Edges:", curves)  
  16. If (num_curves) > 0 Then
  17.         ufs.Ui.OpenListingWindow()  
  18.         ufs.Ui.WriteListingWindow("Selected count: " & num_curves.ToString & n)  
  19. End If
  20. End Sub
  21. Function select_curves_or_edges(ByVal prompt As String, _  
  22. ByRef curves() As NXOpen.Tag) As Integer
  23. Dim cnt As Integer = 0  
  24. Dim response As Integer
  25. Dim inx As Integer = 0  
  26. Dim mask_crvs As UFUi.SelInitFnT = AddressOf mask_for_curves  
  27.     ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  28. Try
  29.         ufs.Ui.SelectWithClassDialog(prompt, "Curves:", _  
  30.             UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY, _  
  31.             mask_crvs, Nothing, response, cnt, curves)  
  32. Finally
  33.         ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)  
  34. End Try
  35. For inx = 0 To curves.Length - 1  
  36.         ufs.Disp.SetHighlight(curves(inx), 0)  
  37. Next
  38. Return cnt  
  39. End Function
  40. Function mask_for_curves(ByVal select_ As IntPtr, _  
  41. ByVal userdata As IntPtr) As Integer
  42. Dim num_triples As Integer = 6  
  43. Dim mask_triples(5) As UFUi.Mask  
  44.     mask_triples(0).object_type = UFConstants.UF_line_type  
  45.     mask_triples(0).object_subtype = 0  
  46.     mask_triples(0).solid_type = 0  
  47.     mask_triples(1).object_type = UFConstants.UF_circle_type  
  48.     mask_triples(1).object_subtype = 0  
  49.     mask_triples(1).solid_type = 0  
  50.     mask_triples(2).object_type = UFConstants.UF_conic_type  
  51.     mask_triples(2).object_subtype = 0  
  52.     mask_triples(2).solid_type = 0  
  53.     mask_triples(3).object_type = UFConstants.UF_spline_type  
  54.     mask_triples(3).object_subtype = 0  
  55.     mask_triples(3).solid_type = 0  
  56.     mask_triples(4).object_type = UFConstants.UF_point_type  
  57.     mask_triples(4).object_subtype = 0  
  58.     mask_triples(4).solid_type = 0  
  59.     mask_triples(5).object_type = UFConstants.UF_solid_type  
  60.     mask_triples(5).object_subtype = 0  
  61.     mask_triples(5).solid_type = UFConstants.UF_UI_SEL_FEATURE_ANY_EDGE  
  62.     ufs.Ui.SetSelMask(select_, _  
  63.                        UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, _  
  64.                        num_triples, mask_triples)  
  65. Return UFConstants.UF_UI_SEL_SUCCESS  
  66. End Function
  67. Public Function GetUnloadOption(ByVal dummy As String) As Integer
  68.       GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY  
  69. End Function
  70. End Module

原文:http://blog.csdn.net/begtostudy/archive/2009/10/30/4750017.aspx

前往Begtostudy的编程知识博客(CSDN)