Author Topic: draw chainage on polyline-HELP WITH A LISP  (Read 2926 times)

0 Members and 1 Guest are viewing this topic.

pedroantonio

  • Guest
draw chainage on polyline-HELP WITH A LISP
« on: February 28, 2019, 02:37:01 AM »
Hi am using this code to add chanage to a a polyline but i need to do some changes
1) the chainage block to have 2 layers .One for the line and one for the text (Text color white ,line color 90)
2) convert chainage block  to Annotative with text paper size 2.5
3)Fix the last chainage of the polyline because is wrong


Code - Auto/Visual Lisp: [Select]
  1. (defun div-error (msg)
  2.  (if (vl-position msg '("console break" "Function cancelled" "quit / exit abort"))
  3.    (princ "Error!")
  4.    (princ msg)
  5.  )
  6.  (while (> (getvar "cmdactive") 0) (command))
  7.  (setq *error* olderror)
  8.  (princ)
  9. )
  10.  
  11. (defun divplus (len segm / num lst)
  12.  (setq num (fix (/ len segm)))
  13.  (setq cnt 0)
  14.  (while (<= cnt num)
  15.    (setq tmp (* cnt segm))
  16.    (setq lst (append lst (list tmp)))
  17.    (setq cnt (1+ cnt))
  18.  )
  19.  (setq delta (- len (last lst)))
  20.  (if (not (zerop delta))
  21.    (setq lst (append lst (list (+ (last lst) delta))))
  22.    lst
  23.  )
  24. )
  25.  
  26. (defun divminus (len segm / lst)
  27.  (while (>= len 0.) (setq lst (append lst (list len))) (setq len (- len segm)))
  28.  (if (not (zerop (last lst)))
  29.    (setq lst (append lst (list 0.0)))
  30.  )
  31.  lst
  32. )
  33.  
  34. (defun alg-ang (obj pnt)
  35. )
  36.  
  37. (defun answer (quest / wshl ans)
  38.  (setq wshl (vlax-get-or-create-object "WScript.Shell"))
  39.  (setq ans (vlax-invoke-method wshl 'popup quest 7 "Answer This Question:" vlax-vbyesno))
  40.  (cond  ((= ans 6) (setq opt t))
  41. ((= ans 7) (setq opt nil))
  42.  )
  43.  opt
  44. )
  45.  
  46.  
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48. (defun make-station (bname / acsp adoc atprom attag at_obj blk_obj hgt lay line_obj sfar)
  49.  (if (and (= (getvar "tilemode") 0) (= (getvar "cvport") 1))
  50.    (setq acsp (vla-get-paperspace adoc))
  51.    (setq acsp (vla-get-modelspace adoc))
  52.  )
  53.  (if (not (tblsearch "block" bname))
  54.    (progn (setq attag   "NUMBER"        ;(strcase (getstring "\nAttribute tag : \n"))
  55.          atprom "NUMBER"        ;(strcase (getstring T "\nAttribute prompt : \n"))
  56.          hgt    1.0             ;(getreal "\nAttribute text height : \n")
  57.    )
  58.    (setq lay (getvar "clayer"))
  59.    (setvar "clayer" "0")
  60.    (setvar "attreq" 0)
  61.    (setq line_obj (vlax-invoke acsp 'addline '(0. -3. 0.) (list 0. (* hgt 2.) 0.)))
  62.    (vla-put-color line_obj acred)
  63.    (setq blk_obj (vla-add (vla-get-blocks adoc) (vlax-3d-point '(0. 0. 0.)) bname)
  64.          sfar    (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list line_obj))
  65.    )
  66.    (vla-copyobjects adoc sfar blk_obj)
  67. ;;;  RetVal = object.AddAttribute(Height, Mode, Prompt, InsertionPoint, Tag, Value)
  68.    (setq at_obj (vla-addattribute
  69.                   blk_obj
  70.                   hgt
  71.                   acattributemodeverify
  72.                   atprom
  73.                   (vlax-3d-point '(0 10. 0.))
  74.                   attag
  75.                   "0"
  76.                 )
  77.    )
  78.    (vla-put-rotation at_obj (* pi 1.5))
  79.    (vlax-release-object blk_obj)
  80.    )
  81.    (progn (princ "\n\t >> Block does already exist!\n") (princ))
  82.  )
  83.  (if (tblsearch "block" bname)
  84.    t
  85.    (progn (alert "Impossible to add block"))
  86.  )
  87.  (setvar "attreq" 1)
  88.  (setvar "clayer" lay)
  89.  (vl-catch-all-apply (function (lambda () (vla-delete line_obj))))
  90.  (vla-regen adoc acactiveviewport)
  91.  (princ)
  92. )
  93. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  94. (defun c:chg (/ *error* acsp adoc appd div-error len num olderror pl pt pt_list step util)
  95.  (or appd (setq appd (vla-get-application adoc)))
  96.  (or acsp (setq acsp (vla-get-block (vla-get-activelayout adoc))))
  97.  (or util (setq util (vla-get-utility adoc)))
  98.  (setq olderror *error*)
  99.  (setq *error* div-error)
  100.  (if (not (tblsearch "block" "Station"))
  101.    (make-station "Station")
  102.  )
  103.  (vla-getentity util 'pl 'pt "\nSelect line NEAR OF POINT TO START measure: >>> \n")
  104.  (if (and pl (or (setq step (getdist "\Enter step distance [<10>]: ")) (setq step 10)))
  105.    (progn
  106.      
  107.      (setq opt (answer "Rotate text perpendicularly to pline?"))
  108.      (if (not step)
  109. (setq step 10.)
  110.      )
  111.      (if (< (distance (vlax-safearray->list pt) (vlax-curve-getstartpoint pl))
  112.      (distance (vlax-safearray->list pt) (vlax-curve-getendpoint pl))
  113.   )
  114. (setq pt_list (divplus len step))
  115. (setq pt_list (divminus len step))
  116.      )
  117.      (setq pt_list (vl-remove-if
  118.               (function not)
  119.               (mapcar (function (lambda (x) (vlax-curve-getpointatdist pl x))) pt_list)
  120.             )
  121.      )
  122.      (setq num 0)
  123.      (mapcar
  124.   (lambda (x / dr ang att_list at blk_obj)
  125.     (progn
  126.       (setq ang (alg-ang pl x)
  127.             ang (cond ((< (/ pi 2) ang (* pi 1.5)) (+ pi ang))
  128.                       (t ang)
  129.                 )
  130.       )
  131.       (setq blk_obj (vlax-invoke acsp 'insertblock x "Station" 1 1 1 ang))
  132.       (setq att_list (vlax-invoke blk_obj 'getattributes))
  133.       (foreach at att_list
  134.         (if (eq (vlax-get at 'tagstring) "NUMBER")
  135.           (progn (vlax-put at 'textstring (strcat "ch: " (rtos num 2 2) " m"))
  136.                  (if (not opt)
  137.                    (vlax-put at 'rotation 0)
  138.                  )
  139.                  (vla-update at)
  140.           )
  141.         )
  142.       )
  143.       (vla-update blk_obj)
  144.       (vlax-release-object blk_obj)
  145.       (setq num (+ num step))
  146.     )
  147.   )
  148. )
  149. pt_list
  150.      )
  151.      (if (not (vlax-object-released-p pl))
  152.      )
  153.    )
  154.    (princ "\nNothing selected try again\n")
  155.  )
  156.  (vla-regen adoc acactiveviewport)
  157.  (setq  *error* olderror
  158. div-error nil
  159.  )
  160.  (princ)
  161. )
  162. (prompt "\n")
  163. (prompt "\n    ***    Type chg to execute    *** \n")
  164.  
  165.  

thanks

pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #1 on: February 28, 2019, 04:35:46 AM »
I uupdate the code but i need the changes from post 1

Code - Auto/Visual Lisp: [Select]
  1. (defun div-error (msg)
  2.  (if (vl-position msg '("console break" "Function cancelled" "quit / exit abort"))
  3.    (princ "Error!")
  4.    (princ msg)
  5.  )
  6.  (while (> (getvar "cmdactive") 0) (command))
  7.  (setq *error* olderror)
  8.  (princ)
  9. )
  10.  
  11. (defun divplus (len segm / num lst)
  12.  (setq num (fix (/ len segm)))
  13.  (setq cnt 0)
  14.  (while (<= cnt num)
  15.    (setq tmp (* cnt segm))
  16.    (setq lst (append lst (list tmp)))
  17.    (setq cnt (1+ cnt))
  18.  )
  19.  (setq delta (- len (last lst)))
  20.  (if (not (zerop delta))
  21.    (setq lst (append lst (list (+ (last lst) delta))))
  22.    lst
  23.  )
  24. )
  25.  
  26. (defun divminus (len segm / lst)
  27.  (while (>= len 0.) (setq lst (append lst (list len))) (setq len (- len segm)))
  28.  (if (not (zerop (last lst)))
  29.    (setq lst (append lst (list 0.0)))
  30.  )
  31.  lst
  32. )
  33.  
  34. (defun alg-ang (obj pnt)
  35. )
  36.  
  37. (defun answer (quest / wshl ans)
  38.  (setq wshl (vlax-get-or-create-object "WScript.Shell"))
  39.  (setq ans (vlax-invoke-method wshl 'popup quest 7 "Answer This Question:" vlax-vbyesno))
  40.  (cond  ((= ans 6) (setq opt t))
  41. ((= ans 7) (setq opt nil))
  42.  )
  43.  opt
  44. )
  45.  
  46.  
  47. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  48. (defun make-station (bname / acsp adoc atprom attag at_obj blk_obj hgt lay line_obj sfar)
  49.  (if (and (= (getvar "tilemode") 0) (= (getvar "cvport") 1))
  50.    (setq acsp (vla-get-paperspace adoc))
  51.    (setq acsp (vla-get-modelspace adoc))
  52.  )
  53. (COMMAND "_layer" "_m" "_chg text" "_c" "7" "" "")
  54.  (if (not (tblsearch "block" bname))
  55.    (progn (setq attag   "NUMBER"        ;(strcase (getstring "\nAttribute tag : \n"))
  56.          atprom "NUMBER"        ;(strcase (getstring T "\nAttribute prompt : \n"))
  57.          hgt    1               ;(getreal "\nAttribute text height : \n")
  58.    )
  59.    (setq lay (getvar "clayer"))
  60.    (setvar "clayer" "0")
  61.    (setvar "attreq" 0)
  62.    (setq line_obj (vlax-invoke acsp 'addline '(0. -1. 0.) (list 0. (* hgt 1.) 0.)))
  63.    (vla-put-color line_obj acred)
  64.    (setq blk_obj (vla-add (vla-get-blocks adoc) (vlax-3d-point '(0. 0. 0.)) bname)
  65.          sfar    (vlax-safearray-fill (vlax-make-safearray vlax-vbobject '(0 . 0)) (list line_obj))
  66.    )
  67.    (vla-copyobjects adoc sfar blk_obj)
  68. ;;;  RetVal = object.AddAttribute(Height, Mode, Prompt, InsertionPoint, Tag, Value)
  69.    (setq at_obj (vla-addattribute
  70.                   blk_obj
  71.                   hgt
  72.                   acattributemodeverify
  73.                   atprom
  74.                   (vlax-3d-point '(0 10. 0.))
  75.                   attag
  76.                   "0"
  77.                 )
  78.    )
  79.    (vla-put-rotation at_obj (* pi 1.5))
  80.    (vlax-release-object blk_obj)
  81.    )
  82.    (progn (princ "\n\t >> Block does already exist!\n") (princ))
  83.  )
  84.  (if (tblsearch "block" bname)
  85.    t
  86.    (progn (alert "Impossible to add block"))
  87.  )
  88.  (setvar "attreq" 1)
  89.  (setvar "clayer" lay)
  90.  (vl-catch-all-apply (function (lambda () (vla-delete line_obj))))
  91.  (vla-regen adoc acactiveviewport)
  92.  (princ)
  93. )
  94. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  95. (defun c:chg2 (/ *error* acsp adoc appd div-error len num olderror pl pt pt_list step util)
  96.  (or appd (setq appd (vla-get-application adoc)))
  97.  (or acsp (setq acsp (vla-get-block (vla-get-activelayout adoc))))
  98.  (or util (setq util (vla-get-utility adoc)))
  99.  (setq olderror *error*)
  100.  (setq *error* div-error)
  101.  (if (not (tblsearch "block" "CHAINAGE"))
  102.    (make-station "CHAINAGE")
  103.  )
  104.  (vla-getentity util 'pl 'pt "\nSelect line NEAR OF POINT TO START measure: >>> \n")
  105.  (if (and pl (or (setq step (getdist "\Enter step distance [<10>]: ")) (setq step 10)))
  106.    (progn
  107.      
  108.      (setq opt (answer "Rotate text perpendicularly to pline?"))
  109.      (if (not step)
  110. (setq step 10.)
  111.      )
  112.      (if (< (distance (vlax-safearray->list pt) (vlax-curve-getstartpoint pl))
  113.      (distance (vlax-safearray->list pt) (vlax-curve-getendpoint pl))
  114.   )
  115. (setq pt_list (divplus len step))
  116. (setq pt_list (divminus len step))
  117.      )
  118.      (setq pt_list (vl-remove-if
  119.               (function not)
  120.               (mapcar (function (lambda (x) (vlax-curve-getpointatdist pl x))) pt_list)
  121.             )
  122.      )
  123.      (setq num 0)
  124.      (mapcar
  125.   (lambda (x / dr ang att_list at blk_obj)
  126.     (progn
  127.       (setq ang (alg-ang pl x)
  128.             ang (cond ((< (/ pi 2) ang (* pi 1.5)) (+ pi ang))
  129.                       (t ang)
  130.                 )
  131.       )
  132. (setq blk_obj (vlax-invoke
  133.                   acsp 'Insertblock    x "CHAINAGE" 1 1 1 ang)
  134.           )
  135.           (setq att_list (vlax-invoke blk_obj 'Getattributes))
  136.           (foreach at att_list
  137.         (if (eq (vlax-get at 'Tagstring) "NUMBER")
  138.           (progn
  139.             (vlax-put at 'Textstring (if (< num 990.)
  140.     (strcat "CH: 0+" (rtos num 2 2))
  141. (strcat "CH: "
  142.     (itoa (fix (/ num 1000.)));<--- changes 1200. on num (typo)
  143.     "+"
  144.     (rtos (- num (* (fix (/ num 1000.)) 1000)) 2 2))
  145. ))
  146.             (if (not opt)
  147.             (vlax-put at 'Rotation 0))
  148.             (vla-update at)
  149.           )
  150.         )
  151.           )
  152.           (vla-update blk_obj)
  153.           (vlax-release-object blk_obj)
  154.           (setq num (+ num step))
  155.         )
  156.       )
  157.     )
  158.     pt_list
  159.      )
  160.      (if (not (vlax-object-released-p pl))
  161.      )
  162.    )
  163.    (princ "\nNothing selected try again\n")
  164.  )
  165.  (vla-regen adoc acactiveviewport)
  166.  (setq  *error* olderror
  167. div-error nil
  168.  )
  169.  (princ)
  170. )
  171. (prompt "\n")
  172. (prompt "\n    ***    Type chg2 to execute    *** \n")
  173.  
  174.  
« Last Edit: February 28, 2019, 05:06:15 AM by Topographer »

pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #2 on: February 28, 2019, 05:08:12 AM »
I update the code , but i need help. My bigest problem is the last chainage text. Is not correct. Can any one fix it

Thanks

Dlanor

  • Bull Frog
  • Posts: 263
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #3 on: February 28, 2019, 06:48:56 AM »
Having a copy of the block (autoCAD 2010) might help.

pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #4 on: February 28, 2019, 09:48:51 AM »
Hi Dlanor.The lisp create the block

pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #5 on: February 28, 2019, 10:09:35 AM »
I upload a dwg file to help you understand my question

Dlanor

  • Bull Frog
  • Posts: 263
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #6 on: February 28, 2019, 01:53:10 PM »
Attached is the updated lisp.

When the block is created it is made annotative thanks to a little Lee Mac snippet I found [link is in file above the new (defun annotativeblock)].

The block now has two layers. The line is in layer 0 colour 90 The attribute is in layer "_chg text". I hope this is correct.

The Error on the last chainage was caused by the increment inside the mapcar exceeding the polyline length. I've added a check so that when num > len set num = len

Although the block is annotative I'm afraid I have no idea how to make it 2.5? in paperspace, perhaps someone else can step in as I have never worked with annotative blocks.

I have also tidied up a few lines of code. The layer is now only created if it doesn't exist.


pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #7 on: February 28, 2019, 05:50:33 PM »
Thank you Dlanor. Can toy add a new layer for the green line with name "_chg line "?

Dlanor

  • Bull Frog
  • Posts: 263
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #8 on: March 01, 2019, 03:52:13 AM »
Attached is updated lisp

pedroantonio

  • Guest
Re: draw chainage on polyline-HELP WITH A LISP
« Reply #9 on: March 01, 2019, 05:16:17 AM »
Thank you  Dlanor