Author Topic: How do I find the Midpoint of the last segment of a pline.  (Read 1289 times)

0 Members and 1 Guest are viewing this topic.

Browny501

  • Mosquito
  • Posts: 15
How do I find the Midpoint of the last segment of a pline.
« on: March 28, 2020, 05:32:54 AM »
HI All

I'm trying to draw a "tadpole/fall indicator", then rotate it to the angle the user wants on the screen, then scale to the size the user wants on the screen.

Code: [Select]
(defun DRAW_TAD ( / TAD_LOC cmde osmd)
 (setq cmde (getvar "cmdecho"))
 (setq osmd (getvar "osmode"))
 (setvar "cmdecho"  0)
 
  (Command "Layer" "T" "CP Ext Fall Indicator" "")
  (Command "Layer" "m" "CP Ext Fall Indicator" "colour" "T" "2,130,0"    "CP Ext Fall Indicator" "")

  (setvar "osmode" 0)

  (command "_.layer" "_set" "0" "")       ;; make the tadpole look like temp graphics
  (command "_color"   "yellow"  "")       ;;                                         
  (command "_linetype" "_s" "hidden" "")    ;;                                         
 
  (command "pline" "0,0" "0,1" "-0.22,1.4" "0.32,1.56" "-0.42,1.74" "0.60,2.00" "-0.6,2.0" "")
  (setq   TAD_LOC (getpoint "\nSelect Tadpole insertion point :"))
  (command "move" "l" "" "0,0" TAD_LOC "")

  ; (setq TAD (entget (entlast)))        ;; not used cause it blew my mind trying to figure out midpt

  (command "rotate" "l" "" TAD_LOC "r" "90" pause ) 
  (command "scale"  "l" "" TAD_LOC "r" TAD_LOC pause pause)
  (Command "chprop" "l" "" "LA" "CP Ext Fall Indicator" "")
  (Command "chprop" "l" "" "C"  "bylayer" "")
  (Command "chprop" "l" "" "LT" "bylayer" "")
  (command "pedit"  "l" "W" "0.05" "")

  (command "_linetype" "_s" "bylayer" "") ; return back to normal
  (command "_color" "bylayer" "")         ; return back to normal
 
 (setvar "cmdecho" cmde)
 (setvar "osmode"  osmd)
 (princ)
)   ; end defun 

The above works, but I'd prefer to be able to use the midpoint of the last segment of the polyline as the second scale reference point, but i'm struggling.  I hope that makes sense...

Any help would by greatly appreciated.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #1 on: March 28, 2020, 05:40:56 AM »
Hi,

Code - Auto/Visual Lisp: [Select]
  1. (defun getMidPointOfLastSegment (pline)
  2.     pline
  3.     (- (vlax-curve-getendParam pline) 0.5)
  4.   )
  5. )
Speaking English as a French Frog

Browny501

  • Mosquito
  • Posts: 15
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #2 on: March 28, 2020, 07:16:41 AM »
Thanks gile, but could you put some comments against each line so I can understand whats going on.

I'm struggling to incorporate it into my code.

thanks 

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #3 on: March 28, 2020, 07:51:07 AM »
For the curve entities (line, arc, circle, polyline, ...) AutoCAD uses a data called "parameter" (a real number).
 According to the curve type, the parameter does not represent the same thing. For a line the parameter represents the lenth from start point, for an arc, the angle from start angle.
For polylines the parameter value is an integer value at each vertex and between vertices the value of the parameter at previous vertex plus the percent of segment length. For example:
- the parameter at polyline start point is equal to 0.0
- the parameter at polyline second vertex is equal to 1.0
- the parameter at the last vertex is equal to the number of vertices minus one, it's called end parameter
- the parameter at the quarter of the second segment length is 1.25 (1.0 + 0.25)
- the parameter at the mid point of the last segment is equal to the end parameter minus 0.5

(vlax-curve-getEndparam pline) returns the value of the end parameter,
(- (vlax-curve-getEndparam pline) 0.5) returns the value of the parameter at the middle of the last segment
vlax-curve-getPointAtParam return exactly what it means: the point at specified parameter.
Speaking English as a French Frog

Browny501

  • Mosquito
  • Posts: 15
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #4 on: March 28, 2020, 11:43:10 AM »
Sorry gile, i'm still none the wiser, this is beyond my knowledge.

if i want to call the midpt of the last segment TAD_LOC2 how do i get the value using this defun....I tried to amend it to the following and I'm clearly failing.

Code: [Select]
(setq TAD (entlast))     
(setq TAD_LOC2
  (vlax-curve-getPointAtParam
    TAD
    (- (vlax-curve-getendParam TAD) 0.5)
  )
)

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #5 on: March 28, 2020, 11:54:29 AM »
Browny, the code you posted works just fine. Why do you think it isn't?

Browny501

  • Mosquito
  • Posts: 15
Re: How do I find the Midpoint of the last segment of a pline.
« Reply #6 on: March 28, 2020, 01:33:29 PM »
Hi Jeff, your right... i think its being cooped up inside it is sending me doolally

cheers