Author Topic: Script to determine perpendicular point of insertion of block and polyline  (Read 1346 times)

0 Members and 1 Guest are viewing this topic.

dubb

  • Swamp Rat
  • Posts: 1105
Amazingly I got part of this code to work for me but I can't seem to figure out why its bugging out.
To reproduce the error:
Draw a 300 ft meandering polyline, insert some blocks along that polyline
run the script, pick the polyline, pick the blocks. As soon as you pick the farthest block it will do weird things and error out.
Code: [Select]
;draws a perpendicular line from the insertion point of a block to another line that is offset
;prints the length for each block
(defun test(/ obj VLAobj ent1 ent2 revline inspt perppt len)
  (vl-load-com)
(setq obj (entsel "\n >> Select profile >>"))
(redraw (car obj) 3) ;highlight polyline
(setq VLAobj (vlax-ename->vla-object (car obj)))
(markpt VLAobj)

(initget "Y y N n")
(setq revline (getkword "\nReverse line?"))
(cond
  ((or
     (= revline "y")
     (= revline "Y")
     )
   (princ "\nReverse polyline")
   (command "pedit" obj "r" "")
   (entdel ent1)
   (entdel ent2)
   (markpt VLAobj)
   )
  ((or
     (= revline "n")
     (= revline "N")
     )
   (princ "\nKeep polyline")
   (markpt VLAobj)
   )
  (princ "\nDone!")
  );end cond

(while
(setq blk (car (entsel "\nSelect block: ")))
(setq inspt (cdr (assoc 10 (entget blk))))
(setvar "lastpoint" inspt)
(setq perpPt (osnap (cadr obj)"per"))
(setq len (vlax-curve-getdistatpoint VLAobj perppt))
(command "line" inspt perppt "")
;(command "point" perppt)
  ;(command "point" inspt)
  (princ len)
  )
  (entdel ent1)
  (entdel ent2) 
);end defun


(defun markpt(entobj / )
(setq startpt (vlax-curve-getStartPoint entobj))
(setq mkpt (list
     (cons 0 "POINT") ;entity type
     (cons 10 startpt) ;coordinates
     (cons 62 1) ;color
     (cons 8 "0") ;layer
     )
      )
(setq  ent1 (entmakex mkpt))
(setq mktxt (list
      (cons 0 "TEXT")
      (cons 10 startpt) ;coordinates
      (cons 40 4.0) ;text height
      (cons 1 "Start Point") ;text contents
      (cons 62 1) ;color
      (cons 8 "0") ;layer
     )
      )
(setq ent2 (entmakex mktxt))
  )

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Change:
Code: [Select]
(setq perpPt (osnap (cadr obj)"per"))to:
Code: [Select]
(setq perpPt (vlax-curve-getclosestpointto (car obj) inspt))

And:
Code: [Select]
(command "line" inspt perppt "")to:
Code: [Select]
(command "_.line" "_non" inspt "_non" perppt "")

dubb

  • Swamp Rat
  • Posts: 1105
Wow! It works now! Thanks Lee.

Change:
Code: [Select]
(setq perpPt (osnap (cadr obj)"per"))to:
Code: [Select]
(setq perpPt (vlax-curve-getclosestpointto (car obj) inspt))

And:
Code: [Select]
(command "line" inspt perppt "")to:
Code: [Select]
(command "_.line" "_non" inspt "_non" perppt "")