GM Scripting - EXPORT_VECTOR using import file name without extension

Hi,

I would be very grateful if anyone please help out with my script below?

I would like to export my data using the original import file name, but without the file's original extension in the name. Basically I would like to use the file name and then append '_TIN' to it.

I have tried using %LAYER_FNAME_WO_EXT% but this just uses that actual text for the file name instead. Been on this for hours and getting absolutely nowhere. With there was a GUI for building scripts because the guide makes little sense to me  :/


Attempt 2: File name ends up as TerrainExample.tif_TIN.dxf

// Prompt for .tif file to load in folder. Cancel if nothing selected.
DEFINE_VAR NAME="%FNAME%" PROMPT="FILE" VALUE="%DIR_TO_LOAD%" ABORT_ON_CANCEL=YES
// Load the file.
IMPORT FILENAME="%FNAME%"
// Convert to contours
GENERATE_CONTOURS INTERVAL=1 ELEV_UNITS=METERS
// Generate TIN mesh from contours
GENERATE_ELEV_GRID GRID_SAVE_TIN=YES GRID_IGNORE_ZERO GRID_TYPE=ELEVATION
// Unload layers
UNLOAD_LAYER FILENAME="GENERATED CONTOURS" FILENAME="GENERATED CONTOURS (Elevation Grid)"
// Export TIN mesh
EXPORT_VECTOR FILENAME="%FNAME%_TIN.dxf" TYPE=DXF GEN_PRJ_FILE=NO
// Unload the loaded data
UNLOAD_ALL


Attempt 1: File name ends up as %FNAME_WO_EXT%_TIN.DXF

// Prompt for .tif file to load in folder. Cancel if nothing selected.
DEFINE_VAR NAME="FILE_TO_LOAD" PROMPT="FILE" VALUE="%DIR_TO_LOAD%" ABORT_ON_CANCEL=YES
// Load the file.
IMPORT FILENAME="%FILE_TO_LOAD%"
// Convert to contours
GENERATE_CONTOURS INTERVAL=1 ELEV_UNITS=METERS
// Generate TIN mesh from contours
GENERATE_ELEV_GRID GRID_SAVE_TIN=YES GRID_IGNORE_ZERO GRID_TYPE=ELEVATION
// Unload layers
UNLOAD_LAYER FILENAME="GENERATED CONTOURS" FILENAME="GENERATED CONTOURS (Elevation Grid)"
// Export TIN mesh
EXPORT_VECTOR FILENAME="%FNAME_WO_EXT%_TIN.DXF" TYPE=DXF GEN_PRJ_FILE=NO
// Unload the loaded data
UNLOAD_ALL


Cheers,
Steve

Best Answer

  • bmg_bobbmg_bob Global Mapper Programmer Posts: 2,177
    Answer ✓
    Hi Steve,

    Regarding Open vs. Save button:

    In the DEFINE_VAR statement that prompts for the file, FILE_MUST_EXIST=YES indicates to use the File Open dialog. From the comments in your script, I thought you were selecting a file to open. Change that to FILE_MUST_EXIST=NO, or simply remove it altogether, to get a File Save dialog instead.

    I suggest that the following statement be inserted after your DEFINE_VAR for FNAME.
    DEFINE_VAR NAME="MY_OUT_FNAME" FORMULA="clip(FNAME, '', '.') + '_TIN.DXF'"
    As an aside, in the DEFINE_VAR command, the text in NAME= does not need to be surrounded with % characters. That is NAME="FNAME" will work in your case.

    Cheers,
    Bob

Answers

  • bmg_bobbmg_bob Global Mapper Programmer Posts: 2,177
    Hello Steve,

    You will need to use a DEFINE_VAR with FORMULA to generate the file name without an extension. It would look like this:
    DEFINE_VAR NAME="FNAME" PROMPT="FILE" VALUE="" FILE_MUST_EXIST=YES ABORT_ON_CANCEL=YES
    DEFINE_VAR NAME="MY_OUT_FNAME" FORMULA="clip(FNAME, '', '.') + '_TIN.DXF'"
    The first statement prompts the user for a file name and puts it into the FNAME variable.

    The second statement uses FORMULA containing the CLIP function. The clip function reads the string in FNAME until it finds the character in the first argument ('' - an empty string means to start at the beginning) and begins copying the characters until it finds the character in the next argument ('.' - a period), and returns all of the characters except for the ones used as delimiters. In this case, it will copy characters from the start of FNAME until it reaches the first period. The final step in the formula appends "_TIN.DXF" to the string.

    For example, if FNAME is "C:\my_path\my_folder\my_file.ext", the clip function will return "C:\my_path\my_folder\my_file" and the final result will be "C:\my_path\my_folder\my_file.TIN.DXF". 

    Cheers,
    Bob

  • sphillipssphillips Posts: 205
    bmg_bob said:
    Hello Steve,

    You will need to use a DEFINE_VAR with FORMULA to generate the file name without an extension. It would look like this:
    DEFINE_VAR NAME="FNAME" PROMPT="FILE" VALUE="" FILE_MUST_EXIST=YES ABORT_ON_CANCEL=YES
    DEFINE_VAR NAME="MY_OUT_FNAME" FORMULA="clip(FNAME, '', '.') + '_TIN.DXF'"
    The first statement prompts the user for a file name and puts it into the FNAME variable.

    The second statement uses FORMULA containing the CLIP function. The clip function reads the string in FNAME until it finds the character in the first argument ('' - an empty string means to start at the beginning) and begins copying the characters until it finds the character in the next argument ('.' - a period), and returns all of the characters except for the ones used as delimiters. In this case, it will copy characters from the start of FNAME until it reaches the first period. The final step in the formula appends "_TIN.DXF" to the string.

    For example, if FNAME is "C:\my_path\my_folder\my_file.ext", the clip function will return "C:\my_path\my_folder\my_file" and the final result will be "C:\my_path\my_folder\my_file.TIN.DXF". 

    Cheers,
    Bob

    Hi Bob,

    Thank you for taking the time to answer my question and explain how it works. It is greatly appreciated as always.

    I have tried to add this to my script but when I enter the file name and click on Open it says "File not found. Check the file name and try again". Not sure why is only shows an Open button instead of Save anyway.

    Could you please suggest where I add this in my script? I have tried inserting it before and after this:


    // Prompt for .tif file to load in folder. Cancel if nothing selected.
    DEFINE_VAR NAME="%FNAME%" PROMPT="FILE" VALUE="%DIR_TO_LOAD%" ABORT_ON_CANCEL=YES
    // Load the file.
    IMPORT FILENAME="%FNAME%"
    // Convert to contours


    Cheers,
    Steve
  • sphillipssphillips Posts: 205
    bmg_bob said:
    Hi Steve,

    Regarding Open vs. Save button:

    In the DEFINE_VAR statement that prompts for the file, FILE_MUST_EXIST=YES indicates to use the File Open dialog. From the comments in your script, I thought you were selecting a file to open. Change that to FILE_MUST_EXIST=NO, or simply remove it altogether, to get a File Save dialog instead.

    I suggest that the following statement be inserted after your DEFINE_VAR for FNAME.
    DEFINE_VAR NAME="MY_OUT_FNAME" FORMULA="clip(FNAME, '', '.') + '_TIN.DXF'"
    As an aside, in the DEFINE_VAR command, the text in NAME= does not need to be surrounded with % characters. That is NAME="FNAME" will work in your case.

    Cheers,
    Bob

    Thank you again Bob.

    I have the script running as I wanted it but cannot stop GM prompting to Save rather than Open. This is not a big deal for me but it's odd because one of the sample scripts in the GM Scripting Reference v20 does the same when run (see page 25 or below):

    Example to prompt the user for a folder and then a .zip file in that folder (you could have just prompted for the file all at once, but this is for demonstration), then loads it.

    // Prompt for folder to load
    DEFINE_VAR NAME="DIR_TO_LOAD" PROMPT="DIR" VALUE="d:\temp\export test\" \ ABORT_ON_CANCEL=NO
    // Prompt for .zip file to load in folder. Cancel if nothing selected.
    DEFINE_VAR NAME="FILE_TO_LOAD" PROMPT="FILE" VALUE="%DIR_TO_LOAD%.zip" ABORT_ON_CANCEL=YES
    // Load the file.
    IMPORT FILENAME="%FILE_TO_LOAD%"



    By the way, this was the final script. It just creates an optimised TIN mesh when compared against a gridded TRN mesh. Ironically I notice that they are working on Mesh Simplification in v22. meaning this script is going to be unnecessary  :D

    // Prompt for file to load in folder. Cancel if nothing selected.
    DEFINE_VAR NAME="FILE_TO_LOAD" PROMPT="FILE" VALUE="%DIR_TO_LOAD%" ABORT_ON_CANCEL=YES
    // Load the file.
    IMPORT FILENAME="%FILE_TO_LOAD%"
    // Generate file name
    DEFINE_VAR NAME="MY_OUT_FNAME" FORMULA="clip(FILE_TO_LOAD, '', '.') + '_TIN.DXF'"
    // Convert to contours
    GENERATE_CONTOURS INTERVAL=1 ELEV_UNITS=METERS
    // Generate TIN mesh from contours
    GENERATE_ELEV_GRID GRID_SAVE_TIN=YES GRID_IGNORE_ZERO GRID_TYPE=ELEVATION
    // Unload layers
    UNLOAD_LAYER FILENAME="GENERATED CONTOURS" FILENAME="GENERATED CONTOURS (Elevation Grid)"
    // Export TIN mesh
    EXPORT_VECTOR FILENAME=%MY_OUT_FNAME% TYPE=DXF GEN_PRJ_FILE=NO
    // Unload the loaded data
    UNLOAD_ALL


    Cheers,
    Steve
Sign In or Register to comment.