Author Topic: How to copy the "Contents" attribute of a Text Object to a Circle Object  (Read 1603 times)

0 Members and 1 Guest are viewing this topic.

autogis

  • Guest
I have many circle objects with text next to them.  I need to create a lisp to allow the user to first click on a text object, then click on a circle object and copy the "Contents" from the Text object to the "Hyperlink" of the Circle object.  I would like this to be a loop, allowing the user to click on a text, then the circle,  then the next text, then the next circle etc until he presses ESC or similar key to end the routine.  Any help would be appreciated.  Thanks.

ChrisCarlson

  • Guest
You'll probably get some more help if you sketch out something in LISP first.

autogis

  • Guest
Thanks Buddy I am going to do that right now.  I appreciate your advice.

autogis

  • Guest
This is what I have so far, but still needs alot of work:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:TextToCircleLink (/ textObject circleObject);; declaring the variables as local
  2.   (prompt "\nSelect the text object first: ");; prompts the user for text object
  3.   (if (setq textObject (ssget "_:L"));; selects and test if a valid selection set exists
  4.     (if (setq circleObject (ssget "_:L"));;
  5.       (command "_.chprop" circleObject "" "Hyperlink" "3" "");; if exists, runs the chprop command
  6.     );; circle
  7.   );; text
  8.   (princ);; exits quietly
  9. );; ends the function

A couple of things that do not work right from the above function:
- It should only select one text object and then continue prompting for the circle object
- It should not require user to press enter after selecting each object
- It does not like to change the "Hyperlink" property of the circle
- How do I extract the text property to use it when running the chprop command to save it in the circle's Hyperlink property?
- Need the loop to continue until user presses ESC

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Consider the following code:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:copytexttohyperlink ( / cir txt )
  2.     (while
  3.         (and (setq txt (LM:selectifobject "\nSelect text <exit>: " "*TEXT"))
  4.              (setq cir (LM:selectifobject "\nSelect circle <exit>: " "CIRCLE"))
  5.         )
  6.         (LM:createorupdatehyperlink (entget cir) (cdr (assoc 1 (entget txt))) "")
  7.     )
  8.     (princ)
  9. )
  10.  
  11. ;; Create or Update Hyperlink  -  Lee Mac
  12. ;; enx - [lst] DXF data list
  13. ;; dsc - [str] Hyperlink Description or View Name
  14. ;; url - [str] Hyperlink URL (use "" for View)
  15.  
  16. (defun LM:createorupdatehyperlink ( enx dsc url )
  17.     (regapp "PE_URL")
  18.     (entmod
  19.         (append enx
  20.             (list
  21.                 (list -3
  22.                     (append
  23.                         (list
  24.                             "PE_URL"
  25.                             (cons 1000 url)
  26.                            '(1002 . "{")
  27.                         )
  28.                         (if (= "" url) (list (cons 1000 dsc)))
  29.                         (list
  30.                             (cons 1000 dsc)
  31.                            '(1002 . "{")
  32.                            '(1071 . 1)
  33.                            '(1002 . "}")
  34.                            '(1002 . "}")
  35.                         )
  36.                     )
  37.                 )
  38.             )
  39.         )
  40.     )
  41. )
  42.  
  43. ;; Select if Object  -  Lee Mac
  44. ;; Continuously prompts the user for a selection of a specific object
  45.  
  46. (defun LM:selectifobject ( msg typ / ent )
  47.     (while
  48.         (progn (setvar 'errno 0) (setq ent (car (entsel msg)))
  49.             (cond
  50.                 (   (= 7 (getvar 'errno))
  51.                     (princ "\nMissed, try again.")
  52.                 )
  53.                 (   (null ent) nil)
  54.                 (   (not (wcmatch (cdr (assoc 0 (entget ent))) typ))
  55.                     (princ "\nInvalid object selected.")
  56.                 )
  57.             )
  58.         )
  59.     )
  60.     ent
  61. )
  62.  
« Last Edit: April 06, 2016, 12:58:22 PM by Lee Mac »

autogis

  • Guest
Lee, thank you very much!  That worked perfect!  :-)

ribarm

  • Gator
  • Posts: 3255
  • Marko Ribar, architect
Much easier...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:TextToCircleLink ( / textObject circleObject )
  2.  
  3.  
  4.   (while t
  5.     (setq textObject (car (entsel "\nPick the text object first - ESC TO FINISH : ")))
  6.     (if (not textObject)
  7.       (alert "Missed picking text object... Repeat procedure from start again...")
  8.       (progn
  9.         (while (not (setq circleObject (car (entsel "\nPick coresponding circle object : "))))
  10.           (alert "Missed picking circle object... Repeat procedure from start again...")
  11.         )
  12.         (if (and textObject circleObject)
  13.           (vla-add (vla-get-hyperlinks (vlax-ename->vla-object circleObject)) (cdr (assoc 1 (entget textObject))))
  14.         )
  15.       )
  16.     )
  17.   )
  18.  
  19.   (princ)
  20. )
  21.  
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

autogis

  • Guest
Thanks, just a quick note for your own information.  Since I needed to do an export of the data.  When I export with the first version created by Lee, the hyperlink data comes across blank.  When I export the data with the second version create by ribarm, the hyperlink contains the data correctly.  For this reason I need to use the second version since it allows for correct exporting of hyperlink data.  Otherwise as far as functionality works, they both worked the same.

Update: I think I see what it might be.  The one that exports correctly, the hyperlink is set as "Existing File or Web Page".  The one where the hyperlink does not export is set as "View of This Drawing".   Exporting was not part of the original criteria therefore this is just an FYI.
« Last Edit: April 06, 2016, 02:55:46 PM by autogis »

autogis

  • Guest
I ran into an issue with the above lisp created by ribarm.  If the text object is modified, it is still copying the older data the "hyperlink" property of the text object.   The reason being that the older data stays in the "hyperlink" property of the text object.  The newly modified text is contained in the "contents" property of the text object.  I am trying to figure out how to copy over the "contents" property to the "hyperlink" property to the circle object.  I noticed this after someone changed the wording on a text object and the new text was not being copied over to the circle object.  Thanks.
« Last Edit: August 25, 2016, 01:32:07 PM by autogis »

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
@autogis: I think you are confused about the functionality of the code in this topic. The hyperlinks that get attached to the circles do not have a link to the text objects.

autogis

  • Guest
Re: How to copy the "Contents" attribute of a Text Object to a Circle Object
« Reply #10 on: August 26, 2016, 12:57:29 PM »
Roy, thanks for your input.  Maybe I did not explain it correctly.  I do understand that the hyperlinks do not have a link to the text objects.  Perhaps this will help:

1. open autocad, create a text object and a circle object
2. run the lisp created by ribarm above.
3. click the text object, then click the circle object.
4. you will see the hyperlink is set correctly in the circle object.
5. now change the text on the text object
6. run the lisp again
7. click the text object, then click the circle object.
8. this time you will notice that it does not set the text on the circle object to the new text value

Thats basically the issue.  I believe the fix is to extract the text from the "contents" property of the text object to get the latest text.  That is the issue I am having.  Thanks.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How to copy the "Contents" attribute of a Text Object to a Circle Object
« Reply #11 on: August 26, 2016, 03:53:51 PM »
OK, now I understand. :-D
Programmatically you can add multiple hyperlinks to an object, but only the first is shown to the user. This explains what you are seeing with Ribarm's code.
Code - Auto/Visual Lisp: [Select]
  1. (defun KGA_Data_XdataSet (ename app dataLst)
  2.   (regapp app)
  3.   (entmod
  4.     (append
  5.       (entget ename)
  6.       (list (list -3 (cons app dataLst)))
  7.     )
  8.   )
  9. )
  10.  
  11. (defun c:test ( / srcElst srcEnm trgEnm)
  12.   (while
  13.     (and
  14.       (setq srcEnm (car (entsel "\nSelect text: ")))
  15.       (wcmatch (cdr (assoc 0 (setq srcElst (entget srcEnm)))) "*TEXT")
  16.       (setq trgEnm (car (entsel "\n  Select entity to attach hyperlink to: ")))
  17.     )
  18.     (KGA_Data_XdataSet trgEnm "PE_URL" (cons 1000 (cdr (assoc 1 srcElst))))
  19.   )
  20.   (princ)
  21. )