Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Block Name Retrieve Text

23 REPLIES 23
SOLVED
Reply
Message 1 of 24
Dj_T_Rex2002
1285 Views, 23 Replies

Block Name Retrieve Text

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 ...

Dj_T_Rex2002_0-1725382309124.png

 

Thanks

23 REPLIES 23
Message 2 of 24
hak_vz
in reply to: Dj_T_Rex2002

@Dj_T_Rex2002

Attach sample dwg with some of your blocks attached.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 3 of 24
Dj_T_Rex2002
in reply to: hak_vz

Here you go. Hope it makes sense lol

Message 4 of 24
ronjonp
in reply to: Dj_T_Rex2002

@Dj_T_Rex2002 

Code removed. Working version below.

 

 

2024-09-03_16-52-03.gif

 

Message 5 of 24
Sea-Haven
in reply to: Dj_T_Rex2002

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)
Message 6 of 24
Dj_T_Rex2002
in reply to: ronjonp

Thank you so much for your help. Not all parts have PNC on them. I want to be able to retrieve whatever part number or name it is and grab it from the list of parts that I have. The two fasteners are only examples. 

Tags (1)
Message 7 of 24
ronjonp
in reply to: Dj_T_Rex2002

@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. 

Message 8 of 24
Dj_T_Rex2002
in reply to: ronjonp

@Sea-Haven Thanks, but at times I have different description. Examples:

 

Sealant Tape

[GBR2300]

 

Butyl Sealant

[BWH7100]

 

Fastening Clip

(2) Per Clip

[4800GNC]

Message 9 of 24
Dj_T_Rex2002
in reply to: ronjonp

What would be the best way to write the description? I'm open to suggestions 😁 Thanks for your help

Message 10 of 24
paullimapa
in reply to: Dj_T_Rex2002

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. 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 11 of 24
Dj_T_Rex2002
in reply to: paullimapa

You lost me on that my friend lol can you put an example please?

Message 12 of 24
Dj_T_Rex2002
in reply to: Dj_T_Rex2002

@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

Message 13 of 24
paullimapa
in reply to: Dj_T_Rex2002

This is just as example of using a keynote system 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 14 of 24
Dj_T_Rex2002
in reply to: paullimapa

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.

Message 15 of 24
paullimapa
in reply to: Dj_T_Rex2002

You are welcome…cheers!!!


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 16 of 24
Kent1Cooper
in reply to: Dj_T_Rex2002

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.

Kent Cooper, AIA
Message 17 of 24
ronjonp
in reply to: Dj_T_Rex2002

@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)
)

 

 

 

Message 18 of 24
Dj_T_Rex2002
in reply to: ronjonp

@ronjonp This looks like it's working but it's adding the "DES" and not placing it like it's supposed to (shown in red). I tried it with the sealant tape but didn't do anything. 

 

 

Message 19 of 24
ronjonp
in reply to: Dj_T_Rex2002

@Dj_T_Rex2002 I updated the code above .. seems to work now

2024-09-04_11-25-34.gif

Message 20 of 24
Kent1Cooper
in reply to: Dj_T_Rex2002

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:

Kent1Cooper_0-1725476614418.png

[EDIT:  Corrected former inclusion of backslash in 2\" to just 2".]

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report