使用 for 迴圈將圖形新增至圖形框你好,
我正在嘗試使用 for 循環將圖形(矩形)添加到圖形框中。 預期結果是由 31 個矩形組成的十字架。 我有一個工作腳本,我在其中逐行編寫程式碼,但希望使其更短且更具可擴展性。
下面的腳本正在運行,但圖形框中僅顯示最後一個矩形。
當我用兩行「編寫腳本時新增圖形腳本「(在腳本底部註解掉)它新增了 2 個矩形,而不是 for 迴圈。 我本來希望 for 迴圈也能做同樣的事情。 我怎樣才能做到這一點?
Names default to here(1);
//dt that holds the parameters for the rectangles
dt_loc = open (momo_dir || "rectangle_locations.csv" );
//create a graphic box
base_box = graph Box(
Frame Size( 400, 400 ),
XName(""), XScale(19,81), yScale(19,91), SuppressAxes, Pen Color( 1 );
);
//define the frame box in which the rectangles will e displayed
framebox = base_box[frame box( 1 )];
//ran a for loop for all 31 rectangles to be added to the Base_box
For( f = 1, f <= N Rows(dt_loc), f++,
//f=1;
//location/sizes of each box is defined here based on a csv file
rec_left = dt_loc:left[f];
rec_top = dt_loc:top[f];
rec_right = dt_loc:right[f];
rec_bottom = dt_loc:bottom[f];
//add the graphic script to the base_box for each row (aka rectangle) in the table
framebox << Add Graphics Script(Rect(rec_left, rec_top, rec_right, rec_bottom, fill = 0););
); //end f for loop
try(close(dt_loc, no save));
//Running the following 2 lines will add 2 rectangles
//framebox << Add Graphics Script(Rect(40,75,50,70, fill = 0););
//framebox << Add Graphics Script(Rect(40,70,50,65, fill = 0););
cross_rep = New window ("Cross", vlistbox(base_box));自動化和腳本編寫實驗設計大規模客製化回覆:使用 for 迴圈將圖形新增至圖形框一個後續問題/評論。
dt_loc 變數未傳遞到圖形框中。 我發現有兩個選項可以讓它工作:
- 開啟圖形框中的 dt_loc 表(如果我僅在腳本的該部分使用該表,則該表有效)
- 使 dt_loc 成為全域變數 (::dt_loc)
有沒有其他方法可以讓 dt_loc 變數在圖形框中可用?
謝謝
奧萊
回覆:使用 for 迴圈將圖形新增至圖形框我同意。 第二個腳本確實簡潔明了。 我也嘗試了圖形框中的 for 循環,但腳本中一定存在錯誤,導致其無法工作。
多謝。
奧萊
回覆:使用 for 迴圈將圖形新增至圖形框完美的。 那行得通。 非常感謝。
奧萊
回覆:使用 for 迴圈將圖形新增至圖形框圖形腳本稍後運行,每當圖形需要更新(縮放等)時。 您在一種情況下指定了變量,在另一種情況下指定了硬編碼數字。 您可以透過多種方式將變數轉換為數字。 我通常這樣做的方式看起來像這樣:
eval(evalexpr(
framebox << Add Graphics Script(Rect(expr(rec_left), expr(rec_top), expr(rec_right), expr(rec_bottom), fill = 0););
));
它用 expr(...) 包裝變量,使用 evalexpr(...) 將 expr(...) 替換為實際值,並使用 eval(...) 計算 evalexpr(...) 傳回的表達式) 。 其他人可能會表現出他們喜歡的方式。 <編輯>我喜歡@tx尼爾森第二個答案。
回覆:使用 for 迴圈將圖形新增至圖形框您使腳本變得比需要的複雜得多。以下是對您的腳本的 2 處修改。第一個是獲取您的流程,並將其轉換為工作模型。第二個是按照您的要求使其非常簡潔(小)。
Names Default To Here( 1 );
//dt that holds the parameters for the rectangles
dt_loc = Open( momo_dir || "rectangle_locations.csv" );
//create a graphic box
base_box = Graph Box(
Frame Size( 400, 400 ),
XName( "" ),
X Scale( 19, 81 ),
Y Scale( 19, 91 ),
SuppressAxes,
Pen Color( 1 );
//ran a for loop for all 31 rectangles to be added to the Base_box
For( f = 1, f <= N Rows( dt_loc ), f++,
// location/sizes of each box is defined here based on a csv file
rec_left = dt_loc:left[f];
rec_top = dt_loc:top[f];
rec_right = dt_loc:right[f];
rec_bottom = dt_loc:bottom[f];
// Draw the rectangle
Rect( rec_left, rec_top, rec_right, rec_bottom, fill = 0 );
);
);
Try( Close( dt_loc, no save ) );
cross_rep = New Window( "Cross", V List Box( base_box ) );
Names Default To Here( 1 );
//dt that holds the parameters for the rectangles
dt_loc = Open( momo_dir || "rectangle_locations.csv" );
cross_rep = New Window( "Cross",
//create a graphic box
base_box = Graph Box(
Frame Size( 400, 400 ),
XName( "" ),
X Scale( 19, 81 ),
Y Scale( 19, 91 ),
SuppressAxes,
Pen Color( 1 );
//ran a for loop for all 31 rectangles to be added to the Base_box
For( f = 1, f <= N Rows( dt_loc ), f++,
// location/sizes of each box is defined here based on a csv file
Rect( dt_loc:left[f], dt_loc:top[f], dt_loc:right[f], dt_loc:bottom[f] );
);
)
);
Try( Close( dt_loc, no save ) );