Author Topic: New to Autolisp want to know where to start Example of what im trying to do.  (Read 1589 times)

0 Members and 1 Guest are viewing this topic.

abe123456789

  • Newt
  • Posts: 24
Good evening yall,

I am new to auto lisp and want to learn more especially manipulating Dynamic blocks. I using bricscad since it is a lot cheaper than the full version of autocad.

Attached is a drawing with steps 1 - 4. I need some help on creating a lisp routine that can accomplish the steps 2- 4.
There is 2 dynamic blocks im trying to manipulate.
Step 1
is just a random layout of a dynamic block with a line i want to connect to

Step 2
Inserts one of my dynamic blocks to the center of my other dynamic blocks

Step 3
connect my dynamic block to the line from step 1

Step 4
Removes the line and insert dynamic block as shown.

Thank you in advance.

BIGAL

  • Swamp Rat
  • Posts: 1416
  • 40 + years of using Autocad
This was posted elsewhere maybe forums/autodesk the 1st step can be done pretty easy using Lee-mac dynamic block.lsp to set the visibility.

The joining has been discussed on the other forums.
A man who never made a mistake never made anything


abe123456789

  • Newt
  • Posts: 24
Can some one point me in the direction on how to use lee mac dynamic functions?

d2010

  • Bull Frog
  • Posts: 326
I am new to auto lisp and want to learn more especially manipulating Dynamic blocks. I using bricscad since it is a lot cheaper than the full version of autocad.
if your autolisp-skill is too leak, then you do not develop the dynamic/sBlocks.lisp, because, the DynamicsBlocks> create too many problem/s for everyone.
« Last Edit: April 03, 2022, 11:18:12 AM by d2010 »

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
Can some one point me in the direction on how to use lee mac dynamic functions?

Quote
Here, I've put something together for you, but something's not good... Though, for me and my eyes, it should be pretty well done... Now too late, see if you can fix it... For a starter, it's quite good to dig into the coding... As you see, no Lee's subs used, but it's all coded through AutoLISP and VisualLisp - all needed functions for manipulating Dynamic Blocks are there - built-in...

EDIT : I've updated the code - and fixed all issues I encountered - it should be working just fine now...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:sprinklers2pipes ( / *cad* *adoc* *spc* *error* ss2ss unique blnames blkbegin2blkdef _and_or boundfunc _and_ _or_ cmde ss ssins sslin linlst blnlst init n bln opt blkdef ch p nlst props prop-names distlst anglst extdict paramentlst visparam visname vischk visvallst pipevisparam pipeprops pipedist pipedistlst pipeang pipeanglst pw pp tmpli tmplilst lil li lilst d1 d2 lix li1 li2 endpts pipe ptl )
  2.  
  3.   (or *cad* (not (vl-catch-all-error-p (setq *cad* (vl-catch-all-apply (function vlax-get-acad-object) nil)))) (vl-load-com))
  4.   (or *adoc* (setq *adoc* (vla-get-activedocument *cad*)))
  5.  
  6.   (defun *error* ( m )
  7.     (if (= 8 (logand 8 (getvar 'undoctl)))
  8.       (vla-endundomark *adoc*)
  9.     )
  10.     (sssetfirst nil nil)
  11.     (if cmde (setvar 'cmdecho cmde))
  12.     (if m (prompt m))
  13.     (princ)
  14.   )
  15.  
  16.   (defun ss2ss ( masterss etype-filter-str )
  17.     (sssetfirst nil nil)
  18.     (sssetfirst nil masterss)
  19.     (ssget "_I" (list (cons 0 etype-filter-str)))
  20.   )
  21.  
  22.   (defun unique ( lst )
  23.     (if lst
  24.       (cons (car lst) (unique (vl-remove (car lst) (cdr lst))))
  25.     )
  26.   )
  27.  
  28.   (defun blnames ( / objnlst )
  29.     (vlax-for obj (vla-get-blocks *adoc*)
  30.       (if
  31.         (and
  32.           (vlax-property-available-p obj 'isdynamicblock)
  33.           (= (vla-get-isdynamicblock obj) :vlax-true)
  34.           (vlax-property-available-p obj 'effectivename)
  35.         )
  36.         (setq objnlst (cons (vla-get-effectivename obj) objnlst))
  37.         (vlax-for subobj obj
  38.           (if
  39.             (and
  40.               (vlax-property-available-p subobj 'isdynamicblock)
  41.               (= (vla-get-isdynamicblock subobj) :vlax-true)
  42.               (vlax-property-available-p subobj 'effectivename)
  43.             )
  44.             (setq objnlst (cons (vla-get-effectivename subobj) objnlst))
  45.           )
  46.         )
  47.       )
  48.     )
  49.     (unique objnlst)
  50.   )
  51.  
  52.   (defun blkbegin2blkdef ( enx )
  53.     (cdr (assoc 330 (reverse (entget (cdr (assoc -2 enx))))))
  54.   )
  55.  
  56.   (defun _and_or ( _and_or_flg boundlst / r )
  57.     (progn (vl-some (function (lambda ( x ) (cond ( _and_or_flg (setq r x) (not r) ) ( t (setq r x) r )))) boundlst) r)
  58.   )
  59.   (defun boundfunc ( boundlst / r ) (progn (setq r (mapcar (function read) (mapcar (function (lambda ( x ) (setq *n* (if (not *n*) 1 (1+ *n*))) (strcat "***" (itoa *n*) "***"))) boundlst))) (setq *n* nil) r))
  60.   (defun _and_ ( boundlst ) (_and_or t boundlst))
  61.   (defun _or_ ( boundlst ) (_and_or nil boundlst))
  62.  
  63.   (setq cmde (getvar 'cmdecho))
  64.   (setvar 'cmdecho 0)
  65.   (if (= 8 (logand 8 (getvar 'undoctl)))
  66.     (vla-endundomark *adoc*)
  67.   )
  68.   (vla-startundomark *adoc*)
  69.   (princ "\n")
  70.   (prompt "\nInitial selection [ Blocks (Sprinklers) + Lines ]...")
  71.   (princ "\n")
  72.   (if (setq ss (ssget '((0 . "INSERT,LINE"))))
  73.     (progn
  74.       (setq ssins (ss2ss ss "INSERT")
  75.             sslin (ss2ss ss "LINE")
  76.             linlst (vl-remove-if 'listp (mapcar 'cadr (ssnamex sslin)))
  77.       )
  78.       (sssetfirst nil nil)
  79.       (if (setq blnlst (blnames))
  80.         (if
  81.           (and
  82.             (not (initget (setq init (apply 'strcat (mapcar '(lambda ( x ) (or n (setq n 0)) (setq n (1+ n)) (if (= n (length blnlst)) x (strcat x " "))) blnlst)))))
  83.             (setq n 0)
  84.             (setq bln (getkword (setq opt (strcat "\nChoose Piping Block name [ " init " ] : "))))
  85.             (if (/= (vla-get-isdynamicblock (setq blkdef (vlax-ename->vla-object (blkbegin2blkdef (entget (tblobjname "BLOCK" bln)))))) :vlax-true)
  86.               (progn
  87.                 (while (/= (vla-get-isdynamicblock (setq blkdef (vlax-ename->vla-object (blkbegin2blkdef (entget (tblobjname "BLOCK" bln)))))) :vlax-true)
  88.                   (prompt "\n\nWrong selection - choosen Block is not Dynamic - choose again...")
  89.                   (initget blnlst)
  90.                   (setq bln (getkword opt))
  91.                 )
  92.                 t
  93.               )
  94.               t
  95.             )
  96.             (not (initget "Yes No"))
  97.             (or (= (setq ch (getkword "\nDo you want starting default connection - Pipe Block [ Yes / No ] <No> : ")) nil) (= ch "Yes") (= ch "No"))
  98.           )
  99.           (progn
  100.             (foreach ref (vl-remove-if 'listp (mapcar 'cadr (ssnamex ssins)))
  101.               (setq nlst (cons (list (vla-insertblock *spc* (vlax-3d-point (setq p (trans (cdr (assoc 10 (entget ref))) ref 0))) bln 1 1 1 0) p) nlst))
  102.             )
  103.             (setq prop-names (mapcar 'vla-get-propertyname (setq props (vlax-invoke (caar nlst) 'getdynamicblockproperties))))
  104.             (setq distlst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (vla-get-propertyname x) "Distance*") x)) props)))
  105.             (setq anglst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (vla-get-propertyname x) "Angle*") x)) props)))
  106.             (if
  107.               (and
  108.                 (= (length distlst) (length anglst) 1)
  109.                 (= (vla-get-hasextensiondictionary blkdef) :vlax-true)
  110.                 (setq paramentlst (mapcar 'cdr (vl-remove-if-not '(lambda ( x ) (= (car x) 360)) (setq extdict (dictsearch (vlax-vla-object->ename (vla-getextensiondictionary blkdef)) "ACAD_ENHANCEDBLOCK")))))
  111.                 (setq visparam (vl-some '(lambda ( x ) (if (= (cdr (assoc 0 (entget x))) "BLOCKVISIBILITYPARAMETER") x)) paramentlst))
  112.                 (setq visname (cdr (assoc 301 (entget visparam))))
  113.                 (vl-position (strcase visname) (mapcar 'strcase prop-names))
  114.                 (setq visvallst (vl-sort (vlax-get (vl-some '(lambda ( x ) (if (= (strcase (vla-get-propertyname x)) (strcase visname)) x)) props) 'allowedvalues) '<))
  115.               )
  116.               (progn
  117.                 (foreach pipe_pt nlst
  118.                   (if (= ch "Yes")
  119.                     (progn
  120.                       (vla-put-value (setq pipevisparam (vl-some '(lambda ( x ) (if (= (strcase (vla-get-propertyname x)) (strcase visname)) x)) (setq pipeprops (vlax-invoke (car pipe_pt) 'getdynamicblockproperties)))) (vlax-make-variant (car visvallst) (vlax-variant-type (vla-get-value pipevisparam))))
  121.                       (setq pipedist (car (setq pipedistlst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Distance*")) x)) pipeprops)))))
  122.                       (setq pipeang (car (setq pipeanglst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Angle*")) x)) pipeprops)))))
  123.                       (vla-put-value pipedist (vlax-make-variant 12.0 (vlax-variant-type (vla-get-value pipedist))))
  124.                       (vla-put-value pipeang (vlax-make-variant 0.0 (vlax-variant-type (vla-get-value pipeang))))
  125.                       (setq p (polar (trans (cadr pipe_pt) 0 1) 0.0 12.0))
  126.                       (setq pw (trans p 1 0))
  127.                       (setq pp (trans (car (vl-sort (mapcar '(lambda ( x ) (trans (vlax-curve-getclosestpointto x pw) 0 1)) linlst)
  128.                                             '(lambda ( a b ) (< (distance a p) (distance b p))))) 1 0)
  129.                       )
  130.                       (setq tmplilst (cons (setq tmpli (entmakex (list (cons 0 "LINE") (cons 10 pw) (cons 11 pp)))) tmplilst))
  131.                       (setq nlst (subst (list (car pipe_pt) (cadr pipe_pt) pw pp tmpli) pipe_pt nlst))
  132.                     )
  133.                     (progn
  134.                       (setq p (trans (cadr pipe_pt) 0 1))
  135.                       (setq pw (trans p 1 0))
  136.                       (setq pp (trans (car (vl-sort (mapcar '(lambda ( x ) (trans (vlax-curve-getclosestpointto x pw) 0 1)) linlst)
  137.                                             '(lambda ( a b ) (< (distance a p) (distance b p))))) 1 0)
  138.                       )
  139.                       (setq tmplilst (cons (setq tmpli (entmakex (list (cons 0 "LINE") (cons 10 pw) (cons 11 pp)))) tmplilst))
  140.                       (setq nlst (subst (list (car pipe_pt) (cadr pipe_pt) pw pp tmpli) pipe_pt nlst))
  141.                       (vla-delete (car pipe_pt))
  142.                     )
  143.                   )
  144.                 )
  145.                 ;;; Change here number of repeat - it's now 1 - (repeating of shortcuts search - if you need to get the most optimal results) ;;;
  146.                 (repeat 1
  147.                   (setq n (1+ n))
  148.                   (foreach pipe_pt nlst
  149.                     (setq pw (caddr pipe_pt))
  150.                     (setq pp (trans (car (vl-sort (mapcar '(lambda ( x ) (trans (vlax-curve-getclosestpointto x pw) 0 1)) (vl-remove (setq lil (last pipe_pt)) (append tmplilst linlst)))
  151.                                           '(lambda ( a b ) (< (distance a pw) (distance b pw))))) 1 0)
  152.                     )
  153.                     (if (setq li (vl-some '(lambda ( x ) (if (equal (distance pp (vlax-curve-getclosestpointto x pp)) 0.0 1e-6) x)) (append tmplilst linlst)))
  154.                       (progn
  155.                         (setq lilst (cons li lilst))
  156.                         (if (< (setq d1 (distance pw pp)) (setq d2 (distance pw (cadddr pipe_pt))))
  157.                           (progn
  158.                             ;;; (alert (strcat "Shortcut IS founded at loop : " (itoa n))) ;;; debugging... ;;;
  159.                             (setq nlst (subst (subst nil lil pipe_pt) pipe_pt nlst))
  160.                             (if (not (equal d1 0.0 1e-6))
  161.                               (setq tmplilst (cons (entmakex (list (cons 0 "LINE") (cons 10 pw) (cons 11 pp))) tmplilst))
  162.                             )
  163.                             (if (not (vlax-erased-p lil))
  164.                               (entdel lil)
  165.                             )
  166.                             (setq tmplilst (cons (setq li1 (entmakex (list (cons 0 "LINE") (assoc 10 (setq lix (entget li))) (cons 11 pp)))) tmplilst)
  167.                                   tmplilst (cons (setq li2 (entmakex (list (cons 0 "LINE") (cons 10 pp) (assoc 11 lix)))) tmplilst)
  168.                                   tmplilst (vl-remove-if '(lambda ( x ) (or (vlax-erased-p x) (equal (distance (vlax-curve-getstartpoint x) (vlax-curve-getendpoint x)) 0.0 1e-6))) tmplilst)
  169.                                   lilst (cdr lilst) lilst (cons li1 lilst) lilst (cons li2 lilst)
  170.                             )
  171.                           )
  172.                         )
  173.                         (if (vl-position lil lilst)
  174.                           (if (< d1 d2)
  175.                             (princ)
  176.                             ;;; (alert "But processing element/connection of (foreach) loop after first connections of sprinklers to lines founded in a list of connections that are supposed to be unchanged through updating shortcuts...") ;;; debugging... ;;;
  177.                             (princ)
  178.                             ;;; (alert "Shortcut NOT founded, but processing element/connection of (foreach) loop after first connections of sprinklers to lines founded in a list of connections that are supposed to be unchanged through updating shortcuts...") ;;; debugging... ;;;
  179.                           )
  180.                         )
  181.                       )
  182.                     )
  183.                   )
  184.                   ;;; (or lilst (alert (strcat "No shortcut founded at loop : " (itoa n)))) ;;; debugging... ;;;
  185.                   (setq nlst (vl-remove-if-not '(lambda ( x ) (and (last x) (vl-position (last x) tmplilst))) nlst)
  186.                         lilst nil
  187.                   )
  188.                 )
  189.                 (setq endpts (mapcar 'vlax-curve-getendpoint tmplilst))
  190.                 (foreach lin tmplilst
  191.                   (setq pipe (vla-insertblock *spc* (vlax-3d-point (vlax-curve-getstartpoint lin)) bln 1 1 1 0))
  192.                   (vla-put-value (setq pipevisparam (vl-some '(lambda ( x ) (if (= (strcase (vla-get-propertyname x)) (strcase visname)) x)) (setq pipeprops (vlax-invoke pipe 'getdynamicblockproperties)))) (vlax-make-variant (car visvallst) (vlax-variant-type (vla-get-value pipevisparam))))
  193.                   (setq pipedist (car (setq pipedistlst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Distance*")) x)) pipeprops)))))
  194.                   (setq pipeang (car (setq pipeanglst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Angle*")) x)) pipeprops)))))
  195.                   (vla-put-value pipedist (vlax-make-variant (distance (vlax-curve-getstartpoint lin) (vlax-curve-getendpoint lin)) (vlax-variant-type (vla-get-value pipedist))))
  196.                   (vla-put-value pipeang (vlax-make-variant (angle (vlax-curve-getstartpoint lin) (vlax-curve-getendpoint lin)) (vlax-variant-type (vla-get-value pipeang))))
  197.                   (if (not (vlax-erased-p lin))
  198.                     (entdel lin)
  199.                   )
  200.                 )
  201.                 (foreach li linlst
  202.                   (setq ptl (vl-remove-if-not '(lambda ( x ) (equal (distance x (vlax-curve-getclosestpointto li x)) 0.0 1e-6)) endpts))
  203.                   (setq ptl (vl-sort ptl '(lambda ( a b ) (< (vlax-curve-getparamatpoint li a) (vlax-curve-getparamatpoint li b)))))
  204.                   (setq ptl (append (list (vlax-curve-getstartpoint li)) ptl (list (vlax-curve-getendpoint li))))
  205.                   (mapcar '(lambda ( p1 p2 )
  206.                     (setq pipe (vla-insertblock *spc* (vlax-3d-point p1) bln 1 1 1 0))
  207.                     (vla-put-value (setq pipevisparam (vl-some '(lambda ( x ) (if (= (strcase (vla-get-propertyname x)) (strcase visname)) x)) (setq pipeprops (vlax-invoke pipe 'getdynamicblockproperties)))) (vlax-make-variant (car visvallst) (vlax-variant-type (vla-get-value pipevisparam))))
  208.                     (setq pipedist (car (setq pipedistlst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Distance*")) x)) pipeprops)))))
  209.                     (setq pipeang (car (setq pipeanglst (vl-remove nil (mapcar '(lambda ( x ) (if (wcmatch (strcase (vla-get-propertyname x)) (strcase "Angle*")) x)) pipeprops)))))
  210.                     (vla-put-value pipedist (vlax-make-variant (distance p1 p2) (vlax-variant-type (vla-get-value pipedist))))
  211.                     (vla-put-value pipeang (vlax-make-variant (angle p1 p2) (vlax-variant-type (vla-get-value pipeang))))
  212.                     ) ptl (cdr ptl)
  213.                   )
  214.                   (if (not (vlax-erased-p li))
  215.                     (entdel li)
  216.                   )
  217.                 )
  218.               )
  219.               (if (not visparam)
  220.                 (prompt (strcat "\nChoosen Dynamic Block : " bln " doesn't have Visibility Property assigned which confronts supposed state of initially proposed DWG..."))
  221.               )
  222.             )
  223.           )
  224.         )
  225.         (prompt "\nThere are no Block entities detected in DWG which confronts supposed state of initially proposed DWG...")
  226.       )
  227.     )
  228.     (prompt "\nEmpty selection set... Repeat procedure again next time!!!")
  229.   )
  230.   (*error* nil)
  231. )
  232.  

HTH. M.R.
« Last Edit: April 10, 2022, 11:46:29 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3279
  • Marko Ribar, architect
The code updated...

Regards, M.R.
HTH.
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

abe123456789

  • Newt
  • Posts: 24
I appreciate the time  you took on this, but its not working for me.
I keep getting this error.

Ive been following the autodesk lesson on activeX Developer guide to learn.
I am using bricscad instead of autocad will that make a difference?

Initial selection [ Blocks (Sprinklers) + Lines ]...



; ----- LISP : Call Stack -----

;
  • ...C:SPRINKLERS2PIPES <<--



; ----- Error around expression -----
;
(VLE-ENTGETOLD 10 REF)

; in file :
;
C:\Users\abe7e\Documents\Bricscad support\Lisp\asdf.lsp

bad argument type <NIL> ; expected VLA-OBJECT at [vla-insertblock]

Ive been trying to learn how to program by following the autodesk lesson on activeX Developer guide. Do you think thats a good starting point?
I am using bricscad instead of autocad will that make a difference?