help pulling info from middle of dwgprefix

Travis.Biddle
Advocate
Advocate

help pulling info from middle of dwgprefix

Travis.Biddle
Advocate
Advocate

I need to pull one of the folder names from the dwgpreix variable.

 

"C:\\Users\\trab01\\Box\\AX Projects\\S012345 Test\\Production\\Truck 12\\Programs\\INSULATION_XPS_2.5\\Nest 1\\TXT\\"

 

I need to pull the "S012345 Test" from the prefix.  So basically need to pull from the "SO" to the first "\\"

 

note:

The character combination of "S0" will never appear anywhere else in the string

Character counts before and after this location will change from drawing to drawing.

 

Can anyone please help? 

 

0 Likes
Reply
Accepted solutions (3)
507 Views
10 Replies
Replies (10)

paullimapa
Mentor
Mentor

Try this...after saving & loading get_val.lsp enter at Command prompt:

(get_val "C:\\Users\\trab01\\Box\\AX Projects\\S012345 Test\\Production\\Truck 12\\Programs\\INSULATION_XPS_2.5\\Nest 1\\TXT\\" "S0*")

Returns:

"S012345 Test"

; get_val function given path string and search pattern returns matching value
; Arguments:
; str = path string
; pat = string pattern
; Returns:
; found value
; (get_val "c:\\Autodesk\\Projects\\Category\\" "Pr*")
; Returns: 
; "Projects"
; nil if none found
(defun get_val (str pat / aec_str2lst aec_vl-positionw idx lst val)
;;;--- aec_str2lst function
; splits given string with separator 
; returns list with each item
; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/how-do-i-split-a-file-path-into-bits-based-on-back-slash/td-p/2396237
; Usage:
; (aec_str2lst (findfile "acad.exe") "\\")
; Returns:
; ("C:" "Program Files" "Autodesk" "AutoCAD 2020" "acad.exe")
; Usage:
; (aec_str2lst "0 1 2 3" " ")
; Returns:
; ("0" "1" "2" "3")
(defun aec_str2lst (str sep / pos)
  (if (setq pos (vl-string-search sep str))
   (cons (substr str 1 pos)
    (aec_str2lst (substr str (+ (strlen sep) pos 1)) sep)
   )
   (if(not(zerop(strlen str)))(list str))
  )
) ; defun
;;;--- aec_vl-positionw function given wildcard expression and list returns index with position match 
; Arguments:
; exprw = expression with wildcard
; lst = list
; Usage:
; (aec_vl-positionw "*acad*" '("This is Autodesk" "That is not Revit" "Now is acad"))
; Returns:
; 2
(defun aec_vl-positionw (exprw lst / i j)
 (setq i 0)
 (while (< i (length lst))
  (if (wcmatch (nth i lst) exprw)
   (progn
    (setq j i) ; wildcard match found
    (setq i (length lst)) ; stop looping
   )
  ) ; if match
  (setq i (1+ i)) ; continue to next item
 ) ; while list items
 j
) ; defun
 (if(setq lst(aec_str2lst str "\\"))
   (if(setq idx (aec_vl-positionw pat lst))
     (setq val(nth idx lst))
   ) ; if
 ) ; if
 val
) ; defun get_val

 

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Travis.Biddle
Advocate
Advocate

this works great, but how can I call this out in a program and set a variable of the results?

 

Setq XX (results of running Get_Val on dwgprefix variable)

 

XX = "S012345 Test"

0 Likes

paullimapa
Mentor
Mentor

Simple:

(setq xx (get_val "C:\\Users\\trab01\\Box\\AX Projects\\S012345 Test\\Production\\Truck 12\\Programs\\INSULATION_XPS_2.5\\Nest 1\\TXT\\" "S0*"))

You can even do a test to see if there's a value found:

(if (setq xx (get_val "C:\\Users\\trab01\\Box\\AX Projects\\S012345 Test\\Production\\Truck 12\\Programs\\INSULATION_XPS_2.5\\Nest 1\\TXT\\" "S0*"))

 (princ"Something")

 (princ"Nothing")

) ;if


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Travis.Biddle
Advocate
Advocate

yes this works, but I want to pull it from the DWGPREFIX variable.  Something like..

 

(setq xx (get_val (getvar "dwgprefix”)))

 

or

 

(setq prefix  (getvar "dwgprefix"))

(setq xx (get_val prefix))

0 Likes

paullimapa
Mentor
Mentor
Accepted solution

then just include (getvar "dwgprefix") as the first argument and still include the second argument which is the matching pattern "S0*":

(setq xx (get_val (getvar "dwgprefix") "S0*"))


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Travis.Biddle
Advocate
Advocate

Ahh, I see.  Thank you so much!!

 

0 Likes

paullimapa
Mentor
Mentor
you are welcome...cheers!!!

Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
0 Likes

Sea-Haven
Mentor
Mentor
Accepted solution

Maybe I am missing something. This finds 5th directory.

 

; thanks to Lee-mac for this defun 
; www.lee-mac.com
; 44 is comma 9 is tab 34 is space 92 backslash
(defun _csv->lst ( str / pos )
	(if (setq pos (vl-string-position 92 str))
		(cons (substr str 1 pos) (_csv->lst (substr str (+ pos 2))))
		(list str)
    )
)
(setq ans (_csv->lst (getvar 'dwgprefix)))
; use (nth x ans) and look for the item you want
(setq dir (nth 4 ans))

 

0 Likes

ВeekeeCZ
Consultant
Consultant
Accepted solution

Another way. Search for "\S0", cut the string from there to the following "\". 

 

(defun :projectnumber (path / start)   ;; returns "" if \\S0 not found
  (if (wcmatch path "*\\S0*")
    (substr path
	    (setq start (+ (vl-string-search "\\S0" path) 2))
	    (1+ (- (vl-string-position (ascii "\\") path start) start)))
    ""))

;; Usage
(:projectnumber (getvar 'dwgprefix)) 

 

0 Likes

Travis.Biddle
Advocate
Advocate

Very nice!!  This helps me in other things as well.  Thank you very much!

0 Likes