adrath Posted Thursday at 02:51 PM Posted Thursday at 02:51 PM (edited) Hello all! I work in the land development field and I'm trying to use Microsoft Copilot to write some LISP routines to make our sheet production easier on multi-phase subdivision projects. Each phase will have a separate job folder with separate xref's, C3D data files and a sheetset. Our base files will follow a naming convention that includes the unit number. So for unit 1 the base file would be X-BASE-UN01 and so on. In our base files for a given unit, we'll have a file called X-BASE-PREV-UNXX. This will have no data in and of itself, but will contain attached xref's of previous unit base files that need to be shown as existing on the current unit plans. It's essentially a one stop shop so I don't have to go hunting through 4 different job folders to find the previous unit base files every time I set up a new sheet. I'm wanting this LISP routine to do the following: 1. Prompt the user for a path to look for xref's. 2. Prompt the user for the current phase number 3. Prompt the user for previous phase numbers 4. Bring in all dwg files in the supplied location, with the exception of the titleblock, legend base and detail base. 5. Place the xref's on the X-REF layer (and make one if it doesn't exist) and lock it. 6. Analyze the layers in the drawing and change the colors of any layer in an xref that matches the user input for previous phases to 251. Eventually, I'll expand this into several LISP routines and have them do specific layer control for the sheet depending on the discipline (i.e. one for street sheets, one for sewer, one for water one for drainage). That's why I have it asking for the current unit number. But I need to get this basic framework in place first. The code that Copilot is giving me will go through the first three steps just fine but then returns an ;error too few arguments when it comes time to bring in the references. I've pasted the code below. I appreciate any light y'all can shed on this since I know very little about LISP coding or how it works (why I'm trying to use CoPilot to do this). (defun c:BatchXref ( / folder files file skipList fullpath curPhase prevPhases phaseList layerTable layerName xrefParts matched layerEnt layerData) (setq skipList '("X-TTLB.dwg" "X-DETL.dwg" "X-LGND.dwg")) ;; Prompt for folder path using file dialog (setq folder (getfiled "Select any DWG file in target folder" "" "dwg" 8)) (if folder (progn ;; Ensure folder ends with a backslash (setq folder (vl-filename-directory folder)) (if (/= (substr folder (strlen folder)) "\\") (setq folder (strcat folder "\\"))) ;; Prompt for current phase number (setq curPhase (getstring "\nEnter current phase number (e.g., 03): ")) ;; Prompt for previous phase numbers (setq prevPhases (getstring "\nEnter previous phase numbers (comma-separated, e.g., 01,02): ")) (setq phaseList (mapcar (function (lambda (x) (strcase (strcat "X-BASE-UN" x)))) (vl-remove "" (mapcar 'vl-string-trim (vl-string->list (vl-string-translate "," " " prevPhases)))) ) ) ;; Display phase info (princ (strcat "\nCurrent Phase: X-BASE-UN" curPhase)) (princ (strcat "\nPrevious Phases: " (apply 'strcat (mapcar (function (lambda (p) (strcat " " p))) phaseList)))) ;; Create X-REF layer if needed (if (not (tblsearch "LAYER" "X-REF")) (command "_.-LAYER" "_Make" "X-REF" "_Color" "7" "X-REF" "") ) ;; Get DWG files (setq files (vl-directory-files folder "*.dwg" 1)) ;; Insert XREFs (foreach file files (if (not (member (strcase file) (mapcar 'strcase skipList))) (progn (setq fullpath (strcat folder file)) (command "_.-XREF" "_Overlay" fullpath '(0 0 0) 1 1 0) (command "_.CHPROP" "L" "" "_LA" "X-REF" "") ) ) ) ;; Lock the layer (command "_.-LAYER" "_Lock" "X-REF" "") ;; Change color of layers with previous phase names in XREF or nested XREF (setq layerTable (tblnext "LAYER" T)) (while layerTable (setq layerName (cdr (assoc 2 layerTable))) (setq matched nil) ;; Split layer name into parts (XREF nesting) (setq xrefParts (vl-string-split layerName "|")) ;; Check each part for a match with previous phase names (foreach part xrefParts (if (member (strcase part) phaseList) (setq matched T) ) ) ;; If matched, change layer color using entmod (if matched (progn (setq layerEnt (tblobjname "LAYER" layerName)) (if layerEnt (progn (setq layerData (entget layerEnt)) (if (assoc 62 layerData) (setq layerData (subst (cons 62 251) (assoc 62 layerData) layerData)) (setq layerData (append layerData (list (cons 62 251)))) ) (entmod layerData) (entupd layerEnt) ) ) ) ) (setq layerTable (tblnext "LAYER")) ) (princ "\nOverlay XREFs added. Layers in matching XREFs and nested XREFs set to color 251.") ) (princ "\nNo folder selected.") ) (princ) ) Edited Thursday at 06:02 PM by SLW210 Added Code Tags!! Quote
SLW210 Posted Thursday at 06:03 PM Posted Thursday at 06:03 PM In the future, please use Code Tags for your code. (<> in the editor toolbar) Quote
rlx Posted Thursday at 08:47 PM Posted Thursday at 08:47 PM (edited) without the actual drawings unable to test this so this is untested : (defun c:BatchXref ( / skipList folder curPhase prevPhases phaseList ) (setq skipList '("X-TTLB.dwg" "X-DETL.dwg" "X-LGND.dwg")) (setq folder (GetShellFolder "Select target folder")) ;;; Prompt for current phase number (setq curPhase (getstring "\nEnter current phase number (e.g., 03): ")) ;;; Prompt for previous phase numbers (setq prevPhases (getstring "\nEnter previous phase numbers (comma-separated, e.g., 01,02): ")) ;;; split up previous phases (setq phaseList (SplitStr prevPhases ",")) ;; Display phase info (princ (strcat "\nCurrent Phase: X-BASE-UN" curPhase)) (princ (strcat "\nPrevious Phases: " (apply 'strcat (mapcar (function (lambda (p) (strcat " " p))) phaseList)))) ;;; Create X-REF layer if needed (if (not (tblsearch "LAYER" "X-REF")) (command "_.-LAYER" "_Make" "X-REF" "_Color" "7" "X-REF" "")) ;;; Get DWG files (if (vl-consp (setq files (vl-directory-files folder "*.dwg" 1))) (progn ;;; Insert XREFs (foreach file files (if (not (member (strcase file) (mapcar 'strcase skipList))) (progn (setq fullpath (strcat folder file)) (command "_.-XREF" "_Overlay" fullpath '(0 0 0) 1 1 0) (command "_.CHPROP" "L" "" "_LA" "X-REF" "") ) ) ) ;;; Lock the layer (command "_.-LAYER" "_Lock" "X-REF" "") ;;; Change color of layers with previous phase names in XREF or nested XREF (setq layerTable (tblnext "LAYER" T)) (while layerTable (setq layerName (cdr (assoc 2 layerTable))) (setq matched nil) ;;; *** vl-string-split *** made up by glorified paperclip ;;; Split layer name into parts (XREF nesting) (setq xrefParts (SplitStr layerName "|")) ;;; Check each part for a match with previous phase names (foreach part xrefParts (if (member (strcase part) phaseList) (setq matched T))) ;;; If matched, change layer color using entmod (if matched (progn (setq layerEnt (tblobjname "LAYER" layerName)) (if layerEnt (progn (setq layerData (entget layerEnt)) (if (assoc 62 layerData) (setq layerData (subst (cons 62 251) (assoc 62 layerData) layerData)) (setq layerData (append layerData (list (cons 62 251)))) ) (entmod layerData) (entupd layerEnt) ) ) ) ) (setq layerTable (tblnext "LAYER")) ) (princ "\nOverlay XREFs added. Layers in matching XREFs and nested XREFs set to color 251.") ) (princ "\nNo folder selected / files to proces") ) (princ) ) ;;; s = string d = delimiter p = position delimiter (setq r (SplitStr "01,02" ",")) -> '("01" "02") (defun SplitStr ( s d / p ) (if (setq p (vl-string-search d s))(cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s))) ;;; (setq f (GetShellFolder "Select a folder")) -> "C:\\Temp\\Lisp\\" (defun GetShellFolder ( m / f s) (if (and (setq s (vlax-create-object "Shell.Application")) (setq f (vlax-invoke s 'browseforfolder 0 m 65536 "")))(setq f (vlax-get-property (vlax-get-property f 'self) 'path)) (setq f nil))(vl-catch-all-apply 'vlax-release-object (list s)) (if f (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" f)) "\\"))) Just a couple remarks : getfolder (or GetShellFolder) is more generic than selecting a drawing and stripping out path. It's not wrong but soooo last century. ChatGPT or Copilot : stop using them and learn to do it yourself. As long as those glorified paperclips are not star-trek level you can't trust them. They make up commands like in your code : (setq xrefParts (vl-string-split layerName "|")) , maybe somebody at one time created this (vl-string-split) as a custom defun but in my visual lisp editor it didn't turn blue so its not a core command. I replaced it with SplitStr. Oh I also don't see a save command anywhere so I assume that's handled by you or Copilot? I hope code above works , if not... bite me Edited Thursday at 08:57 PM by rlx 1 Quote
adrath Posted 2 hours ago Author Posted 2 hours ago On 9/25/2025 at 1:03 PM, SLW210 said: In the future, please use Code Tags for your code. (<> in the editor toolbar) Sorry. I'll do that next time. Quote
adrath Posted 2 hours ago Author Posted 2 hours ago On 9/25/2025 at 3:47 PM, rlx said: without the actual drawings unable to test this so this is untested : (defun c:BatchXref ( / skipList folder curPhase prevPhases phaseList ) (setq skipList '("X-TTLB.dwg" "X-DETL.dwg" "X-LGND.dwg")) (setq folder (GetShellFolder "Select target folder")) ;;; Prompt for current phase number (setq curPhase (getstring "\nEnter current phase number (e.g., 03): ")) ;;; Prompt for previous phase numbers (setq prevPhases (getstring "\nEnter previous phase numbers (comma-separated, e.g., 01,02): ")) ;;; split up previous phases (setq phaseList (SplitStr prevPhases ",")) ;; Display phase info (princ (strcat "\nCurrent Phase: X-BASE-UN" curPhase)) (princ (strcat "\nPrevious Phases: " (apply 'strcat (mapcar (function (lambda (p) (strcat " " p))) phaseList)))) ;;; Create X-REF layer if needed (if (not (tblsearch "LAYER" "X-REF")) (command "_.-LAYER" "_Make" "X-REF" "_Color" "7" "X-REF" "")) ;;; Get DWG files (if (vl-consp (setq files (vl-directory-files folder "*.dwg" 1))) (progn ;;; Insert XREFs (foreach file files (if (not (member (strcase file) (mapcar 'strcase skipList))) (progn (setq fullpath (strcat folder file)) (command "_.-XREF" "_Overlay" fullpath '(0 0 0) 1 1 0) (command "_.CHPROP" "L" "" "_LA" "X-REF" "") ) ) ) ;;; Lock the layer (command "_.-LAYER" "_Lock" "X-REF" "") ;;; Change color of layers with previous phase names in XREF or nested XREF (setq layerTable (tblnext "LAYER" T)) (while layerTable (setq layerName (cdr (assoc 2 layerTable))) (setq matched nil) ;;; *** vl-string-split *** made up by glorified paperclip ;;; Split layer name into parts (XREF nesting) (setq xrefParts (SplitStr layerName "|")) ;;; Check each part for a match with previous phase names (foreach part xrefParts (if (member (strcase part) phaseList) (setq matched T))) ;;; If matched, change layer color using entmod (if matched (progn (setq layerEnt (tblobjname "LAYER" layerName)) (if layerEnt (progn (setq layerData (entget layerEnt)) (if (assoc 62 layerData) (setq layerData (subst (cons 62 251) (assoc 62 layerData) layerData)) (setq layerData (append layerData (list (cons 62 251)))) ) (entmod layerData) (entupd layerEnt) ) ) ) ) (setq layerTable (tblnext "LAYER")) ) (princ "\nOverlay XREFs added. Layers in matching XREFs and nested XREFs set to color 251.") ) (princ "\nNo folder selected / files to proces") ) (princ) ) ;;; s = string d = delimiter p = position delimiter (setq r (SplitStr "01,02" ",")) -> '("01" "02") (defun SplitStr ( s d / p ) (if (setq p (vl-string-search d s))(cons (substr s 1 p) (SplitStr (substr s (+ p 1 (strlen d))) d)) (list s))) ;;; (setq f (GetShellFolder "Select a folder")) -> "C:\\Temp\\Lisp\\" (defun GetShellFolder ( m / f s) (if (and (setq s (vlax-create-object "Shell.Application")) (setq f (vlax-invoke s 'browseforfolder 0 m 65536 "")))(setq f (vlax-get-property (vlax-get-property f 'self) 'path)) (setq f nil))(vl-catch-all-apply 'vlax-release-object (list s)) (if f (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" f)) "\\"))) Just a couple remarks : getfolder (or GetShellFolder) is more generic than selecting a drawing and stripping out path. It's not wrong but soooo last century. ChatGPT or Copilot : stop using them and learn to do it yourself. As long as those glorified paperclips are not star-trek level you can't trust them. They make up commands like in your code : (setq xrefParts (vl-string-split layerName "|")) , maybe somebody at one time created this (vl-string-split) as a custom defun but in my visual lisp editor it didn't turn blue so its not a core command. I replaced it with SplitStr. Oh I also don't see a save command anywhere so I assume that's handled by you or Copilot? I hope code above works , if not... bite me Thanks for the reply, RLX! I would love to learn how to do this myself, but I've had a hard time finding any instructional resources. Seems you just have to work with somebody that knows and get taught. Autodesk has very little about writing LISP's. If you know of any resources, I'm all ears. I would rather be in charge of the coding than doing this iterative, dystopian machine-building-machine nonsense. But for now, CoPilot writes a code, I read the code and get a sense of what it does, then I change the code and read it again and I'm at least starting to lay the groundwork that way. As far as your code, it didn't throw any errors and it brought in all the xref's but it didn't change the layer colors. I do like the folder dialogue you used better though and not throwing errors is a huge step in the right direction. Thank you. I'll keep messing with it. Quote
rlx Posted 1 hour ago Posted 1 hour ago (edited) Haven't really looked at the rest of your code , simply changed the getfolder & split text part and pasted the rest. As far for learning , there are lots of posts here on this site asking the same thing so no use summing them up all again but Lee Mac's site and Afralisp are good places to start. Just kiss... (keep it simple stupid) , start easy and if you like it enough you'll find the motivation to take it next level. Lisp has some quirks & limitations but for many (repeating) tasks you can get great results with relatively little effort once you get the hang of it. But some lisp concepts will need a little time to sink in (if ever). 3d and matrixes for example are not really my thing but that's because I use them so little , when I need them I beg , steal , borrow , cut , copy & paste something together until it works and forget all about it until I need it again. Edited 1 hour ago by rlx Quote
BIGAL Posted 40 minutes ago Posted 40 minutes ago You are right about learning lisp the CAD suppliers give you example of how to use a lisp function but really no actual step by step tutorials. Look for Graden path tutorial if using Acad. A good tutorial. Ther are some good books out there I found some on kindle very cheap and can copy code from the electronic book. Also a good reference is 'The visual Lisp Developers Bible" Agree Afralisp has come good tutorials. A comment "6. Analyze the layers" we used to process output from other software brought into cad so had a text file with all the layer details, Oldname Newname etc. Eg for road surveys had a existing layer LIP it was renamed EX-LIP and color changed, when designing a layer called LIP was known then as a design layer. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.