Hello, I have tons of blocks with Part Numbers. I want to know if there is a way to create a lisp to retrieve block description, into Multileader Text, from a text file that contains that part number. Example ... Block has an Attribute Definition Part Number that says 1616PNC. I have a Notepad text file that has the following:
Part No. Description
1608PNC #14 x 1" HWH TEK III, @ 12" O.C. [1608PNC]
1616PNC #14 x 2" HWH TEK III, @ 12" O.C. [1616PNC]
So, when I run the command, I just click on the block and then select the text and then it'll look like this ...
Thanks
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by ronjonp. Go to Solution.
Attach sample dwg with some of your blocks attached.
Miljenko Hatlak
If you want a 3 line answer.
(command "_.MLEADER" (cadr e) p2 "")
(setq r (vl-string-subst "\\P@" "@" r))
(setq r (vl-string-subst "\\P[" "[" r))
(vla-put-textstring (vlax-ename->vla-object (entlast)) r)
@Dj_T_Rex2002 If there is always a " - " in the string to separate the part number from the description then the data can easily be parsed generically.
@Sea-Haven Thanks, but at times I have different description. Examples:
Sealant Tape
[GBR2300]
Butyl Sealant
[BWH7100]
Fastening Clip
(2) Per Clip
[4800GNC]
What would be the best way to write the description? I'm open to suggestions 😁 Thanks for your help
Would you entertain the thought of using keynotes? Instead of putting the description at the leader only the number is shown. Then a separate list on the page would show a list with the numbers and descriptions.
@ronjonp @Sea-Haven Maybe this might help. What someone had created where I used to work was an .ini file and it would reference to the file attached, but I renamed it to .txt because it was an .ini file
Oh ok, I see it. For what I do it's relatively small parts so I usually add the description. Looks really good though. I will look into it later on. Thanks for the advise dude.
You are welcome…cheers!!!
So the lines in the file at Message 12 are not formatted in the same way as the example at Message 3. [The former at least all seem to have an = sign immediately before a list of strings for the description part.] Which is it? AutoLisp is going to need some kind of absolutely regular aspect that it can rely on as a basis for working with the line it pulls from a file. Is what is between the part number and the description [Message 3] a space and a hyphen and a space, or is it [Message 12] some unknown quantity of spaces and an = sign, or tabs and an = sign? Is the description part [3] a single text string or [12] a list of multiple text strings? If it's a list, and the first thing in it is "DES", should that be omitted? If it's a list, are the separate strings in it to be the basis of the breakdown into lines in the Mtext result? If it's a single string, what kind of punctuation that is absolutely consistent should be the basis of the breakdown into lines? Etc., etc., etc.
@Dj_T_Rex2002 With your "MTS" data the delimiter would be "=(" that separates the part number from the description. Give this modified code a try. It cleans up spaces and tabs that exist between the part number and description and removes the parenthesis that enclose the description.
(defun c:foo (/ _getdata bn e p2 r str)
;; RJP » 2024-09-04
(defun _getdata (fn / a h r str)
(cond ((and (eq 'str (type fn)) (setq fn (findfile fn)) (setq h (open fn "r")))
(while (setq str (read-line h))
(if (wcmatch str "*=(*)")
(progn (setq a (cdr (read (substr str (+ 2 (vl-string-search "=" str))))))
(setq r
(cons (list (vl-string-right-trim " \t" (substr str 1 (vl-string-search "=" str)))
(apply 'strcat (mapcar '(lambda (x) (strcat x "\\P")) a))
)
r
)
)
)
)
)
(close h)
(reverse r)
)
)
)
;; Put your data file in a directory that's in YOUR support paths
(if (or *data* (setq *data* (_getdata (findfile "MTS.txt"))))
(while (and (setq e (entsel "\nPick block with part no: "))
(setq bn (cdr (assoc 2 (entget (car e)))))
)
(cond ((not (setq r (cadr (assoc bn *data*)))) (alert "DESCRIPTION NOT FOUND!"))
((setq p2 (getpoint (cadr e) "\nPick second point: "))
(command "_.MLEADER" (cadr e) p2 "")
(vla-put-textstring (vlax-ename->vla-object (entlast)) r)
)
)
)
(alert "DATA FILE NOT FOUND!")
)
(princ)
)
My take on it [minimally tested in your sample drawing]:
(defun C:TEST (/ file blkname test info)
(setq file (open "C:\\Users\\kcooper\\Downloads\\MTS.txt" "r"))
(setq blkname (cdr (assoc 2 (entget (car (entsel "\nSelect Block: "))))))
(while
(not
(and
(setq test (read-line file))
(= (vl-string-trim "\t " (substr test 1 (vl-string-search "=" test))) blkname)
); and
); not
); while
(close file); done with it
(setq
info (substr test (+ (vl-string-search "=" test) 2))
info (vl-string-right-trim "\"\)" (substr info (+ (vl-string-search "\" \"" info) 4)))
); setq
(while (vl-string-search "\" \"" info); new lines
(setq info (vl-string-subst "\\P" "\" \"" info))
); while
(while (vl-string-search "\\\"" info); inches mark if present
(setq info (vl-string-subst "\"" "\\\"" info))
); while
(command "_.mleader" pause pause info)
(prin1)
)
Fix the filepath etc. in line 2 to suit your situation.
It doesn't make an all-encompassing list [huge!] of all part numbers and their descriptions, but rather reads the file until it hits the line that starts with the Block name, uses that and reads no further.
It depends on all data-line entries having some space(s) and/or tab(s) followed by an = sign after the part number, and one " " [the closing double-quote after DES and a space and the opening double-quote for what follows] immediately preceding the beginning of the "meat" of the description part, as [I think] is the case in your sample file. Not having your hundreds of Blocks, I can't try beyond that. Not like the example in Message 3.
It did the white parts here:
[EDIT: Corrected former inclusion of backslash in 2\" to just 2".]
Can't find what you're looking for? Ask the community or share your knowledge.