I want to create a LISP Routine for Autodesk Civil 3d 2022.
- User
I want to create a LISP Routine for Autodesk Civil 3d 2022.
- Assistant
Creating a Lisp routine for Autodesk Civil 3D 2022 involves writing a Lisp code that interacts with the software's API to automate certain tasks or add custom functionality. Here's an example of a simple Lisp routine that creates a new alignment in Civil 3D:
(defun c:create-alignment (name start-pt end-pt) (command "_ALIGNMENTCREATE" "_POLYLINE" name (list (list start-pt) (list end-pt))) ) (defun c:test-create-alignment () (setq name "My Alignment") (setq start-pt (getpoint "\nEnter start point: ")) (setq end-pt (getpoint "\nEnter end point: ")) (command "._undo" "_begin") (c:create-alignment name start-pt end-pt) (command "._undo" "_end") (princ) )To use this routine, follow these steps:
- Open Autodesk Civil 3D 2022.
- Open the LISP console by typing "Command: 'EX' " and hit Enter.
- Type "LOAD" in the LISP console and press Enter.
- Select the Lisp routine file (.lsp) you created and press Enter.
- Execute the routine by typing "c:test-create-alignment" in the command prompt and hitting Enter.
- User
I want a routine that will gather data from a layer that includes area of all hatches and the length of all lines and poly lines. then I want it to put that data into a CSV file and save it in a selected location.
- Assistant
Here's a Lisp routine that gathers data from a layer in Autodesk Civil 3D 2022, calculates the area of all hatched objects and the length of all lines and polylines on that layer, and exports the data to a CSV file of your choice:
(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (getfiled "Save CSV File" nil "Select a location to save the CSV file" "CSV Files (*.csv)")) (setq data '("Object Type" "Area" "Length")) ; Header of CSV file (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) (setq csv-str (apply 'strcat (mapcar '(lambda (x) (strcat (vl-string-quote x) ",")) data))) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved.") ) (defun c:test-gather-data () (c:gather-data) (princ) ) - User
an error occured while executing stating there are to many arguments in line 3.
- Assistant
Apologies for the error. The issue might be related to the usage of a function that expects point coordinates as separate arguments rather than a list. Let's modify the code to fix the error. Use the updated version provided below:
(defun c:create-alignment (name start-pt end-pt) (command "_ALIGNMENTCREATE" "_POLYLINE" name start-pt end-pt) ) (defun c:test-create-alignment () (setq name "My Alignment") (setq start-pt (getpoint "\nEnter start point: ")) (setq end-pt (getpoint "\nEnter end point: ")) (command "._undo" "_begin") (c:create-alignment name start-pt end-pt) (command "._undo" "_end") (princ) ) (c:test-create-alignment)The updated code fixes the issue by passing
start-ptandend-ptas separate arguments rather than as a list. Additionally, the(c:test-create-alignment)function call at the end ensures that the routine runs when loading the Lisp file.Load the updated Lisp routine as described earlier and execute it by running
(c:test-create-alignment). You will be prompted to enter the alignment name, start point, and end point for creating a new alignment in Autodesk Civil 3D. - User
Im talking about my gather data routine not the alignment
- Assistant
I apologize for the confusion. Let's correct the issue in the gather data routine. The modified version is provided below:
(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (getfiled "Save CSV File" nil "CSV Files (*.csv)")) (setq data '("Object Type" "Area" "Length")) ; Header of CSV file (setq csv-str "") ; Initialize CSV string (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= layer (vla-get-layer obj)) (progn (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) ) ) (foreach item data (setq csv-str (strcat csv-str (vl-string-quote (strcase (car item))) "," (vl-princ-to-string (cadr item)) ",")) ) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved.") ) (defun c:test-gather-data () (c:gather-data) (princ) ) (c:test-gather-data) - User
ok instead of prompting you where to save it just create a csv file and save to desktop.
- Assistant
Certainly! Here's the modified version of the Lisp routine that automatically saves the CSV file to the desktop:
(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (strcat (vl-filename-directory (getenv "TEMP")) "\\Desktop\\data.csv")) (setq data '("Object Type" "Area" "Length")) ; Header of CSV file (setq csv-str "") ; Initialize CSV string (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= layer (vla-get-layer obj)) (progn (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) ) ) (foreach item data (setq csv-str (strcat csv-str (vl-string-quote (strcase (car item))) "," (vl-princ-to-string (cadr item)) ",")) ) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved on the desktop.") ) (defun c:test-gather-data () (c:gather-data) (princ) ) (c:test-gather-data) - User
bad argument type: stringp nil this is the code I get after selecting the layer name
- Assistant
I apologize for any confusion caused. It appears that the issue might be related to the
nilvalue returned when selecting a layer that doesn't exist. To handle this situation, we can modify the code to check if the selected layer exists before proceeding. Here's an updated version of the routine:(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (strcat (vl-filename-directory (getenv "TEMP")) "\\Desktop\\data.csv")) ;; Check if the layer exists (if (tblsearch "layer" layer) (progn (setq data '("Object Type" "Area" "Length")) ; Header of CSV file (setq csv-str "") ; Initialize CSV string (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= layer (vla-get-layer obj)) (progn (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) ) ) (foreach item data (setq csv-str (strcat csv-str (vl-string-quote (strcase (car item))) "," (vl-princ-to-string (cadr item)) ",")) ) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved on the desktop.") ) (progn (princ "\nSpecified layer does not exist.") ) ) ) (defun c:test-gather-data () (c:gather-data) (princ) ) (c:test-gather-data) - User
same error has occured
- Assistant
Apologies for the inconvenience caused. The error might occur when the selected layer is not found in the "layer" table. To resolve the issue, we can use the
tblsearchfunction with "_all" as the search option to check for the layer's existence in the drawing. Here's the updated code:(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (strcat (vl-filename-directory (getenv "TEMP")) "\\Desktop\\data.csv")) ;; Check if the layer exists (if (tblsearch "_all" layer) (progn (setq data '("Object Type" "Area" "Length")) ; Header of CSV file (setq csv-str "") ; Initialize CSV string (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= layer (vla-get-layer obj)) (progn (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) ) ) (foreach item data (setq csv-str (strcat csv-str (vl-string-quote (strcase (car item))) "," (vl-princ-to-string (cadr item)) ",")) ) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved on the desktop.") ) (progn (princ "\nSpecified layer does not exist.") ) ) ) (defun c:test-gather-data () (c:gather-data) (princ) ) (c:test-gather-data) - User
I keep getting a break point in line 3 with the same error as stated before
- Assistant
Apologies for the continued issues. Let's modify the code further to handle the situation more effectively. Please try the updated version below:
(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (strcat (vl-filename-directory (getenv "TEMP")) "\\Desktop\\data.csv")) ;; Check if the layer exists (if (tblsearch "layer" (strcase layer)) (progn (setq data '("Object Type" "Area" "Length")) ; Header of the CSV file (defun add-to-data (obj) (if (= "HATCH" (vla-get-objectname obj)) (progn (setq area (vla-get-area obj)) (setq data (append data (list (vla-get-objectname obj) area))) ) ) (if (or (= "LINE" (vla-get-objectname obj)) (= "LWPOLYLINE" (vla-get-objectname obj))) (progn (setq length (vla-get-length obj)) (setq data (append data (list (vla-get-objectname obj) length))) ) ) ) (vlax-for obj (vlax-invoke (vla-get-activedocument (vlax-get-acad-object)) 'ModelSpace) (if (= layer (strcase (vla-get-layername obj))) (add-to-data obj) ) ) (setq csv-str "") ; Initialize CSV string (foreach item data (setq csv-str (strcat csv-str (vl-string-quote (strcase (car item))) "," (vl-princ-to-string (cadr item)) ",")) ) (setq csv-file (open csv-file "w")) (write-line csv-str csv-file) (close csv-file) (princ "\nData gathering complete. CSV file saved on the desktop.") ) (progn (princ "\nSpecified layer does not exist.") ) ) ) (defun c:test-gather-data () (c:gather-data) (princ) ) (c:test-gather-data) - User
on line 26 I get a member not found error
- Assistant
Apologies for the oversight. It seems there was an error in accessing the layer name property. Let's modify the code to fix the issue:
(defun c:gather-data () (setq layer (getstring "\nEnter layer name: ")) (setq csv-file (strcat (vl-filename-directory (getenv "TEMP")) "\\Desktop\\data.csv