Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

distance between two components

Status
Not open for further replies.

Rai Ruan

Mechanical
Mar 14, 2019
4
hi,

i have a journal to get distance between two components by name or select, and a list of many pairs of components in excel table

how to get name of those pairs of components from excel table to NX and put the distance back into the excel table

macro to get distance:
Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module1

    Sub Main()

        Dim theSession As Session = Session.GetSession()
        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

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

        Const undoMarkName As String = "measure to clipboard"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        Dim obj1 As DisplayableObject
        Dim obj2 As DisplayableObject

        Try
            ' TODO: Add your application code here 
            If SelectAnObject("Select first object for distance measurement", obj1) = Selection.Response.Cancel Then
                Return
            End If

            If SelectAnObject("Select second object for distance measurement", obj2) = Selection.Response.Cancel Then
                Return
            End If

            Dim nullNXObject As NXObject = Nothing

            Dim measureDistanceBuilder1 As MeasureDistanceBuilder
            measureDistanceBuilder1 = workPart.MeasureManager.CreateMeasureDistanceBuilder(nullNXObject)

            measureDistanceBuilder1.Mtype = MeasureDistanceBuilder.MeasureType.Minimum

            measureDistanceBuilder1.Object1.Value = obj1

            measureDistanceBuilder1.Object2.Value = obj2

            Dim unit1 As Unit
            If workPart.PartUnits = BasePart.Units.Inches Then
                unit1 = CType(workPart.UnitCollection.FindObject("Inch"), Unit)
            Else
                unit1 = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)
            End If


            Dim measureDistance1 As MeasureDistance
            measureDistance1 = workPart.MeasureManager.NewDistance(unit1, MeasureManager.MeasureType.Minimum, obj1, obj2)

            'measureDistance1.Information()

            System.Windows.Forms.Clipboard.SetText(measureDistance1.Value.ToString)

            measureDistance1.Dispose()

            measureDistanceBuilder1.Destroy()

        Catch ex As NXException
            theSession.UndoToMark(markId1, undoMarkName)
            MsgBox(ex.Message)

        Finally

        End Try

        lw.Close()

    End Sub

    Function SelectAnObject(prompt As String, _
           ByRef selObj As TaggedObject) As Selection.Response

        Dim theUI As UI = UI.GetUI
        Dim cursor As Point3d
        Dim typeArray() As Selection.SelectionType = _
            {Selection.SelectionType.All, _
                Selection.SelectionType.Faces, _
                Selection.SelectionType.Edges}

        Dim resp As Selection.Response = theUI.SelectionManager.SelectTaggedObject( _
                prompt, "Selection", _
                Selection.SelectionScope.AnyInAssembly, _
                False, typeArray, selObj, cursor)

        If resp = Selection.Response.ObjectSelected Or _
                resp = Selection.Response.ObjectSelectedByName Then
            Return Selection.Response.Ok
        Else
            Return Selection.Response.Cancel
        End If

    End Function

End Module

excel table:
Capture_gfkoma.jpg


result:

result_scumnz.jpg



Please help me with the problem.

Thank you very much in advance!

-------
NX11
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor