Skip to content

Instantly share code, notes, and snippets.

@davidtolsma
Created November 25, 2010 00:11
Show Gist options
  • Save davidtolsma/714679 to your computer and use it in GitHub Desktop.
Save davidtolsma/714679 to your computer and use it in GitHub Desktop.
A Unigraphics NX batch program to import and export assemblies into Teamcenter
@ECHO OFF
cls
TITLE Import/Export NX Files
:: Check if required folders exist, creates it if it doesn't exist
CALL :CREATEDIR Import
CALL :CREATEDIR Export
:: Check Arguments
:: set to pause incase user double clicks from O/S
set Pause=pause
if "%1"=="" GOTO :NOARGUMENT
:: passing at least one argument so probably running in Command shell so not going to pause
set Pause=
set input=%2
set inputFile=%3
set folderArgument=%4
set folderName=%5
:: Set Arguments
set operation=%1
set cloneFile=%operation%.clone
if exist .\%operation%\%cloneFile% (set cloneFileExists=true
) else (set cloneFileExists=false)
if "%cloneFileExists%"=="false" (set inputFileRelative=.\%operation%\%inputFile%)
if "%cloneFileExists%"=="false" if "%input%"=="" GOTO :NOARGUMENT
:: Check if file exists when it is doing an import
if "%cloneFileExist%"=="false" CALL :CHECKINPUTFILE
:: Sets where this program is located at
set thisScriptLocation=%~dp0
:: Check if required folders exist, creates it if it doesn't
CALL :CREATEDIR Import
CALL :CREATEDIR Export
:: Project Setting
:: Not able to set from command prompt
set workDir=%thisScriptLocation%
set importExportFolder=%workDir%%operation%
:: Allow these to be set outside of this script
if "%logFile%"=="" set logFile=false
if "%logFileName%"=="" set logFileName=logfile.txt
if "%user%"=="" CALL :GETLOGINCREDIANTIALS
if "%password%"=="" CALL :GETLOGINCREDIANTIALS
:: Optional Argument to create Mapping File
:: Will quit program after this
if "%generateMappingFile%"=="yes" GOTO :CREATEMAPPINGFILE
:: Application name
set aplicationName=ug_clone.exe
:: the Login credentials are set, capture in a variable
set LoginInformation=-u=%user% -p=%password%
:: Set the Existing Directory location to reset at the end of script
set initalDirectory="%CD%"
:: Check Environment Variables
if "%UGII_BASE_DIR%"=="" GOTO :NOTRIGHTENVIRONMENT
if "%UGII_ROOT_DIR%"=="" GOTO :NOTRIGHTENVIRONMENT
:: Checking for Teamcenter environment Variables
if "%IMAN_ROOT%"=="" GOTO :NOTRIGHTENVIRONMENT
if "%IMAN_DATA%"=="" GOTO :NOTRIGHTENVIRONMENT
:: If log file is enabled write the log file
if "%logFile%"=="yes" CALL :LOGFILE
:: Check which file and where to use for mapping
CALL :SETCLONEFILE
set mappingFile=.\%operation%\mappingfile.txt
:: Checking if a mapping file exists
:: if it exists then do the import or export operation
if exist .\%operation%\%cloneFile% GOTO :IMPORTEXPORT
:: mapping file doesn't exist so we are going to create one
GOTO :GENERATEASSEMBLYLIST
:: Program LOGIC done
GOTO :QUIT
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:PRINTMESSAGE
echo Your input file "%inputFile%" does not exist
GOTO :QUIT
:CREATEDIR
set argument=%*
:: If the folder does not exist this will create it
if not exist %thisScriptLocation%\%argument% md %argument%
GOTO :EOF
:GETLOGINCREDIANTIALS
:: set a Prompt for username and password
cls
echo Enter Your Teamcenter Username
echo.
SET user=
SET /P user=username:
if not "%user%"=="" set user=%user:~0,25%
cls
echo Enter Your Teamcenter Password
echo.
SET password=
SET /P password=password:
if not "%password%"=="" set password=%password:~0,25%
cls
GOTO :EOF
:SETCLONEFILE
set NXManagerMode=-pim=yes
set transferDirectory=%importExportFolder%\
GOTO :EOF
:SETNXCOMMANDS
:: need to set to execute NX functions
set PATH=%UGII_BASE_DIR%\ugii;%UGII_BASE_DIR%\ugmanager;%PATH%
:: cd %importExportFolder%
:: cd %UGII_BASE_DIR%\ugmanager
GOTO :EOF
:GENERATEASSEMBLYLIST
echo Creating Clone File...
echo.
CALL :SETNXCOMMANDS
:: If there is a folderOption passed in set it
set folderOption=
if "%folderName%" neq "" (set folderOption=-default_folder=%user%:%folderName%)
:: Import should autogeneration the numbers
:: export should try to use the DB_ attributes to set the name
set operationSpecificArguments=
if "%operation%"=="import" (set operationSpecificArguments=-default_naming=autogen %folderOption%
) else (set operationSpecificArguments=-default_naming=autotranslate)
set arguments=-operation=%operation% -assembly="%inputFile%" -dryrun -save_log_file="%cloneFile%" %operationSpecificArguments%
:: When logFile is set to true, print the command to the logfile
if "%logFile%"=="true" echo %aplicationName% -u=PROTECTED -p=PROTECTED %arguments% >> %transferDirectory%%logFileName%
:: To keep the import and export seperate
cd .\%operation%
:: -default_naming=autotranslate
:: -autotranslate_mode=legacy
%aplicationName% %NXManagerMode% %LoginInformation% %arguments%
:: return back to previous directory
cd ..
GOTO :QUIT
:IMPORTEXPORT
echo Performing %operation%...
echo.
:: setting the path again, in case a mapping file exists but it is a new command prompt
CALL :SETNXCOMMANDS
:: To keep the import and export seperate
cd .\%operation%
set arguments=-operation=%operation% -load_log_file="%cloneFile%" -save_log_file="%cloneFile%"
:: When logFile is set to true, print the command to the logfile
if "%logFile%"=="true" echo %aplicationName% -u=PROTECTED -p=PROTECTED %arguments% >> %transferDirectory%%logFileName%
%aplicationName% %NXManagerMode% %LoginInformation% %arguments%
:: return back to previous directory
cd ..
GOTO :QUIT
:CREATEMAPPINGFILE
:: setting the path again, in case a mapping file exists but it is a new command prompt
CALL :SETNXCOMMANDS
:: Creating the temporary assembly list of files for UGMANAGER_IMPORT
ugpc %inputFileRelative% > %operation%\assembly.txt
:: Create the default header for the mapping file
CALL :CREATEMAPPINGFILEHEADER
:: Looking at each line in the assembly.txt file and grabbing the part name using the '\' delimiter
FOR /F "tokens=1,2,3 delims=\" %%i in (%operation%\assembly.txt) DO CALL :CREATEMAPPINGFILE %%k
:: Delete the temporary assembly list of files text file
if exist "%operation%\assembly.txt" del %operation%\assembly.txt
GOTO :QUIT
:CREATEMAPPINGFILEHEADER
:: Delete the existing Mapping File if there is one
if exist "%MappingFile%" del %MappingFile%
echo [Defaults] >> %MappingFile%
echo existing_data=$USE_EXISTING >> %MappingFile%
echo assoc_files=yes >> %MappingFile%
echo !import_folder="Import Project" >> %MappingFile%
echo. >> %MappingFile%
GOTO :EOF
:CREATEMAPPINGFILEBODY
set argument=%*
echo [%argument%] >> %MappingFile%
echo db_part_no=%argument% >> %MappingFile%
echo db_part_rev=AA >> %MappingFile%
echo db_part_name="%argument%" >> %MappingFile%
echo !db_part_desc="" >> %MappingFile%
echo db_part_type=TI_Product >> %MappingFile%
echo !db_model_type= >> %MappingFile%
echo !db_model_name= >> %MappingFile%
echo !assoc_files=yes >> %MappingFile%
echo !existing_data=$OVERWRITE_EXISTING >> %MappingFile%
echo. >> %MappingFile%
GOTO :EOF
:: This environment must have the following environments set
:NOTRIGHTENVIRONMENT
ECHO Missing the correct environment variables
ECHO ------------------------------------------
ECHO.
ECHO The following environment variables must be set
ECHO.
if "%UGII_BASE_DIR%"=="" echo UGII_BASE_DIR = ^<not set^>
if "%UGII_BASE_DIR%" neq "" echo UGII_BASE_DIR = %UGII_BASE_DIR%
if "%UGII_ROOT_DIR%"=="" echo UGII_ROOT_DIR = ^<not set^>
if "%UGII_ROOT_DIR%" neq "" echo UGII_ROOT_DIR = %UGII_ROOT_DIR%
if "%IMAN_ROOT%"=="" echo IMAN_ROOT = ^<not set^>
if "%IMAN_ROOT%" neq "" echo IMAN_ROOT = %IMAN_ROOT%
if "%IMAN_DATA%"=="" echo IMAN_DATA = ^<not set^>
if "%IMAN_DATA%" neq "" echo IMAN_DATA = %IMAN_DATA%
ECHO.
%Pause%
GOTO :QUIT
:LOGFILE
echo. > %transferDirectory%%logFileName%
echo ~~~~ NX SETTINGS ~~~~ >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo UGII_BASE_DIR= %UGII_BASE_DIR% >> %transferDirectory%%logFileName%
echo UGII_ROOT_DIR= %UGII_ROOT_DIR% >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo ~~~~ TEAMCENTER SETTINGS ~~~~ >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo IMAN_ROOT= %IMAN_ROOT% >> %transferDirectory%%logFileName%
echo IMAN_DATA= %IMAN_DATA% >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo ~~~ PATH INFORMATION ~~~~ >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo PATH= %PATH% >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo ~~~ THIS SCRIPT SETTINGS ~~~~ >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
echo WORKDIR= %workDir% >> %transferDirectory%%logFileName%
echo IMPORTEXPORTFOLDER= %importExportFolder% >> %transferDirectory%%logFileName%
echo. >> %transferDirectory%%logFileName%
GOTO :EOF
:: What is the argument needed
:NOARGUMENT
ECHO Usage: importexport.bat
ECHO import^|export
ECHO file=^<assembly file^> do not include path
ECHO [folder=^<optional^>] the folder name to import to
ECHO.
ECHO Example (native):
ECHO importexport.bat import file=assembly.prt folder=inputFolder
ECHO.
ECHO Example (Teamcenter):
ECHO importexport.bat export file=@DB/000000026/AA
ECHO.
ECHO.
ECHO ~~~~~~~ Optional Environment Variables ~~~~~~~
ECHO.
ECHO set logFile=yes (this will generate a log file in the appropriate Import or Export folder)
ECHO.
ECHO set generateMappingFile=yes (this will generate a Mapping File in the appropriate Import or Export folder)
ECHO.
:: Exit the program
%Pause%
GOTO :QUIT
:QUIT
cd %thisScriptLocation%
ECHO.
:: reset the windows title to default
title %comspec%
@sourabhchavan
Copy link

Hi David,

I am trying to export a NX part file from TC using importexport.bat file, it says export was successfull, butthe output is not created in the transfer directory, could you please help us with this.

Regards,
Sourabh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment