admin 发表于 2013-11-5 17:34:33

NX二次开发源码: 装配中选点,并报告父对象


NX二次开发源码: 装配中选点,并报告父对象

<P>Option Strict Off
Imports System
Imports NXOpen
Imports NXOpen.UF

Module Module1

    Dim theSession As Session = Session.GetSession()
    Dim theUI As UI = UI.GetUI()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim lw As ListingWindow = theSession.ListingWindow
    Dim pickedPoint As Point
    Dim myModelingTolerance As Double

    Sub Main()

      theUfSession.Modl.AskDistanceTolerance(myModelingTolerance)

      Dim workPart As Part = theSession.Parts.Work
      lw.Open()

      Dim myPointTag As Tag

      If Not select_point("Select Point", myPointTag) = UFConstants.UF_UI_OK Then
            Exit Sub
      End If

      lw.WriteLine("pickedPoint: " & pickedPoint.Coordinates.ToString)

      AskParents(myPointTag)

    End Sub

    Sub AskParents(ByVal objTag As Tag)

      Dim n_parents As Integer
      Dim parentTags As Tag()
      Dim myPoint As Point
      Dim myEdge As Edge
      Dim myXform As Xform
      Dim myTaggedObject As TaggedObject
      myTaggedObject = Utilities.NXObjectManager.Get(objTag)

      If myTaggedObject.GetType.ToString.ToLower.Contains("point") Then
            myPoint = myTaggedObject
            lw.WriteLine("the tagged object: " & myTaggedObject.GetType.ToString)
            lw.WriteLine("")

            Try
                theUfSession.So.AskParents(objTag, UFConstants.UF_SO_ASK_ALL_PARENTS, n_parents, parentTags)
                lw.WriteLine("num parents: " & n_parents.ToString)
                For Each parentTag As Tag In parentTags
                  Dim parent_object As TaggedObject = Utilities.NXObjectManager.Get(parentTag)

                  AskParents(parent_object.Tag)

                  If parent_object.ToString.ToLower.Contains("edge") Then
                        myEdge = parent_object
                        lw.WriteLine("")
                        lw.WriteLine("edge type: " & myEdge.SolidEdgeType.ToString)
                        lw.WriteLine("edge length: " & myEdge.GetLength.ToString)
                        lw.WriteLine("edge owning part: " & myEdge.OwningPart.FullPath.ToString)

                        Dim partTag As Tag = myEdge.OwningPart.Tag
                        Dim occTags() As Tag
                        theUfSession.Assem.AskOccsOfPart(Tag.Null, partTag, occTags)
                        lw.WriteLine("number of occurences: " & occTags.Length.ToString)
                        For Each temp As Tag In occTags
                            Dim myComp As Assemblies.Component
                            myComp = Utilities.NXObjectManager.Get(temp)
                            Dim myCompPos As Point3d
                            Dim myCompOrientation As Matrix3x3
                            myComp.GetPosition(myCompPos, myCompOrientation)

                            lw.WriteLine("")
                            lw.WriteLine("component name: " & myComp.Name)
                            lw.WriteLine("component display name: " & myComp.DisplayName)
                            lw.WriteLine("distance from picked point: " & MeasureDistance(pickedPoint, myComp).ToString)

                            If MeasureDistance(pickedPoint, myComp) < myModelingTolerance Then
                              lw.WriteLine("** This is the component that was picked **")
                            End If

                            lw.WriteLine("")
                        Next

                        lw.WriteLine("")
                  End If
                Next

            Catch ex As Exception
                ' NXOpen.NXException: Current object is not smart
                lw.WriteLine(" Error: " + ex.Message)
                lw.WriteLine("    " & myTaggedObject.GetType.ToString)
            End Try


      End If


    End Sub

    Function select_point(ByVal cue As String, ByRef pt_tag As Tag) As Integer

      Dim base_pt As Double() = New Double(2) {}
      'Dim point_tag As NXOpen.Tag = NXOpen.Tag.Null
      Dim response As Integer = 0
      Dim base_method As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointEndPt

      theUfSession.Ui.LockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)
      theUfSession.Ui.PointConstruct(cue, base_method, pt_tag, base_pt, response)
      theUfSession.Ui.UnlockUgAccess(NXOpen.UF.UFConstants.UF_UI_FROM_CUSTOM)

      Dim pointLocation As Point3d
      pointLocation.X = base_pt(0)
      pointLocation.Y = base_pt(1)
      pointLocation.Z = base_pt(2)
      pickedPoint = theSession.Parts.Display.Points.CreatePoint(pointLocation)
      'pickedPoint.SetVisibility(SmartObject.VisibilityOption.Visible)

      Return response

    End Function

    Function SelectPointObject(ByVal prompt As String, ByRef selPoint As Point) As Selection.Response

      Dim selObj As TaggedObject
      Dim theUI As UI = UI.GetUI
      Dim title As String = "Select a Point"
      Dim includeFeatures As Boolean = False
      Dim keepHighlighted As Boolean = False
      Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
      Dim cursor As Point3d
      Dim scope As Selection.SelectionScope = Selection.SelectionScope.AnyInAssembly
      Dim selectionMask_array(0) As Selection.MaskTriple

      With selectionMask_array(0)
            .Type = UFConstants.UF_point_type
            .Subtype = UFConstants.UF_all_subtype
      End With

      Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject(prompt, _
         title, scope, selAction, _
         includeFeatures, keepHighlighted, selectionMask_array, _
         selobj, cursor)
      If resp = Selection.Response.ObjectSelected OrElse resp = Selection.Response.ObjectSelectedByName Then
            selPoint = selObj
            Return Selection.Response.Ok
      Else
            Return Selection.Response.Cancel
      End If

    End Function

    Function MeasureDistance(ByVal obj1 As DisplayableObject, ByVal obj2 As DisplayableObject) As Double

      Dim result As Double

      Try
            Dim nullNXObject As NXObject = Nothing

            Dim measureDistanceBuilder1 As MeasureDistanceBuilder
            measureDistanceBuilder1 = theSession.Parts.Display.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)

            measureDistanceBuilder1.InfoWindow = False

            measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum

            Dim nullUnit As Unit = Nothing

            Dim measureDistance1 As MeasureDistance
            measureDistance1 = theSession.Parts.Display.MeasureManager.NewDistance(nullUnit, MeasureManager.MeasureType.Minimum, obj1, obj2)

            result = measureDistance1.Value

            'measureDistance1.Information()

            measureDistance1.Dispose()

      Catch ex As NXException
            MsgBox(ex.Message)
            Return Nothing

      End Try


      Return result

    End Function


    Public Function GetUnloadOption(ByVal dummy As String) As Integer

      'Unloads the image when the NX session terminates
      GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

    End Function

End Module
</P>

admin 发表于 2013-11-5 17:35:29

直接 跑下 Journal 就可以看结果了
页: [1]
查看完整版本: NX二次开发源码: 装配中选点,并报告父对象