Convert attribute text to a field with a hyperlink

Convert attribute text to a field with a hyperlink

JBerns
Advisor Advisor
512 Views
7 Replies
Message 1 of 8

Convert attribute text to a field with a hyperlink

JBerns
Advisor
Advisor

Community,

 

I have a title block that contains several attributes. One of the attributes is a DWGREF (drawing reference).

Currently it just stores text (drawing number) typed by the designer.

 

My goal is to convert the attribute from text to a Field that stores a hyperlink.

This can all be done manually. My goal is to automate with AutoLISP or Visual Lisp.

 

I have searched the forums and tasked AI tools to write the code, but have been unsuccessful.

 

Here is the code I have so far:

 

 

;;; Convert attribute text to a field and hyperlink
;;;
(defun C:ConvertAttribToHyperlink (/
				   atttag blkAttribs blkData blkEnt blkName foundAttTag hyperlinkurl ss)
  ;; defaults
  (setq blkName      "MyBlockName"        ;; block name
	attTag       "MyAttribTag"           ;; attribute tag name
	hyperlinkURL "http://example.com" ;; hhyperlink
	) ;_setq

  ;; Create the selection set using ssget with filters:
  (setq ss (ssget "_X" (list (cons 0 "INSERT")    ;; Filter for block references (INSERT)
                             (cons 2 blkName))))  ;; Filter for block name
  
  ;; Check if the selection set is valid
  (if ss
    (progn
      (setq blkEnt  (ssname ss 0)         ;; Get first block (for now) from selection set
	    blkData (entget blkEnt)       ;; Get the entity data
	    ) ;_setq

      (if (and blkEnt blkData)	
	(progn
	  
	  ;; Get the block reference attributes
	  (setq blkAttribs (vlax-invoke (vlax-ename->vla-object blkEnt) 'GetAttributes))

	  ;; Loop through attributes to find the attribute tag
	  (setq foundAttTag nil)
	  (foreach att blkAttribs
	    (if (= (vla-get-tagstring att) attTag)
	      (progn
		(setq foundAttTag t)     ;; Set flag if attribute found

		;;;
		;;; This is where I need to insert a field into the attribute
		;;; and then set the field to store a hyperlink
		;;;

              ) ;_progn
	    ) ;_if
	  ) ;_foreach
	) ;_progn
      ) ;_if
    ) ;_progn
    (princ (strcat "\nNo block named, '" blkName "' found in the drawing."))  ;; No blocks found
  ) ;_if
  (princ)
) ;_C:ConvertAttribToHyperlink

(princ)

 

 

Lines 35 - 38 are where I would expect the conversion to occur.

I cannot determine how to "insert" a field into the attribute text.

Once the field is inserted, a hyperlink needs to be set in the field.

 

Here is some text I discovered when manually inserting the field and hyperlink:

JBerns_1-1740173433747.png

 

I have attached a drawing with block and attribute that match the code.

 

Thank you for your time attention. I look forward to your replies.

 

Regards,

Jerry

 

P.S. I found this forum post, but it uses VB.NET

https://forums.autodesk.com/t5/net/inserting-block-amp-adding-fields-to-attributes/m-p/6423370#M4927... 

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (1)
513 Views
7 Replies
Replies (7)
Message 2 of 8

paullimapa
Mentor
Mentor
Accepted solution

These are the modifications I made:

Changed:

  ;; Create the selection set using ssget with filters:
  (setq ss (ssget "_X" (list (cons 0 "INSERT")    ;; Filter for block references (INSERT)
                             (cons 2 blkName))))  ;; Filter for block name

To also filter selection to blocks with attributes only:

  ;; Create the selection set using ssget with filters:
  (setq ss (ssget "_X" (list (cons 0 "INSERT")    ;; Filter for block references (INSERT)
                             '(66 . 1)            ;; contains attributes
                             (cons 2 blkName))))  ;; Filter for block name

Instead of the following sections of code:

;	  ;; Get the block reference attributes;
	  (setq blkAttribs (vlax-invoke (vlax-ename->vla-object blkEnt) 'GetAttributes))

	  ;; Loop through attributes to find the attribute tag
	  (setq foundAttTag nil)
	  (foreach att blkAttribs
	    (if (= (vla-get-tagstring att) attTag)
	      (progn
	       (setq foundAttTag t)     ;; Set flag if attribute found

		;;;
		;;; This is where I need to insert a field into the attribute
		;;; and then set the field to store a hyperlink
		;;;

      ) ;_progn
	    ) ;_if
	  ) ;_foreach

I assume that there's a matching attribute tagname and then simplified the code to this:

;        inserts a field with hyperlink into the attribute     
;        assumes block contains attribute with matching tagname        
         (setpropertyvalue blkent atttag (strcat "%<\\AcVar \\href \"" hyperlinkURL "##" (substr hyperlinkURL 8) "#1\">%"))  
         (command "_.UpdateField" blkEnt "") 

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

JBerns
Advisor
Advisor

@paullimapa (Paul),

 

Brilliant! I thought it was going to require a more complex solution involving VLA/VLAX functions.

Thank you, Paul!

 

Kind regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 4 of 8

paullimapa
Mentor
Mentor

you are welcome...cheers!!!


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

JBerns
Advisor
Advisor

This morning I was testing hyperlinks to other AutoCAD drawings. For example, C:\Temp\123456.DWG.

I can get the hyperlink to work, but for some reason AutoCAD changes from a 'Maximized' window to a 'Restore Down' window size.

Annoying.

AutoCAD normally opens maximized.

Any suggestions as to how to prevent AutoCAD (or Windows OS) from changing the window size?

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 6 of 8

paullimapa
Mentor
Mentor

Seems to be a bug that’s been around for awhile 

https://forums.autodesk.com/t5/autocad-forum/hyperlink-to-another-dwg-minimizes-window/td-p/7945504


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

JBerns
Advisor
Advisor

Thanks for verifying, Paul.

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 8 of 8

paullimapa
Mentor
Mentor

Once again you are welcome...cheers!!!


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