Line width in macro


#1

Hi everyone,

I have this macro which opens a txt file containing x and y coordinates of points that will be used to draw a polyline selection on an opened picture

chemintotal="D:\\Documents\\path_to_nodes_txt\\nodes.txt"
hauteur=getHeight();

filestring=File.openAsString(chemintotal);
	rows=split(filestring, "\n");
	nb_rows=rows.length;
	x=newArray(nb_rows); 
	y=newArray(nb_rows); 
	for(k=0; k<nb_rows; k++){ 
		columns=split(rows[k],"\t"); 
		x[k]=parseInt(columns[0]); 
		y[k]=hauteur-parseInt(columns[1]); 
	}
makeSelection("polyline",x,y,nb_rows)

Before launching this macro, I choose manually the line width with the menu (Edit>Option>Line Width). This works great and the yellow area around my polyline depend on the input value.

I’m trying to use this macro in a more “complex” macro that do the same for several pictures. As pictures are different, the line width has to be adjusted by the user. I write the following macro with some dialog boxes. The previous macro is the get_nodes function in the folllowing macro.

for (i=0;i<nb_photo_cone;i++){                                          //nb_photo_cone is defined in another part of the macro
chemin_photo=chemin_dossier_photo_cone_array[i]; //this array is defined in another part of the macro
numero_photo=numero_photo_cone_array[i];             //this array is defined in another part of the macro
waitForUser("Photo n " + numero_photo);

open(chemin_photo+"temp"+File.separator + "interface.tif");
hauteur=getHeight();                                                    // this variable is defined at the beginning of the macro thx to var command
waitForUser("some text.");

line_width=20;    // this variable is defined at the beginning of the macro thx to var command

while (conserver_line_width_checkbox_value==false){
	dialog_box_choix_line_width(line_width);
	run("Line Width...", "line="+line_width+"");
	run("Select None");
	get_nodes(chemin_photo);
	waitForUser("Check the yellow area. Is everything is ok, check the next checkbox");
	dialog_conserver_line_width(line_width);
}
}
function dialog_box_choix_line_width(line_width){
	label="Merci d'indiquer la valeur de ''Line width''.";
	Dialog.create("Line Width");
	Dialog.addMessage(label);
	Dialog.addNumber("Line Width :",line_width);
	Dialog.show();
	line_width = Dialog.getNumber();
	waitForUser(line_width);
}

function dialog_conserver_line_width(line_width) {
	label="Merci de confirmer si vous voulez conserver cette line width.";
	Dialog.create("Confirmer le line width ?");
	Dialog.addMessage(label);
	Dialog.addCheckbox("Oui?",false);
	Dialog.show();
	conserver_line_width_checkbox_value = Dialog.getCheckbox();
}

function get_nodes(chemin_photo){
	chemintotal=chemin_photo + "temp"+ File.separator + "nodes.txt";
	filestring=File.openAsString(chemintotal);
	rows=split(filestring, "\n");
	nb_rows=rows.length;
	x=newArray(nb_rows); 
	y=newArray(nb_rows); 
	for(k=0; k<nb_rows; k++){ 
		columns=split(rows[k],"\t"); 
		x[k]=parseInt(columns[0]); 
		y[k]=hauteur-parseInt(columns[1]); 
	}
	makeSelection("polyline",x,y,nb_rows);
}

Even if I modify the line_width in the dialog_box_choix_line_width function, the yellow area defined by the get_nodes function didn’t change and stays the same as the first attempt.

Have you any idea?

Thanks,

Fabien


#2

find the origin of the problem but I don’t understand the reason.

If I write :

while (conserver_line_width_checkbox_value==false){
	dialog_box_choix_line_width(line_width);
	waitForUser(line_width);  //waitForUser n°2
	run("Line Width...", "line="+line_width);
	run("Select None");
	get_nodes(chemin_photo);
	dialog_conserver_line_width(line_width);
}

and

function dialog_box_choix_line_width(line_width){
	label="Merci d'indiquer la valeur de ''Line width''.";
	Dialog.create("Line Width");
	Dialog.addMessage(label);
	Dialog.addNumber("Line Width :",line_width);
	Dialog.show();
	line_width = Dialog.getNumber();
	waitForUser(line_width); //WaitForUser n°1
}

At the end of the function, the WaitForUser n°1 shows the correct line_width. But just after this function, the waitForUser N°2 shows the line_width of the beginning.

any suggestion?

At the begginnig of the whole macro, variables are defined like th following:

   var macro_chemin_complet,split_macro_chemin_complet=newArray(),nombre_dossier_macro_chemin,macro_chemin_complet_sans_fin,chemin_photo_cone_array=newArray(),macro_chemin_complet_sans_fin=newArray();
var numero_photo_cone_array=newArray(),nom_photo_cone_complet_array=newArray(),chemin_photo_cone_array=newArray(),liste_photo_cone_avec_oeillets_array=newArray(),chemin_dossier_photo_cone_array=newArray();
var cheminTemp=getDirectory("current");
var chemintotal;
var list;
var nom_fichier;
var nom_fichier_sans_ext;
var x=newArray();
var y=newArray();
var filestring;
var fileName;
var chemin_dossier;
var hauteur;
var conserver_line_width_checkbox_value=false;
var line_width;

EDIT :
Find the solution, but I don’t understand the reason.

If I change the variable name in the dialog_box_choix_line_width function (line_width_test instead of line_width), it works great:

function dialog_box_choix_line_width(line_width_test){
    label="Merci d'indiquer la valeur de ''Line width''.";
    Dialog.create("Line Width");
    Dialog.addMessage(label);
    Dialog.addNumber("Line Width :",line_width_test);
    Dialog.show();
    line_width_test = Dialog.getNumber();
    line_width=line_width_test;
    waitForUser("fin macro :" + line_width);
}

So there is something in function use, that I don’t understand. Could anyone explain me why I can’t use the same variable name in the function and outside the function? (I did it several times in other macro/function without any problem.)

Thanks


#3

Hi @Fabien,

I can replicate your problem, and I’m not familiar enough with global variables to figure out what is going wrong - but I’m glad that you solved it! I’m sure somebody else can contribute more knowledge on that matter.


However, I did have an idea that might help you with your macro, it involves adding a tool/shortcut to the startup macros in Plugins > Macros > Startup Macros.

During your macro you want to check if the line width is suitable for the structure you are looking at, a way you could do this is to use a shortcut key(s) (that activates a small macro) which decreases/increases the line width. I have written this very quickly below:

var line_width

macro "Line width increase [t]" {
		line_width += 1;
		run("Line Width...", "line=" + line_width);
		run("Properties... ", "  width=" + line_width);
        print("Width = " + line_width);
    }    

macro "Line width decrease [g]" {
		line_width -= 1;
		run("Line Width...", "line=" + line_width);
		run("Properties... ", "  width=" + line_width);
        print("Width = " + line_width);
    }   

There is a problem with updating the live line width using run("Line Width...", "line="+line_width);, see topic here: Change line weight in selection. So, instead I have used the properties to update the line width live, so you can see the width as you decrease or increase it using the g (decrease) or t (increase) keys.

This means that during your macro you dont have to ask the user for an arbitrary line width, but instead can use a waitForUser("Update line width"); line in your code and then the user can use the keyboard shortcuts to find a good line width and just press “OK”, rather than going through the dialog boxes that you have included in your macro and adjusting the number every time.

The problem with this being that they’d have to update their StartupMacro.txt file and that it shares the line_width variable name with your code, so you might want to update that. I also don’t know if it will function exactly as expected, I’ve noticed that sometimes it has a transparent appearance and sometimes it is opaque. But I thought it might be something you could work with?

Best,

Rob


#4

Hi Rob,

Thanks a lot for your answer and sorry for my late reply. I will try your code and tell you the result.

Regards

Fabien


#5

Hi Rob,

Thanks for your advice. The g/t increase/decrease function works great. I don’t look for automatically update the line width variable. As the line width is printed in the log, I just have to type it in my dialog box.

Thanks!