Creating a variable from dwgprefix

Creating a variable from dwgprefix

charles_neelyFG4LK
Contributor Contributor
510 Views
7 Replies
Message 1 of 8

Creating a variable from dwgprefix

charles_neelyFG4LK
Contributor
Contributor

My goal is to have a lisp that will create a variable of a directory. It will start with getting the current drawing directory and then remove the last subfolder and add a new subfolder. I’m getting “; error: too many arguments”

 

Anyone see where I’ve strayed?

 

(defun c:ModifyDirectory ()
  (setq currentPath (getvar "DWGPREFIX")) ; Get the path of the current drawing
  (setq pathList (vl-string->list currentPath "\\")) ; Convert path to a list of subfolders
  (setq newPath (vl-list->string (reverse (cdr (reverse pathList))) "\\")) ; Remove the last subfolder
  (setq newSubfolder "01_Basefiles") ; Define the new subfolder name
  (setq modifiedPath (strcat newPath "\\" newSubfolder "\\")) ; Add the new subfolder
  (princ (strcat "\nModified Path: " modifiedPath)) ; Print the modified path
  (princ)
)

 

 

0 Likes
Accepted solutions (2)
511 Views
7 Replies
  • Lisp
Replies (7)
Message 2 of 8

cadffm
Consultant
Consultant
Accepted solution

Hi,

 

read the help about string> list again

and compare with your use

 

https://help.autodesk.com/view/ACD/2024/ENU/?guid=GUID-3C1B15F7-CF97-4783-A725-231C3A1ABB11

 

Sebastian

0 Likes
Message 3 of 8

charles_neelyFG4LK
Contributor
Contributor

Thanks, that made me look in the right place!

0 Likes
Message 4 of 8

Kent1Cooper
Consultant
Consultant
Accepted solution

Try something like this, which requires no variables except for the modifiedPath one that presumably you want to keep to do something else with:

(defun C:ModifyDirectory ()
  (setq modifiedPath
    (strcat
      (substr test
        1
        (1+ (vl-string-position 92 (vl-string-right-trim "\\" (getvar 'dwgprefix)) 1 T))
      ); substr
      "01_Basefiles\\"
    ); strcat
  ); setq
  (prompt (strcat "\nModified Path: " modifiedPath))
  (prin1)
)

It takes advantage of the fact that (vl-string-position) can search from the end, so you can leave everything up through the next-to-last last folder in the path together in one piece, rather than splitting it up into a list of sub-folders and putting them back together [even if done in a valid way].

Kent Cooper, AIA
Message 5 of 8

paullimapa
Mentor
Mentor

Try this modification to your routine using aec_splitdir function:

; ModifyDirectory gets current drawing directory, then removes last subfolder and add new subfolder
; OP:
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/creating-a-variable-from-dwgprefix/td-p/13427507
(defun c:ModifyDirectory 
  (/ aec_splitdir currentPath modifiedPath newPath newSubfolder pathList) ; localize functions & variables
;;;--- aec_splitdir function 
; splits path and returns list with each preceding directory
; (aec_splitdir (getvar"dwgprefix"))
; Returns:
; ("C:\\Users\\PauLpc\\Documents" "C:\\Users\\PauLpc" "C:\\Users" "C:")
; (aec_splitdir (findfile "acad.exe"))
; Returns:
; ("C:\\Program Files\\Autodesk\\AutoCAD 2020" "C:\\Program Files\\Autodesk" "C:\\Program Files" "C:")
(defun aec_splitdir (path-arg / idx-found lst path-found path-length path-remaining)
  (setq path-found path-arg
        path-remaining path-found
        path-length 0
        idx-found -2
  )
  (while (wcmatch path-remaining "*\\*")
   (if (< path-length (strlen path-arg))
    (progn
     (setq idx-found (vl-string-search "\\" path-arg (+ 2 idx-found)))
     (setq path-found (substr path-arg 1 idx-found))
     (setq path-remaining (substr path-arg (+ 2 idx-found)))
     (setq lst (append (list path-found) lst))
     (setq path-length (+ path-length idx-found))
    )
   )
  )
  (if(not lst) (setq lst (list path-arg)))
  lst
)
  (setq currentPath (getvar "DWGPREFIX")) ; Get the path of the current drawing
  (setq pathList (aec_splitdir currentPath)) ; split current path to list
  (if (= (length pathList) 1) ; chk if only 1 item in list
   (setq newPath (nth 0 pathList)) ; then current folder is last
   (setq newPath (nth 1 pathList)) ; else 1st item constains last subfolder removed
  )
  (setq newSubfolder "01_Basefiles") ; Define the new subfolder name
  (setq modifiedPath (strcat newPath "\\" newSubfolder "\\")) ; Add the new subfolder
  (princ (strcat "\nModified Path: " modifiedPath)) ; Print the modified path
  (princ)
)  

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes
Message 6 of 8

charles_neelyFG4LK
Contributor
Contributor

...one that presumably you want to keep to do something else with:

 

Exactly! Now that I have the correct directory I need to get the correct file. The filename has a 4 digit prefix that is fluid but the rest is static. I've tried using "****-" as a wildcard but not getting anywhere.

0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant

@charles_neelyFG4LK wrote:

.... Now that I have the correct directory I need to get the correct file. The filename has a 4 digit prefix that is fluid but the rest is static. I've tried using "****-" as a wildcard but not getting anywhere.


The asterisk wildcard is for anything including multiple characters or no characters, so multiple asterisks doesn't tie it down for you on the prefix part.  But you do need one to stand in for the after-the-prefix content, or include the static part explicitly.

The @ wildcard is for any single alphabetic character, and the ? wildcard for any single character [numeric, alphabetic, punctuation].  Try:

"@@@@-*"

if the prefix will always be four letters, and accepting anything following the hyphen, or

"????-TheStaticPart"

if always four characters but not necessarily always letters, and you want to incorporate the static part.  Mix and match as needed.

Read about the wildcard options in Help for the (wcmatch) function.

Kent Cooper, AIA
0 Likes
Message 8 of 8

charles_neelyFG4LK
Contributor
Contributor

I was following the wrong scent. I ended up w/ this

 (setq currentDir (getvar "DWGPREFIX"))
  ;; Replace "03_Sheets" with "01_Basefiles"
  (setq newDir (vl-string-subst "01_Basefiles" "03_Sheets" currentDir))
  ;; Store the new directory in a variable
  (setq baseDir newDir)
  (setq drawingName (getvar "DWGNAME"))
  (setq prefix (substr drawingName 1 4))
  ;; Construct the xref file path
  (setq xrefFilePath (strcat newDir prefix "-CP-SITE-BASE.dwg"))
  ;; Add the external reference
  (command "_.XREF" "_Attach" xrefFilePath "0,0,0" "1" "1" "0")
(setvar "CMDECHO" 1)
(princ)
)

It's working great. Next step is trying to attach a reference template w/o using the dialog box. 

0 Likes