Author Topic: Grvecs and cursor position  (Read 4848 times)

0 Members and 1 Guest are viewing this topic.

CincyJeff

  • Newt
  • Posts: 89
Grvecs and cursor position
« on: August 31, 2015, 11:36:53 AM »
Can anybody help me figure out how to use grvecs to display near the cursor? I'm running a grread loop to track the cursor and I want to display information to the user concerning the number of rows and columns in a dynamic block. I want to use Lee Mac's LM:GrText routine to create the dynamic output but I can't get grvecs to follow the cursor. It displays the text but not any where near the cursor. What do I use with grvecs?

Code - Auto/Visual Lisp: [Select]
  1. (setq code (grread t))
  2. (grvecs (LM:GrText #ofrows)
  3.         (list (list 1.0 0.0 0.0 (caadr code))
  4.         (list 0.0 1.0 0.0 (cadadr code))
  5.         '(0.0 0.0 1.0 0.0)
  6.         '(0.0 0.0 0.0 1.0)))

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Grvecs and cursor position
« Reply #1 on: August 31, 2015, 11:49:26 AM »
Hi Jeff,

As noted in the description on my site, my LM:GrText function will return a list of vectors in pixel units, relative to the origin (this representation is necessary so that such vectors may be scaled & positioned at any zoom level whilst maintaining the 'pixel-to-pixel' resolution).

I would suggest studying the GrTextDemo.lsp file available on my site, which contains several examples showing how to use my LM:GrText function (including a LM:DisplayGrText function to take care of the necesary scaling & positioning relative to the cursor).

If you have further questions, just ask.

Lee


CincyJeff

  • Newt
  • Posts: 89
Re: Grvecs and cursor position
« Reply #2 on: August 31, 2015, 12:50:19 PM »
Thanks Lee. I'll take a look at the demo program.

CincyJeff

  • Newt
  • Posts: 89
Re: Grvecs and cursor position
« Reply #3 on: September 01, 2015, 08:39:56 AM »
I got it to work most of the time but I'm having trouble transforming coordinates. In some drawings the rectangle is drawn displaced by the value of the target system variable. If I transform from World to UCS, instead of UCS to DCS, it eliminates the 'target' displacement. Am I introducing other issues by changing this?

Also, it does not take into account rotated blocks. Is it possible to draw the rectangle to match the block orientation?

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Grvecs and cursor position
« Reply #4 on: September 01, 2015, 09:06:50 AM »
I'm not sure what rectangle you are referring to?  :?

- My LM:GrText function will only display text.

CincyJeff

  • Newt
  • Posts: 89
Re: Grvecs and cursor position
« Reply #5 on: September 01, 2015, 09:26:47 AM »
I was not clear in my post. I am using your Demo3 that displays the area of a rectangle. The blocks I am working with use a rectangular array so I want a rectangle to be drawn to represent the modified size of the array. I don't know how to grip_stretch the dynamic block and update the cursor at the same time so I am just dragging a rectangle instead. As you drag your cursor in Demo3 there is a rectangle that grows from the base point. If I have a block that has been rotated 90 degrees counterclockwise the rectangle is drawn to the upper right from the base point instead of the lower right quadrant to match the block orientation.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Grvecs and cursor position
« Reply #6 on: September 01, 2015, 09:40:52 AM »
I was not clear in my post. I am using your Demo3 that displays the area of a rectangle. The blocks I am working with use a rectangular array so I want a rectangle to be drawn to represent the modified size of the array. I don't know how to grip_stretch the dynamic block and update the cursor at the same time so I am just dragging a rectangle instead. As you drag your cursor in Demo3 there is a rectangle that grows from the base point. If I have a block that has been rotated 90 degrees counterclockwise the rectangle is drawn to the upper right from the base point instead of the lower right quadrant to match the block orientation.

To clarify, my 'Demo 3' example program simply prompts the user for a base point and constructs the rectangle based on the delta X & Y coordinate values between the cursor position and the given base point - it does not care about objects in the drawing, as the only input is the point supplied by the user.

For your task, you would need to prompt the user to select the block reference entity, retrieve the insertion point & rotation of the block, and then calculate the positions of the two remaining rectangle vertices, given that the block insertion point and cursor position will be located along one of the rectangle diagonals.

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Grvecs and cursor position
« Reply #7 on: September 01, 2015, 11:03:19 AM »
Here's a quick example to get you started:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test ( / cpt dis ent enx ins pt1 pt2 pt3 pt4 rot suf vec )
  2.     (if (and (setq ent (car (entsel "\nSelect block: ")))
  3.              (= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
  4.         )
  5.         (progn
  6.             (setq suf (strcat (if (< (getvar 'lunits) 3) "mm" "in") (chr 178))
  7.                   rot (cdr (assoc 50 enx))
  8.                   vec (list (cos rot) (sin rot))
  9.                   ins (cdr (assoc 10 enx))
  10.                   pt1 (trans ins ent vec)
  11.                   ins (trans ins ent 1)
  12.             )
  13.             (while (= 5 (car (setq pt3 (grread nil 13 0))))
  14.                 (redraw)
  15.                 (setq cpt (cadr pt3)
  16.                       pt3 (trans cpt 1 vec)
  17.                       dis (mapcar '- pt3 pt1)
  18.                       pt2 (trans (list (car pt1) (cadr pt1) (caddr pt3)) vec 1)
  19.                       pt4 (trans (list (car pt3) (cadr pt3) (caddr pt1)) vec 1)
  20.                 )
  21.                 (LM:DisplayGrText cpt (LM:GrText (strcat (rtos (abs (* (car dis) (caddr dis))) 2 4) suf)) 3 15 -31)
  22.                 (grvecs (list 256 ins pt2 pt2 cpt cpt pt4 pt4 ins))
  23.             )
  24.         )
  25.     )
  26.     (princ)
  27. )

The above could also be achieved using polar.

CincyJeff

  • Newt
  • Posts: 89
Re: Grvecs and cursor position
« Reply #8 on: September 01, 2015, 01:08:39 PM »
Lee,
It's almost there. In your Demo3 lisp you have a great feature that draws the rectangle with a dashed line when the x-value goes negative. I would like to do something similar where my prompt changes and the rectangle is dashed anytime the cursor is not in the positive x-y quadrant from the entity frame of reference. I just can't get my head around the translations going on.
Your line of code was:
Code - Auto/Visual Lisp: [Select]
  1. (if (< (car dis) 0.0) -256 256
which I modified to:
Code - Auto/Visual Lisp: [Select]
  1. (if (or (< (car dis) 0.0) (< (cadr dis) 0.0)) -256 256)
Would I translate the cursor position to relate to the entity?

Code - Auto/Visual Lisp: [Select]
  1. (defun C:ARRSIZE (/ basept col# coldist colspac method row# rowdist rowspac)
  2.   (UTIL:SETDEBUG "ARSI:UPDATEBLOCK")
  3.   (setq entnam  (car entsel)
  4.         blkrot  (vla-get-rotation (vlax-ename->vla-object entnam))
  5.         rowspac 72 ; from block and retrieved with xdata
  6.         colspac 30 ; from block and retrieved with xdata
  7.   ) ;_ end of setq
  8.   (initget 7 "Cursor Keyboard")
  9.   (setq method (getkword "\nSelect input method [Cursor/Keyboard]: "))
  10.   (if (= method "Cursor")
  11.     (if (setq basept (UTIL:DXF 10 (entget entnam)))
  12.       (ARSI:DRAWRECTANGLE)
  13.     ) ;_ end of if
  14.     (progn
  15.       (initget 7)
  16.       (setq row# (getint "\nInput the number of rows: "))
  17.       (initget 7)
  18.       (setq col# (getint "Input the number of columns: "))
  19.     ) ;_ end of progn
  20.   ) ;_ end of if
  21.   (princ "\nUpdating the array. ")
  22.   (if (setq rowdist (* row# rowspac))
  23.     (UTIL:PUTPROPERTY
  24.       entnam
  25.       "Vertical Array Distance"
  26.       rowdist
  27.       vlax-vbdouble
  28.     ) ;_ end of UTIL:PUTPROPERTY
  29.     (alert "Row Distance not set.")
  30.   ) ;_ end of if
  31.   (if (setq coldist (* col# colspac))
  32.     (UTIL:PUTPROPERTY
  33.       entnam
  34.       "Horizontal Array Distance"
  35.       coldist
  36.       vlax-vbdouble
  37.     ) ;_ end of UTIL:PUTPROPERTY
  38.     (alert "Column Distance not set.")
  39.   ) ;_ end of if
  40.   (vl-cmdf "._updatefield" entnam "")
  41.   (UTIL:RESETDEBUG)
  42. ) ;_ end of defun - ARSI:UPDATEBLOCK
  43.  
  44. ;;;-------------------------------------------------------------------------------------;
  45. ;;;  Function: ARSI:DRAWRECTANGLE                                                       ;
  46. ;;;-------------------------------------------------------------------------------------;
  47. ;;;
  48. ;;;
  49. ;; GrText Demo Program 3  -  Lee Mac
  50. ;; Prompts the user for a base point and displays
  51. ;; the area of the rectangular region enclosed by the
  52. ;; base point and cursor as the cursor is moved across the screen.
  53. ;;
  54. ;; Requires: GrText.lsp
  55. ;;;-------------------------------------------------------------------------------------;
  56.  
  57. (defun ARSI:DRAWRECTANGLE (/ cursor dis pt1 pt2 pt3 pt4 vector)
  58.   (setq basept (reverse (cdr (reverse basept)))
  59.         vector (list (cos blkrot) (sin blkrot))
  60.         pt1    (trans basept entnam vector)
  61.         basept (trans basept entnam 1)
  62.   ) ;_ end of setq
  63.   (while (= 5 (car (setq cursor (grread nil 13 0))))
  64.     (redraw)
  65.     (setq cursor (cadr cursor)
  66.           pt3    (trans cursor 1 vector)
  67.           dis    (mapcar '- pt3 pt1)
  68.           row#   (fix (/ (- (cadr cursor) (cadr basept)) rowspac))
  69.           col#   (fix (/ (- (car cursor) (car basept)) colspac))
  70.           pt2    (trans (list (car pt1) (cadr pt1) (caddr pt3)) vector 1)
  71.           pt4    (trans (list (car pt3) (cadr pt3) (caddr pt1)) vector 1)
  72.     ) ;_ end of setq
  73.     (if (and (>= row# 1) (>= col# 1))
  74.       (progn
  75.         (LM:DisplayGrText
  76.           cursor
  77.           (LM:GrText
  78.             (strcat (itoa row#) " Rows, " (itoa col#) " Columns")
  79.           ) ;_ end of LM:GrText
  80.           3
  81.           15
  82.           45
  83.         ) ;_ end of LM:DisplayGrText
  84.         (LM:DisplayGrText
  85.           cursor
  86.           (LM:GrText (strcat (itoa (* row# col#)) " Units"))
  87.           4
  88.           15
  89.           15
  90.         ) ;_ end of LM:DisplayGrText
  91.       ) ;_ end of progn
  92.       (LM:DisplayGrText
  93.         cursor
  94.         (LM:GrText "Zero or negative values NOT allowed!")
  95.         1
  96.         15
  97.         15
  98.       ) ;_ end of LM:DisplayGrText
  99.     ) ;_ end of if
  100.     (grvecs
  101. ;; ** line to update **
  102.       (list (if (or (< (car dis) 0.0) (< (cadr dis) 0.0))    
  103. ;; ** line to update **
  104.               -256
  105.               256
  106.             ) ;_ end of if
  107.             basept pt2 pt2 cursor cursor pt4 pt4 basept
  108.       ) ;_ end of list
  109.     ) ;_ end of grvecs
  110.   ) ;_ end of while
  111.   (redraw)
  112. ) ;_ end of defun - ARSI:DRAWRECTANGLE
  113.  
  114. ;; Display GrText  -  Lee Mac
  115. ;; pnt  -  cursor point in UCS
  116. ;; vec  -  GrText vector list
  117. ;; col  -  Text Colour (ACI Colour)
  118. ;; xof  -  x-offset from cursor in pixels
  119. ;; yof  -  y-offset from cursor in pixels
  120.  
  121. (defun LM:DisplayGrText ( pnt vec col xof yof / scl )
  122.     (setq scl (/ (getvar 'viewsize) (cadr (getvar 'screensize)))
  123.           pnt (trans pnt 1 2)
  124.     )
  125.     (grvecs (cons col vec)
  126.         (list
  127.             (list scl 0.0 0.0 (+ (car  pnt) (* xof scl)))
  128.             (list 0.0 scl 0.0 (+ (cadr pnt) (* yof scl)))
  129.             (list 0.0 0.0 scl 0.0)
  130.            '(0.0 0.0 0.0 1.0)
  131.         )
  132.     )
  133. )
  134.  
  135. (defun UTIL:PUTPROPERTY (entname propname varval vartype / object proplist)
  136.   (setq object (vlax-ename->vla-object entname))
  137.   (setq proplist (vla-getdynamicblockproperties object))
  138.   (setq proplist (vlax-variant-value proplist))
  139.   (setq proplist (vlax-safearray->list proplist))
  140.   (foreach item proplist
  141.     (if (= (strcase (vla-get-propertyname item)) (strcase propname))
  142.       (vla-put-value
  143.         item
  144.         (vlax-make-variant varval vartype)
  145.       ) ;_ end of vla-put-value
  146.     ) ;_ end of if
  147.   ) ;_ end of foreach
  148. ) ;_ end of defun - UTIL:PUTPROPERTY