Author Topic: autolisp exercise  (Read 8613 times)

0 Members and 1 Guest are viewing this topic.

ELOQUINTET

  • Guest
autolisp exercise
« 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;

Crank

  • Water Moccasin
  • Posts: 1503
Re: autolisp exercise
« Reply #1 on: January 09, 2006, 01:36:11 PM »
Just curious... Why so much cancels in one macro?
Vault Professional 2023     +     AEC Collection

ELOQUINTET

  • Guest
Re: autolisp exercise
« Reply #2 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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: autolisp exercise
« Reply #3 on: January 09, 2006, 01:48:18 PM »
Which way you want to go?
Lisp/VLisp?
ActiveX?
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

ELOQUINTET

  • Guest
Re: autolisp exercise
« Reply #4 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

deegeecees

  • Guest
Re: autolisp exercise
« Reply #5 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

T.Willey

  • Needs a day job
  • Posts: 5251
Re: autolisp exercise
« Reply #6 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.
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: autolisp exercise
« Reply #7 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

)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Crank

  • Water Moccasin
  • Posts: 1503
Re: autolisp exercise
« Reply #8 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)
)
Vault Professional 2023     +     AEC Collection

ELOQUINTET

  • Guest
Re: autolisp exercise
« Reply #9 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)
)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: autolisp exercise
« Reply #10 on: January 09, 2006, 04:20:55 PM »
And what happens when the layer exist & is frozen? :evil:
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #11 on: January 09, 2006, 04:27:34 PM »
partypooper !


 :-P
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

deegeecees

  • Guest
Re: autolisp exercise
« Reply #12 on: January 09, 2006, 04:31:13 PM »
Code: [Select]
(if (= (logand 1 (cdr (assoc 70 ts))) 1)
(thawthething)
)

 :-)

ELOQUINTET

  • Guest
Re: autolisp exercise
« Reply #13 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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: autolisp exercise
« Reply #14 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)
)
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: autolisp exercise
« Reply #15 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)

)
« Last Edit: January 09, 2006, 11:24:20 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: autolisp exercise
« Reply #16 on: January 09, 2006, 07:58:37 PM »
Your code makes me smile. 8-)
I've reached the age where the happy hour is a nap. (°Ώ°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #17 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #18 on: January 09, 2006, 09:14:57 PM »
.. except for the next to last comment :-)
Quote
    ;;
    ;;  change back to pspace
    ;;
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: autolisp exercise
« Reply #19 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).

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #20 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 ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: autolisp exercise
« Reply #21 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
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #22 on: January 09, 2006, 10:26:26 PM »
 :oops:
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: autolisp exercise
« Reply #23 on: January 09, 2006, 10:32:13 PM »
Man I've missed this place.

 :-D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.com • http://cadanalyst.slack.com • http://linkedin.com/in/cadanalyst

LE

  • Guest
Re: autolisp exercise
« Reply #24 on: January 09, 2006, 10:34:00 PM »
:oops:

What type of job.... the scoreboard does ?  it is only for AutoCAD 2006 ? :lol:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #25 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 ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

LE

  • Guest
Re: autolisp exercise
« Reply #26 on: January 09, 2006, 11:07:48 PM »
Oops.... I do not know what I was saying.....  :ugly:

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: autolisp exercise
« Reply #27 on: January 09, 2006, 11:13:17 PM »
heh, that's my speech :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ELOQUINTET

  • Guest
Re: autolisp exercise
« Reply #28 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.