.net语言 开发UG NX二次开发 NX12 (第八天)

1.体底面打三孔

AskBodyFaces     通过实体的Tag得到其全部面的Tag
AskFaceData     通过面的Tag得到其中心点、类型、方向、等...
CreateSimpleHole  创建简单孔
CreateList     创建链表
AskListCount    得到链表中元素数量
PutListItem     向链表尾部添加元素
AskListItem     通过指定过元素位置得到链表中的元素
DeleteBodyParms  通过实体的Tag给其去参


  1 Imports System
  2 Imports NXOpen
  3 Imports NXOpen.UF
  4 Imports NXOpen.UF.UFConstants
  5 Module NXHole
  6     Dim theSession As NXOpen.Session = NXOpen.Session.GetSession()
  7     Dim workPart As NXOpen.Part = theSession.Parts.Work
  8     Dim ufs As UFSession = UFSession.GetUFSession()
  9     Dim Lw = theSession.ListingWindow
 10     Dim listTag As Tag() = {}
 11     Sub Main(ByVal args() As String)
 12         ' Try
 13         ' MsgBox(FindFaces())
 14         ' Catch
 15         '     MsgBox("失败")
 16         ' End Try
 17 
 18         ufs.Modl.CreateList(listTag)
 19 
 20         Dim bodyTag As NXOpen.Tag
 21         While select_a_body(bodyTag, "") = Selection.Response.Ok
 22 
 23             MsgBox(FindFaces(bodyTag))
 24 
 25             ufs.Disp.SetHighlight(bodyTag, 0)
 26         End While
 27         Dim Count_ As Integer
 28         ufs.Modl.AskListCount(listTag, Count_)
 29         If Count_ > 0 Then ufs.Modl.DeleteBodyParms(listTag)
 30     End Sub
 31     Function select_a_body(ByRef bodyTag As NXOpen.Tag, ByVal message As String) As Selection.Response
 32         Dim title As String = "选择一个实体底面打孔"
 33         Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY
 34         Dim response As Integer
 35         Dim view As NXOpen.Tag
 36         Dim cursor(2) As Double
 37         Dim ip As UFUi.SelInitFnT = AddressOf init_proc
 38         ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
 39         Try
 40             ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, Nothing, response, bodyTag, cursor, view)
 41         Finally
 42             ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
 43         End Try
 44         If response <> UFConstants.UF_UI_OBJECT_SELECTED And response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then
 45             Return Selection.Response.Cancel
 46         Else
 47             Return Selection.Response.Ok
 48         End If
 49     End Function
 50     Function init_proc(ByVal select_ As IntPtr, ByVal userdata As IntPtr) As Integer
 51         Dim num_triples As Integer = 1
 52         Dim mask_triples(0) As UFUi.Mask
 53         mask_triples(0).object_type = UFConstants.UF_solid_type
 54         mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype
 55         mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY
 56         ufs.Ui.SetSelMask(select_, UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, num_triples, mask_triples)
 57         Return UFConstants.UF_UI_SEL_SUCCESS
 58     End Function
 59     Function FindFaces(ByVal BodyTag As Tag)
 60         Dim lwstring As String = "成功"
 61 
 62         ' Lw.open()
 63 
 64         ' Dim BodyTag As Tag = 43912
 65         Dim FaceTagList As Tag() = {}
 66         Dim FaceTag As Tag = Tag.Null
 67         Dim hole_tag As Tag = Tag.Null
 68 
 69         ufs.Modl.AskBodyFaces(BodyTag, FaceTagList)
 70         Dim tmpCount As Integer = 1
 71         ufs.Modl.AskListCount(FaceTagList, tmpCount)
 72         Dim GetXYZ As Double() = {0, 0, 0}
 73         Dim i As Integer
 74         For i = 0 To tmpCount - 1
 75             ufs.Modl.AskListItem(FaceTagList, i, FaceTag)
 76             Dim tmpType As Integer
 77             Dim tmpPoint As Double() = {0, 0, 0}
 78             Dim tmpDir As Double() = {0, 0, 0}
 79             Dim tmpBox As Double() = {0, 0, 0, 0, 0, 0}
 80             Dim tmpRadius As Double
 81             Dim tmpRadData As Double
 82             Dim tmpNormDir As Integer
 83             Dim XY As Integer = 0
 84             ufs.Modl.AskFaceData(FaceTag, tmpType, tmpPoint, tmpDir, tmpBox, tmpRadius, tmpRadData, tmpNormDir)
 85             If tmpType = UF_MODL_PLANAR_FACE And tmpDir(2) = -1 Then '22 = bounded plane 条件 平面 及 向量Z向下
 86                 ufs.Obj.SetColor(FaceTag, 120)
 87                 GetXYZ(0) = tmpPoint(0)
 88                 GetXYZ(1) = tmpPoint(1)
 89                 GetXYZ(2) = tmpPoint(2)
 90 
 91                 XY = IIf(tmpBox(4) - tmpBox(1) > tmpBox(3) - tmpBox(0), 1, 0)  '(三元运算式)判断面的长边方向 
 92 
 93                 'Dim cen_point_tag As Tag = Tag.Null
 94                 ' ufs.Curve.CreatePoint(GetXYZ, cen_point_tag) '中心孔
 95                 ' Lw.WriteLine("1")
 96                 If tmpBox(4) - tmpBox(1) > 15 And tmpBox(3) - tmpBox(0) > 15 Then
 97                     ufs.Modl.PutListItem(listTag, BodyTag)
 98                     ufs.Modl.CreateSimpleHole(GetXYZ, {0, 0, 1}, "6.8", "15", "118", FaceTag, Tag.Null, hole_tag)
 99                     'ufs.Obj.SetColor(hole_tag, 211)
100 
101                     GetXYZ(XY) = GetXYZ(XY) - 12.5
102                     ufs.Modl.CreateSimpleHole(GetXYZ, {0, 0, 1}, "8", "15", "118", FaceTag, Tag.Null, hole_tag)
103                     'ufs.Obj.SetColor(hole_tag, 186)
104 
105                     GetXYZ(XY) = GetXYZ(XY) + 25
106                     ufs.Modl.CreateSimpleHole(GetXYZ, {0, 0, 1}, "8", "15", "118", FaceTag, Tag.Null, hole_tag)
107                     'ufs.Obj.SetColor(hole_tag, 186)
108                 Else
109                     lwstring = "底面太小不宜打孔"
110                 End If
111             End If
112         Next i
113         Return lwstring
114     End Function
115 End Module

 

上一篇:NX二次开发-UFUN执行三维向量加法并返回向量UF_VEC3_add


下一篇:.net语言 开发UG NX二次开发