- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm not sure why my script works 10 percent of the time but any help would be great. Essentially I have a LISP that accesses an exe file that creates a CSV where a row represents a point on a polygon and the columns represent the polygon it is part of, the y coordinate, and the x coordinate. The order the CSV lists the points is the order they should be drawn in (I have attached a CSV of what the output looks like). The LISP then accesses the CSV and runs through the points creating the necessary polylines for some reason the output changes between runs and I'm not sure why (LISP shared as well).
(defun c:DrawPolylineFromCSV ()
;; Function to split a line by a delimiter
(defun split-line (line delimiter)
(if (not (vl-string-search delimiter line))
(list line)
(cons (substr line 1 (vl-string-search delimiter line))
(split-line (substr line (+ (vl-string-search delimiter line) 2)) delimiter))))
;; Function to create a polyline from the points
(defun create-polyline (points)
;; Start the polyline command
(if points
(progn
(print points)
(command "_.pline")
;; Iterate over each point in the points list
(foreach point points
(print point)
(command point))
(command "Close")
;; End the polyline command
(command ""))))
;; Function to read the CSV and extract points
(defun read-csv (file-path)
(setq points '())
(setq polyind 1)
(setq file (open file-path "r"))
;; Skip the first line (header)
(read-line file)
;; Read the file line by line
(while (setq line (read-line file))
(setq fields (split-line line ","))
(setq polycount (atoi (nth 0 fields))) ;; Convert to integer
(setq latitude (distof (nth 1 fields)))
(setq longitude (distof (nth 2 fields)))
;; Check for valid numeric values
(if (= polycount polyind)
(setq points (append points (list (list longitude latitude))))
(progn
(create-polyline points) ;; Create polyline for previous points
(setq points '()) ;; Start new polyline
(setq polyind (+ polyind 1))))) ;; Update polygon index
;; Handle the last polyline
(close file)
)
;;(setq start-time (getvar "cputicks"))
;; Main code starts here
(setq file-path (getfiled "Select CSV File" "" "csv" 4))
(if file-path
(progn
(princ "\nProcessing CSV...")
(read-csv file-path)
(princ "\nCompleted successfully!"))
(princ "\nNo file selected."))
;;(setq checkpoint (getvar "cputicks"))
;;(setq elapsed-op (- checkpoint start-time))
;;(print elapsed-op)
)
Solved! Go to Solution.