Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

NX Journal - Cylinders from CSV File

alj722

Aerospace
Nov 4, 2004
39
I've been pulling what little hair I have left out trying to make an NX Journal to read a CSV file of Pt1X, Pt1Y, Pt1Z, Pt2X, Pt2Y, Pt2Z, Dia and generate a bunch of cylinders to create a trusswork. I've gotten bits and pieces to work (start point, length, diameter), but I can't figure out how to get the direction set. There seems to be a difference between vector3d and direction. I can create a vector but it seems to want a direction and can't find a way to convince it otherwise. I stole this subroutine (which used a DatumAxis and tried to adapt it to a vector without success. An Journal Gurus out there have some tips?

????Function createCyl(ByVal pt3D As Point3D, dia As Double, height As Double, datumAxis1 As DatumAxis)????
????Function createCyl(ByVal pt3D As Point3D, dia As Double, height As Double, direction1 As Direction)????
Dim nullNXOpen_Features_Feature As NXOpen.Features.Feature = Nothing
Dim cylinderBuilder1 As NXOpen.Features.CylinderBuilder
cylinderBuilder1 = workPart.Features.CreateCylinderBuilder(nullNXOpen_Features_Feature)
cylinderBuilder1.BooleanOption.Type = NXOpen.GeometricUtilities.BooleanOperation.BooleanType.Create
cylinderBuilder1.Diameter.RightHandSide = Replace(dia.ToString,",",".")
cylinderBuilder1.Height.RightHandSide = Replace(height.ToString,",",".")
Dim origin1 As NXOpen.Point3d = New NXOpen.Point3d(0.0, 0.0, 0.0)
Dim pt As Point = workPart.Points.CreatePoint(pt3D)
Dim axis1 As NXOpen.Axis
axis1 = cylinderBuilder1.Axis
axis1.Direction = direction1
axis1.Point = pt
Dim nxObject1 As NXObject
nXObject1 = cylinderBuilder1.Commit()
Dim body1 As Body = CType(workPart.Bodies.FindObject(nXObject1.journalidentifier), Body)
cylinderBuilder1.Destroy()
Return body1
End Function
 
Replies continue below

Recommended for you

Here's a quick version that uses 2 unassociative point objects; it uses the distance between the points as the height of the cylinder and the point-point vector as the direction. My first attempt was using associative point objects, but it got a bit messy as it creates 3 expressions for each point. I think the previous code could be improved upon if you need that level of associativity.

Code:
Option Strict Off
Imports System
Imports NXOpen

Module Module2

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

    Sub Main()

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        Dim lw As ListingWindow = theSession.ListingWindow
        lw.Open()

        Const undoMarkName As String = "Cylinder"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

        'values for testing, replace with code that parses the values from the CSV file
        Dim pt1 As New Point3d(0, 0, 0)
        Dim pt2 As New Point3d(1, 2, 3)
        Dim dia As Double = 0.75

        CreateCylinderPointsDia(pt1, pt2, dia)

        lw.Close()

    End Sub


    Sub CreateCylinderPointsDia(ByVal startPoint As Point3d, ByVal endPoint As Point3d, ByVal theDiameter As Double)

        'the "height" of the cylinder is calculated from the distance between the points
        Dim cylHeight As Double = Math.Sqrt((endPoint.X - startPoint.X) ^ 2 + (endPoint.Y - startPoint.Y) ^ 2 + (endPoint.Z - startPoint.Z) ^ 2)

        'Create cylinder code, mostly copied and pasted from a recorded journal
        Dim nullFeatures_Feature As Features.Feature = Nothing

        Dim cylinderBuilder1 As Features.CylinderBuilder
        cylinderBuilder1 = workPart.Features.CreateCylinderBuilder(nullFeatures_Feature)
        cylinderBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create
        cylinderBuilder1.Diameter.RightHandSide = theDiameter.ToString
        cylinderBuilder1.Height.RightHandSide = cylHeight.ToString
        Dim unit1 As NXOpen.Unit = Nothing
        unit1 = cylinderBuilder1.Height.Units

        'create points for the cylinder feature
        Dim cylStartPoint As Point = CreatePoint(startPoint, unit1)
        Dim cylEndPoint As Point = CreatePoint(endPoint, unit1)

        'Create a direction using the start and end points
        Dim direction1 As Direction
        direction1 = workPart.Directions.CreateDirection(cylStartPoint, cylEndPoint, NXOpen.SmartObject.UpdateOption.WithinModeling)

        cylinderBuilder1.Axis.Point = cylStartPoint
        cylinderBuilder1.Axis.Direction = direction1
        cylinderBuilder1.Axis.Evaluate()

        cylinderBuilder1.ParentAssociativity = True

        Dim nXObject2 As NXObject
        nXObject2 = cylinderBuilder1.Commit()

        cylinderBuilder1.Destroy()

    End Sub

    Function CreatePoint(ByVal thePointCoords As Point3d, ByVal theUnits As Unit) As Point

        'Dim expressionX As NXOpen.Expression = Nothing
        'expressionX = workPart.Expressions.CreateSystemExpressionWithUnits(thePointCoords.X.ToString, theUnits)

        'Dim scalarX As NXOpen.Scalar = Nothing
        'scalarX = workPart.Scalars.CreateScalarExpression(expressionX, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling)

        'Dim expressionY As NXOpen.Expression = Nothing
        'expressionY = workPart.Expressions.CreateSystemExpressionWithUnits(thePointCoords.Y.ToString, theUnits)

        'Dim scalarY As NXOpen.Scalar = Nothing
        'scalarY = workPart.Scalars.CreateScalarExpression(expressionY, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling)

        'Dim expressionZ As NXOpen.Expression = Nothing
        'expressionZ = workPart.Expressions.CreateSystemExpressionWithUnits(thePointCoords.Z.ToString, theUnits)

        'Dim scalarZ As NXOpen.Scalar = Nothing
        'scalarZ = workPart.Scalars.CreateScalarExpression(expressionZ, NXOpen.Scalar.DimensionalityType.None, NXOpen.SmartObject.UpdateOption.WithinModeling)

        Dim point1 As NXOpen.Point = Nothing
        'point1 = workPart.Points.CreatePoint(scalarX, scalarY, scalarZ, NXOpen.SmartObject.UpdateOption.WithinModeling)

        'create non-associative point for now
        point1 = workPart.Points.CreatePoint(thePointCoords)

        point1.RemoveViewDependency()
        point1.SetVisibility(SmartObject.VisibilityOption.Visible)

        Return point1

    End Function

End Module
 
Unbelievable Cowski! You have bailed me out again! Thank you!
 
Unbelievable Cowski! You have bailed me out again! Thank you!
Here is the final code. A bit of a kludge but it works. Reads a CSV file with 7 fields (Pt1X, Pt1Y, Pt1Z, Pt2X, Pt2Y, Pt2Z, Dia) and creates cylinders. Non-Timestamp Points can be simply deleted after creation.

Cylindrical Beams.jpg

Code:
'Adapted from NXJournaling.com
' http://www.nxjournaling.com/?q=content/read-text-file
'http://www.nxjournaling.com/?q=content/point3d-and-point-objects
'https://www.nxjournaling.com/content/creating-lines-part-1
'https://www.eng-tips.com/threads/nx-journal-cylinders-from-csv-file.526751/
'Comma separated variable file should be structured without a header
'Each line of the input file should be 7 numbers separated by commas (Pt1X, Pt1Y, Pt1Z, Pt2X, Pt2Y, Pt2Z, Dia)
'The numbers will be interpreted as line start points, end points, Diameter
 
Option Strict Off 
Imports System 
Imports System.IO 
Imports System.Windows.Forms 
Imports NXOpen 
Imports NXOpen.UF
Imports NXOpen.Assemblies

 
Module Module1 
     Dim theSession As Session = Session.GetSession()
     Dim workPart As Part = theSession.Parts.Work
     Dim ufs As UFSession = UFSession.GetUFSession

    Sub Main() 

        If IsNothing(theSession.Parts.Work) Then
            'active part required
            Return
        End If

        Const undoMarkName As String = "Cylinder"
        Dim markId1 As Session.UndoMarkId
        markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, undoMarkName)

            Dim openFileDialog1 As New OpenFileDialog() 
 
          openFileDialog1.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*"
         openFileDialog1.FilterIndex = 1
         openFileDialog1.RestoreDirectory = True
 
         If openFileDialog1.ShowDialog() = DialogResult.OK Then
              Dim theSession As Session = Session.GetSession()
               Dim workPart As Part = theSession.Parts.Work
               Dim displayPart As Part = theSession.Parts.Display
             Dim lw As ListingWindow = theSession.ListingWindow
             Dim line As String
             Dim endPoint As Point3d
             Dim startPoint as Point3d
             Dim i As Integer = 0
             Dim firstPass as Boolean = True
             Dim delim As Char() = {","c}
             Dim USculture As system.globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
 
            Dim Dia as double
            Dim line1 As Line
 
             Using sr As StreamReader = New StreamReader(openFileDialog1.FileName)
             Try
                 line = sr.ReadLine()
                 While Not line Is Nothing
                     Dim strings As String() = line.Split(delim)
                     endPoint.x = Double.Parse(strings(0), USculture)
                     endPoint.y = Double.Parse(strings(1), USCulture)
                     endPoint.z = Double.Parse(strings(2), USCulture)
                     endPoint = Abs2WCS(endPoint)
                     startPoint.x = Double.Parse(strings(3), USculture)
                     startPoint.y = Double.Parse(strings(4), USCulture)
                     startPoint.z = Double.Parse(strings(5), USCulture)
                     Dia = Double.Parse(strings(6), USCulture)
                     startPoint = Abs2WCS(startPoint)
                     lw.WriteLine("Creating Line: " & startPoint.ToString & " to" & endPoint.ToString)
                     CreateCylinderPointsDia(startPoint, endPoint, Dia)
                     line = sr.ReadLine()
                 End While
             Catch E As Exception
                 MessageBox.Show(E.Message)
             End Try
             End Using
         End If
     End Sub
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'Date:  11/18/2010
'Subject:  Sample NX Open .NET Visual Basic routine : map point from absolute to wcs
'
'Note:  function taken from GTAC example code
 
    Function Abs2WCS(ByVal inPt As Point3d) As Point3d 
        Dim pt1(2), pt2(2) As Double 
 
        pt1(0) = inPt.X 
        pt1(1) = inPt.Y 
        pt1(2) = inPt.Z 
 
        ufs.Csys.MapPoint(UFConstants.UF_CSYS_ROOT_COORDS, pt1, _ 
            UFConstants.UF_CSYS_ROOT_WCS_COORDS, pt2) 
 
        Abs2WCS.X = pt2(0) 
        Abs2WCS.Y = pt2(1) 
        Abs2WCS.Z = pt2(2) 
 
    End Function   
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'From https://www.eng-tips.com/threads/nx-journal-cylinders-from-csv-file.526751/
  Sub CreateCylinderPointsDia(ByVal startPoint As Point3d, ByVal endPoint As Point3d, ByVal theDiameter As Double)

        'the "height" of the cylinder is calculated from the distance between the points
        Dim cylHeight As Double = Math.Sqrt((endPoint.X - startPoint.X) ^ 2 + (endPoint.Y - startPoint.Y) ^ 2 + (endPoint.Z - startPoint.Z) ^ 2)

        'Create cylinder code, mostly copied and pasted from a recorded journal
        Dim nullFeatures_Feature As Features.Feature = Nothing

        Dim cylinderBuilder1 As Features.CylinderBuilder
        cylinderBuilder1 = workPart.Features.CreateCylinderBuilder(nullFeatures_Feature)
        cylinderBuilder1.BooleanOption.Type = GeometricUtilities.BooleanOperation.BooleanType.Create
        cylinderBuilder1.Diameter.RightHandSide = theDiameter.ToString
        cylinderBuilder1.Height.RightHandSide = cylHeight.ToString
        Dim unit1 As NXOpen.Unit = Nothing
        unit1 = cylinderBuilder1.Height.Units

        'create points for the cylinder feature
        Dim cylStartPoint As Point = CreatePoint(startPoint, unit1)
        Dim cylEndPoint As Point = CreatePoint(endPoint, unit1)

        'Create a direction using the start and end points
        Dim direction1 As Direction
        direction1 = workPart.Directions.CreateDirection(cylStartPoint, cylEndPoint, NXOpen.SmartObject.UpdateOption.WithinModeling)

        cylinderBuilder1.Axis.Point = cylStartPoint
        cylinderBuilder1.Axis.Direction = direction1
        cylinderBuilder1.Axis.Evaluate()

        cylinderBuilder1.ParentAssociativity = True

        Dim nXObject2 As NXObject
        nXObject2 = cylinderBuilder1.Commit()

        cylinderBuilder1.Destroy()

    End Sub
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    Function CreatePoint(ByVal thePointCoords As Point3d, ByVal theUnits As Unit) As Point


        Dim point1 As NXOpen.Point = Nothing

        'create non-associative point (these can be deleted later)
        point1 = workPart.Points.CreatePoint(thePointCoords)

        point1.RemoveViewDependency()
        point1.SetVisibility(SmartObject.VisibilityOption.Visible)

        Return point1

    End Function
'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    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
 

Part and Inventory Search

Sponsor