Passing parameters from command line before macros loading

Hi,

I want to pass some parameters from command line, so they can be used while loading the environment.

I've already checked the forum for similar threads (e.g. this thread), but from what I see and understand from the debugging mode, the definition of the externally given variables is happening after macros loading and they are available after the klayout is opened.

How I can define variables before macros loading, so they can be used if and only they are given?

Thanks in advance,
Chris

Comments

  • Hi Chris,

    You can define global variables from the command line:

    klayout -rd name=value ...
    

    The value will always be a string. In Ruby you can do something like this in your macro:

    # Set $cmdline_var with:
    #   klayout -rd cmdline_var=value ...
    
    if $cmdline_var
      puts "Variable 'cmdline_var' given with value: #{$cmdline_var}"
    else
      puts "Variable 'cmdline_var' not given"
    end
    

    Another way of controlling the flow inside your macros are environment variables, but I prefer the -rd solution for it's clarity.

    Is this what you are looking for?

    Matthias

  • edited June 2019

    Hi Matthias,

    This is what I am trying to do, unsuccessfully.

    The error I am getting:

    ERROR: global name 'myVar' is not defined in PCellDeclaration.produce
      <some_path>/klayout/bin/python/###.py:6
      ...
      :/built-in-pymacros/pcell_declaration_helper.lym:185
    

    After KLayout opens, if I recompile my scripts, there is no problem, and all the global variables that I defined are available.

    The command I run:

    klayout -rd myVar='True' -d 20
    

    I cleaned up KLayout's debug output:


    Click for the -d output.

    ...
    Lots of KLayout warnings. e.g.:
    Warning: Class Instance: no Python mapping for method []=
    ...
    Scanning /users/crizos/.klayout/salt for packages
    Scanning some_path/klayout/bin/salt for packages
    Scanning some_other_path/klayout/salt for packages
    Auto-importing technology from some_other_path/klayout/tech/tech.lyt
    ...
    Scanning macro path :/built-in-macros (readonly=true)
    Loading lots of built-in-macros from :/built-in-macros/
    ...
    Scanning macro path :/built-in-pymacros (readonly=true)
    Loading lots of built-in-macros from :/built-in-pymacros/
    ...
    Scanning macro path /users/crizos/.klayout/macros (readonly=false)
    Scanning macro path /users/crizos/.klayout/ruby (readonly=false)
    Scanning macro path /users/crizos/.klayout/pymacros (readonly=false)
    Scanning macro path /users/crizos/.klayout/python (readonly=false)
    Scanning macro path /users/crizos/.klayout/drc (readonly=false)

    Scanning macro path some_path/klayout/bin/macros (readonly=true)
    Scanning macro path some_path/klayout/bin/ruby (readonly=true)
    Scanning macro path some_path/klayout/bin/pymacros (readonly=true)
    Loading macro from some_path/klayout/bin/pymacros/Utils.lym
    Loading macro from some_path/klayout/bin/pymacros/myUtil.lym
    Loading macro from some_path/klayout/bin/pymacros/myLib.lym
    Scanning macro path some_path/klayout/bin/python (readonly=true)
    ...
    Loading lots of macros from some_path
    ...
    Scanning macro path some_path/klayout/bin/python/PCells (readonly=true)
    ...
    Loading lots of macros from some_path/klayout/bin/python/PCells/
    ...
    Scanning macro path some_path/klayout/bin/drc (readonly=true)

    Scanning macro path some_other_path/klayout/macros (readonly=true)
    Scanning macro path some_other_path/klayout/ruby (readonly=true)
    Scanning macro path some_other_path/klayout/pymacros (readonly=true)
    Loading macro from some_other_path/klayout/pymacros/myLib.lym
    Scanning macro path some_other_path/klayout/python (readonly=true)
    Scanning macro path some_other_path/klayout/python/PCells (readonly=true)
    Scanning macro path some_other_path/klayout/drc (readonly=true)
    ...
    Running lots of built-in-macros :/built-in-macros/
    ...
    Scanning macro path some_path/klayout/bin/drc (readonly=true)
    Scanning macro path some_path/klayout/bin/macros (readonly=true)
    Scanning macro path some_path/klayout/bin/pymacros (readonly=true)
    Scanning macro path some_path/klayout/bin/python (readonly=true)
    Scanning macro path some_path/klayout/bin/ruby (readonly=true)

    Scanning macro path some_other_path/klayout/drc (readonly=true)
    Scanning macro path some_other_path/klayout/macros (readonly=true)
    Scanning macro path some_other_path/klayout/pymacros (readonly=true)
    Scanning macro path some_other_path/klayout/python (readonly=true)
    Scanning macro path some_other_path/klayout/ruby (readonly=true)

    Scanning macro path /users/crizos/.klayout/drc (readonly=false)
    Scanning macro path /users/crizos/.klayout/macros (readonly=false)
    Scanning macro path /users/crizos/.klayout/pymacros (readonly=false)
    Scanning macro path /users/crizos/.klayout/python (readonly=false)
    Scanning macro path /users/crizos/.klayout/ruby (readonly=false)

    Scanning macro path :/built-in-macros (readonly=true)
    Scanning macro path :/built-in-pymacros (readonly=true)
    Initializing plugins:
    ...
    Lots of plugins. e.g.:
    ClipDialogPlugin [20000]
    ...
    KLayout path:
    /users/crizos/.klayout
    some_path/klayout/bin
    some_other_path/klayout
    Config file to write: /users/crizos/.klayout/klayoutrc
    Config files to read:
    some_other_path/klayout/klayoutrc
    some_path/klayout/bin/klayoutrc
    ...
    ===========
    The external variable I want to use internally via -rd command flag:
    ===========
    ...
    Variable definition: myVar='True'
    ...
    Running lots of macros some_path/klayout/bin/macros/.lym
    Running lots of macros some_path/klayout/bin/pymacros/
    .lym
    ...
    Loading lots of KLayout macro-templates
    ...

  • edited June 2019

    Hi Chris,

    "global" is a relative measure in Python - actually global Variables are not provided by the Python interpreter but by the application. I need to check whether they are available within PCell code as this is a different context than scripts.

    A simple solution to overcome this is to read the global variable in the PCell class __init__ and store it there as an attribute. You should be able to access it then from the PCell's produce method.

    BTW: 'True' is becoming a string "True", not a boolean value.

    Matthias

  • Hi Matthias,

    A simple solution to overcome this is to read the global variable in the PCell class __init__

    I'm not really sure I understood how to overcome this issue. Again, I get the same error as before when I'm trying to use the variable directly, and globals() doesn't contain the external variable so I can take it from there.

    Also, I tried to initialize a global variable I want to pass from outside with a dummy value, so it can be defined from the tool, but then, it seems that it can't be overwritten from the external values afterwards.

    BTW: 'True' is becoming a string "True", not a boolean value.

    Thanks, name and value is for proof of concept :smiley:

    Chris

  • Hi Chris,

    Ok, I'll be more specific:

    class PCell(pya.PCellDeclarationHelper):
    
      globalParameter = ""
    
      # PCell is parametrized with a given value
      def __init__(self, globalParameter):
    
        # Important: initialize the super class
        super(PCell, self).__init__()
    
        # keep the parameter inside the implementation class
        self.globalParameter = globalParameter
        ...
    
      end
      ...
    end
    
    class PCellLib(pya.Library):
    
      def __init__(self):
    
        # set by "-rd globalParameter=...":
        global globalParameter
    
        ...  
        self.layout().register_pcell("PCell", PCell(globalParameter))
        ...
        self.register("PCellLib")
    
    
    PCellLib()
    

    So essentially, the global value is passed to the PCell implementation class through the constructor and kept there as an attribute. It can then by used inside your code.

    Matthias

  • Hi Matthias,

    Your explanation did the trick! Thank you very much for providing the code example, it was really helpful.

    Chris

  • Very good :)

Sign In or Register to comment.