Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

VB code to run a batch file from NX

Status
Not open for further replies.

NXProf

Mechanical
Nov 28, 2012
45
Hello,

I'm attempting to initiate a .bat file from NX with VB code, in which I've Googled searched and tried several bits of code, but without success. Can someone please help guide me on the right track? Later, I will embed this section of code into a larger program without shelling out to DOS.

The attempts I've made thus far are as follows:

Code:
' NX 7.5.5.4
'
Option Strict Off
Imports System
Imports System.IO
Imports System.Collections
Imports System.Windows.Forms
Imports System.Windows.Forms.MessageBox
Imports NXOpen
Imports NXOpen.UF
'___________________________________________
Module NXJournal
Sub Main

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

Dim displayPart As Part = theSession.Parts.Display

        '_____________________________________________________________________________
        [b]'1st attempt:[/b]
        'Dim shell As New wscript.shell
        'shell.run("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        '_____________________________________________________________________________
        [b]'2nd attempt:[/b]
        'System.Diagnostics.Process.Start("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        '_____________________________________________________________________________
        [b]'3rd attempt:[/b]
        'Dim psi As New ProcessStartInfo("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        'psi.RedirectStandardError = True
        'psi.RedirectStandardOutput = True
        'psi.CreateNoWindow = False
        'psi.WindowStyle = ProcessWindowStyle.Hidden
        'psi.UseShellExecute = False
        'Dim process As Process = Process.Start(psi)

        'Dim p As New System.Diagnostics.Process()
        'p.StartInfo.FileName = "H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat"
        'p.Start()
        '_____________________________________________________________________________
        [b]'4th attempt:[/b]
         'Shell("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat", AppWinStyle.Hide)
        '_____________________________________________________________________________
        [b]'5th attempt:[/b]
        'Dim psi As New System.Diagnostics.ProcessStartInfo("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        'psi.RedirectStandardOutput = True
        ''psi.WindowStyle = ProcessWindowStyle.Hidden
        'psi.UseShellExecute = False
        'Dim listFiles As System.Diagnostics.Process
        'listFiles = System.Diagnostics.Process.Start(psi)
        'Dim myOutput As System.IO.StreamReader = listFiles.StandardOutput
        'listFiles.WaitForExit(2000)
        ''If listFiles.HasExited Then
        ''    Dim output As String = myOutput.ReadToEnd
        ''    Debug.WriteLine(output)
        ''End If
        '_____________________________________________________________________________
        [b]'6th attempt:[/b]
        'Process.Start("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
    End Sub
End Module

Thank you greatly for your help ahead of time.
NX Prof
 
Replies continue below

Recommended for you

NX Prof,

Here is one approach.

This invokes the command processor (cmd.exe) and passes the script to it as an argument.
The /c switch controls the behavior of the command shell window when the batch script exits.
(Use cmd /? to see the explanation of the /c and /k options.)

Hopefully this is helpful,

Joe

Code:
Option Strict Off
Imports System
Imports NXOpen

Imports System.Diagnostics

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

Dim theSession As Session = Session.GetSession()
Dim theUI As UI = UI.GetUI()

Dim startInfo As New ProcessStartInfo("cmd.exe")
startInfo.WindowStyle = ProcessWindowStyle.Normal
startInfo.Arguments = " /c c:\temp\test.bat"
Process.Start(startInfo)

End Sub
End Module
 
Thank you very much Joe!

I'll have to try out your code when I return to work on Tues. and let you know how it runs.

Have a great weekend!

NX Prof
 
Joe,

I finally had a chance to try out your code. Although it found the proper .bat file and started running, the cmd.exe dialog box stated "The system cannot find the path specified" for the .hpgl files that the .bat file was to convert into separate .tiff files, then combining all tif files into one multi-sheet .tif file. But if I double-click on the .bat file, it runs as expected and performs the above conversions.

Besides the "Imports System" and "Imports System.Diagnostics" settings, could there be another setting that would help it to run from NX7.5 within Teamcenter?

The .bat routine looks like this:
Code:
@echo off

if  exist h:\plot_files\*.hpgl goto gothpp

echo *** NO hpp files found in PLOT directory *** 
echo *** NO hpp files found in PLOT directory *** 
pause
exit

:gothpp

echo
set shdelim=2
set shtorder=
set sufix=
if exist h:\plot_files\*.tif del h:\plot_files\*.tif

set ns=0
for    %%i in (h:\plot_files\*.hpgl) do set /a ns=ns+1 
if %ns% gtr 9 call :renit

for /f "usebackq"  %%i in (`dir h:\plot_files\*.hpgl /on /b`) do call :dotif %%i 

echo ***** creating Multipage tiff file 

.\hp2xx\tiffcp -c g4   %shtorder%  h:\plot_files\Multi_page.tif

dir h:\plot_files\*.tif
echoecho *** Tif creation complete 
pause

exit /b

rem *********************
rem *** end Main body ***
rem *********************

rem **************************************************
rem *** subroutine to process each hpp file        ***
rem **************************************************

:dotif

set key=%1%

echo *** processing %key%

set jobid=%2%

rem set fname=d:\plot_tif\%1%
set nameonly=%~n1

rem ********* creating sheet tiff 
.\hp2xx\hp2xx_w7.exe  -t -c11111111 -p11111111 -m tiff -q -f h:\plot_files\%nameonly%.tif -d 300 -N -S 4 h:\plot_files\%nameonly%.hpgl

set  shtorder=%shtorder% h:\plot_files\%nameonly%.tif

:tifout

goto :eof 

rem **************************************************
rem *** subroutine to rename if more than 9 sheets ***
rem **************************************************

:renit

for /f "usebackq"  %%i in (`dir h:\plot_files\*.hpgl /b`) do call :doren %%i 

goto :eof

:doren

set rsname=%1%
set rnameonly=%~n1

:fixsht

for /f "delims=_. tokens=%shdelim%" %%i in ("%rnameonly%") do set rsheet=%%i

set shtest=%rsheet:~0,2%
if /i "%shtest%" equ "sh" goto gotsht

set/a shdelim=%shdelim% + 1

if %shdelim% gtr 8 echo Sheet ordering failed ... contact administrator
if %shdelim% gtr 8 pause

goto fixsht

:gotsht

set rsheet=%rsheet:sh=%
set rsheet=%rsheet:draw=%
set rsheet=%rsheet:-=%

if %rsheet% gtr 9 ren h:\plot_files\%rsname% z-%rnameonly%.*

goto :eof

I've also attached an image of the output from cmd.exe:
left side of image = when the user double-clicks directly on the .bat routine
right side of image = when attempting to run the .bat via the NX VB program

I appreciate any other suggestions or advise. And thank you very much for your help!

Regards,
NX Prof

 
 http://files.engineering.com/getfile.aspx?folder=247f1c22-4427-41e8-9bc7-19bf29669728&file=cmd_batRoutine2.png
I think the problem is how you are calling your executables.

Code:
.\hp2xx\hp2xx_w7.exe

.\hp2xx\tiffcp

Calling them with a dot at the begining, means find it in the working directory, which when you double click the batch file is the same as the directory where your batch file resides.
Starting the batch from an NX journal, it may be setting the working directory to a diferent one.

Try puting

Code:
echo %CD%

at the begining of your batch file, just below the @echo off, to see what the working directory is.
 
I think Apekas has identified the issue.

Using the following will substitute the directory of the script for defining the path to the executable.

Code:
"%~dp0\hp2xx\hp2xx_w7.exe"
"%~dp0\hp2xx\tiffcp"

HTH,

Joe
 
Thank you Joe and apekas very much for your excellent advise and solution, which worked well!!! The files were not only being saved to a different folder, but a different drive too.

The only small issue I have to see if I can correct is as follows: on about half of the test runs of the VB program calling on the bat routine, only the first sheet or two are made into hpgl files prior to the creation of their respected tif files and finally the "Multi_page.tif" file. But I think I know how to resolve this by either prompting the user with "Select the OK button when all hpgl sheets have been generated" or by putting in a loop (counting every quarter of a second or so) that detects whether the final hpgl sheet has been created before continuing to develop the tif files.

Possibly something like:
Code:
listFiles.WaitForExit(250)
        If listFiles.HasExited Then
            Dim output As String = myOutput.ReadToEnd
            Debug.WriteLine(output)
        End If
Otherwise, do you have any suggestions of how the VB can detect when the bat routine has completed?

Thank you both again so much for your tremendous help!

Regards,
NX Prof
 
NX Prof,

In the batch file use

Code:
Start /wait "Title" "%~dp0\hp2xx\hp2xx_w7.exe"

The "Title" is optional but it it a best practice to use a quoted title argument because the Start command assumes that the first quoted value is a title. And, if you need to quote the command argument omitting the title option becomes problematic. The value "Title" was used as an example - something like "Generating HPGL" would probably be better at documenting the intent.

HTH,

Joe

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor