Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Export CGM Filename 2

Status
Not open for further replies.

carlharr

Mechanical
Mar 20, 2012
389
Morning,

Does anyone know if it is possible to change the default style of filename for an exported CGM (exported to windows) from NX8.5?

For example, if we export 123/A1234 revision A the file comes out as 123SA1234_DWG_A, but when we export to Teamcenter the format is taken from sheet name in part navigator i.e. 123-A1234-A-SHEET1

Apart from having to rename for every export, this also means we need to export multi-sheet drawings one-by-one, as otherwise all sheets have the same filename.

So, is it possible to export CGMs to windows using the sheetnames from Part Navigator, in the same way as the "export drawings to Teamcenter" command works?

Thanks, Carl



NX 8.5 with TC 8.3
 
Replies continue below

Recommended for you

Mike,

Thanks for this, looks like it might be possible then!

I ran the program but it returned an error of "specified filename is invalid" at line 97.

Line 97 is the final line in this section:

sheetName = "Printing Sheet: " + mySheet.Name

cgmFileName = tmp_dir + "\" + partName + "_" + mySheet.Name + ".cgm"

ufs.Ui.SetPrompt("Waiting for CGM file: " & cgmFileName)
plot.SaveCgm(mySheet.Tag, jobOptions, JobName, bannerOptions, cgmFileName)


So I'm guessing this is something to do with either the filename being created, or possibly the directory I'm saving to.

The error refers to C:\Users\me\AppData\Local\Temp\NXJournals15008\journal.vb:line97
I've had a look, and there is no NXJournals... folder inside Temp

Unfortunately I have zero programming knowledge, but also our normal CAD boxes wouldn't have access to the C drive.

So do you or anyone know how I can change this journal to specify my own directory, and ensure name is correct?

NX 8.5 with TC 8.3
 
I wouldn't worry about the NXJournals folder, that is a temp location created on the fly as the journal is compiled and run. The error most likely refers to the file path/name of the CGM file. What is the value of the cgmFileName variable when the journal is run? You can check this by adding the line:

Code:
cgmFileName = tmp_dir + "\" + partName + "_" + mySheet.Name + ".cgm"
[highlight #FCE94F]msgbox("cgmFileName: " & cgmFileName)[/highlight]
ufs.Ui.SetPrompt("Waiting for CGM file: " & cgmFileName)
plot.SaveCgm(mySheet.Tag, jobOptions, JobName, bannerOptions, cgmFileName)

Make note of the value, it probably has characters not allowed in a file name or points to a folder location that does not exist.

www.nxjournaling.com
 
Cowski,

Thank you very much, you were right the filename included the part name, which includes slashes.
So I've cut that bit out as I don't want it anyway, leaving just:

cgmFileName = tmp_dir + "\" + mySheet.Name + ".cgm"

I had a similar error at line 126 relating to TIFFs, but I don't want TIFFs.
So, I guessed which lines to delete, ran the journal again and it worked! I am completely stunned!

Attached are the versions with and without TIFFs.

Would you mind having a look just to check I haven't deleted anything critical?

And final (stupid) question, how do I change the location where it's putting the CGMs to e.g. H:\My Documents\Carls Documents ?

NX 8.5 with TC 8.3

 
Your edits look OK to me.
I've added a variable for the output directory (outputDir), change this value as desired.

Code:
' -------------------------------------------------------------------------
' This example loops through all the sheets in the part.
' For each sheet, we open the sheet, update all views in it, and then 
' create a CGM of that sheet.
' -------------------------------------------------------------------------

Option Strict Off
Imports System

Imports NXOpen
Imports NXOpen.Utilities
Imports NXOpen.UF
Imports NXOpen.Drawings
Imports System.Text

Module make_cgm_of_all_drawing_sheets

    Dim s As Session = Session.GetSession()
    Dim ufs As UFSession = UFSession.GetUFSession()
    Dim plot As UFPlot = ufs.Plot
    Dim lw As ListingWindow = s.ListingWindow

    Sub Echo(ByVal output As String)
        s.ListingWindow.Open()
        s.ListingWindow.WriteLine(output)
        s.LogFile.WriteLine(output)
    End Sub

    Sub Main()

        '
        ' ----------------------------------------------- make sure we have a part

        Dim this_part As NXOpen.Tag

        Try

            this_part = s.Parts.Work.Tag

        Catch ex As Exception

            If this_part = NXOpen.Tag.Null Then

                MsgBox("You need an open part to run this program.", MsgBoxStyle.OkOnly)

                Exit Sub    ' no part, so exit program gracefully

            End If

        End Try

        ' -------------------------------------------------------------------------

        Dim workPart As Part = s.Parts.Work
        Dim workView As View = s.Parts.Work.Views.WorkView

        Dim drawingSheets As DrawingSheet()
        drawingSheets = workPart.DrawingSheets.ToArray

        Dim mySheet As DrawingSheet
        Dim JobName As String = Nothing
        Dim tmp_dir As String = Nothing
        Dim outputDir As String = "H:\My Documents\Carls Documents"
        If Not IO.Directory.Exists(outputDir) Then
            MsgBox("Specified output directory does not exist: " & outputDir, MsgBoxStyle.OkOnly, "Error")
            Return
        End If

        Dim partName As String = workPart.Leaf.ToString

        Dim sheetName As String

        Dim cgmFileName As String

        Dim jobOptions As UFPlot.JobOptions
        Dim bannerOptions As UFPlot.BannerOptions = Nothing


        plot.AskDefaultJobOptions(jobOptions)
        plot.AskDefaultBannerOptions(bannerOptions)

        ufs.UF.TranslateVariable("UGII_TMP_DIR", tmp_dir)

        For Each mySheet In drawingSheets '----------- Iterate through the sheets

            mySheet.Open()

            ' ------------------------------------------------------------ Update Views

            s.Parts.Work.DraftingViews.UpdateViews( _
                           DraftingViewCollection.ViewUpdateOption.All, mySheet)

            ' ---------------------------------------------------------------- Plotting

            sheetName = "Printing Sheet: " + mySheet.Name

            'cgmFileName = tmp_dir + "\" + mySheet.Name + ".cgm"
            cgmFileName = IO.Path.Combine(outputDir, mySheet.Name & ".cgm")

            '  msgbox("cgmFileName: " & cgmFileName)
            ufs.Ui.SetPrompt("Waiting for CGM file: " & cgmFileName)
            plot.SaveCgm(mySheet.Tag, jobOptions, JobName, bannerOptions, cgmFileName)



        Next '------------------------------------------ End of the FOR-NEXT loop
        ufs.Ui.SetPrompt("Finished")

    End Sub

    Public Function GetUnloadOption(ByVal dummy As String) As Integer

        GetUnloadOption = UFConstants.UF_UNLOAD_IMMEDIATELY

    End Function

End Module

www.nxjournaling.com
 
Cowski,

Thank you, I see you've also deleted my leftover references to TIFF!

I'm now getting a filename error again, which still makes reference to the TEMP directory, and line 102 now which is:

plot.SaveCgm(mySheet.Tag, jobOptions, JobName, bannerOptions, cgmFileName)

So I checked the filename using your message box and it comes up:

cgmFileName: H:\My Documents\Carls Documents\123-A1234-1-SHEET1.cgm which looks correct

So I'm confused why this new error in the temp directory... would you know why?

Apologies for asking again.

NX 8.5 with TC 8.3
 
OK ignore the last, running it through this time has completed so I've no idea why I got the error previously.

I will try a few more locations and files and see how it goes.

Thank you cowski and mike for your help, much appreciated!

NX 8.5 with TC 8.3
 
Hmmm, ok still getting the error at line 102. Never mind it's weekend, will come back to it next week.

Thanks again

NX 8.5 with TC 8.3
 
It may be that the file already exists and the .SaveCgm function doesn't want to overwrite the existing file. If that's the case, you can add a check and delete the existing file. The code below is a simple implementation:

Code:
cgmFileName = IO.Path.Combine(outputDir, mySheet.Name & ".cgm")

If IO.File.Exists(cgmFileName) Then
    Try
        IO.File.Delete(cgmFileName)

    Catch ex As Exception
        'could not delete existing file
        'take appropriate action
        'such as warning user of error and exiting journal
        MsgBox("Cannot over write existing file: " & cgmFileName & ControlChars.NewLine & _
               "Please close the file if it is open and try again", MsgBoxStyle.OkOnly, "Error")
        Return
    End Try
End If

'  msgbox("cgmFileName: " & cgmFileName)
ufs.Ui.SetPrompt("Waiting for CGM file: " & cgmFileName)
plot.SaveCgm(mySheet.Tag, jobOptions, JobName, bannerOptions, cgmFileName)

www.nxjournaling.com
 
Hi Cowski,

I've done some more testing this morning.

I think the error is due to choice of output directory, and related to some problems my laptop is having with synchronising. It's due a rebuild to fix the problems but I'm too busy at the mo!

I've changed the folder a few times, and with some it always works, with others it always fails reporting specified filename is invalid.

In the directories that work, it has no problem overwriting existing files (I've made minor changes to check).

So I'm going to send it to a few people to see if it works.

Attached is the final version if anyone's interested, thank you very much for your help.

NX 8.5 with TC 8.3

 
Carl

If other people are going to be using this program you may wish to add in some code to allow them to select the output folder. Insert this where after your line ' ***---------CHANGE YOUR OUTPUT DIRECTORY----------***

Code:
   Dim outputDir As String

        outputDir = GetSetting("NX journal", "Export CGM", "ExportPath", "H:\My Documents\Carls Documents")

        Dim FolderBrowserDialog1 As New FolderBrowserDialog

        With FolderBrowserDialog1
            ' Desktop is the root folder in the dialog.
            .RootFolder = Environment.SpecialFolder.Desktop
            If IO.Directory.Exists(outputDir) Then
                .SelectedPath = outputDir
            Else
                .SelectedPath = "H:\My Documents\Carls Documents"
            End If
            .Description = "Select the directory to export STEP file"
            If .ShowDialog = DialogResult.OK Then
                outputDir = .SelectedPath
                ' save the output folder path in the registry for use on next run
                SaveSetting("NX journal", "Export CGM", "ExportPath", .SelectedPath)
            Else
                'user pressed 'cancel', exit the subroutine
                outputDir = 0
                Exit Sub
            End If
        End With

You will also need to add
Code:
Imports System.Windows.Forms

Each users' output folder selection will be saved to their registry to be re-used next time they run the program.

Mike Hyde
NX8.5 with TC9.1
 
Hi Mike,

Thank you for that, makes sense what it's doing.

I'm going to be pretty tied up for the rest of this week, so I'll have a go at it early next week and let you know.

In the meantime two quick questions:

1. It refers twice to Carls Documents - presumably this won't be a problem for the folder browser?
Particularly in the line Else .SelectedPath ?
Or should they use their own name (or a default that won't fail) in these two bits of code?

2. Where do I add Imports System.Windows.Forms ?

Apologies again for the questions, it's probably obvious but I'm no programmer!


On a bright note, the version created earlier has now been tested with good feedback.




NX 8.5 with TC 8.3
 
Hi Carl

It probably would be better to assign a string with the folder name and replace both instance of the folder name with the string variable. The first instance of Carls Documents is defining a default folder if the registry doesn't return a value. The second instance is in case the selected folder does not exist - quite unlikely if the user has selected it, so it could possibly be deleted.
This is just some code I grabbed from one of our programs so I haven't looked at it for a while. I tend to just grab blocks of code to reuse in other programs. You could set the default folder for each user with Environ("username") in the folder name, depending on your folder names.

The Imports statement goes at the beginning of the journal where you will find other Imports statements.

Despite your denials, you are now a programmer. Maybe only a beginner, but as you are getting good feedback it won't be long before they want more!

Mike Hyde
NX8.5 with TC9.1
 
Hi Mike,

To prove my point: assign a string with a folder name? Environ("username")? - now I am lost!

That said, I'm happy to have a crack at it next week, what's the worst that could happen.

Thanks again for your time.






NX 8.5 with TC 8.3
 
I'd suggest changing

Code:
If IO.Directory.Exists(outputDir) Then
    .SelectedPath = outputDir
Else
    .SelectedPath = [highlight #FCE94F]"H:\My Documents\Carls Documents"[/highlight]
End If

to:

Code:
If IO.Directory.Exists(outputDir) Then
    .SelectedPath = outputDir
Else
    .SelectedPath = [highlight #FCE94F]My.Computer.FileSystem.SpecialDirectories.MyDocuments[/highlight]
End If

If the saved directory path no longer exists (it was moved or deleted since the last run), this will point to the current user's "My Documents" directory; you will not have to determine the current user, the code will do that for you.

www.nxjournaling.com
 
Morning,

So I'm having a go at making these changes to bring up the output directory selection.

I've added the code provided by MikeHyde, and changed the line Else .Selected path to the "MyDocuments" line as per cowski's post.

Three questions:

1. Our MyDocuments directories are on the H drive, presumably this won't be a problem?

2. This still leaves the first reference to Carls Documents in the line outputDir. I was completely lost at this:
"You could set the default folder for each user with Environ("username") in the folder name, depending on your folder names."
and I am still lost?

3. Running this journal, I have an unexpected error which is:
Line 57: 'View' is ambiguous, imported from the namespaces or types 'System.Windows.Forms, NXOpen'.

Line 57 was in the earlier version of the journal and hasn't changed, but the earlier line
Imports System.Windows.Forms
has been added.

Is there something else I need to do here?

Thanks in advance, Carl


NX 8.5 with TC 8.3

 
[ol 1]
[li]The line:
.SelectedPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
will point to your "My Documents" folder as defined in Windows; this is the location shown when you click on the Win start button, click on your username, and double click the My Documents folder. If this takes you to the H drive, then all is well. If this takes you to a different location, you'll have to add a bit more code as suggested by Mike.[/li]
[li]Run the test above to see where your My Documents folder is defined in Windows. If you end up in the H drive, no more work necessary.[/li]
[li]NXOpen and the Windows.Forms namespace both have an object named "View". The message is telling you that the code doesn't know which one to use. To remove the ambiguity, supply the full name of the object in line 57.

Code:
Dim workView As [highlight #FCE94F]NXOpen.View[/highlight] = s.Parts.Work.Views.WorkView
[/li]
[/ol]

www.nxjournaling.com
 
Hi cowski,

Thank you very much for the info, that's got rid of the error I saw and it's working on my computer!

And yes I think it should go straight to the H drive for people in the line:
Else
.SelectedPath = "My.Computer.FileSystem.SpecialDirectories.MyDocuments"

I will get some people to test it first, but that makes sense.

Unfortunately I am still confused by number 2, although I don't think I've explained properly.
Earlier in the journal, a line says:

outputDir = GetSetting("NX journal", "Export CGM", "ExportPath", "H:\My Documents\Carls Documents")

And I don't know what I'm supposed to write here so that anyone can use it?

Thank you again for your help.


NX 8.5 with TC 8.3
 
Or should I just delete Carls Documents, leaving:

outputDir = GetSetting("NX journal", "Export CGM", "ExportPath", "H:\My Documents\") ?

NX 8.5 with TC 8.3
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor