Hi Everyone,
In this weeks' post we'll uncover the secret behind writing point information from a string file to a csv file by simply clicking on points in graphics. The code block below is the entire macro, an astonishing (for the functionality it gives you) 12 lines long. I've left out some error trapping for simplicity, so you'll need to ensure you have data in a layer before running the macro. My code comments are italicized and in bold so you can learn, edit and hopefully write a variation of this macro that suits your needs.
See if you can vary the code to do the following;
- Rearrange the column order (Y, X, Z)
- Write the selected points to a different file name
- Print the layer name to the message window
- Calculate the average X value from all the points selected (You'll need the "expr" command for this and some other variables.)
Good luck, and as always, if you have any questions please leave them in the comments section below.
Cheers,
Ash
# This opens the CSV file. The "w" means that we can write to it. a+ (append) and r (read) are other common switches. set writeFile [open points.csv "w"] # The puts statement with two parameters allows us to write information to the opened file. Note we are referencing \$writeFile. puts \$writeFile "X,Y,Z,D1,D2,D3,D4,D5,D6,D7,D8,D9,D10" # The while loop allows us to select as many points as the user wants until escape is pressed. while {1} { # The SclSelectPoint command allows us to capture information about any point we click on. That information is stored in the variables -> layer, strNum, segNum, pntNum, x, y, z, desc. set status [SclSelectPoint point "Select points to write to CSV file - Press Escape to finish." layer strNum segNum pntNum x y z desc] # The if statement below is how we exit the macro once someone presses escape. if {\$status!= "\$SCL_OK"} { break } # Once again we are writing information into the csv file opened at the start. Note we are referencing \$writeFile again. puts \$writeFile "\$x,\$y,\$z,\$desc" } # You should always close any file that you open. The code below does this. close \$writeFile # Write a message to the user in Surpac's message window using the "puts" command. puts "Selected points written to points.csv in current working directory"