Creating Lines, part 1

Creating unassociative lines

 

If you need to create some unassociative lines (think old basic curves), you are in luck; there are 2 quick and easy methods to accomplish this. To create these lines you can use the curve collection's CreateLine method which has two variations: you can pass it two Point3d variables or two Point objects. A quick example of each method is below:

 

'NXJournaling
'CreateLine example
 
Option Strict Off
Imports System
Imports NXOpen
 
Module Module1
 
    Sub Main()
 
        Dim theSession As Session = Session.GetSession()
        Dim workPart As Part = theSession.Parts.Work
 
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJournaling.com")
 
        'create line given 2 point3d variables
        Dim ptStart As Point3d = New Point3d(0, 0, 0)
        Dim ptEnd As Point3d = New Point3d(-4, 5, -6)
 
        Dim line1 As Line
        line1 = workPart.Curves.CreateLine(ptStart, ptEnd)
 
 
        'create line given 2 point objects
        Dim pt1 As New Point3d(5, 5, 5)
        Dim pt2 As New Point3d(-5, -5, -5)
 
        'create point objects
        Dim ptObj1 As Point
        ptObj1 = workPart.Points.CreatePoint(pt1)
        ptObj1.SetVisibility(SmartObject.VisibilityOption.Visible)
 
        Dim ptObj2 As Point
        ptObj2 = workPart.Points.CreatePoint(pt2)
        ptObj2.SetVisibility(SmartObject.VisibilityOption.Visible)
 
        Dim line2 As Line
        line2 = workPart.Curves.CreateLine(ptObj1, ptObj2)
        line2.SetVisibility(SmartObject.VisibilityOption.Visible)
        line2.RemoveParameters()
 
    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

 

Using the Point3d version of the method is super easy, simply pass in two Point3d variables to the CreateLine method representing the start point and end point of the line - and that's it, you are done.

Using the CreateLine method that takes two Point objects is only slightly more complicated; after you create the line you will need to make it visible and remove the parameters. If you do not remove the parameters, the resulting line will remain associative to the two point objects (comment out the line of code that removes the parameters to try it out, move one or both points and the line will follow). An associative line doesn't sound so bad until you realize there is no way to edit the associativities in interactive NX, nor does it show up in the feature tree as an associative line feature would. We'll look at creating associative line features in an upcoming tutorial.

 

Finally, here is some code that uses the CreateLine method and a recursive subroutine to create some interesting fractal trees.

'NXJournaling
'a journal that creates fractal trees using the CreateLine method and recursion
 
Option Strict Off
Imports System
Imports System.Math
Imports NXOpen
 
Module Module1
 
    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim deg_to_rad As Double = Math.PI / 180
 
    Sub Main()
 
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJournaling.com")
 
        Dim depth As Integer = 9
        Dim ptStart As Point3d = New Point3d(0, 0, 0)
        DrawTree(ptStart, 90, depth)
 
    End Sub
 
    Sub DrawTree(ByVal startPoint As Point3d, ByVal angle As Double, ByVal depth As Integer)
 
        If depth = 0 Then
            Exit Sub
        End If
 
        Dim ptEnd As Point3d
        ptEnd.X = startPoint.X + Math.Cos(deg_to_rad * angle) * depth * 10
        ptEnd.Y = startPoint.Y + Math.Sin(deg_to_rad * angle) * depth * 10
        ptEnd.Z = 0
        workPart.Curves.CreateLine(startPoint, ptEnd)
        DrawTree(ptEnd, angle - 20, depth - 1)
        DrawTree(ptEnd, angle + 20, depth - 1)
 
 
    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

Comments

What is the " Public Function GetUnloadOption " for?

Carlo Tony Daristotile

The function "GetUnloadOption" is used in compiled code to specify when/if the code gets unloaded from memory. Leaving your code in memory can speed up subsequent runs, but these options only work for compiled journals; if you run an uncompiled journal it is unloaded by default when it finishes executing.

You can find a little more discussion in http://nxjournaling.com/?q=comment/157#comment-157

'NXJournaling
'a journal that creates fractal trees using the CreateLine method and recursion

Option Strict Off
Imports System
Imports System.Math
Imports NXOpen

Module Module1

Dim theSession As Session = Session.GetSession()
Dim workPart As Part = theSession.Parts.Work
Dim deg_to_rad As Double = Math.PI / 180

Sub Main()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "NXJournaling.com")

Dim depth As Integer = 10
Dim ptStart As Point3d = New Point3d(0, 0, 0)
Dim ptEnd As Point3d = New Point3d(10, 10, 0)
DrawTree(ptStart, ptEnd, depth)

End Sub

Sub DrawTree(ByVal startPoint As Point3d, ByVal endPoint As Point3d, ByVal depth As Integer)

If depth = 0 Then
Exit Sub
End If

'find End Points of 3 lines , one straight, one left, one right
Dim ptEndS As Point3d
Dim ptEndL As Point3d
Dim ptEndR As Point3d

'find End Point of straight line
ptEndS.X = endPoint.X + (endPoint.X - startPoint.X)*0.5
ptEndS.Y = endPoint.Y + (endPoint.Y - startPoint.Y)*0.5
ptEndS.Z = endPoint.Z + (endPoint.Z - startPoint.Z)*0.5

'find End Point of right line
ptEndL.X = endPoint.X - (endPoint.X - startPoint.X)*0.5
ptEndL.Y = endPoint.Y + (endPoint.Y - startPoint.Y)*0.5
ptEndL.Z = endPoint.Z + 0

'find End Point of left line
ptEndR.X = endPoint.X + (endPoint.X - startPoint.X)*0.5
ptEndR.Y = endPoint.Y - (endPoint.Y - startPoint.Y)*0.5
ptEndR.Z = endPoint.Z + 0

'draw 3 lines , one straight, one left, one right
workPart.Curves.CreateLine(endPoint , ptEndS )
workPart.Curves.CreateLine(endPoint , ptEndL )
workPart.Curves.CreateLine(endPoint , ptEndR )

'make 3 new lines , one straight, one left, one right, for each line just made
DrawTree(endPoint , ptEndS , depth-1)
DrawTree(endPoint , ptEndL , depth-1)
DrawTree(endPoint , ptEndR , depth-1)

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

Carlo Tony Daristotile

Very cool design.
Thanks for sharing.

'MAKE THE FOLLOWING CHANGE TO MY CODE ABOVE
'IT WILL LOOK LIKE A MAPLE LEAF

'find End Point of right line
ptEndL.X = endPoint.X - (endPoint.X - startPoint.X)*0.45
ptEndL.Y = endPoint.Y + (endPoint.Y - startPoint.Y)*0.45
ptEndL.Z = endPoint.Z + 0

'find End Point of left line
ptEndR.X = endPoint.X + (endPoint.X - startPoint.X)*0.45
ptEndR.Y = endPoint.Y - (endPoint.Y - startPoint.Y)*0.45
ptEndR.Z = endPoint.Z + 0

Carlo Tony Daristotile

HOW TO ADD "ARC OR LINE OR POINT" TO THE MODEL REFERENCE SET ?

Carlo Tony Daristotile

I'd suggest recording a journal while performing the operation desired. This operation should be fully supported by the journal recorder and will result in example code that you can change to your needs.

Hello,
The recorded macro has a line , but how do i guarantee this is always the reference set MODEL("PART")

Dim referenceSet1 As ReferenceSet = CType(workPart.FindObject("HANDLE R-3322"), ReferenceSet)

Carlo Tony Daristotile

To find a part's "model" reference set, have a look here:
http://www.nxjournaling.com/?q=content/model-reference-set

I am not able to delete ptObj1 and ptObj2 after the line is created. I tried the following:

        Dim line2 As Line
        line2 = workPart.Curves.CreateLine(ptObj1, ptObj2)
        line2.SetVisibility(SmartObject.VisibilityOption.Visible)
         line2.RemoveParameters()
 
	theSession.UpdateManager.AddToDeleteList(ptObj1)
	theSession.UpdateManager.AddToDeleteList(ptObj2)

and

        Dim line2 As Line
        line2 = workPart.Curves.CreateLine(ptObj1, ptObj2)
        line2.SetVisibility(SmartObject.VisibilityOption.Visible)
 
	theSession.UpdateManager.AddToDeleteList(ptObj1)
	theSession.UpdateManager.AddToDeleteList(ptObj2)

Both snippets can be run but in the NX graphic window, I can still see those two points. I ask this because I have a project wih a grid of points that are to be projected down to many faces. Only the projected points are meaningful to me. So I want to delete the original and auxillary grid points.

Don't forget to call the .DoUpdate method after adding the points to the delete list. Below is a snippet from a recorded journal.

theSession.UpdateManager.ClearErrorList()
 
Dim markId2 As Session.UndoMarkId
markId2 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete")
 
Dim objects1(0) As NXObject
Dim point1 As Point = CType(workPart.Points.FindObject("ENTITY 2 1 1"), Point)
 
objects1(0) = point1
Dim nErrs1 As Integer
nErrs1 = theSession.UpdateManager.AddToDeleteList(objects1)
 
Dim nErrs2 As Integer
nErrs2 = theSession.UpdateManager.DoUpdate(markId2)