ScriptBuilder bad indentation on elif and else

Hi all,

Look at what indentation this code snippet from typescript gives. Typescript:

// only for testing
builder.ifCondition("myVar")
    builder.localVariable("resultVar", "1")
builder.elseIfCondition("yourVar")
    builder.localVariable("resultVar", "2")
builder.end()

When i view the resulting code in the script view of my robot(simulation) it appears like this:

   if (myVar):
     local resultVar = 1
     elif (yourVar):
       local resultVar = 2
     end

Why is the indentation off? I expected the ‘elif’ line to be at the same indentation level as the ‘if’ line.
The same thing happens on else statements:

         if (bolltecDCTrackTimer < 10 ):
           set_standard_digital_out(5, False)
           else:
             set_standard_digital_out(5, True)
           end

Also everything uses double space indentation. I prefer tabs, which is irrelevant i suppose, but more importantly it is inconsistent with the default code in the script file:

   def ur_get_joint_speeds_before_offset(previous_q, time):
       local current_q = get_joint_positions()
       local delta_q = current_q - previous_q
       return delta_q / time
   end

Solved, you use the endBlock method:

builder.ifCondition("myVar")
    builder.localVariable("resultVar", "3")
    builder.endBlock()
builder.elseIfCondition("yourVar")
    builder.localVariable("resultVar", "4")
builder.end()

Gives:

   if (myVar):
     local resultVar = 3
   elif (yourVar):
     local resultVar = 4
   end

It seems silly to me. Would you not always want to endblock when you reach the next elif? So why require it written explicitly? Anyway.

I also thought maybe i could change the indentation to tabs with this line:

static readonly SINGLE_INDENT = "    ";

from script-builder.ts but that did not work, so I am still looking for solutions for that.

I just use writer.appendLine() for every single command I have the CAP write. I can have a different editor format it or whatever and then I just slap the syntax on at the beginning.

1 Like

That is actually a great idea, and probably what I should have done from the start. You would even have proper linting and stuff if you could move your whole URscript program into a separate file. You would just need a way to read the file and, as you say, slap the syntax on at the beginning.

String str = "";
        InputStream is = getClass().getResourceAsStream("/Scripts/yourScriptFilenameHere");
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            if (is != null) {                            
                while ((str = reader.readLine()) != null) {   
                   if(str.startsWith("#")) {
                	   
                   }
                   else {
                       writer.appendLine(str);
                   }
                }                
            }
            
            reader.close();
        } catch (IOException e) {
			e.printStackTrace();
		} finally {
            try { is.close(); } catch (Throwable ignore) {}    
        }

Pretty sure I lifted this code straight from one of the sample UR CAPs. It reads the script from the file and injects it as writer.append calls. Just run it from the generate script method. Pretty nice because VSCode has a URScript option, so you still get syntax highlighting and such. Use “#” to comment out any lines you don’t want added to the script.

1 Like