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