×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

VB code to run a batch file from NX

VB code to run a batch file from NX

VB code to run a batch file from NX

(OP)
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 --> VB

' 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

        '_____________________________________________________________________________
        '1st attempt:
        'Dim shell As New wscript.shell
        'shell.run("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        '_____________________________________________________________________________
        '2nd attempt:
        'System.Diagnostics.Process.Start("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat")
        '_____________________________________________________________________________
        '3rd attempt:
        '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()
        '_____________________________________________________________________________
        '4th attempt:
         'Shell("H:\plot_files\MULTI-TIFF-MAKER\MakeTif.bat", AppWinStyle.Hide)
        '_____________________________________________________________________________
        '5th attempt:
        '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
        '_____________________________________________________________________________
        '6th attempt:
        '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

RE: VB code to run a batch file from NX

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

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 

RE: VB code to run a batch file from NX

(OP)
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

RE: VB code to run a batch file from NX

(OP)
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 --> cmd.exe

@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

RE: VB code to run a batch file from NX

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.

RE: VB code to run a batch file from NX

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 --> DOS_batch

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

HTH,

Joe

RE: VB code to run a batch file from NX

(OP)
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 --> VB.NET

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

RE: VB code to run a batch file from NX

NX Prof,

In the batch file use

CODE --> DOS

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources