Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Visual Basic, Place a point on a Curve 1

Status
Not open for further replies.

Twullf

Mechanical
Jan 24, 2012
196
I have tried journaling this, but the result is so convoluted and long, I can't figure what is needed and what is not. Is there a nice clean way to place a point on a curve in a 3D environment when setting the distance of the point by an expression value?

The journal looks as follows, after the little cleaning I felt was safe to do:

Code:
Option Strict Off
Imports System
Imports NXOpen

Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display

' ----------------------------------------------
'   Menu: Insert->Datum/Point->Point...
' ----------------------------------------------
Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)

Dim expression1 As Expression
expression1 = workPart.Expressions.CreateSystemExpressionWithUnits("p0_x=0.00000000000", unit1)

Dim expression2 As Expression
expression2 = workPart.Expressions.CreateSystemExpressionWithUnits("p1_y=0.00000000000", unit1)

Dim expression3 As Expression
expression3 = workPart.Expressions.CreateSystemExpressionWithUnits("p2_z=0.00000000000", unit1)

Dim expression4 As Expression
expression4 = workPart.Expressions.CreateSystemExpressionWithUnits("p3_xdelta=0.00000000000", unit1)

Dim expression5 As Expression
expression5 = workPart.Expressions.CreateSystemExpressionWithUnits("p4_ydelta=0.00000000000", unit1)

Dim expression6 As Expression
expression6 = workPart.Expressions.CreateSystemExpressionWithUnits("p5_zdelta=0.00000000000", unit1)

Dim expression7 As Expression
expression7 = workPart.Expressions.CreateSystemExpressionWithUnits("p6_radius=0.00000000000", unit1)

Dim unit2 As Unit = CType(workPart.UnitCollection.FindObject("Degrees"), Unit)

Dim expression8 As Expression
expression8 = workPart.Expressions.CreateSystemExpressionWithUnits("p7_angle=0.00000000000", unit2)

Dim expression9 As Expression
expression9 = workPart.Expressions.CreateSystemExpressionWithUnits("p8_zdelta=0.00000000000", unit1)

Dim expression10 As Expression
expression10 = workPart.Expressions.CreateSystemExpressionWithUnits("p9_radius=0.00000000000", unit1)

Dim expression11 As Expression
expression11 = workPart.Expressions.CreateSystemExpressionWithUnits("p10_angle1=0.00000000000", unit2)

Dim expression12 As Expression
expression12 = workPart.Expressions.CreateSystemExpressionWithUnits("p11_angle2=0.00000000000", unit2)

Dim expression13 As Expression
expression13 = workPart.Expressions.CreateSystemExpressionWithUnits("p12_distance=0", unit1)

Dim expression14 As Expression
expression14 = workPart.Expressions.CreateSystemExpressionWithUnits("p13_arclen=0", unit1)

Dim nullUnit As Unit = Nothing

Dim expression15 As Expression
expression15 = workPart.Expressions.CreateSystemExpressionWithUnits("p14_percent=0", nullUnit)

expression4.RightHandSide = "0"

expression5.RightHandSide = "0"

expression6.RightHandSide = "0"

expression7.RightHandSide = "0"

expression8.RightHandSide = "0"

expression9.RightHandSide = "0"

expression10.RightHandSide = "0"

expression11.RightHandSide = "0"

expression12.RightHandSide = "0"

expression13.RightHandSide = "0"

expression15.RightHandSide = "100"

theSession.SetUndoMarkName(markId1, "Point Dialog")

Dim expression16 As Expression
expression16 = workPart.Expressions.CreateSystemExpressionWithUnits("p15_x=0.00000000000", unit1)

Dim scalar1 As Scalar
scalar1 = workPart.Scalars.CreateScalarExpression(expression16, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim expression17 As Expression
expression17 = workPart.Expressions.CreateSystemExpressionWithUnits("p16_y=0.00000000000", unit1)

Dim scalar2 As Scalar
scalar2 = workPart.Scalars.CreateScalarExpression(expression17, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim expression18 As Expression
expression18 = workPart.Expressions.CreateSystemExpressionWithUnits("p17_z=0.00000000000", unit1)

Dim scalar3 As Scalar
scalar3 = workPart.Scalars.CreateScalarExpression(expression18, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim point1 As Point
point1 = workPart.Points.CreatePoint(scalar1, scalar2, scalar3, SmartObject.UpdateOption.WithinModeling)

workPart.Points.DeletePoint(point1)

Dim expression19 As Expression
expression19 = workPart.Expressions.CreateSystemExpressionWithUnits("p0=0.11", nullUnit)

Dim scalar4 As Scalar
scalar4 = workPart.Scalars.CreateScalarExpression(expression19, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim spline1 As Spline = CType(workPart.Splines.FindObject("ENTITY 9 1 1"), Spline)

Dim point2 As Point
point2 = workPart.Points.CreatePoint(spline1, scalar4, SmartObject.UpdateOption.WithinModeling)

Dim expression20 As Expression
expression20 = workPart.Expressions.CreateSystemExpressionWithUnits("p1=0", nullUnit)

workPart.Points.DeletePoint(point2)

Dim scalar5 As Scalar
scalar5 = workPart.Scalars.CreateScalarExpression(expression20, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim point3 As Point
point3 = workPart.Points.CreatePoint(spline1, scalar5, SmartObject.UpdateOption.WithinModeling)

Dim expression21 As Expression
expression21 = workPart.Expressions.CreateSystemExpressionWithUnits("p2_arclen=26535.86391015533", unit1)

Dim scalar6 As Scalar
scalar6 = workPart.Scalars.CreateScalarExpression(expression21, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim point4 As Point
point4 = workPart.Points.CreatePoint(spline1, point3, scalar6, PointCollection.AlongCurveOption.Distance, Sense.Forward, SmartObject.UpdateOption.WithinModeling)

expression1.RightHandSide = "0"

expression2.RightHandSide = "0"

expression3.RightHandSide = "0"

expression14.RightHandSide = "S3"

Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(point4)

Dim expression22 As Expression
expression22 = workPart.Expressions.CreateSystemExpressionWithUnits("p3_arclen=S3", unit1)

Dim scalar7 As Scalar
scalar7 = workPart.Scalars.CreateScalarExpression(expression22, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

Dim point5 As Point
point5 = workPart.Points.CreatePoint(spline1, point3, scalar7, PointCollection.AlongCurveOption.Distance, Sense.Forward, SmartObject.UpdateOption.WithinModeling)

Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Invisible, "Point")

theSession.DeleteUndoMark(markId2, Nothing)

theSession.SetUndoMarkName(markId1, "Point")

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression1)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression2)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression3)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression4)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression5)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression6)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression7)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression8)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression9)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression10)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression11)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression12)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression13)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression14)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression15)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try

point5.SetVisibility(SmartObject.VisibilityOption.Visible)

Dim nullFeatures_Feature As Features.Feature = Nothing


If Not workPart.Preferences.Modeling.GetHistoryMode Then
    Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim pointFeatureBuilder1 As Features.PointFeatureBuilder
pointFeatureBuilder1 = workPart.BaseFeatures.CreatePointFeatureBuilder(nullFeatures_Feature)

pointFeatureBuilder1.Point = point5

Dim nXObject1 As NXObject
nXObject1 = pointFeatureBuilder1.Commit()

pointFeatureBuilder1.Destroy()

' ----------------------------------------------
'   Menu: Tools->Journal->Stop Recording
' ----------------------------------------------

End Sub
End Module

Any help is greatly appreciated.
 
Replies continue below

Recommended for you

I was able to clean this up a great deal more, but I don't understand why some are still in the code.

Code:
Imports System
Imports NXOpen

Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display


Dim spline1 As Spline = CType(workPart.Splines.FindObject("ENTITY 9 1 1"), Spline)

Dim point3 As Point

Dim scalar7 As Scalar

Dim point5 As Point

Dim scalar5 As Scalar

Dim expression20 As Expression

Dim expression22 As Expression



expression20 = workPart.Expressions.CreateSystemExpressionWithUnits("p1=0", nullUnit)

scalar5 = workPart.Scalars.CreateScalarExpression(expression20, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

point3 = workPart.Points.CreatePoint(spline1, scalar5, SmartObject.UpdateOption.WithinModeling)


'Why is point 3 needed, what is it pointing to, could point five 
'have point3 removed and replace it with a point with a 
'definition of pt.x = 0, pt.y = 0, and pt.z=0?

expression22 = workPart.Expressions.CreateSystemExpressionWithUnits("p3_arclen=S3", unit1)

'Does setting the expressions make the point show up in the history?  
'Could you just put the "p3_arclen=S3" or just "S3" into the 
'point generator? 
'and what purpose are the scalars providing?


scalar7 = workPart.Scalars.CreateScalarExpression(expression22, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)

point5 = workPart.Points.CreatePoint(spline1, point3, scalar7, PointCollection.AlongCurveOption.Distance, Sense.Forward, SmartObject.UpdateOption.WithinModeling)



Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression20)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try



Try
  ' Expression is still in use.
  workPart.Expressions.Delete(expression22)
Catch ex As NXException
  ex.AssertErrorCode(1050029)
End Try


point5.SetVisibility(SmartObject.VisibilityOption.Visible)


Dim nullFeatures_Feature As Features.Feature = Nothing


If Not workPart.Preferences.Modeling.GetHistoryMode Then
    Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
End If

Dim pointFeatureBuilder1 As Features.PointFeatureBuilder
pointFeatureBuilder1 = workPart.BaseFeatures.CreatePointFeatureBuilder(nullFeatures_Feature)

pointFeatureBuilder1.Point = point5

Dim nXObject1 As NXObject
nXObject1 = pointFeatureBuilder1.Commit()

pointFeatureBuilder1.Destroy()


End Sub
End Module

Again thanks for helping me to understand. Sorry for replying to my own thread but I seem to only receive the "eureka moments" after I have put the previous thought out for the world to see.
 
Here is the parameter signature for the CreatePoint(...) function that your journal uses:
Code:
Public Function CreatePoint ( _
	edgeCurve As IBaseCurve, _
	[highlight]pointOffset As Point[/highlight], _
	distancePercent As Scalar, _
	option As PointCollection.AlongCurveOption, _
	sense As Sense, _
	updateOption As SmartObject.UpdateOption _
) As Point

It requires a point object (2nd parameter) as input to create your new point. Point3 in your journal appears to be one of the endpoints of your spline.

The p3_arclen expression will show up in the expression list and will be referenced by your new associative point feature.
 
I managed to get the code to work . . . once. I put it in a sub to call in a loop in which a counter is incrementally increased.

Code:
    Public Sub PlacePoint( ByRef counter as Integer ) 
       'Do the work to place the point on the line using counter

       Dim spline1 As Spline = CType(workPart.Splines.FindObject("ENTITY 9 1 1"), Spline)
       Dim point1 As Point
       Dim scalar1 As Scalar
       Dim point2 As Point
       Dim scalar2 As Scalar
       Dim expression1 As Expression
       Dim expression2 As Expression
       Dim strExp1 As String
       Dim strExp2 As String
       Dim nullFeatures_Feature As Features.Feature = Nothing
       Dim nullUnit As Unit = Nothing
       Dim unit1 As Unit = CType(workPart.UnitCollection.FindObject("MilliMeter"), Unit)

       strExp1 = "p" & counter & "=0"
       expression1 = workPart.Expressions.CreateSystemExpressionWithUnits( strExp1, nullUnit )
       scalar2 = workPart.Scalars.CreateScalarExpression(expression1, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)
       point1 = workPart.Points.CreatePoint(spline1, scalar2, SmartObject.UpdateOption.WithinModeling)
       strExp2 = "E" & counter & "_arclen=S" & counter
       expression2 = workPart.Expressions.CreateSystemExpressionWithUnits( strExp2, unit1 )
       scalar1 = workPart.Scalars.CreateScalarExpression(expression2, Scalar.DimensionalityType.None, SmartObject.UpdateOption.WithinModeling)
       point2 = workPart.Points.CreatePoint(spline1, point1, scalar1, PointCollection.AlongCurveOption.Distance, Sense.Forward, SmartObject.UpdateOption.WithinModeling)

       Try
         ' Expression is still in use.
         workPart.Expressions.Delete(expression1)
       Catch ex As NXException
         ex.AssertErrorCode(1050029)
       End Try


       Try
         ' Expression is still in use.
         workPart.Expressions.Delete(expression2)
       Catch ex As NXException
         ex.AssertErrorCode(1050029)
       End Try


       point2.SetVisibility(SmartObject.VisibilityOption.Visible)

       If Not workPart.Preferences.Modeling.GetHistoryMode Then
           Throw(New Exception("Create or edit of a Feature was recorded in History Mode but playback is in History-Free Mode."))
       End If

       Dim pointFeatureBuilder1 As Features.PointFeatureBuilder
       pointFeatureBuilder1 = workPart.BaseFeatures.CreatePointFeatureBuilder(nullFeatures_Feature)

       pointFeatureBuilder1.Point = point2

       Dim nXObject1 As NXObject
       nXObject1 = pointFeatureBuilder1.Commit()

       pointFeatureBuilder1.Destroy()


       'Name the point Point_counter
    End Sub

This code puts one point but then gives me an error saying that the expression already exists even though i am trying to change the exp name using the counter in every instance.

Can you tell what I am over looking?

Error Reads "NXOpen.NXException: The specified expression variable already exists.
at NXOpen.ExpressionCollection.CreateSystemExpressionWithUnits( String string, Unit unitType)
at NXJournal.PlacePoint(Int32_counter)
 
Found the problem, thanks for your help cowski, you are quickly becoming my hero.
 
I was just about to post and ask for the code of the loop that calls your subroutine, but glad to hear you got it worked out!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor