Export dxf of all curves on a specific layer

Hello,
I am new to this and trying to write my first Journal.

My goal is to dxf export all of the curves on a specific layer and save the dxf to the work part folder.

I am able to record a journal and make it work, but I cant get over the "stickiness" of the curve selection.

Any help is appreciated, thanks in advance.

Mike

Imports System
Imports NXOpen

Module NXJournal
Sub Main (ByVal args() As String)

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

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: File->Export->AutoCAD DXF/DWG...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim dxfdwgCreator1 As NXOpen.DxfdwgCreator = Nothing
dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator()

dxfdwgCreator1.ExportData = NXOpen.DxfdwgCreator.ExportDataOption.Drawing

dxfdwgCreator1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2004

dxfdwgCreator1.ViewEditMode = True

dxfdwgCreator1.FlattenAssembly = True

dxfdwgCreator1.ExportScaleValue = 1.0

dxfdwgCreator1.SettingsFile = "C:\Program Files\Siemens\NX2007\DXFDWG\FabLite_dxf.def"

dxfdwgCreator1.ExportAs = NXOpen.DxfdwgCreator.ExportAsOption.ThreeD

dxfdwgCreator1.ExportSelectionBlock.SelectionScope = NXOpen.ObjectSelector.Scope.SelectedObjects

dxfdwgCreator1.ObjectTypes.Solids = True

dxfdwgCreator1.AutoCADRevision = NXOpen.DxfdwgCreator.AutoCADRevisionOptions.R2018

dxfdwgCreator1.ExportSplinesAs = NXOpen.DxfdwgCreator.ExportSplinesAsOptions.Polyline2D

dxfdwgCreator1.FlattenAssembly = False

dxfdwgCreator1.ViewEditMode = False

dxfdwgCreator1.InputFile = "Z:\PIONEER_BROACH_CO\V2-121-138-09\MASTERS\CNC\V2-121-138-09_FLAT_LASER.prt"

theSession.SetUndoMarkName(markId1, "Export AutoCAD DXF/DWG File Dialog")

' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
Dim objects1(3) As NXOpen.NXObject
Dim compositeCurve1 As NXOpen.Features.CompositeCurve = CType(workPart.Features.FindObject("LINKED_CURVE(1)"), NXOpen.Features.CompositeCurve)

Dim arc1 As NXOpen.Arc = CType(compositeCurve1.FindObject("CURVE 19 {5 (0.9137246625956,0.8089335818273,0)}"), NXOpen.Arc)

objects1(0) = arc1
Dim line1 As NXOpen.Line = CType(compositeCurve1.FindObject("CURVE 59 {3 (1.9085231756756,1.0133331099717,0)}"), NXOpen.Line)

objects1(1) = line1
Dim arc2 As NXOpen.Arc = CType(compositeCurve1.FindObject("CURVE 33 {5 (1.2937246625956,0.1789335818273,0)}"), NXOpen.Arc)

objects1(3) = arc65
Dim added1 As Boolean = Nothing
added1 = dxfdwgCreator1.ExportSelectionBlock.SelectionComp.Add(objects1)

' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
' ----------------------------------------------
' Dialog Begin Export AutoCAD DXF/DWG File
' ----------------------------------------------
Dim markId2 As NXOpen.Session.UndoMarkId = Nothing
markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Export AutoCAD DXF/DWG File")

theSession.DeleteUndoMark(markId2, Nothing)

Dim markId3 As NXOpen.Session.UndoMarkId = Nothing
markId3 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Invisible, "Export AutoCAD DXF/DWG File")

dxfdwgCreator1.OutputFile = "Y:\3D Fab Light\MIKE4.dxf"

dxfdwgCreator1.TextFontMappingFile = "C:\Users\MichaelB\AppData\Local\Temp\mich34B05B94mnbj.txt"

dxfdwgCreator1.WidthFactorMode = NXOpen.DxfdwgCreator.WidthfactorMethodOptions.AutomaticCalculation

dxfdwgCreator1.CrossHatchMappingFile = "C:\Users\MichaelB\AppData\Local\Temp\mich34B05B94mnbk.txt"

dxfdwgCreator1.LineFontMappingFile = "C:\Users\MichaelB\AppData\Local\Temp\mich34B05B94mnbl.txt"

dxfdwgCreator1.LayerMask = "81"

dxfdwgCreator1.DrawingList = ""

dxfdwgCreator1.ViewList = ""

dxfdwgCreator1.ProcessHoldFlag = True

Dim filename1 As String = Nothing
filename1 = dxfdwgCreator1.InputFile

Dim nXObject1 As NXOpen.NXObject = Nothing
nXObject1 = dxfdwgCreator1.Commit()

theSession.DeleteUndoMark(markId3, Nothing)

theSession.SetUndoMarkName(markId1, "Export AutoCAD DXF/DWG File")

dxfdwgCreator1.Destroy()

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

End Sub

End Module

Mike

If you are working in a single part file, the code below will give you a list of curve objects (the curvesOnLayer variable). Many NXOpen API functions will require an array of objects; in this case, you can use the list's .ToArray method to satisfy the function's requirements.
Function_that_needs_array_of_curves(curvesOnLayer.ToArray)

'NXJournaling.com
'September 1, 2016

'Find all curves on a given layer.

Option Strict Off
Imports System
Imports System.Collections.Generic
Imports NXOpen
Imports NXOpen.UF

Module Module1

Dim theSession As Session = Session.GetSession()
Dim theUfSession As UFSession = UFSession.GetUFSession()

Dim theUI As UI = UI.GetUI()
Dim lw As ListingWindow = theSession.ListingWindow

Sub Main()

Dim markId1 As Session.UndoMarkId
markId1 = theSession.SetUndoMark(Session.MarkVisibility.Visible, "find curves on layer")

lw.Open()

Dim curvesOnLayer As New List(Of Curve)
Dim targetLayer As Integer = 1

For Each tempCurve As Curve In theSession.Parts.Work.Curves
If tempCurve.Layer = targetLayer Then
curvesOnLayer.Add(tempCurve)
End If
Next

lw.WriteLine(curvesOnLayer.Count.ToString & " curves found on layer: " & targetLayer.ToString)

lw.Close()

End Sub

Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image immediately after execution within NX
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

End Function

End Module

If you are working in the context of an assembly and multiple components contribute to the curves you want to export, we may need a different strategy.

I think I see what is going on, but again, fairly new to this.

I ran the code you posted and it found exactly what I was looking to export. Now I need to get it to run through the remaining export function.

Can I take the code you provided from the Sub Main to the End Sub and insert it into the example that I posted? I am sure there are a few more things that need tweaked as well?

Thank you for your time and help.

Mike

Add the following line up in the "imports" section:

Imports System.Collections.Generic

Add the curve collection code in your sub Main, before the DXF importer code:

.
.
.
Sub Main (ByVal args() As String)

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

Dim displayPart As NXOpen.Part = theSession.Parts.Display

' ----------------------------------------------
' Menu: File->Export->AutoCAD DXF/DWG...
' ----------------------------------------------
Dim markId1 As NXOpen.Session.UndoMarkId = Nothing
markId1 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Start")

Dim curvesOnLayer As New List(Of Curve)
Dim targetLayer As Integer = 1

For Each tempCurve As Curve In theSession.Parts.Work.Curves
If tempCurve.Layer = targetLayer Then
curvesOnLayer.Add(tempCurve)
End If
Next

Dim dxfdwgCreator1 As NXOpen.DxfdwgCreator = Nothing
dxfdwgCreator1 = theSession.DexManager.CreateDxfdwgCreator()
.
.
.

Finally, change the export selection code to:

Dim added1 As Boolean = Nothing
added1 = dxfdwgCreator1.ExportSelectionBlock.SelectionComp.Add(curvesOnLayer.ToArray)

That should do the trick. Post back with questions if you run into trouble.

Thank you, I got it to work. A few tweaks here and there.

Mike

NXJournaling

Could you send me an email?

MIKEB3

Mike

You can reach me directly at info@nxjournaling.com. My email provider will block messages that have a source code file attached (.vb, .cs, .py, etc). I've found that if you attach ".txt" to the end of the file name it will allow those through. So, example.vb would be blocked, but example.vb.txt would be ok.

I am trying to export all curves in an assembly found on layer 3, 57 and 58 to a DXF file. Curves on layer 3 are within each component while the curves on layer 57 and 58 are in the display part (assembly) and were generated using the view/clip section command.

I am having trouble piecing together a working journal based on the example shown.

ttoreki

If some of these curves are coming from components, the curves will be occurrence objects and will not appear in the assembly file's .Curves collection. We will need another way to get these curves; I'd suggest trying the .GetAllObjectsOnLayer function. This will return all the objects on a given layer, so we'll need to filter out anything that isn't a curve. Repeat the process for each desired layer.

The GetAllObjectsOnLayer collection did the trick. Thanks for your help.

ttoreki