Clean up flat pattern for NC processing

Clean up sheet metal flat pattern

Here is a journal submitted by user hppianoman. The intended use is to clean up the sheet metal flat pattern before performing a DXF export for further processing. The journal performs the following steps:

  1. Rename the "flat-pattern" modeling view in order to remove the random number NX puts on the end.
  2. Replace the current view with the flat-pattern view as the "work" view.
  3. Select all lines and arcs of the flat-pattern and copy them to the clipboard.
  4. Replace the flat-pattern view with the "top" view as the "work view.
  5. Select all features of the model history and delete everything.
  6. Make layer 204 the working view.
  7. Paste the lines and arcs of the flat-pattern from the clipboard to the top view.
  8. Orient the WCS to the Absolute Csys.
  9. Fit to screen.


  10. The journal was written and tested on NX 7.5.


    [vbnet]'CleanUp Program to clean up CAD models in NX in order to prepare the flat pattern for DXF creation for the NC programming software.
    'This program will:
    '1. Rename the "flat-pattern" modeling view in order to remove the random number NX puts on the end.
    '2. Replace the current view with the flat-pattern view as the "work" view.
    '3. Select all lines and arcs of the flat-pattern and copy them to the clipboard.
    '4. Replace the flat-pattern view with the "top" view as the "work view.
    '5. Select all features of the model history and delete everything.
    '6. Make layer 204 the working view.
    '7. Paste the lines and arcs of the flat-pattern from the clipboard to the top view.
    '8. Orient the WCS to the Absolute Csys.
    '9. Fit to screen.

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'General imports needed at the beginning of the program

    Option Strict Off
    Imports System
    Imports NXOpen
    Imports NXOpenUI
    Imports NXOpen.Features

    Module NXJournal
    Sub Main()

    'Declarations needed at the beginning of each program to tell the program what NX session to be working in, and what part is the working part.
    Dim theSession As Session = Session.GetSession()
    Dim workPart As Part = theSession.Parts.Work

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'Declarations in order to Pause the program during testing.
    'Dim theUI As UI = UI.GetUI()
    'Dim Message As String
    'Message = "Code is good to here."

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'Rename "FLAT-PATTERN-#" Modeling view to just "FLAT-PATTERN"; without the random number on the end.
    For Each obj As NXObject In workPart.ModelingViews
    If obj.Name.Contains("FLAT-PATTERN") Then
    obj.SetName("FLAT-PATTERN")
    End If
    Next

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just finished renaming the FLAT-PATTERN, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'REPLACE THE CURRENT VIEW WITH THE FLAT-PATTERN VIEW
    Dim markID1 As Session.UndoMarkId
    markID1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Replace View")

    Dim layout1 As Layout = CType(workPart.Layouts.FindObject("L1"), Layout)
    Dim modelingView1 As ModelingView = CType(workPart.ModelingViews.FindObject("FLAT-PATTERN"), ModelingView)

    layout1.ReplaceView(workPart.ModelingViews.WorkView, modelingView1, True)

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just finished replacing the default view with the FLAT-PATTERN, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'SELECT ALL ARCS AND LINES IN FLAT PATTERN GEOMETRY IN ORDER TO COPY THEM TO THE CLIPBOARD

    Dim markID3 As Session.UndoMarkId
    markID3 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Copy")

    'Loop to remove bend tangency lines on layer 102"
    For Each obj As DisplayableObject In theSession.Parts.Work.Curves
    If TypeOf obj Is Line Then
    If obj.Layer = 102 Then
    obj.Blank()
    End If
    End If
    Next

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

    Dim copycutBuilder1 As Gateway.CopyCutBuilder
    copycutBuilder1 = workPart.ClipboardOperationsManager.CreateCopyCutBuilder()
    copycutBuilder1.CanCopyAsSketch = True
    copycutBuilder1.IsCut = False
    copycutBuilder1.ToClipboard = True
    copycutBuilder1.DestinationFilename = Nothing

    Dim flatPatternEntities() As NXObject

    For Each myFeature As Feature In workPart.Features.GetFeatures()
    If myFeature.FeatureType = "FLAT-PATTERN" Then
    Dim myFlatPattern As FlatPattern = myFeature
    'lw.WriteLine("flat pattern found, feature number: " & myFeature.GetFeatureName)
    flatPatternEntities = myFlatPattern.GetCurves
    'lw.WriteLine("Entity Count: " & flatPatternEntities.Length)
    End If
    Next
    'lw.Close()

    If flatPatternEntities.Length > 0 Then
    copycutBuilder1.SetObjects(flatPatternEntities)
    Dim nxObject1 As NXObject
    nxObject1 = copycutBuilder1.Commit()
    End If

    copycutBuilder1.Destroy()

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just finished copying to clipboard, OK?")

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'REPLACE THE "FLAT PATTERN" VIEW WITH THE "TOP" VIEW AS THE "WORK" VIEW.
    Dim markID4 As Session.UndoMarkId
    markID4 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Replace View")

    Dim layout2 As Layout = CType(workPart.Layouts.FindObject("L1"), Layout)
    Dim modelingView2 As ModelingView = CType(workPart.ModelingViews.FindObject("TOP"), ModelingView)

    layout2.ReplaceView(workPart.ModelingViews.WorkView, modelingView2, True)

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just finished replacing the FLAT-PATTERN with the TOP view, OK?")

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'DELETE ALL GEOMETRY IN ORDER TO BE ABLE TO PASTE FLAT PATTERN BACK TO SCREEN

    Dim notifyOnDelete1 As Boolean
    notifyOnDelete1 = theSession.Preferences.Modeling.NotifyOnDelete

    theSession.UpdateManager.ClearErrorList()

    Dim markID5 As Session.UndoMarkId
    markID5 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Delete")

    Dim featureLength As Integer
    featureLength = 0

    For Each obj As NXObject In theSession.Parts.Work.Sketches
    If TypeOf obj Is Sketch Then
    featureLength = featureLength + 1
    End If
    Next

    For Each obj As NXObject In theSession.Parts.Work.Features
    If TypeOf obj Is Feature Then
    featureLength = featureLength + 1
    End If
    Next

    Dim objects2(featureLength) As NXObject
    Dim featureJournalID As String
    Dim i2 As Integer
    i2 = 0

    For Each obj As NXObject In theSession.Parts.Work.Sketches
    If TypeOf obj Is Sketch Then
    featureJournalID = obj.JournalIdentifier
    objects2(i2) = CType(workPart.Sketches.FindObject(featureJournalID), Sketch)
    i2 = i2 + 1
    End If
    Next

    For Each obj As NXObject In theSession.Parts.Work.Features
    If TypeOf obj Is Feature Then
    featureJournalID = obj.JournalIdentifier
    objects2(i2) = CType(workPart.Features.FindObject(featureJournalID), Feature)
    i2 = i2 + 1
    End If
    Next

    Dim Errs1 As Integer
    Errs1 = theSession.UpdateManager.AddToDeleteList(objects2)

    Dim notifyOnDelete2 As Boolean
    notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete

    Dim Errs2 As Integer
    Errs2 = theSession.UpdateManager.DoUpdate(markID5)

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "You should have just deleted all geometry. OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'MAKE LAYER 204 THE WORKING LAYER

    Dim markID10 As Session.UndoMarkId
    markID10 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Start")

    'theSession.SetUndoMark(markID10, "Layer Settings Dialog")

    Dim stateArray1(1) As Layer.StateInfo
    stateArray1(0).Layer = 1
    stateArray1(0).State = Layer.State.Selectable
    stateArray1(1).Layer = 204
    stateArray1(1).State = Layer.State.WorkLayer
    workPart.Layers.ChangeStates(stateArray1, True)

    'theSession.SetUndoMarkName(markID10, "Layer Settings")

    theSession.DeleteUndoMark(markID10, Nothing)

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just made layer 204 the working layer, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'PASTE GEOMETRY FROM CLIPBOARD INTO TOP VIEW

    Dim markID8 As Session.UndoMarkId
    markID8 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "Paste")

    Dim pasteBuilder1 As Gateway.PasteBuilder
    pasteBuilder1 = workPart.ClipboardOperationsManager.CreatePasteBuilder()

    Dim nxObject3 As NXObject
    nxObject3 = pasteBuilder1.Commit()

    pasteBuilder1.Destroy()

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just pasted part geometry into TOP view, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'FORMAT WCS: SET WCS TO ABSOLUTE

    Dim origin1 As Point3d = New Point3d(0.0, 0.0, 0.0)
    Dim matrix1 As Matrix3x3

    matrix1.Xx = 1.0
    matrix1.Xy = 0.0
    matrix1.Xz = 0.0
    matrix1.Yx = 0.0
    matrix1.Yy = 1.0
    matrix1.Yz = 0.0
    matrix1.Zx = 0.0
    matrix1.Zy = 0.0
    matrix1.Zz = 1.0
    workPart.WCS.SetOriginAndMatrix(origin1, matrix1)

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just set the WCS to Absolute, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'FIT MODELING VIEW
    modelingView2.Fit()

    'theUI.NXMessageBox.Show("Information", NXMessageBox.DialogType.Information, "Just fit the modeling view to screen, OK?")

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    End Sub
    End Module[/vbnet]


    Thank you, hppianoman for sharing your code with us!


    If you would like to share some code that you have written, email it to: info@nxjournaling.com along with a short description of what it does and what version of NX you are using. Code submissions do not need to be entire working programs, if you have a function, subroutine, or class that you find useful - send it in!

Comments

Thanks to NXJournaling in helping with the "Copy to Clipboard" section.

hppianoman