Author Topic: [XDrX-PlugIn(19)] Select 2 points on POLYLINE and drag out a new segment  (Read 1037 times)

0 Members and 1 Guest are viewing this topic.

xdcad

  • Swamp Rat
  • Posts: 527
Mainly introduce point monitoring dynamic drag and AcGe geometry library

Several new functions are introduced:
Code - Auto/Visual Lisp: [Select]
  1. 1. xdrx-sysvar-push
  2. System variables are pushed onto the stack to save the scene and set new values.
  3.  
  4. (xdrx-sysvar-push '(("osmode" 512)....("cmdecho" 0)))
  5.  
  6. 2. xdrx-sysvar-pop
  7. The saved system variables are popped out of the stack, one level up, and the last scene is restored.
  8.  
  9. (xdrx-sysvar-pop) Restore the last call to xdrx-sysvar-push, one level
  10.  
  11. (xdrx-sysvar-pop t)
  12. Revert to the original call xdrx-sysvar-push
  13.  
  14. When the above two functions are paired, they can be nested
  15.  
  16. (xdrx-sysvar-push.....)
  17.          (xdrx-sysvar-push....)
  18.          .....
  19.          (xdrx-sysvar-pop)
  20. (xdrx-sysvar-pop)
  21.  



Code - Auto/Visual Lisp: [Select]
  1. (defun c:tt (/ int1 int2 p1 p2 pts seg inx inx1 inx2 p1-xline p2-xline p1-vecx p2-vecy p1-vecy)
  2. ;|
  3.     Drag the newly added segment and construct an XLINE parallel to P1P2 according to the mouse point dynpt
  4.     Find the intersection point with P1 vertical XLINE and P2 vertical XLINE, which is the new position of the two vertices.
  5. |;
  6.   (defun _callback (dynpt / xline crvcrvint1 crvcrvint2 int1 int2)
  7. ;|
  8. drag callback function.
  9. The intersection method of AcGe geometry library is completely used below.
  10. |;
  11.     (if (not (xdrx-points-iscolinear dynpt p1 p2));;Mouse Drag Point( DYNPT ) and P1, P2 is not collinear
  12.       (progn
  13.         (setq xline (xdge::constructor "kLine3d" dynpt (mapcar '+ dynpt p1-vecx))
  14.               crvcrvint1 (xdge::constructor "kCurveCurveInt3d" xline p1-xline '(0 0 1.0))
  15.               int1       (car (xdge::getpropertyvalue crvcrvint1 "intpoints"))
  16.               crvcrvint2 (xdge::constructor "kCurveCurveInt3d" xline p2-xline '(0 0 1.0))
  17.               int2       (car (xdge::getpropertyvalue crvcrvint2 "intpoints"))
  18.         )
  19. ;|
  20. if have two inter pnt,set the new Vertex
  21. |;
  22.         (if (and int1 int2)
  23.           (xdrx-setproperty e "pointat" (list inx1 int1) "pointat" (list inx2 int2))
  24.         )
  25.         (xdge::free crvcrvint1 crvcrvint2 xline);;Release GE Object Memory
  26.       )
  27.     )
  28.   )
  29. ;|
  30. Determine whether the two points P1 and P2 are within the same segment of the polyline
  31. |;
  32.   (defun _isSameSeg (e p1 p2)
  33.     (and
  34.       (setq seg1 (xdrx-getpropertyvalue e "onsegat" p1))
  35.       (setq seg2 (xdrx-getpropertyvalue e "onsegat" p2))
  36.       (= (car seg1) (car seg2))
  37.     )
  38.     (car seg1)
  39.   )
  40. ;|
  41. Pick point function, sort two points according to the polyline direction,
  42. Make sure P1 is before P2
  43. |;
  44.   (defun _pick-pnt ()
  45.     (setq p1 (getpoint "\nPoint to get starting point <exit>:"))
  46.     (setq p2 (getpoint "\nClick to get the end point <exit>:"))
  47.     (setq pts (xdrx-points-sortoncurve e (list p1 p2)))
  48.     (mapcar 'set '(p1 p2) (mapcar 'cadr pts))
  49.   )
  50. ;|
  51.    Add two vertices P1 and P2
  52. |;
  53.   (defun _add-vertex ()
  54.     (xdrx-setpropertyvalue e "addvertexat" (list (1+ inx) p1))
  55.     (setq seg (xdrx-getpropertyvalue e "onsegat" p2))
  56.     (xdrx-setpropertyvalue e "addvertexat" (list (1+ (car seg)) p2))
  57.   )
  58. ;|
  59. Construct XLINE through point PT and direction vector vecy
  60. |;
  61.   (defun _construct-xline-pnt (pt vecy)
  62.     (xdge::constructor "kLine3d" pt (mapcar '+ pt vecy))
  63.   )
  64. ;|
  65. Add two new vertices before and after the two picked points
  66. P1 and P2 as the two vertices of the dragged segment.
  67. |;
  68.   (defun _construct-drag-2pt (/ )
  69.     (setq inx1 (xdrx-getpropertyvalue e "nearindex" p1)
  70.           _short-len (/ (distance p1 p2) 10.0)
  71.     )
  72.     (setq temp-p1 (mapcar '+ p1 (xdrx-vector-product p1-vecx _short-len)))
  73.     (setq temp-p2 (mapcar '+ p2
  74.                           (xdrx-vector-product (xdrx-vector-negate p1-vecx) _short-len)))
  75.     (xdrx-setpropertyvalue e "addvertexat" (list (1+ inx1) temp-p1))
  76.     (setq inx1 (1+ inx1))
  77.     (setq inx2 (xdrx-getpropertyvalue e "nearindex" p2))
  78.     (xdrx-setpropertyvalue e "addvertexat" (list inx2 temp-p2))
  79.   )
  80.   (defun _drag-new-seg ()
  81.     (xdrx-pointmonitor "_callback")
  82.     (getpoint "\nSelect the appropriate location:")
  83.     (xdrx-pointmonitor)
  84.   )
  85.   ;; Main Program
  86.   (xdrx-begin)
  87.   ;;Save the specified system variable and then set the new value
  88.   (xdrx-sysvar-push '(("osmode" 544)("autosnap" 39)))
  89.   (if
  90.     (and (setq e (car (xdrx-entsel "\nSelect Polyline <Exit>:" '((0 . "*polyline")))))
  91.          (_pick-pnt)
  92.          (setq inx (_isSameSeg e p1 p2))
  93.          (_add-vertex)
  94.          (setq p1-vecx (xdrx-getpropertyvalue e "firstderiv" p1)
  95.                p1-vecy (xdrx-vector-perpvector p1-vecx)
  96.          )
  97.          (setq p1-xline (_construct-xline-pnt p1 p1-vecy))
  98.          (setq p2-xline (_construct-xline-pnt p2 p1-vecy))
  99.     )
  100.      (progn
  101.       (_construct-drag-2pt)
  102.       (_drag-new-seg)
  103.      )
  104.   )
  105.   ;;restore system variable
  106.   (xdrx-sysvar-pop)
  107.   (xdrx-end)
  108.   (princ)
  109. )
  110.  
« Last Edit: November 28, 2023, 07:58:36 PM by xdcad »
The code I wrote uses XDRX-API,which can be downloaded from github.com and is updated at any time.
===================================
https://github.com/xdcad
https://sourceforge.net/projects/xdrx-api-zip/
http://bbs.xdcad.net

Atook

  • Swamp Rat
  • Posts: 1031
  • AKA Tim
Re: [XDrX-PlugIn(19)] Select 2 points on POLYLINE and drag out a new segment
« Reply #1 on: November 28, 2023, 10:28:30 PM »
Very nice, that looks like it could be quite useful for some.