TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ELOQUINTET on January 09, 2006, 01:24:03 PM

Title: autolisp exercise
Post by: ELOQUINTET on January 09, 2006, 01:24:03 PM
i have some free time for a couple days so i would like to convert a macro we have into lisp. the macro creates a border layer. then creates a viewport. it captures then last modelspace view then centers that view inside the viewport with the scale set to 1:2. i would like to be able to check to see if the layer is present and if it isn't create it. i just feel it's getting to complicated for a macro and would be much cleaner as a lisp. besides i need the excercise. so if anyone would like to help me out i'd really appreciate it. i don't want it written but some big hints on how to structure it would be nice.

^C^C_-LAYER;M;BORDER;C;251;;P;N;;;^C^C_MVIEW;\\;^C^C_CHANGE;L;;P;C;BYLAYER;LA;BORDER;;_.MSPACE;Z;O;L;;Z;S;1/2XP;.PSPACE;^C^C_LAYERP; ^M;
Title: Re: autolisp exercise
Post by: Crank on January 09, 2006, 01:36:11 PM
Just curious... Why so much cancels in one macro?
Title: Re: autolisp exercise
Post by: ELOQUINTET on January 09, 2006, 01:44:55 PM
yeah the guy who wrote the macro was not very experienced and was under the impression everything had to be cancelled
Title: Re: autolisp exercise
Post by: T.Willey on January 09, 2006, 01:48:18 PM
Which way you want to go?
Lisp/VLisp?
ActiveX?
Title: Re: autolisp exercise
Post by: ELOQUINTET on January 09, 2006, 01:53:46 PM
mmm...i think lisp or vlisp. i would say lisp being that it is the only thing i have limited knowledge of. i am very much a beginner so please be patient with me. i am looking through some lisp i have already trying to find an example of how to check for a given layer and if it's not present create it and set current
Title: Re: autolisp exercise
Post by: deegeecees on January 09, 2006, 01:56:36 PM
yeah the guy who wrote the macro was not very experienced and was under the impression everything had to be cancelled

Did I write that?

 :-D
Title: Re: autolisp exercise
Post by: T.Willey on January 09, 2006, 02:03:04 PM
mmm...i think lisp or vlisp.
In that case, you want to look at the table funtions.  If you want to get complicated then you can use "entmake" for the layer (I think, but I don't use entmake all that often) if it doesn't exist, or just use the layer command to add it.
This should get you started.
Title: Re: autolisp exercise
Post by: MP on January 09, 2006, 02:06:41 PM
I'd start by framing in the functional outline in comments. I just bashed this out quick to give you an idea --

Code: [Select]
(defun c:foo ( )

    ;;  save the current layer

    ;;  make the border layer if it doesn't exist
   
    ;;  set the colors of the border layer
   
    ;;  make the border layer non printable
   
    ;;  set the current layer to the border layer
   
    ;;  set tilemode to 1
   
    ;;  switch to paperspace
   
    ;;  make a viewport, record it's viewport id
   
    ;;  switch to modelspace
   
    ;;  make the viewport we just made active (use the viewport id)
   
    ;;  zoom all, then 1/2 xp
   
    ;;  change back to paperspace
   
    ;;  set the active layer back to the one we started with

)
Title: Re: autolisp exercise
Post by: Crank on January 09, 2006, 02:46:36 PM
I think the TILEMODE must be 0.

Why not follow the macro?

Code: [Select]
(defun c:foo ()
(if (tblsearch "LAYER" "BORDER")
(setvar "CLAYER" "BORDER")
(vl-cmdf ".layer" "m" "BORDER" "C" "251" "" "P" "N" "" "")
)
(setvar "TILEMODE" 0); be sure that you start in a layout
(command ".mview" (getpoint "\nSelect rectangle of viewport...")(getcorner "to..." (getvar "LASTPOINT")))
(vl-cmdf ".chprop" "L" "" "c" "bylayer" "")
(vl-cmdf ".mspace")
(vl-cmdf ".zoom" "all")
(vl-cmdf ".zoom" ".5xp")
(vl-cmdf ".pspace")
(vl-cmdf ".layerp")
(princ)
)
Title: Re: autolisp exercise
Post by: ELOQUINTET on January 09, 2006, 03:28:03 PM
foo that worked if i only had 1 object in my drawing which is never the case. i modified it to work like the macro does and seem to have gotten it. i have printed out the tblsearch and vl-cmdf from help to look at as well. here's what i got:
thanks alot for the help :)

Code: [Select]
(defun c:foo ()
(if (tblsearch "LAYER" "BORDER")
(setvar "CLAYER" "BORDER")
(vl-cmdf ".layer" "m" "BORDER" "C" "251" "" "P" "N" "" "")
)
(setvar "TILEMODE" 0); be sure that you start in a layout
(command ".mview" (getpoint "\nSelect rectangle of viewport...")(getcorner "to..." (getvar "LASTPOINT")))
(vl-cmdf ".chprop" "L" "" "c" "251" "")
(vl-cmdf ".mspace")
(vl-cmdf ".zoom" "O" "L" "")
(vl-cmdf ".zoom" "S" ".5XP")
(vl-cmdf ".pspace")
(vl-cmdf ".layerp")
(princ)
)
Title: Re: autolisp exercise
Post by: CAB on January 09, 2006, 04:20:55 PM
And what happens when the layer exist & is frozen? :evil:
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 04:27:34 PM
partypooper !


 :-P
Title: Re: autolisp exercise
Post by: deegeecees on January 09, 2006, 04:31:13 PM
Code: [Select]
(if (= (logand 1 (cdr (assoc 70 ts))) 1)
(thawthething)
)

 :-)
Title: Re: autolisp exercise
Post by: ELOQUINTET on January 09, 2006, 04:35:04 PM
o god here we go leave it to cab to throw a monkey wrench into the bicycle tire.
Title: Re: autolisp exercise
Post by: CAB on January 09, 2006, 04:47:18 PM
I was thinking more like this: 8-)
Code: [Select]
(defun c:foo (/ lyr p1 p2)
  (setq lyr "BORDER")
  (if (tblsearch "LAYER" lyr)
    (vl-cmdf "._Layer" "_Thaw" lyr "_On" lyr "_UnLock" lyr "_Set" lyr "")
    (vl-cmdf ".layer" "m" lyr "C" "251" "" "P" "N" "" "")
  )
  (setvar "TILEMODE" 0) ; be sure that you start in a layout
  (if (and
        (setq p1 (getpoint "\nSelect rectangle of viewport..."))
        (setq p2 (getcorner p1 "to..."))
      )
    (progn
      (vl-cmdf ".mview" p1 p2)
      (vl-cmdf ".chprop" "L" "" "c" "251" "")
      (vl-cmdf ".mspace")
      (vl-cmdf ".zoom" "O" "L" "")
      (vl-cmdf ".zoom" "S" ".5XP")
      (vl-cmdf ".pspace")
      (vl-cmdf ".layerp")
    )
  )
  (princ)
)
Title: Re: autolisp exercise
Post by: MP on January 09, 2006, 07:40:39 PM
This is wordy due to my non concise coding style and inline comments, but hopefully Dan will find it illuminating ...

Code: [Select]
(defun c:foo ( / layername layerobj vportobj delta acadobj docobj )

    ;;
    ;;  make / get the border layer
    ;;

    (setq layerobj
        (vla-add
            (vla-get-layers
                (setq docobj
                    (vla-get-activedocument
                        (setq acadobj
                            (vlax-get-acad-object)
                        )
                    )
                )
            )
            (setq layername "Border")
        )
    )

    ;;
    ;;  set the layer's properties as we see fit
    ;;

    (foreach propertypair

       '(
            (Plottable . :vlax-false)
            (Freeze    . :vlax-false)
            (LayerOn   . :vlax-true)
            (Lock      . :vlax-false)
            (Color     . 251)
        )

        (vlax-put-property
            layerobj
            (car propertypair)
            (cdr propertypair)
        )

    )

    ;;
    ;;  switch to paperspace
    ;;

    (vla-put-activespace docobj 0)

    ;;
    ;;  switch to pspace
    ;;

    (vla-put-mspace docobj :vlax-false)

    ;;
    ;;  get the coords for the vport
    ;;

    (initget 1)
    (setq p1 (getpoint "\nSelect the first corner of the vport: "))

    (initget 1)
    (setq p2 (getcorner p1 "Now the other corner: "))

    ;;
    ;;  make the viewport
    ;;

    (setq vportobj
        (vla-addpviewport
            (vla-get-paperspace docobj)
            (vlax-3d-point
                (mapcar
                   '(lambda (i) (/ i 2.0))
                    (mapcar '+ p1 p2)
                )
            )
            (car
                (setq delta
                    (mapcar
                       '(lambda (a b) (abs (- a b)))
                        p1
                        p2
                    )
                )
            )
            (cadr delta)
        )
    )

    ;;
    ;;  change the vport's layer
    ;;

    (vla-put-layer vportobj layername)

    ;;
    ;;  make sure the vport is on
    ;;

    (vla-display vportobj :vlax-true)

    ;;
    ;;  switch to mspace
    ;;

    (vla-put-mspace docobj :vlax-true)

    ;;
    ;;  zoom extents to center the view
    ;;

    (vla-zoomextents acadobj)

    ;;
    ;;  now zoom relative to paperspace
    ;;

    (vla-zoomscaled acadobj 0.5 acZoomScaledRelativePSpace)

    ;;
    ;;  change back to pspace
    ;;

    (vla-put-mspace docobj :vlax-false)

    ;;
    ;;  shhhh
    ;;

    (princ)

)
Title: Re: autolisp exercise
Post by: CAB on January 09, 2006, 07:58:37 PM
Your code makes me smile. 8-)
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 09:10:18 PM
Your code makes me smile. 8-)

Makes me feel that all is well in the world, and that there is hope for mankind.
 :kewl:

when contemplating something so rational, how can it be otherwise.
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 09:14:57 PM
.. except for the next to last comment :-)
Quote
    ;;
    ;;  change back to pspace
    ;;
Title: Re: autolisp exercise
Post by: MP on January 09, 2006, 09:53:43 PM
CAB -- your comments make me smile. :)

Kerry, I dunno, I (failthfully) took my stupid pills today so yourlast  comment just confuses me.  :|

(But your first one is nice).

:)
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 10:11:40 PM
I took a double dose of my obtuse pill Michael .. you forgot the layer;P;  reading my comment now, it seems that I'm querying the pspace ..


thats my story, and I'm sticking with it   :lol:


added: You left that for Dan, right ?
Title: Re: autolisp exercise
Post by: MP on January 09, 2006, 10:18:40 PM
I took a double dose of my obtuse pill Michael .. you forgot the layer;P;  reading my comment now, it seems that I'm querying the pspace ..


thats my story, and I'm sticking with it   :lol:


added: You left that for Dan, right ?

There's no need -- I don't change the active layer.

:P
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 10:26:26 PM
 :oops:
Title: Re: autolisp exercise
Post by: MP on January 09, 2006, 10:32:13 PM
Man I've missed this place.

 :-D
Title: Re: autolisp exercise
Post by: LE on January 09, 2006, 10:34:00 PM
:oops:

What type of job.... the scoreboard does ?  it is only for AutoCAD 2006 ? :lol:
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 10:36:56 PM
What type of job.... the scoreboard does ?  it is only for AutoCAD 2006 ? :lol:

I assume you mean the piccy Luis ? That's ObjectDCL ..
Title: Re: autolisp exercise
Post by: LE on January 09, 2006, 11:07:48 PM
Oops.... I do not know what I was saying.....  :ugly:
Title: Re: autolisp exercise
Post by: Kerry on January 09, 2006, 11:13:17 PM
heh, that's my speech :-)
Title: Re: autolisp exercise
Post by: ELOQUINTET on January 10, 2006, 12:20:20 PM
wow you guys have been chattin away in here. i'm so happy someone finally responded to one of my posts. i'll take a hard look at cab's and mps code thanks for your help guys.