TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on June 16, 2012, 07:53:29 AM

Title: question about gr**** function
Post by: Coder on June 16, 2012, 07:53:29 AM
hello .

I am using a program that can insert a text string from a dialog (example "cubic feet per minute") and I want to see the text string
with the cursor before choosing the insertion point .

Is that possible ?

thank you all
Title: Re: question about gr**** function
Post by: CAB on June 16, 2012, 07:56:52 AM
Check this out.
http://www.theswamp.org/index.php?topic=29153.0  Align Text to Curve by Lee Mac
Title: Re: question about gr**** function
Post by: Lee Mac on June 16, 2012, 08:05:39 AM
Some examples that exhibit this functionality:

http://lee-mac.com/label.html (http://lee-mac.com/label.html)

http://lee-mac.com/curvealignedtext.html (http://lee-mac.com/curvealignedtext.html)

http://lee-mac.com/numinc.html (http://lee-mac.com/numinc.html)

http://lee-mac.com/slinkytext.html (http://lee-mac.com/slinkytext.html)
Title: Re: question about gr**** function
Post by: Lee Mac on June 16, 2012, 08:14:01 AM
Very simple example:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:example ( / e s p )
  2.     (if (/= "" (setq s (getstring t "\nEnter Text: ")))
  3.         (progn
  4.             (while (/= 5 (car (setq p (grread t 13 0)))))
  5.             (setq e
  6.                 (cons -1
  7.                     (entmakex
  8.                         (list
  9.                            '(0 . "TEXT")
  10.                             (cons 10 (cadr p))
  11.                             (cons 11 (cadr p))
  12.                             (cons 40 (getvar 'textsize))
  13.                             (cons 1 s)
  14.                            '(72 . 1)
  15.                            '(73 . 2)
  16.                         )
  17.                     )
  18.                 )
  19.             )
  20.             (princ "\nPlace Text...")
  21.             (while (= 5 (car (setq p (grread t 13 0))))
  22.                 (entmod (list e (cons 11 (cadr p))))
  23.             )
  24.         )
  25.     )
  26.     (princ)
  27. )
Title: Re: question about gr**** function
Post by: Coder on June 16, 2012, 08:19:36 AM
 :-)

Thanks CAB.

Very nice Lee.

Thank you so much , a very kind guy indeed .
Title: Re: question about gr**** function
Post by: Lee Mac on June 16, 2012, 08:59:43 AM
You're very welcome coder  :-)
Title: Re: question about gr**** function
Post by: Coder on June 16, 2012, 02:16:46 PM
Hello .

Is it possible to activate the osnap modes while trying to pick a point ?

Thanks a lot
Title: Re: question about gr**** function
Post by: Lee Mac on June 16, 2012, 05:03:21 PM
Is it possible to activate the osnap modes while trying to pick a point ?

No.

Not the in-built Object Snap functionality in any case - which is what I assume you were referring to.

The grread function simply pauses for user input (via mouse click, mouse movement or keyboard entry depending on the parameters used) and returns a list describing the input received (the first item of the list describes the type of input [5 = mouse movement / 3 = left-click / 25 = right-click / 2 = keyboard input etc.] the second item of the list contains information about the input [a point to which the mouse was moved / a clicked point / the key that was pressed etc.]). Whilst pausing for input, all standard AutoCAD functionality is disabled (Object Snap / Orthomode / Tracking / etc.), hence when used in a loop, drawing aids remain disabled whilst the loop is active.

The effect of Object Snap (and other drawing aids) can of course be imitated using a combination of the AutoLISP osnap function and displaying the appropriate OSnap symbols using grvecs, however, in my opinion, this is a lot of work to produce a [mostly poor] imitation of existing functionality, merely to create a fancy visual effect - that's not to say that I haven't delved into this method myself (http://lee-mac.com/drawgrid.html).
Title: Re: question about gr**** function
Post by: roy_043 on June 19, 2012, 05:10:38 AM
Alternative approach:
Create a temporary block. Using this approach object snap is, of course, possible.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:AltText ( / lastObject str tempBlockObject)
  2.   (if (setq str (getstring T "\nText: "))
  3.     (progn
  4.       (setq tempBlockObject
  5.         (vla-add
  6.           '(0 0 0)
  7.           (strcat "TempTextBlock_" (rtos (getvar 'cdate) 2 8)) ; Temp blockname based on cdate variable.
  8.         )
  9.       )
  10.       (vla-addtext
  11.         tempBlockObject
  12.         str
  13.         '(0 0 0)
  14.         (getvar 'textsize)
  15.       )
  16.       (setvar 'cmdecho 0)
  17.       (command "_.-insert" (vla-get-name tempBlockObject) "_scale" 1)
  18.       (while (> (getvar 'cmdactive) 0)
  19.         (command pause)
  20.       )
  21.       (setvar 'cmdecho 1)
  22.       (setq lastObject (vlax-ename->vla-object (entlast)))
  23.       (if
  24.         (and
  25.           (vlax-property-available-p lastObject 'name)
  26.           (= (vla-get-name lastObject) (vla-get-name tempBlockObject))
  27.         )
  28.         (progn
  29.           (vla-explode lastObject)
  30.           (vla-delete lastObject)
  31.           (vl-catch-all-apply 'vla-delete (list tempBlockObject))
  32.         )
  33.         (princ "\nAltText Error ")
  34.       )
  35.     )
  36.   )
  37.   (princ)
  38. )
  39.  
Title: Re: question about gr**** function
Post by: ribarm on June 19, 2012, 07:08:35 AM
Roy, your lisp is all right, only minor intervention...

'(0 0 0) should be replaced with : (vlax-3d-point '(0 0 0)) in both of lines where it occurs...

M.R.
Title: Re: question about gr**** function
Post by: roy_043 on June 19, 2012, 08:05:41 AM
Thank you ribarm for pointing that out. The code works without a hitch in Bricscad. I guess Bricscad is just a little more flexible here. :-)
There was another issue with the code: the temp block would not be removed if the user cancelled the function.

New code:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:AltText ( / lastObject str tempBlockObject)
  2.   (if (setq str (getstring T "\nText: "))
  3.     (progn
  4.       (setq tempBlockObject
  5.         (vla-add
  6.           (vlax-3d-point '(0 0 0))
  7.           (strcat "TempTextBlock_" (rtos (getvar 'cdate) 2 8)) ; Temp blockname based on cdate variable.
  8.         )
  9.       )
  10.       (vla-addtext
  11.         tempBlockObject
  12.         str
  13.         (vlax-3d-point '(0 0 0))
  14.         (getvar 'textsize)
  15.       )
  16.       (setvar 'cmdecho 0)
  17.       (command "_.-insert" (vla-get-name tempBlockObject) "_scale" 1)
  18.       (while (> (getvar 'cmdactive) 0)
  19.         (command pause)
  20.       )
  21.       (setvar 'cmdecho 1)
  22.       (setq lastObject (vlax-ename->vla-object (entlast)))
  23.       (if
  24.         (and
  25.           (vlax-property-available-p lastObject 'name)
  26.           (= (vla-get-name lastObject) (vla-get-name tempBlockObject))
  27.         )
  28.         (progn
  29.           (vla-explode lastObject)
  30.           (vla-delete lastObject)
  31.         )
  32.         (princ "\nAltText Error ")
  33.       )
  34.       (vl-catch-all-apply 'vla-delete (list tempBlockObject)) ; Always try to delete the tempBlockObject.
  35.     )
  36.   )
  37.   (princ)
  38. )
  39.  
Title: Re: question about gr**** function
Post by: Lee Mac on June 19, 2012, 08:13:43 AM
Nice idea Roy! Reminds me of the similar ideas from this (http://www.cadtutor.net/forum/showthread.php?67557) thread, as alternatives to grread.

Here are some minor amendments to your code for your consideration, I hope you don't mind:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:alttext ( / b i n o s )
  2.     (if (< 0 (strlen (setq s (getstring t "\nEnter Text: "))))
  3.         (progn
  4.             (setq i 0)
  5.             (while (tblsearch "BLOCK" (setq n (itoa (setq i (1+ i))))))
  6.             (setq b
  7.                 (vlax-invoke
  8.                     (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  9.                     'add '(0.0 0.0 0.0) n
  10.                 )
  11.             )
  12.             (vlax-invoke b 'addtext s '(0.0 0.0 0.0) (getvar 'textsize))
  13.             (setq o (entlast))
  14.             (command "_.-insert" n "_S" 1.0 "_R" 0.0 pause)
  15.             (if (and
  16.                     (not (equal o (setq o (entlast))))
  17.                     (vlax-property-available-p (setq o (vlax-ename->vla-object o)) 'name)
  18.                     (eq n (vla-get-name o))
  19.                 )
  20.                 (progn
  21.                     (vla-explode o)
  22.                     (vla-delete  o)
  23.                 )
  24.             )
  25.             (vl-catch-all-apply 'vla-delete (list b))
  26.         )
  27.     )
  28.     (princ)
  29. )
Title: Re: question about gr**** function
Post by: roy_043 on June 20, 2012, 04:32:52 AM
@ Lee:

1.
Good catch on the empty string (I should have better read your code).

2.
So (vlax-invoke) does accept points-as-lists in AC. Whereas (vla-...) and (vlax-invoke-method) don't.

3.
I never use (eq) when comparing strings or numbers. There have been discussions on this (http://www.theswamp.org/index.php?topic=32907.0) before. My view is that (eq) has a very special meaning and I never use it instead of (=).
Using (eq) would make more sense to me here:
Code - Auto/Visual Lisp: [Select]
  1. (not (equal o (setq o (entlast))))

4.
I think your modifications to the (entlast) check are unnecessary considering the program has just created a block with a unique name.
Title: Re: question about gr**** function
Post by: Lee Mac on June 20, 2012, 07:24:08 AM
2. So (vlax-invoke) does accept points-as-lists in AC. Whereas (vla-...) and (vlax-invoke-method) don't.

Correct, to my knowledge, the undocumented vlax-invoke / vlax-get functions are a remnant of Vital LISP, and will accept native LISP data types, so that you can avoid manipulation of variants / safearrays (though, admittedly in this case it would be a simple use of vlax-3D-point)

3. I never use (eq) when comparing strings or numbers. There have been discussions on this (http://www.theswamp.org/index.php?topic=32907.0) before. My view is that (eq) has a very special meaning and I never use it instead of (=).
Using (eq) would make more sense to me here:
Code - Auto/Visual Lisp: [Select]
  1. (not (equal o (setq o (entlast))))

Good point, I agree 'eq' is more suited to comparing variables over strings / numbers, as it compares the variable pointers rather than the values themselves (i.e. whether the variables point to the same data). Though habitually, I do tend to reserve '=' for numerical comparison, even though it could equally (excuse the pun) be used with strings. Whereas equal is more suited to list comparison.

4. I think your modifications to the (entlast) check are unnecessary considering the program has just created a block with a unique name.

I agree, with the addition of the entlast check, the name comparison becomes superfluous since the newly created entity can only be the block.

Given the above discussions, here is an updated program:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:alttext ( / b i n o s )
  2.     (if (< 0 (strlen (setq s (getstring t "\nEnter Text: "))))
  3.         (progn
  4.             (setq i 0)
  5.             (while (tblsearch "BLOCK" (setq n (itoa (setq i (1+ i))))))
  6.             (setq b
  7.                 (vlax-invoke
  8.                     (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
  9.                     'add '(0.0 0.0 0.0) n
  10.                 )
  11.             )
  12.             (vlax-invoke b 'addtext s '(0.0 0.0 0.0) (getvar 'textsize))
  13.             (setq o (entlast))
  14.             (command "_.-insert" n "_S" 1.0 "_R" 0.0 pause)
  15.             (if (not (eq o (setq o (entlast))))
  16.                 (progn
  17.                     (vla-explode o)
  18.                     (vla-delete  o)
  19.                 )
  20.             )
  21.             (vl-catch-all-apply 'vla-delete (list b))
  22.         )
  23.     )
  24.     (princ)
  25. )