Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

PDF/DXF export Journal 1

Status
Not open for further replies.

Lars1978

Mechanical
Dec 30, 2015
327
Hi All,
Attached you'll find a journal which exports a PDF and a DXF out of a stand alone part. The DXF is made out of the flat pattern.

I'd like to change this journal so that it will also export the flat pattern out of a master model draft configuration.

Any suggestions?

Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
 https://files.engineering.com/getfile.aspx?folder=cb318aaf-83d2-42ac-9291-a16a6526c1fd&file=export_pdf_dxf_V3.vb
Replies continue below

Recommended for you

Cowski,

This journal works perfectly when working with stand alone parts. I've used it alot.
The journal does not seem to work for master model drafts with flat patten.

Lars



Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
The logic of the code in the previous thread will do the following:
[ul]
[li]If there are no components in the current display part, the code will look for and export the flat pattern in the display part.[/li]
[li]If there is exactly one component: the code will attempt to fully load the component and export the flat pattern found in the component.[/li]
[li]If there are multiple components: the code assumes it is an assembly file and skips the flat pattern export.[/li]
[/ul]

The code may run into trouble if the component cannot be fully loaded for whatever reason. I've tested the code on NX 9 and it works as described above. The code that you have posted in this thread does not seem to have the improvements from the other thread. Have you tried the other code?

www.nxjournaling.com
 
Cowski,

I fixed it. The custom button was still starting the wrong journal.

I've changed this. Now it's working.

Thanx a lot.


Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
@Cowski,

When I have a master model draft which contains views of a part and the flat patten view. I get the following

Capture1_kdb34y.png


What can I do about it?


Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Sorry wrong attachment:)

Capture3_nitwtl.png





Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
That's to be expected, it falls under the 3rd bullet point in my previous post. The code was written to export the flat pattern from the component of the drawing file. If you want it to process a larger assembly, you'll need to add code to process each component and export the flat pattern from each (assuming you want to export all the flat patterns and not just certain ones).

www.nxjournaling.com
 
Cowski,

All the views (also the flat pattern view) are from the same part?

Can I make a draft with only one component and still al the views, including the flat pattern view?


Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Does the addition of a flat pattern view always result in a second reference component?

I'm trying to make an automated export from a draft resulting in a PDF of the draft and a DXF of the flat pattern.

I'm using the master model approach.



Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Yes, it seems the flat pattern view also creates a "drafting component", which will need to be accounted for. I'll look into a fix as I have time.

www.nxjournaling.com
 
Ok thnx..



Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Cowski,

Did you find the time to tweak this journal?

Kind regards,

Lars

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Here's an update (not heavily tested).

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

Module Module3

    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 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)
        End If

        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
        End If

        myFlatPattern = GetFlatPattern(realComponents.Item(0).Prototype.OwningPart)

        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.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
 
Cowski

Thanx a lot. I'll give it a try.

Lars
Code:

Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
Cowski,

It works pretty good. I have a few small bugs.

Attached I file which gives an error. I've attached journal also.

Is it possible to be making a DXF out of a mm draft and making a DXF out of a stand alone draft with the same journal ?


Kind Regards,

Lars



Lars
NX12.0.1.7 native
Solid Edge ST10
Inventor
 
 https://files.engineering.com/getfile.aspx?folder=a62ec614-f22c-4851-ab3b-e17aed6e4704&file=TEMP.zip
Oops, looks like I misplaced one of the function calls.
Try changing this:
Code:
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
End If

myFlatPattern = GetFlatPattern(realComponents.Item(0).Prototype.OwningPart)

to this:
Code:
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

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

Part and Inventory Search

Sponsor