A Beautiful Mind

Discussion in 'AutoCAD' started by John, May 14, 2004.

  1. John

    John Guest

    Thanks to all "top ten and below ten" beautiful minds. And a special
    thanks to Afralisp.com for providing a free online programming training.
    Yes, You are right about "When I started writing AutoLISP code, I was
    totally confused!! The more I read, the more in a muddle I got.

    I'm posting my problems at this NG since few weeks . Sometime I get
    great advice "also take a look at "nentsel" " and sometime I get "The
    best use of your time would be to hire someone like Rob with experience
    to do these programming tasks for you. If you would like to hire my
    services, then pop me an email". "You need to dig a level deeper to
    extract the room number. Hint: is (assoc 1), but not in 'EL'"

    I noticed this issue not only with me but other who trying to learn it.

    I have years CAD experience and trying to learn Autolisp myself.

    So next time, I don't need services and hints. I just need a function
    name so that I can read and finish what I'm trying to write or an advice
    that I can do better with other way.

    The lisp routine I wrote and I'm trying to write is only for my learning
    purpose.

    Here is my newly routine. It is working fine but I wanted show to all
    beautiful minds if I can write same routine other way.

    ;;checkdwg
    (defun C:dtest (/ a1 lan check)
    (setq lan "A-Anno-Dims")
    (setq check (tblsearch "layer" lan ))
    (if(= check nil) (command "layer" "n" lan "c" "173" lan "")
    )
    (setq A1 (ssget "X" '((0 . "dimension"))))
    (command "change" a1 "" "p" "la" "A-Anno-Dims" "")
    )

    Again, thank you for your help.

    John
     
    John, May 14, 2004
    #1
  2. John

    Mark Propst Guest

    John,

    Your routine works. That's the main objective.
    A secondary objective may be to make it work in all cases, not just some.
    A third objective may be to see how many different ways you can do it?
    and so on...

    One possible variation could be something like:
    (defun C:dtest (/ a1 lan )

    (If (setq a1(ssget "X" '((0 . "dimension"))));if there are any dimensions in
    dwg at this time
    (progn
    (setq lan "A-Anno-Dims")
    (if(not (tblsearch "layer" lan ))
    (command "layer" "n" lan "c" "173" lan "")
    )
    (command "change" a1 "" "p" "la" lan "")
    );progn
    (princ"\nNo dimensions found")
    );if
    )
    or
    (defun C:dtest (/ a1 lan )
    (if(not
    (and (setq a1(ssget "X" '((0 . "dimension"))));if there are any dimensions
    in dwg at this time
    (setq lan "A-Anno-Dims")
    (if(not (tblsearch "layer" lan ))
    (command "layer" "n" lan "c" "173" lan "")
    )
    (command "change" a1 "" "p" "la" lan "")
    (princ(strcat"\nChanged "(itoa(sslength a1))" dims to layer " lan))
    );and
    )
    (princ"\nNo dimensions found")
    );if
    )

    and then there's the whole activex way to do, which if you're starting out,
    there's no reason not to become aware of.

    in general the (command "whatever" way is the easiest to pick up at first
    and get some things working quickly in lisp.
    after awhile you find there are more reliable or less troublesome ways to
    make what you want to happen happen.
    entmake and entmod were the bigboys at one time, requiring 20 times more
    code but producing more certain results with less problems
    now it seems they're like old shoes and activex is the wave of the future
    just my 0.0000001 c
    :)
    happy lisping
     
    Mark Propst, May 14, 2004
    #2
  3. John

    David Kozina Guest

    John

    If there is any thing I've learned here is that there is just about no limit
    to the number of ways you can write a routine. Some ways, however, are
    better - simpler, more elegant, more efficient, faster, (but, strangely,
    almost never all of the above). The more you learn, the more you will find
    better ways (for you) to accomplish tasks. Then, most likely, you'll find
    yourself disgusted (almost totally) with your previous work. :-S

    Nevertheless, I think this is good, because it signifies that you are making
    forward progress, and are discovering better ways to do things.

    As for your routine...
    For now, you feel comfortable using (command... - that's where I think about
    everyone starts - basically one step up from an .scr script routine. Later,
    when you feel comfortable, you should try experimenting with the Object
    Model itself. The trails to this understanding have been blazed for some
    time now by the 'beautiful minds' here, and most, if not all, will be happy
    to help you in your progress. If you are using AutoCAD 2000 or higher, you
    can probably skip messing around with the (entmake..., (entchg..., and
    (entmod... functions, since accessing the Object Model will give you a
    better foothold and overall grasp, and will prove more convenient to use,
    once you get past the initial learning curve.

    As you continue to read the posts in this group, you will likely find that
    the *incomprehensible* language/functions mentioned by others slowly become
    more understandable.

    As for your routine: one thing you could do to streamline it is include the
    check itself as an argument to the if statement, since you're only using it
    one time...

    (if (not (tblsearch "layer" lan))
    (command...
    )

    Then you can delete its use as a local variable altogether.

    However, if it turns out that 'check' is needed for additional tests later
    on in a more complex routine, setq it as you have, then use it like this:

    (if (not check)
    (command...
    )

    And perhaps change its label to LayerExists instead of check, (if used for
    this purpose), so ast to make the code slightly more readable:

    (if (not LayerExists)
    (command...
    )

    I'm sure others may have more to say (perhaps even regarding my own
    comments)
    So - listen up... :)

    Best regards,
    David Kozina
     
    David Kozina, May 14, 2004
    #3
  4. John

    Rudy Tovar Guest

    Like the Bible say's "when you were young, you consumed milk..." and as you
    grow older you will consume more solids.

    As with anyone, we all started by trial and error, but never wrong....why?
    Creative minds... We are all born with it...

    Definitely, as you mature your eye will be opened, and you will be
    enlightened. As I see, and many others, you will see. You see code, we see
    life, objects, and thoughts, a completely different world.

    Think of this as a step into a different realm, you have no idea what you
    will behold before it's even written down.

    The day you see, before it becomes, you will be what we have been.

    Michael Puckett once said..."Imagination makes all things possible". You may
    ponder on this, but in time you will understand...

    --

    AUTODESK
    Authorized Developer
    http://www.Cadentity.com
    MASi
     
    Rudy Tovar, May 14, 2004
    #4
  5. John

    David Bethel Guest

    You might want to start using a more global approach. Lisp is a list
    interpretor. Make a list of the parameter, and then feed them globally
    to standardized calls


    (defun c:dtest1 (/ lay_list ss)

    (setq lay_list '(("A-Anno-Dims" "DIMENSION" 173)
    "A-Anno-Notes" "MTEXT" 144))

    ;;;LAYER TABLES
    (command "_.LAYER")
    (foreach l lay_list
    (if (not (tblsearch "LAYER" (nth 0 l)))
    (command "_New" (nth 0 l) "_Color" (nth 2 l) (nth 0 l))))
    (command"")

    ;;;ENTITY LAYERS
    (foreach l lay_list
    (if (setq ss (ssget "X" (list (cons 0 (nth 1 l)))))
    (command "_.CHPROP" ss "" "_LAyer" (nth 0 l) "")))

    (prin1))


    Now just add stuff to lay_list instead of rewriting the routines.

    -David
     
    David Bethel, May 14, 2004
    #5
  6. John

    Rudy Tovar Guest

    All you can achieve now are enhancements...

    If-layer-on?thawed?-set?-entmake-vla-add-then-edit-attribute...

    Or

    (defun c:dtest (/ ss)
    (if (not (tblsearch "layer" "A-Anno-Dims"))
    (command "layer" "n" "A-Anno-Dims" "c" "173" "" ""))
    (setq ss (ssget "x" '((0 . "DIMENSION"))))
    (if ss(command "change" a1 "" "p" "la" "A-Anno-Dims" ""))
    (princ)
    )
     
    Rudy Tovar, May 14, 2004
    #6
  7. John

    Rudy Tovar Guest

    One last note. Start building a function library. Makes building
    applications faster and easier.
    --

    AUTODESK
    Authorized Developer
    http://www.Cadentity.com
    MASi
     
    Rudy Tovar, May 14, 2004
    #7
  8. John

    John Guest

    Rudy,
    Thanks. I think I'm making progress because I found (AL at command "ss")
    error in your routine. Just kidding. I know you forget to change it.


    (defun c:dtest (/ ss)
    (if (not (tblsearch "layer" "A-Anno-Dims"))
    (command "layer" "n" "A-Anno-Dims" "c" "173" "" ""))
    (setq ss (ssget "x" '((0 . "DIMENSION"))))
    (if ss(command "change" a1 "" "p" "la" "A-Anno-Dims" ""))
    (princ)
    )

    John
     
    John, May 14, 2004
    #8
  9. John

    Rudy Tovar Guest

    You're right, I just copyclipped that part over. I should have double
    checked my work. But I didn't test it out...so thats a no no for me...
     
    Rudy Tovar, May 14, 2004
    #9
  10. One thing you could do to help your further career as a Lisp programmer
    would be to adopt a sensible indentation scheme: experienced Lisp
    programmers read code based on indentation, not on parentheses.
    - Getting an editor that understands Lisp syntax, autoindents and counts
    parens for you is a great help in this. In Common Lisp culture almost
    everyone uses some variant of Emacs for this.

    Here's what GNU Emacs would do to your function (+ some minor style
    changes I'd make):

    ;;checkdwg
    (defun C:dtest (/ a1 layername)
    (setq layername "A-Anno-Dims")
    (if (null (tblsearch "layer" layername))
    (command "layer" "n" layername "c" "173" layername ""))
    (setq a1 (ssget "X" '((0 . "dimension"))))
    (command "change" a1 "" "p" "la" layername ""))

    --
     
    Martti Halminen, May 17, 2004
    #10
  11. I don't see what having the "layername" variable in here is doing for you.
    Would this work?
    Kent Cooper, AIA

    .....
    ...
    ....
     
    Kent Cooper, AIA, May 17, 2004
    #11
  12. My change regarding the variable was just to change the name to more
    readable; something a little shorter would save typing, though. Having
    it as a variable reduces errors in typing if the actual layer name is
    complicated, and it would be easy to convert this to have the layer name
    as a parameter.

    --
     
    Martti Halminen, May 17, 2004
    #12
  13. John

    John Guest

    Thank you for your help. I think something is not right in these code.

    (defun C:dtest (/ a1 lan )
    (if(not
    (and (setq a1(ssget "X" '((0 . "dimension"))));if there are any dimensions
    in dwg at this time
    (setq lan "A-Anno-Dims")
    (if(not (tblsearch "layer" lan ))
    (command "layer" "n" lan "c" "173" lan "")
    )
    (command "change" a1 "" "p" "la" lan "")
    (princ(strcat"\nChanged "(itoa(sslength a1))" dims to layer " lan))
    );and
    )
    (princ"\nNo dimensions found")
    );if
    )

    i tried to find out but no luck..

    John
     
    John, May 19, 2004
    #13
  14. John

    John Guest

    Thanks David.


     
    John, May 19, 2004
    #14
  15. John

    John Guest

    Thanks everyone

    John
     
    John, May 19, 2004
    #15
  16. John

    Adesu Guest

    Hi John,your and others (maybe),I thought same with me.
    first time I was learned AutoLisp,as like "polar" "setq" "cond" etc.I don't
    understand ,how it worked,now after join with "NG",AutoLisp it said "well
    come'" to me and rather understand about it,but more not yet familiar as
    like "nentsel" "logand" "bitwise" "boole" etc.
    The usage join with NG,I've got more suggest and advice from others
    "lisper",and I more learn how program work,I thought to say "Thanks a lot"
    to as "Gurus" in this forum.

    Anne Brown <>
    Alan Henderson <>
    Alaspher <>
    Allen Johnson <ajohnson@dwase>
    A Diaz <>
    autodes <>
    bob.at <>
    Barr <>
    BTO <>
    btlsp <>
    BillZ <>
    Bill DeShawn <>
    Casey Roberts <>
    CAB2k <>
    Chip Harper <>
    Daniel J. Altamura, R.A. <>
    devitg <>
    dean_bourke <>
    Don Butler <>
    Doug Broad <>
    Douglas Barr <dougatsweaterscapesdotcalm>
    David Kozina <>
    dblaha <>
    ECCAD <>
    favline <>
    Fatty <>
    gert <>
    hawstom <>
    Ivan. <>
    jbryant4 <>
    Jesús Urrutia <>
    Josh C <>
    Jon <>
    John Uhden <>
    Jon Fleming <>
    Joe Burke <>
    Jürg Menzi <>
    James Allen <JamesA~AA~mwengrs~DD~com>
    Jason Piercey <Jason AT atreng DOT com>
    Jimmy D <>
    Jim Dee <>
    Jim Claypool <jclaypool(removethis)@kc.rr.com>
    Jeff Mishler <>
    Jan van de Poel <>
    J. Logan <>
    Kent Cooper, AIA <>
    Ken Alexander <kalexATfremontmillworkDOTcom>
    KingCAD <>
    Laurie Comerford <>
    LUCAS <>
    liftedaxis <>
    Martti Halminen <martti.halminen@invalid>
    Mark Propst <>
    Marc'Antonio Alessi <nospam maalessi at tin dot it>
    michael puckett <>
    Mike Weaver <mweaver AT bettisworth DOT com>
    MP <>
    moshe <>
    nalsur8 <>
    naujoke <>
    Nick_Merchant <>
    OLD-CADaver <>
    PG. <>
    Paul Turvill <>
    petersciganek <>
    Phil Kenewell <>
    Rakesh Rao <>
    R.K. McSwain <>
    Rick Keller <>
    Rudy Tovar <>
    R. Togores <>
    R. Robert Bell <>
    Rodney Estep <>
    Rogerio_Brazil <>
    SpeedCAD <>
    StefVdM <>
    Thomas Smith <>
    TCEBob <>
    Tony Tanzillo <tony.tanzillo at caddzone dot com>
    Tom Smith <nospam>
    T.Willey <>
    Walt Engle <>
    zeha <>
     
    Adesu, Jan 6, 2005
    #16
  17. John

    Alaspher Guest

    for example, VLA-code:
    Code:
    (defun C:dtest1 (/ actdoc actsel count lan layers layobj laytmp lay-freeze lay-lock)
    (setq lan    "A-Anno-Dims"
    actdoc (vla-get-ActiveDocument (vlax-get-acad-object))
    actsel (vla-get-ActiveSelectionSet actdoc)
    layers (vla-get-Layers actdoc)
    )
    (vla-Clear actsel)
    (vla-select
    actsel
    acselectionsetall
    nil
    nil
    (vlax-safearray-fill (vlax-make-safearray vlax-vbinteger '(0 . 3)) '(0 -4 8 -4))
    (vlax-safearray-fill
    (vlax-make-safearray vlax-vbvariant '(0 . 3))
    (list "DIMENSION,TOLERANCE" "<NOT" lan "NOT>")
    )
    )
    (if (not (zerop (setq count (vla-get-Count actsel))))
    (progn
    (if (vl-catch-all-error-p
    (setq layobj (vl-catch-all-apply (function vla-item) (list layers lan)))
    )
    (setq layobj (vla-add layers lan))
    )
    (vla-put-Color layobj 173)
    (vlax-for i actsel
    (setq laytmp (vla-item layers (vla-get-Layer i)))
    (if (= (vla-get-Freeze laytmp) :vlax-true)
    (progn (vla-put-Freeze laytmp :vlax-false)
    (setq lay-freeze (cons laytmp lay-freeze))
    )
    )
    (if (= (vla-get-Lock laytmp) :vlax-true)
    (progn (vla-put-Lock laytmp :vlax-false)
    (setq lay-lock (cons laytmp lay-lock))
    )
    )
    (vla-put-Layer i lan)
    )
    (foreach i lay-freeze (vla-put-Freeze i :vlax-true))
    (foreach i lay-lock (vla-put-Lock i :vlax-true))
    (princ (strcat "\nChanged " (itoa count) " dimensions or tolerances to layer '" lan "'"))
    )
    (princ "\nNo dimensions found.")
    )
    (princ)
    )
    best regards!
     
    Alaspher, Jan 6, 2005
    #17
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.