Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

DXF journal export

Status
Not open for further replies.

Lars1978

Mechanical
Dec 30, 2015
327
0
0
NL
Hi All,

I've a journal witch exports a pdf and dxf file out of a draft containing a sheet metal part with a flat pattern view. The dxf file is de flat pattern view exported.

This journals worked like a charm until I think the holes are added differently into the flat pattern.

Does anybody (cowski) have the knowledge to adjust this journal?

a example file (moden and draft) are added for testing [2thumbsup]

Kind regards,

Lars





Lars
NX native (NX13 or NX1847)
Solid Edge ST10
Inventor
Solidworks
 
 https://files.engineering.com/getfile.aspx?folder=4d1ffb06-7aaf-4b10-ae80-b7e9a750d514&file=Journal.zip
Replies continue below

Recommended for you

The model supplied is not the one used in the supplied drawing, so I wasn't able to test with the drawing.

When I ran the code on the model, I got an "object not set" error, which I corrected with the code at the end of the post. The circular cutout was not exported to the DXF file. Since the journal uses the "export flat pattern" command, I started testing in NX without using the journal. I couldn't get the circle to export no matter what options I specified in the export settings. I'd suggest that you contact GTAC to see why the cutout curve won't export when using the "export flat pattern" command.

However, if you use "export dxf" and specify the flat pattern view, it will export the outer curves, the circular cutout, and the annotations. You could get more control by specifying layers to export (to exclude the annotations, for example). Perhaps this would be a good work-around until GTAC can figure out why the cutout isn't exporting...

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

Module Module4

    Dim theSession As Session = Session.GetSession()
    Dim theUfSession As UFSession = UFSession.GetUFSession()
    Dim workPart As Part = theSession.Parts.Work
    Dim lw As ListingWindow = theSession.ListingWindow

    Sub Main()

        lw.Open()

        Dim currentPath As String = GetFilePath()
        Dim currentFile As String = GetFileName()

        Dim pdfFile As String = GetFilePath() & GetFileName() & ".pdf"
        Dim dxfFile As String = GetFilePath() & GetFileName() & ".dxf"

        Dim partUnits As Integer = theSession.Parts.Display.PartUnits
        '0 = inch
        '1 = metric

        Dim MyTimeStamp As String
        Dim Filename As String = workPart.name
        Dim x_order_nr As String
        Dim y_rev_nr As String
        Dim Z_title As String
        Dim File_string() As String


        MyTimeStamp = System.DateTime.Now.ToString("dd-MM-yyyy")
        'MyTimeStamp= System.DateTime.Now.Tostring("yyyy-MM-dd")

        File_string = Filename.Split("_")

        x_order_nr = File_string(0)
        y_rev_nr = File_string(1)
        Z_title = File_string(2)

        workPart.SetUserAttribute("MyTimeStamp", -1, MyTimeStamp, Update.option.now)
        workPart.SetUserAttribute("x_order_nr", -1, x_order_nr, Update.option.now)
        workPart.SetUserAttribute("y_rev_nr", -1, y_rev_nr, Update.option.now)
        workPart.SetUserAttribute("z_title", -1, Z_title, Update.option.now)



        Dim i As Integer = 0
        For Each sheet As Drawings.DrawingSheet In theSession.Parts.Display.DrawingSheets
            'msgbox (sheet.name)
            i = i + 1

            'the pdf export uses 'append file', if we are on sheet 1 make sure the user wants to overwrite
            'if the drawing is multisheet, don't ask on subsequent sheets
            If i = 1 Then
                If IO.File.Exists(pdfFile) Then
                    Dim rspFileExists As System.Windows.Forms.DialogResult
                    rspFileExists = System.Windows.Forms.MessageBox.Show("The file: '" & pdfFile & "' already exists; overwrite?", "Overwrite file?", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question)
                    If rspFileExists = System.Windows.Forms.DialogResult.Yes Then
                        Try
                            IO.File.Delete(pdfFile)
                        Catch ex As Exception
                            System.Windows.Forms.MessageBox.Show(ex.Message & vbCrLf & "Journal exiting", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                            Exit Sub
                        End Try
                    Else
                        'user chose not to overwrite file
                        'msgbox("journal exiting", vbokonly)
                        Exit Sub
                    End If
                End If
            End If

            Try
                ExportPDF(sheet, pdfFile, partUnits)
            Catch ex As Exception
                MsgBox("Error occurred in PDF export" & vbCrLf & ex.Message & vbCrLf & "journal exiting", vbCritical + vbOKOnly)
                Exit Sub
            End Try

        Next

        If i = 0 Then
            MsgBox("This part has no drawing sheets to export")
        Else
            MsgBox("Exported: " & i & " sheet(s) to pdf file" & vbCrLf & pdfFile, vbOKOnly + vbInformation)
        End If



        Dim myFlatPattern As Features.FlatPattern = Nothing

        Dim c As Assemblies.ComponentAssembly = theSession.Parts.Display.ComponentAssembly
        If IsNothing(c.RootComponent) Then
            'Part has no components, look for flat pattern in this part
            lw.WriteLine("part has no components, looking for flat pattern in this file")
            myFlatPattern = GetFlatPattern(theSession.Parts.Display)
        Else

            Dim realComponents As New List(Of Assemblies.Component)
            If c.RootComponent.GetChildren.Length > 1 Then
                'this is an assembly, not a drawing file
                'lw.WriteLine("part is not a drawing (it has more than one component), exiting")
                'Exit Sub

                'look for a single (non-drafting) component
                For Each temp As Assemblies.Component In c.RootComponent.GetChildren
                    Dim isDraftComp As Boolean = False
                    theUfSession.Draw.IsDraftingComponent(temp.Tag, isDraftComp)
                    If Not isDraftComp Then
                        realComponents.Add(temp)
                    End If
                Next

                If realComponents.Count > 1 Then
                    lw.WriteLine("part is an assembly, exiting")
                    Exit Sub
                End If
            End If

            'lw.WriteLine("display part has one component")
            'lw.WriteLine("display part has " & c.RootComponent.GetChildren(0).GetChildren.Length.ToString & " grandchildren")
            If c.RootComponent.GetChildren(0).GetChildren.Length <> 0 Then
                'child component is a subassembly
                Exit Sub
            End If

            If Not LoadComponent(realComponents.Item(0)) Then
                'component not fully loaded, will not be able to access flat pattern feature info
                lw.WriteLine("could not fully load component, exiting")
                Exit Sub
            Else
                myFlatPattern = GetFlatPattern(realComponents.Item(0).Prototype.OwningPart)
            End If


        End If



        If IsNothing(myFlatPattern) Then
            'skip it
            'warn user?
            lw.WriteLine("flat pattern is nothing")
        Else
            'export flat pattern to dxf
            lw.WriteLine("exporting dxf to: " & dxfFile)
            ExportFlatPattern(myFlatPattern, dxfFile)
        End If

        lw.Close()

    End Sub

    Function GetFlatPattern(ByVal thePart As Part) As Features.FlatPattern

        If IsNothing(thePart) Then
            'part required
            Return Nothing
        End If

        For Each myFeature As Features.Feature In thePart.Features
            If TypeOf (myFeature) Is Features.FlatPattern Then
                Return myFeature
            End If
        Next

        'if we make it here, no flat pattern was found
        Return Nothing

    End Function

    Sub ExportFlatPattern(ByVal theFlatPattern As Features.FlatPattern, ByVal outputFile As String)

        Dim exportFlatPatternBuilder1 As NXOpen.Features.SheetMetal.ExportFlatPatternBuilder
        exportFlatPatternBuilder1 = workPart.Features.SheetmetalManager.CreateExportFlatPatternBuilder()

        exportFlatPatternBuilder1.AddedTop = True

        exportFlatPatternBuilder1.AddedBottom = True

        exportFlatPatternBuilder1.InteriorFeature = True

        exportFlatPatternBuilder1.InteriorCutout = True

        exportFlatPatternBuilder1.Type = Features.SheetMetal.ExportFlatPatternBuilder.FileType.Dxf

        exportFlatPatternBuilder1.DxfRevision = NXOpen.Features.SheetMetal.ExportFlatPatternBuilder.DxfRevisionType.R14

        exportFlatPatternBuilder1.OutputFile = outputFile

        exportFlatPatternBuilder1.FlatPattern.Value = theFlatPattern

        Dim nXObject1 As NXOpen.NXObject
        nXObject1 = exportFlatPatternBuilder1.Commit()

        exportFlatPatternBuilder1.Destroy()


    End Sub

    Function GetFileName()
        Dim strPath As String
        Dim strPart As String
        Dim pos As Integer

        'get the full file path
        strPath = theSession.Parts.Display.FullPath
        'get the part file name
        pos = InStrRev(strPath, "\")
        strPart = Mid(strPath, pos + 1)

        strPath = Left(strPath, pos)
        'strip off the ".prt" extension
        strPart = Left(strPart, Len(strPart) - 4)

        GetFileName = strPart
    End Function

    Function GetFilePath()
        Dim strPath As String
        Dim strPart As String
        Dim pos As Integer

        'get the full file path
        strPath = theSession.Parts.Display.FullPath
        'get the part file name
        pos = InStrRev(strPath, "\")
        strPart = Mid(strPath, pos + 1)

        strPath = Left(strPath, pos)
        'strip off the ".prt" extension
        strPart = Left(strPart, Len(strPart) - 4)

        GetFilePath = strPath
    End Function

    Sub ExportPDF(dwg As Drawings.DrawingSheet, outputFile As String, units As Integer)

        Dim printPDFBuilder1 As PrintPDFBuilder

        printPDFBuilder1 = workPart.PlotManager.CreatePrintPdfbuilder()
        printPDFBuilder1.Scale = 1.0
        printPDFBuilder1.Colors = PrintPDFBuilder.Color.BlackOnWhite
        printPDFBuilder1.Size = PrintPDFBuilder.SizeOption.ScaleFactor
        If units = 0 Then
            printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.English
        Else
            printPDFBuilder1.Units = PrintPDFBuilder.UnitsOption.Metric
        End If
        printPDFBuilder1.XDimension = dwg.Height
        printPDFBuilder1.YDimension = dwg.Length
        printPDFBuilder1.OutputText = PrintPDFBuilder.OutputTextOption.Polylines
        printPDFBuilder1.RasterImages = True
        printPDFBuilder1.ImageResolution = PrintPDFBuilder.ImageResolutionOption.High
        printPDFBuilder1.Append = True
        printPDFBuilder1.Watermark = ""

        Dim sheets1(0) As NXObject
        Dim drawingSheet1 As Drawings.DrawingSheet = CType(dwg, Drawings.DrawingSheet)

        sheets1(0) = drawingSheet1
        printPDFBuilder1.SourceBuilder.SetSheets(sheets1)

        printPDFBuilder1.Filename = outputFile

        Dim nXObject1 As NXObject
        nXObject1 = printPDFBuilder1.Commit()

        printPDFBuilder1.Destroy()

    End Sub

    Private Function LoadComponent(ByVal theComponent As Assemblies.Component) As Boolean

        Dim thePart As Part = theComponent.Prototype.OwningPart

        Dim partName As String = ""
        Dim refsetName As String = ""
        Dim instanceName As String = ""
        Dim origin(2) As Double
        Dim csysMatrix(8) As Double
        Dim transform(3, 3) As Double

        Try
            If thePart.IsFullyLoaded Then
                'component is fully loaded
            Else
                'component is partially loaded
            End If
            Return True
        Catch ex As NullReferenceException
            'component is not loaded
            Try
                theUfSession.Assem.AskComponentData(theComponent.Tag, partName, refsetName, instanceName, origin, csysMatrix, transform)

                Dim theLoadStatus As PartLoadStatus
                theSession.Parts.Open(partName, theLoadStatus)

                '_theUfSession.Assem.SetAssemOptions(curLoadOptions)

                If theLoadStatus.NumberUnloadedParts > 0 Then

                    Dim allReadOnly As Boolean = True
                    For i As Integer = 0 To theLoadStatus.NumberUnloadedParts - 1
                        If theLoadStatus.GetStatus(i) = 641058 Then
                            'read-only warning, file loaded ok
                        Else
                            '641044: file not found
                            allReadOnly = False
                        End If
                    Next
                    If allReadOnly Then
                        Return True
                    Else
                        'warnings other than read-only...
                        Return False
                    End If
                Else
                    Return True
                End If

            Catch ex2 As NXException
                Return False
            End Try
        Catch ex As NXException
            'unexpected error
            Return False
        Finally

        End Try

    End Function

    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

www.nxjournaling.com
 
Status
Not open for further replies.
Back
Top