Command line variable on startup

Issue #317 resolved
Brian created an issue

Hi, is there or can there be another command line option that is put into a tcl variable for us to play with, Something like --projectfile?

So usage would be python .\FlatCAM.py --shellfile=./path/to/script.tcl --projectfile=./path/to/project.GTL

This option would then be (for the want of better knowledge/words) set projectfile --projectfile

Then something like this would work

set Path_To_Gerber [file dirname $projectfile]
set Project_Name [file tail $projectfile]

Thanks

Comments (13)

  1. Marius Stanciu

    Hi Brian,

    What are you trying to do? Are you trying to find out the path of a project?

    Right now, if you launch FlatCAM with a parameter that is just the path to a:

    • FlatCAM project = file with extension .FlatPrj
    • FlatCAM config file = file with extension .FlatConfig
    • FlatCAM script = file with extension .FlatScript
    • a Gerber file with the known extension = .GBR, .GBL, .GTL etc
    • a Excellon file with a known extension = .DRL, .TXT etc
    • a GCode file with known extension = .NC, .TAP etc

    it will start the app and then load that file.

    E.g:

    python Flatcam.py D:\\Temp\\project.FlatPrj
    

    will run FlatCAM and try to load the project file: project.FlatPrj

    Or:

    python Flatcam.py C:\\TempFiles\\script_to_run.FlatScript
    

    will run FlatCAM and try to run the script named script_to_run.FlatScript

    The --shellfile parameter is just a legacy parameter, still useful if you try to run a script file having the file extension not .FlatScript

  2. Brian reporter

    If there was a --projectfile command line option then we can use the variable within a script, making the script and whole process easier to use with no need to edit the script every time to run a different board

    python .\FlatCAM.py --shellfile=C:/Temp/Scripts/FlatCAM_09.tcl --projectfile=C:/Temp/TestBoard_v2/TestBoard_v2.GTL

    python .\FlatCAM.py --shellfile=C:/Temp/Scripts/FlatCAM_09.tcl --projectfile=C:/Temp/WiggleBoard/Project1938.GTL

    with this --projectfile in a tcl variable, say $projectfile, we can then process it like this

    if {![info exists projectfile]} { ;# Variable projectfile Exists From Command Line?
        set projectfile C:/Temp/TestBoard_v2/TestBoard_v2.GTL ;# Use This Path If None Set In Command Line
    }
    set Path_To_Gerber [file dirname $projectfile] ;# Path To Project Directory
    set Project_Name [file rootname [file tail $projectfile]] ;# Name Of Project
    

    Thanks

  3. Marius Stanciu

    I don’t know enough of the TCL but …

    I would think that what can be done (looking here: https://www.tek-tips.com/viewthread.cfm?qid=1544204) is the following:

    FlatCAM can be set to have a arg parameter like --shellvars which can load a text file that may look like this:

    a=''this path”
    b_var = “another path”
    c = 3.14
    

    Then FlatCAM will parse this file at start-up and create for each a

    set variable variable_content
    

    But in any case, that’s for another time.

  4. Marius Stanciu

    Actually it was easier than expected. Feature added in latest commit.

    The new command line parameter is --shellvars and the usage is:

    python FlatCAM.py --shellvars list_of_commands.txt
    

    and the text file format should be like this:

    a=54
    b = 15
    c= 'Brian wants a new parameter for FlatCAM.'
    

    The lack or presence of spaces is intentional, to show that it does not matter, all it matter is the ‘=' sign and what’s to the left and to the right.

    LE: although I don’t quite see the reason for this, all could be in the script. It’s just a matter of Copy-Paste from one script to another. I got the feeling that this will go away quite fast. :)

  5. Marius Stanciu

    Ok, I’ve changed the behavior.

    The user can now pass a list of values separated with comma but without spaces like this:

    python FlatCAM.py --shellvar 1,"George",3,"C:\Path_to_file"
    

    Notice that now it is --shellvar and not --shellvars

    The passed variables can be accessed in Tcl with the following special names:

    $shellvar_x
    

    where x is the index in the passed list.

    E.g:

    $shellvar_0
    $shellvar_1
    $shellvar_2
    etc
    

    I think that cover your request.

  6. Brian reporter

    Thanks, that looks good, but does not work with both --shellfile and --shellvar in the same command line

    command line = python .\FlatCAM.py --shellfile=C:/Temp/TestBoard_v2/FlatCAM_test_02.tcl --shellvar="C:/Temp/TestBoard_v2/TestBoard_v4.GTL"

    FlatCAM_test_02.tcl file contains

    if {[info exists shellvar_0]} {
        set test $shellvar_0
    } else {
        set test "Boo"
    }
    puts $test
    

  7. Brian reporter

    Hi Marius,

    Thanks for working on this

    if --shellvar does not have a space it then it works great, but if there is a space in the string then it gives an error

    Works --shellvar="C:\Temp\TestBoard_v2\TestBoard_v2.GTL"

    Error --shellvar="C:\Temp\TestBoard v4\TestBoard_v4.GTL"

    Error from the command window

    [ERROR][MainThread] Exec command Exception: wrong # args: should be "set varName ?newValue?"
        while executing
    "set shellvar_0 C:/Temp/TestBoard v4/TestBoard_v4.GTL"
    

  8. Brian reporter

    If I run this --shellvar="C:\Temp\TestBoard v4\TestBoard_v4.GTL"

    it now removes the space from the path!

    Error [ERROR_NOTCL] Failed to open file: C:/Temp/TestBoardv4/TestBoard_v4.GTL

    Many thanks

    Brian.

  9. Marius Stanciu

    I think it is fixed but I will not update the repo right now as I am in the middle of a big change which might not work after all. And I don’t want to risk conflicts.
    In any case it should be available in the next build. For now, avoid the spaces in the path. It should be doable.

  10. Marius Stanciu

    Hi Brian,

    I’ve updated the repo with the latest changes including also 2 new Tcl commands: set_origin and bounds.

    set_origin will do what it says, it will set the zero for all the loaded objects to the left corner of the common bounding box

    bounds will return a list of bounds values in the form [[xmin, ymin, xmax, ymax], [x01, y01, x11, y11], …] from a supplied list of valid object names. A valid object name is the name of a loaded object in FlatCAM. The returned bounds can be used in a Tcl Script:

    set a [bounds a_obj.GTL,b_obj.GKO]
    

    and a set of bounds can be accessed like this:

    set bounds_obj1 [lindex %a 1]
    

  11. Log in to comment