.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
2.底面3孔更新上面的(20211021)
流程:1.选中一个实-- 2.if 出是平面的面 --3.if出有平行z轴的四个面,其向量相同--4.保存一个x轴向量最大的一组向量值--5.保存四个面的中心点x、y值--6.四个点算出长边--7. 通过长边和保存的向量值定向3孔
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 13 ufs.Modl.CreateList(listTag) 14 Dim bodyTag As NXOpen.Tag 15 While select_a_body(bodyTag, "") = Selection.Response.Ok 16 FindFaces(bodyTag) 17 ufs.Disp.SetHighlight(bodyTag, 0) 18 End While 19 Dim Count_ As Integer 20 ufs.Modl.AskListCount(listTag, Count_) 21 If Count_ > 0 Then ufs.Modl.DeleteBodyParms(listTag) 22 End Sub 23 Function select_a_body(ByRef bodyTag As NXOpen.Tag, ByVal message As String) As Selection.Response 24 Dim title As String = "选择一个实体底面打孔" 25 Dim scope As Integer = UFConstants.UF_UI_SEL_SCOPE_ANY_IN_ASSEMBLY 26 Dim response As Integer 27 Dim view As NXOpen.Tag 28 Dim cursor(2) As Double 29 Dim ip As UFUi.SelInitFnT = AddressOf init_proc 30 ufs.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 31 Try 32 ufs.Ui.SelectWithSingleDialog(message, title, scope, ip, Nothing, response, bodyTag, cursor, view) 33 Finally 34 ufs.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM) 35 End Try 36 If response <> UFConstants.UF_UI_OBJECT_SELECTED And response <> UFConstants.UF_UI_OBJECT_SELECTED_BY_NAME Then 37 Return Selection.Response.Cancel 38 Else 39 Return Selection.Response.Ok 40 End If 41 End Function 42 Function init_proc(ByVal select_ As IntPtr, ByVal userdata As IntPtr) As Integer 43 Dim num_triples As Integer = 1 44 Dim mask_triples(0) As UFUi.Mask 45 mask_triples(0).object_type = UFConstants.UF_solid_type 46 mask_triples(0).object_subtype = UFConstants.UF_solid_body_subtype 47 mask_triples(0).solid_type = UFConstants.UF_UI_SEL_FEATURE_BODY 48 ufs.Ui.SetSelMask(select_, UFUi.SelMaskAction.SelMaskClearAndEnableSpecific, num_triples, mask_triples) 49 Return UFConstants.UF_UI_SEL_SUCCESS 50 End Function 51 Function FindFaces(ByVal BodyTag As Tag) 52 Dim lwstring As String = "成功" 53 54 ' Dim BodyTag As Tag = 43912 55 Dim FaceTagList As Tag() = {} 56 Dim FaceTag As Tag = Tag.Null 57 58 ufs.Modl.AskBodyFaces(BodyTag, FaceTagList) 59 Dim tmpCount As Integer = 1 60 ufs.Modl.AskListCount(FaceTagList, tmpCount) 61 62 Dim Point_ As Double() = {0, 0, 0} 63 Dim Dir_ As Double() = {-1, 0, 0} 64 Dim FaceTag_ As Tag = Tag.null 65 Dim XY As Integer = 0 66 Dim MaxXY(3, 1) As Double 67 68 Dim LenDir As Integer = 0 69 70 Dim i As Integer 71 For i = 0 To tmpCount - 1 72 ufs.Modl.AskListItem(FaceTagList, i, FaceTag) 73 Dim tmpType As Integer 74 Dim tmpPoint As Double() = {0, 0, 0} 75 Dim tmpDir As Double() = {0, 0, 0} 76 Dim tmpBox As Double() = {0, 0, 0, 0, 0, 0} 77 Dim tmpRadius As Double 78 Dim tmpRadData As Double 79 Dim tmpNormDir As Integer 80 81 ufs.Modl.AskFaceData(FaceTag, tmpType, tmpPoint, tmpDir, tmpBox, tmpRadius, tmpRadData, tmpNormDir) 82 83 If tmpType = UF_MODL_PLANAR_FACE Then '22 = bounded plane 条件 平面 及 向量Z向下 84 If tmpDir(2) = 0 Then 85 If Math.Abs(tmpDir(0)) = Math.Abs(Dir_(0)) Or Math.Abs(tmpDir(0)) = Math.Abs(Dir_(1)) Or XY = 0 Then 86 87 If tmpDir(0) > Dir_(0) Then 88 Dir_ = tmpDir 89 End If 90 MaxXY(XY, 0) = tmpPoint(0) 91 MaxXY(XY, 1) = tmpPoint(1) 92 XY += 1 93 End If 94 End If 95 96 If tmpDir(2) = -1 Then 97 ufs.Modl.PutListItem(listTag, BodyTag) '保存体的Tag 给后面去参用 98 99 100 ufs.Obj.SetColor(FaceTag, 140) 101 FaceTag_ = FaceTag 102 Point_ = tmpPoint 103 Else 104 ' lwstring = "底面太小不宜打孔" 105 End If 106 End If 107 Next i 108 109 If XY > 3 Then 110 Dim MaxLenX = iif(Math.Abs(MaxXY(1, 0) - MaxXY(0, 0)) > Math.Abs(MaxXY(3, 0) - MaxXY(2, 0)), Math.Abs(MaxXY(1, 0) - MaxXY(0, 0)), Math.Abs(MaxXY(3, 0) - MaxXY(2, 0))) 111 Dim MaxLenY = iif(Math.Abs(MaxXY(1, 1) - MaxXY(0, 1)) > Math.Abs(MaxXY(3, 1) - MaxXY(2, 1)), Math.Abs(MaxXY(1, 1) - MaxXY(0, 1)), Math.Abs(MaxXY(3, 1) - MaxXY(2, 1))) 112 LenDir = iif(MaxLenX > MaxLenY, 1, -1) 113 CreateHole(Point_, Dir_, FaceTag_, "6.8") 114 CreateHole(GetXYZ(Point_, Dir_, 12.5, LenDir), Dir_, FaceTag_, "8") 115 CreateHole(GetXYZ(Point_, Dir_, -12.5, LenDir), Dir_, FaceTag_, "8") 116 End If 117 Return lwstring 118 End Function 119 Function CreateHole(Point_ As Double(), Dir_ As Double(), FaceTag_ As Tag, HoleSize As String) 120 Dim hole_tag As Tag = Tag.null 121 ufs.Modl.CreateSimpleHole(Point_, {0, 0, 1}, HoleSize, "15", "118", FaceTag_, Tag.Null, hole_tag) 122 End Function 123 Function GetXYZ(Point_ As Double(), Dir_ As Double(), len_ As Double, LenDir As Integer) As Double() 124 125 Dim GetXYZ1 As Double() = {0, 0, 0} 126 If LenDir = -1 And Math.Abs(Dir_(0)) <= Math.Abs(Dir_(1)) Then 127 GetXYZ1(0) = Point_(0) + len_ * Dir_(1) 128 GetXYZ1(1) = Point_(1) + len_ * Dir_(0) 129 Else 130 GetXYZ1(0) = Point_(0) + len_ * Dir_(0) 131 GetXYZ1(1) = Point_(1) + len_ * Dir_(1) 132 End If 133 GetXYZ1(2) = Point_(2) 134 Return GetXYZ1 135 End Function 136 137 End Module