First, copy the VB folder from the ugopenvs_files folder under ug5 to Microsoft Visual Studio 9.0 (I used vs2008) to overwrite the original file.
When vs is enabled, click Create to find the ug5 Visual Basic Template named project. Opening this template will automatically create an initialCodeAs follows:
------------------------------------
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
'Termination as the unload option.
'------------------------------------------------------------
Public Function getunloadoption (byval dummy as string) as integer
Getunloadoption = session. libraryunloadoption. Immediately
End Function
End Module
------------------------------------
Sub main () is the ug entry function.
In this case, right-click solution resource manager on the right side, select "myproject", add a new project, and add a Windows form component named form1.vb by default. A window class named form1 is generated.
Then, add the following code display window in the sub main process:
Dim F as new form1 'defines F as a form1 class
F. showdialog () 'show F window
In this era, code is completed. Select the resource manager, double-click the project, and select start External Department to view the ugraf.exe file.
Then debug and run. After the ug is loaded, create or open an ugpart file, press Ctrl + u to bring up the dialog box, and go to the vs project folder (under your document by default) open the bin folder in the current project folder and select the project generated by. DLL file to display a window. Of course, you can add other controls in the window and use the ugopen function to complete other functions.
Example 1: How to use VB. NET to create a vertex in 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
Example 2: How to use VB. NET to create a line in 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
Example 3: How to use VB. NET to create a cylindrical in UG and change its color?
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
Example 4: How to use VB. NET to create comments in 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
Example 5: How to use VB. NET to create two bodies in UG and then perform The burga operation?
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) = "x Len = 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
Example 6: how to use VB. NET to select an individual in 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
Example 7: how to use VB. NET to select a plane in 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
Example 8: how to use VB. NET to select curves and edges in 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 bytes 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 tables = 0 to curves. Length-1
Ufs. Disp. sethighlight (curves), 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