Update - Sample code was revised to include some additional error handling and return some basic error messages in the case of nil being passed to the function.
When working with file paths in AutoCAD, it is common that you might need to work with a string that contains multiple paths that are separated by a delimiter. Commonly, the delimiter used to separate one path from another is a semi-colon (;).
The ACADPREFIX system variable returns a string that contains a listing of all the AutoCAD support paths.
(getvar "acadprefix")
"C:\Users\ambrosl\appdata\roaming\autodesk\autocad 2017\r21.0\enu\support;C:\program files\autodesk\autocad 2017\support;C:\program files\autodesk\autocad 2017\support\en-us;C:\program files\autodesk\autocad 2017\fonts;C:\program files\autodesk\autocad 2017\help;C:\program files\autodesk\autocad 2017\express;C:\program files\autodesk\autocad 2017\support\color;C:\Program Files\Autodesk\AutoCAD 2017\drv;"
The following code defines a function named SPLIT which breaks up a string into elements with in a list based on the delimiter specified:
;; Usage: (split (getvar "acadprefix") ";")
(defun split (sExpression sDelimiter / len val sList)
(if (and (= (type sExpression) 'STR) (= (type sDelimiter) 'STR))
(progn
; Get the length of the first item from the expression based
; on delimiter position
(setq len (vl-string-position (ascii sDelimiter) sExpression))
; Loop until there are no more instances of the delimiter
; in the expression
(while (numberp len)
; Get the item from the expression
(setq val (substr sExpression 1 len))
; Append the item to the list
(setq sList (cons val sList))
; Get the remaining part of the expression
(setq sExpression (substr sExpression (+ 2 len)))
; Get the length of the next item from the string based
; on delimiter position
(setq len (vl-string-position (ascii sDelimiter) sExpression))
)
; Add the last value to the list
(if (/= sExpression nil)
(setq sList (cons sExpression sList))
)
; Reverses the elements in the list and return the list
(reverse sList)
)
(progn
(if (not (= (type sExpression) 'STR))
(prompt "\nSplit Error: First argument must be the string to split.")
)
(if (not (= (type sDelimiter) 'STR))
(prompt "\nSplit Error: Second argument must be the character that represents the delimiter to use.")
)
(princ)
)
)
)
The results of using the SPLIT function on the ACADPREFIX system variable is as follows:
(split (getvar "acadprefix") ";")
("C:\\Users\\ambrosl\\appdata\\roaming\\autodesk\\autocad 2017\\r21.0\\enu\\support" "C:\\program files\\autodesk\\autocad 2017\\support" "C:\\program files\\autodesk\\autocad 2017\\support\\en-us" "C:\\program files\\autodesk\\autocad 2017\\fonts" "C:\\program files\\autodesk\\autocad 2017\\help" "C:\\program files\\autodesk\\autocad 2017\\express" "C:\\program files\\autodesk\\autocad 2017\\support\\color" "C:\\Program Files\\Autodesk\\AutoCAD 2017\\drv" "")
The SPLIT function can also be used to separate the name of an Xref from a named style or layer name.
For example, Grid Plan|Architectural_Plan_Dim is the layer name created in a host drawing after the drawing file named Grid Plan is attached and that drawing that contains a layer named Architectural_Plan_Dim. The pipe (|) character is used to separate the Xref name from the layer name.
(split "Grid Plan|Architectural_Plan_Dim" "|")
("Grid Plan" "Architectural_Plan_Dim")
Sincerely,
Lee
Recent Comments