Author Topic: Lisp to Insert a block and then mirror it so that it's X value is -1  (Read 847 times)

0 Members and 1 Guest are viewing this topic.

Sisyphus

  • Mosquito
  • Posts: 8

Hi All

Start Preface -
I'm having trouble with some lisp programming. I DID have a great manual on lisp programming that slow walked you through how to do various things, it came out back when Autocad still produced hard copy manuals with their releases (2002-2008?), so it was a while ago.
Now I can't find that blasted manual (I think that I lent it out to someone and they didn't return it).
Anyway, I can remember that it made a big deal of associate numbers and how to manipulate them to your advantage in lisps, converting to strings, angles in radians, etc.

Does anyone know if the old lisp manuals that were available with Acad. I found the 2013 version, the one that I am after was before 2013.
Failing that what is a nice, quick & dirty way of (re)learning simple Lisp programing (with examples)?
- End Preface

I have a lisp where I select an insertion point and insert a block at that point, put the block on a specific layer and change the visibility state to the one that I want.

So far, so good.

Now what I want to do is what I've described above (insert the block at a selected insertion point, change the layer & the visibility state) then mirror the block.
I know that when I want to mirror the block I can just select "last"
I know that the initial insertion point (assoc 10 & assoc 20) of the block will give me my first X, Y point for a mirror command
I know that I can pull apart the initial Y value of the insertion point add 10 to that value and have a second X, Y point for the mirror command. I don't have access to my old manual that walked me through the process of doing this.

Can someone please help me.

Copy of my circuit breaker lisp (visibility state change cribbed from Lee Mac) that I want to add the mirror change at the end of;

(defun C:SCHCB1( / ATD ATR COSM LTS SC)

;;;;; Collect OSMODE settings
(setq COSM (getvar 'osmode))
(setq ATD (getvar 'attdia))
(setq ATR (getvar 'attreq))

;;;;; Set OSMODE to INTERSECTION & NEAREST only
(setq COSM (getvar 'osmode))
(setvar 'osmode 544)
(setvar 'attdia 0)
(setvar 'attreq 0)

;;;;; Collect current LTscale value and create Values for SC
(setq LTS (getvar 'LTSCALE))
(setq SC (* 0.001 LTS))


;;;;; Insert BLOCK ;;;
(command "insert" "Sch Breaker" PAUSE SC SC PAUSE "")
(command "change" "l" "" "p" "la" "SCHEM" "")


;;;;; Return OSMODE to original values
(setvar 'osmode COSM)
(setvar 'attdia ATD)
(setvar 'attreq ATR)

;;;;; change visibility state
(defun CHGDYNPROP (Ename propname newval /  lo obj v vval sal tot i)
       ;; Changes a given variable in your block
       ;; Passed: Ename, Property Name, New value for Property
       ;;
       (setq obj (if (= (type Ename) 'vla-object)
    Ename
    (vlax-ename->vla-object Ename))
     v (vla-getdynamicblockproperties obj)
     vval (vlax-variant-value v)
     sal (vlax-safearray->list vval)
     tot (length sal)
     i 0)
       (while (< i tot)
         (if (= (vlax-get-property (nth i sal) "PropertyName") propname)
           (progn
             (vlax-put-property (nth i sal) "Value" newval)
             (setq i tot)
           )
           (setq i (1+ i))
         )
       )
     )
(CHGDYNPROP (entlast) "Visibility1" "Circuit Breaker Single Phase") ;Circuit Breaker Single Phase is the visibility state name
(princ)
)


BIGAL

  • Swamp Rat
  • Posts: 1396
  • 40 + years of using Autocad
Re: Lisp to Insert a block and then mirror it so that it's X value is -1
« Reply #1 on: March 14, 2023, 07:58:51 PM »
I have 4 manuals all different, Lisp, Customising Autocad, Extras etc R12 version sorry you can not have them. Yes refer to them often.

Kindle has some good books cheap. You can still go to Autocad help just need correct link think developer will try to find.

A man who never made a mistake never made anything

Sisyphus

  • Mosquito
  • Posts: 8
Re: Lisp to Insert a block and then mirror it so that it's X value is -1
« Reply #2 on: March 18, 2023, 11:01:49 PM »
Thanks for your help BigAl

DEVITG

  • Bull Frog
  • Posts: 479
Re: Lisp to Insert a block and then mirror it so that it's X value is -1
« Reply #3 on: March 26, 2023, 12:04:50 PM »
Hi Sisyphus, please give it a try.

It use the VLA-MIRROR

from the  selected mirror line

Code: [Select]
(SETQ MIRROR-ED-OBJ (VLA-MIRROR TO-MIRROR-OBJ (VLAX-3D-POINT MIRROR-START) (VLAX-3D-POINT MIRROR-END)))
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

Sisyphus

  • Mosquito
  • Posts: 8
Re: Lisp to Insert a block and then mirror it so that it's X value is -1
« Reply #4 on: April 04, 2023, 10:09:03 PM »
Hi All

Thanks to BIGAL & DEVITG for replying to me.

I've had a think about this and I'm getting closer to how I'd like the thing to work (The "lastpoint" variable makes my life a bit easier).
I just need to add this on the end of the lisp

(setq P1 (getvar 'lastpoint))
(setq PX1 (car (getvar 'lastpoint)))
(setq PY1 (cadr (getvar 'lastpoint)))
(setq PY2 (+ PY1 100))
(setq P2 (strcat PX1 " " PY2))
(command "mirror" "last" "" P1 P2 "Y")

I'm close to getting it work, I keep stumbling on the second last line (setq P2).
I'm can't get the P2 value to come up as an X, Y value that Cad will recognize for the mirror command. I've tried (setq P2 (list PX1 PY2)) & it didn't work.
I haven't tried the last mirror line yet, I don't foresee a problem with it after I can properly define the position P2.
 
« Last Edit: April 05, 2023, 01:03:01 AM by Sisyphus »

Tharwat

  • Swamp Rat
  • Posts: 707
  • Hypersensitive
Re: Lisp to Insert a block and then mirror it so that it's X value is -1
« Reply #5 on: April 05, 2023, 06:57:04 AM »
Is this what you are after ?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:SCHCB1 (/ *error* ins spc blk scl var val new)
  2.   ;;----------------------------------------------------;;
  3.   ;;    Author : Tharwat Al Choufi                      ;;
  4.   ;; website: https://autolispprograms.wordpress.com    ;;
  5.   ;;----------------------------------------------------;;
  6.  
  7.   (defun *error* (msg)
  8.     (and val (mapcar 'setvar var val))
  9.     (princ)
  10.   )
  11.  
  12.   (and (or (tblsearch "BLOCK" "Sch Breaker")
  13.            (alert "Block name < Sch Breaker > was not found !")
  14.            )
  15.        (or (tblsearch "LAYER" "SCHEM")
  16.            (alert "Layer name < SCHEM > was not found !")
  17.            )
  18.        (setq scl (* 0.001 (getvar 'LTSCALE))
  19.              var '(ATTDIA ATTREQ CLAYER)
  20.              val (mapcar 'getvar var)
  21.              )
  22.        (mapcar 'setvar var '(0 0 "SCHEM"))
  23.        (setq spc (vlax-get
  24.                    (vla-get-activelayout
  25.                      (vla-get-activedocument (vlax-get-acad-object))
  26.                      )
  27.                    'Block
  28.                    )
  29.              )
  30.        (while (setq ins (getpoint "\nSpecify insertion point : "))
  31.          (and (setq blk (vlax-invoke
  32.                           spc
  33.                           'insertblock ins "Sch Breaker" scl scl scl 0.)
  34.                     )
  35.               (setq new (vlax-invoke blk 'Mirror ins (polar ins (* pi 0.5) 1.0)))
  36.               (or (vla-delete blk) t)
  37.               (vl-some
  38.                 '(lambda (x)
  39.                    (and (= (vla-get-propertyname x) "Visibility1")
  40.                            (or (vlax-put x 'Value "Circuit Breaker Single Phase") t))
  41.                    )
  42.                 (vlax-invoke new 'getdynamicBlockproperties)
  43.                 )
  44.               )
  45.          )
  46.        (mapcar 'setvar var val)
  47.        )
  48.   (princ)
  49.  
« Last Edit: April 05, 2023, 08:49:50 AM by Tharwat »