0

I'm looking to make a script in CAD which will return a National Grid Reference from and input Postcode

For example User input: AL1 1BY Returns: TL 14584 06989

I've got a CSV from OS "CodePoint - Open" which supplies the Postcode, Easting, Northing, Lat & Long which can be used to grab part of the grid reference, but getting the first 2 letter "TL" is the main issue as the csv doesn't provide this and I'm not sure on how this is calculated.

I've thought about making multiple CSVs (AL.csv, DN.csv, SL.csv...) with the postcodes in linking the Grid Reference to for example

AL.csv

AL1 1BY TL 14584 06989

AL1 1DQ TL 14524 06737

But would this cause issues with speed reading upwards of 10,000+ lines?

I've also wondered if its possible to implement an API call with AutoLisp to use a postcode to NGR code but this isn't something I've done before or know if its possible?

I haven't put anything into place because I'm struggling to think what the best way to implement this would be.

BlvckSZN
  • 15
  • 3

2 Answers2

0

I haven't worked with 10,000+ lines to read, but you can adjust the code below so that DraftSight will remember what it read after the first scan. If you need a source for autolisp functions, check here.

(defun C:Test01 (/ sFile FileID sLine sDelimiter lDetails sInfo *lPostCodes*)

    ;; Constant
    (setq sDelimiter " "); "\t" for tab deliminator

    ;; Manual File Path
    (setq sFile (getfiled "Select a file" "" "csv" 0))

    ; ;; Automatic file path
    ; (setq sFile "[Drive letter]:\\[File path]\\[File name.extension]")
    ; (setq sFile (findfile sFile))

    ;; Reading the file
    (setq FileID (open sFile "r"))
    (while (setq sLine (read-line FileID))
        
        ;; Reading contents
        (setq sInfo "")
        (while (> (strlen sLine) 0)
            (if (= (substr sLine 1 1) sDelimiter)
                (progn ;; True
                    (if (> (strlen sInfo) 0)(setq lDetails (cons sInfo lDetails)))
                    (setq sInfo "")
                );progn ; True
                ;; False
                (setq sInfo (strcat sInfo (substr sLine 1 1)))
            );if
            (setq sLine (substr sLine 2)); next character
        );while - Each character
        (if (/= (car lDetails) sInfo)(setq lDetails (cons sInfo lDetails)))

        ;; Updating list
        (setq lDetails (reverse lDetails))
        (setq *lPostCodes* (cons lDetails *lPostCodes*))
        (setq lDetails (list))
    );while - Each line
    
    ;; Closing the file
    (close FileID)

    ;; Returning value
    (setq *lPostCodes* (reverse *lPostCodes*))
    (princ "\nResults : ")(prin1 *lPostCodes*)(terpri)(princ)
);C:Test01
Command: TEST01
Results : (("AL1" "1BY" "TL" "14584" "06989") ("AL1" "1DQ" "TL" "14524" "06737"))
G Beck
  • 131
  • 1
  • 14
0

A few years ago I wrote some lisp to return various OSGB map tile references from a point clicked on the the screen. After stripping out the first coordinate and concatinating the two numbers. I used two arrays to convert from numbers to letters by iterating through the first array to find a match then.

;;Get x,y coordinates by left clicking point on screen
(setq p1 (getpoint "\nSelect a point:"))

;;Convert x,y coordinates to strings and store in seporate variables
(setq xcor (rtos (nth 0 p1) 2 0))
(setq ycor (rtos (nth 1 p1) 2 0))

;;Get first character of x & y coordinates and concatinate
(setq xy (strcat (substr xcor 1 1)(substr ycor 1 1)))

;;Build an ordered array of numbers
(setq array1 (list "00" "07" "08" "09" "10" "11" "12" "15" "16" "17" "18" "19" "20" "21" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" "40" "41" "42" "43" "44" "45" "46" "50" "51" "52" "53" "54" "61" "62" "63"))

;;Build an ordered array of equivalent Ordnance Survey(GB) letters
(setq array2 (list "SV" "NL" "NF" "NA" "SW" "SR" "SM" "NW" "NR" "NM" "NG" "NB" "SX" "SS" "SN" "SH" "SC" "NX" "NS" "NN" "NH" "NC" "SY" "ST" "SO" "SJ" "SD" "NY" "NT" "NO" "NJ" "ND" "SZ" "SU" "SP" "SK" "SE" "NZ" "NU" "TV" "TQ" "TL" "TF" "TA" "TR" "TM" "TG"))

;;start counter at 0
(setq i 0)

;;Iterate through first array and return array position of matched number
(repeat 47
    (if (= xy (nth i array1))(setq j i))
    (setq i (1+ i))
)

;;Return value from 2nd array using matched array position
(prompt (strcat "OS prefix = " (nth j array2)))