Point coordinates to expression

Hi All,

Just getting started in the NX/VB world but have an urgent need. Can anyone provide me with a journal (or sample code) that would allow me to verify a point and then store the XYZ coordinates as individual expressions? We need to verify a point and then use those coordinates (from WCS) as input in a PTS dialog for positioning purposes. Any help would be greatly appreciated.

What type of point are you starting with, a dumb point hanging out in space or an associative point?

Well it could be either. My preference will be to have them create associative points but that would require wave linking some geomtry and Im not sure they will like that. So right now I would say it will a dumb point placed at an arc center found on a component part.

We can read point coordinates and store the values in expressions; however, if you change the expression values, the point locations won't update (in the case of dumb points or points associated to other geometry). Is this acceptable? How many points will you need to query? What names do you want to give the expressions?

I have no experience with Product Template Studio (I assume what PTS stands for?), so I'm not entirely clear on your needs or strategy.

Yes the changing of expression values is not an issue in this case. I would say we may need to query up to 20 points. I would have to get with customer as to name of expression. For now if we could assign a WZ#(work zone number)[X,Y,Z] and a HL#(hole number)[X,Y,Z] to each selected point. That would be a start and I could alter as necessary.

Yes PTS is product template studio. In this scenario I have an assembly of a machine tool where the axis components are controlled by distance constraints. We position the machine (with the part/fixture set up on the machines table) to a hole location by entering the distances to the constraint expressions vis the pts dialog. With a model of the tool placed on the spindle we can then to mfg studies about the tool design, clearance requirements etc. The PTS gives the user a nice standard interface to move the machine around. So the journal would provide the user with a fast way to specify the hole locations using the stored expressions. Otherwise we are doing info->point, copying the coordinates and then typing into the PTS fields. This would allow the user to use the drop down and access the expressions for the XYZ locations. Hope this clarifies things a bit.

The code below may work as a starting point; it will allow you to select multiple points, assigns a name to each point, creates an X, Y, and Z expression for each point, and creates a "ptsQty" expression to reference on any subsequent runs. On subsequent runs, it will ignore any points that already have been named and will name new points based on the "ptsQty" expression value.

To see the point names (to match them up with the corresponding expressions), use Preferences -> Visualization -> Names/Borders -> Show Object Names = Work View.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

Const ptNamePrefix As String = "WZ"

Sub Main()

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work

Dim ptsPoints As New List(Of Point)

If SelectPoints("Select points", ptsPoints) = Selection.Response.Cancel Then
Exit Sub
End If

Dim ptsQtyExp As Expression
Try
ptsQtyExp = workPart.Expressions.FindObject("ptsQty")
Catch ex As NXException
If ex.ErrorCode = 3520016 Then
'not found, create it
Dim nullUnit As Unit = Nothing
ptsQtyExp = workPart.Expressions.CreateWithUnits("ptsQty=0", nullUnit)
Else
MsgBox(ex.ErrorCode & ": " & ex.Message)
Exit Sub
End If
End Try

Dim nextPtNum As Integer = ptsQtyExp.Value

For i As Integer = 0 To ptsPoints.Count - 1
nextPtNum += 1

ptsPoints.Item(i).SetName(ptNamePrefix & nextPtNum.ToString)

'create expression for each point coordinate
CreateExpression(workPart, ptNamePrefix & nextPtNum.ToString & "_x", ptsPoints.Item(i).Coordinates.X)
CreateExpression(workPart, ptNamePrefix & nextPtNum.ToString & "_y", ptsPoints.Item(i).Coordinates.Y)
CreateExpression(workPart, ptNamePrefix & nextPtNum.ToString & "_z", ptsPoints.Item(i).Coordinates.Z)

Next

'update points quantity expression
ptsQtyExp.Value = nextPtNum

End Sub

Function SelectPoints(ByVal prompt As String, ByRef pointList As List(Of Point)) As Selection.Response

Dim selObj() As TaggedObject
Dim theUI As UI = UI.GetUI
Dim title As String = "Select points"
Dim includeFeatures As Boolean = False
Dim keepHighlighted As Boolean = False
Dim selAction As Selection.SelectionAction = Selection.SelectionAction.ClearAndEnableSpecific
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.SelectTaggedObjects(prompt, _
title, scope, selAction, _
includeFeatures, keepHighlighted, selectionMask_array, _
selobj)
If resp = Selection.Response.Ok Then
For Each pt As Point In selObj
If Not pt.Name.Contains(ptNamePrefix) Then
pointList.Add(pt)
End If
Next
Return Selection.Response.Ok
Else
Return Selection.Response.Cancel
End If

End Function

Sub CreateExpression(ByVal thePart As Part, ByVal expName As String, ByVal expValue As Double)

Dim nullUnit As Unit = Nothing
thePart.Expressions.CreateWithUnits(expName & "=" & expValue.ToString, nullUnit)

End Sub

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

Sorry...on vacation last week. Great thanks...I'll take this and see how it works. Thanks again for your help on this!

jon

Thanks for this...does just what i needed. I see the point coordinates are taken from absolute. Something I will be looking into is to allow the user to select an exisiting datum csys as the reference for the point coordinates. Or at least from WCS perhaps? Any suggestions as to how to approach this requirement? Thanks again for all your help.

You can use the MapPoint functions to get from absolute to the WCS or vice-versa. There is some code posted here:
http://www.eng-tips.com/viewthread.cfm?qid=325985
That should get what you need.