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!

Need Troubleshooting for NXOpen Journal for creating 3D centerlines around a selected hole in CAD.

Status
Not open for further replies.

raptr1775

Automotive
Nov 17, 2023
3
Hi,
I came up with the below code that allows you to select a point in your CAD model and create centerlines around the point. And, since I have no prior experience with coding in general, the code is not working and giving errors. Please help me fix this.

Code:
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Public Class SelectPointWithPointConstructor
    Public Shared theUfSession As UFSession
    Public Shared theSession As Session

    Public Shared Sub Main(ByVal args As String())
        theSession = Session.GetSession()
        theUfSession = UFSession.GetUFSession()
        Dim theUI As UI = UI.GetUI()

        
        Dim basePt As Point3d = New Point3d()

        While SelectPoint("Select point", basePt) = UFConstants.UF_UI_OK
            ' Create 10mm centrelines around the selected point with respect to WCS
            CreateCenterlines(basePt, 10.0)
        End While
    End Sub

    Public Shared Function SelectPoint(ByVal cue As String, ByRef basePt As Point3d) As Integer
        Dim pointTag As NXOpen.Tag = NXOpen.Tag.Null
        Dim response As Integer = 0
        Dim baseMethod As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointInferred

        theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
        theUfSession.Ui.PointConstruct(cue, baseMethod, pointTag, basePt, response)
        theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)

        Return response
    End Function

    Public Shared Sub CreateCenterlines(ByVal basePt As Point3d, ByVal length As Double)
        Dim workPart As Part = theSession.Parts.Work
        Dim startPoint() As Double
        Dim endPoint() As Double
        Dim line As Line

        ' Create horizontal centreline
        startPoint = New Double() {basePt.X - length / 2, basePt.Y, basePt.Z}
        endPoint = New Double() {basePt.X + length / 2, basePt.Y, basePt.Z}
        line = workPart.Curves.CreateLine(startPoint, endPoint)

        ' Create vertical centreline
        startPoint = New Double() {basePt.X, basePt.Y - length / 2, basePt.Z}
        endPoint = New Double() {basePt.X, basePt.Y + length / 2, basePt.Z}
        line = workPart.Curves.CreateLine(startPoint, endPoint)
        
        ' Create centreline in z axis
        startPoint = New Double() {basePt.X, basePt.Y, basePt/Z - length / 2}
        endPoint = New Double() {basePt.X, basePt.Y, basePt/Z - length / 2}
        line = workPart.Curves.CreateLine(startPoint, end Point)

        
    End Sub
End Class

The error is:

Code:
"Line 30 : Value of type 'NXOpen.Point3d' cannot be converted to '1-dimensional array of Double'.
Line 45 : Overload resolution failed because no accessible 'CreateLine' can be called with these arguments:
    'Public Function CreateLine(startPoint As NXOpen.Point, endPoint As NXOpen.Point) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point'.
    'Public Function CreateLine(startPoint As NXOpen.Point, endPoint As NXOpen.Point) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point'.
    'Public Function CreateLine(startPoint As NXOpen.Point3d, endPoint As NXOpen.Point3d) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point3d'.
    'Public Function CreateLine(startPoint As NXOpen.Point3d, endPoint As NXOpen.Point3d) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point3d'.
Line 50 : Overload resolution failed because no accessible 'CreateLine' can be called with these arguments:
    'Public Function CreateLine(startPoint As NXOpen.Point, endPoint As NXOpen.Point) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point'.
    'Public Function CreateLine(startPoint As NXOpen.Point, endPoint As NXOpen.Point) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point'.
    'Public Function CreateLine(startPoint As NXOpen.Point3d, endPoint As NXOpen.Point3d) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point3d'.
    'Public Function CreateLine(startPoint As NXOpen.Point3d, endPoint As NXOpen.Point3d) As NXOpen.Line': Value of type '1-dimensional array of Double' cannot be converted to 'NXOpen.Point3d'.
"

From my understanding, it has something to do with the incompatibility of data types between the two functions.
 
Replies continue below

Recommended for you

Try this:
Code:
Imports System
Imports NXOpen
Imports NXOpen.UF
Imports NXOpenUI

Public Class SelectPointWithPointConstructor
    Public Shared theUfSession As UFSession
    Public Shared theSession As Session

    Public Shared Sub Main(ByVal args As String())
        theSession = Session.GetSession()
        theUfSession = UFSession.GetUFSession()
        Dim theUI As UI = UI.GetUI()

        
        Dim basePt As Point3d = New Point3d()

        While SelectPoint("Select point", basePt) = UFConstants.UF_UI_OK
            ' Create 10mm centrelines around the selected point with respect to WCS
            CreateCenterlines(basePt, 10.0)
        End While
    End Sub

    Public Shared Function SelectPoint(ByVal cue As String, ByRef basePt As Point3d) As Integer
        Dim pointTag As NXOpen.Tag = NXOpen.Tag.Null
        Dim response As Integer = 0
        Dim baseMethod As UFUi.PointBaseMethod = UFUi.PointBaseMethod.PointInferred
        Dim ptCoords(2) as double

        theUfSession.Ui.LockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
        theUfSession.Ui.PointConstruct(cue, baseMethod, pointTag, ptCoords, response)
        theUfSession.Ui.UnlockUgAccess(UFConstants.UF_UI_FROM_CUSTOM)
        basePt.X = ptCoords(0)
        basePt.Y = ptCoords(1)
        basePt.Z = ptCoords(2)

        Return response
    End Function

    Public Shared Sub CreateCenterlines(ByVal basePt As Point3d, ByVal length As Double)
        Dim workPart As Part = theSession.Parts.Work
        Dim startPoint as Point3d
        Dim endPoint as Point3d
        Dim line As Line

        ' Create horizontal centreline
        startPoint.X = basePt.X - length / 2
        startPoint.Y = basePt.Y
        startPoint.Z = basePt.Z
        endPoint.X = basePt.X + length / 2
        endPoint.Y = basePt.Y
        endPoint.Z = basePt.Z
        line = workPart.Curves.CreateLine(startPoint, endPoint)

        ' Create vertical centreline
        startPoint.X = basePt.X
        startPoint.Y = basePt.Y - length / 2
        startPoint.Z = basePt.Z
        endPoint.X = basePt.X
        endPoint.Y = basePt.Y + length / 2
        endPoint.Z = basePt.Z
        line = workPart.Curves.CreateLine(startPoint, endPoint)
        
        ' Create centreline in z axis
        startPoint.X = basePt.X
        startPoint.Y = basePt.Y
        startPoint.Z = basePt.Z - length / 2
        endPoint.X = basePt.X
        endPoint.Y = basePt.Y
        endPoint.Z = basePt.Z + length / 2
        line = workPart.Curves.CreateLine(startPoint, endPoint)

    End Sub
	
End Class

www.nxjournaling.com
 
Hi Cowski,
It does work! However, the lines are not with respect to the dynamic WCS. I need the lines to sync with the dynamic WCS. Is there some command to achieve this?
Regards.
 
thread561-325985
The thread above has some functions that convert between the absolute coordinate system and the work coordinate system. In your case, I think you'll need to convert the pick point to WCS, add/subtract your XYZ values to compute the start/end point, then convert the start/end points to absolute and create the lines. Note that your resulting lines will not be associative to the WCS in any way.

www.nxjournaling.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor