create layer names from text file and add prefix using LISP

Discussion in 'AutoCAD' started by Tom Hanley, Jan 5, 2005.

  1. Tom Hanley

    Tom Hanley Guest

    I am trying to automate creating layers using a text file containing the
    list of layer names and adding a prefix to the list of layers. Example:

    the text file would have:

    Sewer
    Sewer-text
    Water
    Water-text
    Utils
    Utils-text

    the layers to be created would be

    Lot01-Sewer
    Lot01-Sewer-text
    etc...

    I want to give the user the option to input first lot number and how many
    lots for layers to create. It might be nice to have a dialog box that lists
    all the layers in the text file and the user can select the layers they want
    to create for all the lots range. It would be even better if the user could
    choose a dws file and select the layers from that.

    Thanks,
    Tom Hanley
     
    Tom Hanley, Jan 5, 2005
    #1
  2. Tom Hanley

    Paul Turvill Guest

    Show us what you've written so far ...
    ___
     
    Paul Turvill, Jan 5, 2005
    #2
  3. Tom Hanley

    Tom Hanley Guest

    I didn't get very far. I am not sure how to get a list of layer names from
    the outside file. This routine just creates a single layer for each lot in
    the range so far....



    (Defun C:LLC ()
    ;T.HANLEY FOR L&T 1/5/05
    ;CREATES LAYERS FOR INDIVIDUAL LOTS

    ;(Setvar "Cmdecho" 0)
    (Setq A (Getint "\nStarting LOT number: "))
    (Setq B (Getint "\nEnding LOT number: "))

    (If (> A B)
    (Setq E -1)
    (Setq E 1)
    )
    (Repeat (+ 1 (Abs (- A B)))
    (Setq F (STRCAT "LOT" (Itoa A) "-" "SEWER"))
    (Command "_LAYER" "M" F "")
    (Setq A (+ A E))

    )

    ;(Setvar "Cmdecho" 1)
    )
     
    Tom Hanley, Jan 5, 2005
    #3
  4. Tom Hanley

    Paul Turvill Guest

    To get data from a file, you'll need the (open ... "r") and (read-line ...)
    functions.
    ___
     
    Paul Turvill, Jan 5, 2005
    #4
  5. Tom Hanley

    Paul Turvill Guest

    Oh, and after you've retrieved the data, the (close ...) function as well.
     
    Paul Turvill, Jan 5, 2005
    #5
  6. Tom Hanley

    Tom Hanley Guest

    Thx Paul. I did find that in the LISP reference in the help but I was
    looking for a working example to alter. You can see that my LISP skills are
    limited looking at my code.
     
    Tom Hanley, Jan 5, 2005
    #6
  7. Tom Hanley

    Paul Turvill Guest

    Example:

    (setq filename (open "data.txt" "r"))
    (while (setq layname (read-line filename))
    (<<process the layname you just retrieved>>)
    (<<you can include any number of functions here>>)
    );; while
    (close filename)

    This snippet opens the file DATA.TXT as filename, then reads in and
    processes one line at a time. The (while ...) loop terminates after the last
    line is processed (end of file reached), and then the file is closed.
    ___
     
    Paul Turvill, Jan 5, 2005
    #7
  8. Tom Hanley

    Tom Hanley Guest

    Thx... This should keep me out of trouble for awhile! :)
     
    Tom Hanley, Jan 5, 2005
    #8
  9. Tom Hanley

    hawstom Guest

    Better yet, why not get color and linetype information from the file too?

    First, make the file like this:
    ("Layer1" "yellow" "dashed")
    ("Layer2" "red" "")

    Then read the file and make the LIST like this:
    (setq
    f1
    (open
    (if (= (getvar "filedia") 1)
    (GETFILED "Layer import file"
    (strcat
    (getvar "dwgprefix")
    (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname") 4)
    )
    "txt"
    0
    )
    (findfile "layersettings.txt")
    )
    "r"
    )
    )
    (while (setq rdlin (read-line f1))
    (if (setq layerinput (read rdlin)) (setq layerlist (cons layerinput layerlist)))

    Then you lust have to creat the layers:

    (foreach layer layerlist
    (command "._layer" "m" (car layer) "c" (cadr layer) "" "lt" (caddr later) "" "")
    )

    Does that help?

    Tom Haws
    Find Thomas Gail Haws at Google
     
    hawstom, Jan 5, 2005
    #9
  10. Tom Hanley

    Tom Hanley Guest

    Yes that was my next step and that is why I thought about using the dws
    file.
     
    Tom Hanley, Jan 5, 2005
    #10
  11. Tom Hanley

    Tom Hanley Guest

    Okay... This does find the file and reads the first line and builds the
    layer for the whole range but stops after the first line in my text file.
    attached is the text file.

    (Defun C:LLC ()
    ;T.HANLEY FOR L&T 1/5/05
    ;CREATES LAYERS FOR INDIVIDUAL LOTS

    ;(Setvar "Cmdecho" 0)
    (Setq A (Getint "\nStarting LOT number: "))
    (Setq B (Getint "\nEnding LOT number: "))
    (setq fil "S:/DRAFTING STANDARDS/LOTLAYERS.TXT")
    (If (> A B)
    (Setq E -1)
    (Setq E 1)
    )

    (Repeat (+ 1 (Abs (- A B)))
    (SETQ LNFILE (OPEN fil) "R")
    (while
    (SETQ LN (READ-LINE LNFILE))
    (Setq F (STRCAT "LOT" (Itoa A) "-" LN))
    (Command "_LAYER" "M" F "")
    )
    (CLOSE fil)
    (Setq A (+ A E))
    )


    ;(Setvar "Cmdecho" 1)
    )
     
    Tom Hanley, Jan 5, 2005
    #11
  12. Tom Hanley

    Paul Turvill Guest

    The syntax of your (open ...) function is incorrect:
    (SETQ LNFILE (OPEN fil) "R")
    should be
    (setq LNFILE (open fil "r"))
    ___
     
    Paul Turvill, Jan 5, 2005
    #12
  13. Tom Hanley

    Tom Smith Guest

    He has a good suggestion, but it doesn't address your need to create the
    layers lot by lot. Also, per your previous post, I would not open and read
    the text file again for each lot. I'd get all the "base" layer info in one
    list as per Tom's suggestion, then process that list for each lot number.
    Something roughly like this (untested):

    (foreach n layerlist ;get the list of layer lists per Tom Haws
    (setq currentlotnum startlotnum) ;cycle from starting to ending lot
    numbers
    (while (<= currentlotnum endlotnum)
    (command "._layer" "m" (strcat "LOT" (itoa currentlotnum) "-" (car
    layer)) "c" (cadr layer) "" "lt" (caddr later) "" "")
    (setq currentlotnum (1+ currentlotnum))
    )
    )
     
    Tom Smith, Jan 5, 2005
    #13
  14. Tom Hanley

    Tom Hanley Guest

    Thanks for the help. I now have the list of one lot number with all the
    layers being built but I get an error bad argument type: streamp
    "S:/DRAFTING STANDARDS/LOTLAYERS.TXT" and it stops running. I have been
    trying to understand what Tom Haws and Tom Smith have been offering but I
    can't seem to figure it out. I guess I need to find a working example and
    reverse engineer it to understand what is happening. Thanks for all who
    tried to help.
     
    Tom Hanley, Jan 6, 2005
    #14
  15. Tom Hanley

    Tom Smith Guest

    Don't despair, Tom! If I have time this weekend I'll try to cobble together a working example, if someone doesn't beat me to it.

    It can be very frustrating trying to get a handle on lisp, but hang in there & it will eventually get easier.
     
    Tom Smith, Jan 6, 2005
    #15
  16. Tom Hanley

    Tom Hanley Guest

    Thanks for the inspiration. I used to write a few programs years back but
    usually I start with something close and the tweak it in for what I need.
     
    Tom Hanley, Jan 6, 2005
    #16
  17. Tom Hanley

    ECCAD Guest

    Change:
    (Repeat (+ 1 (Abs (- A B)))
    (SETQ LNFILE (OPEN fil) "R")
    (while
    (SETQ LN (READ-LINE LNFILE))
    (Setq F (STRCAT "LOT" (Itoa A) "-" LN))
    (Command "_LAYER" "M" F "")
    )
    (CLOSE fil)
    --------------
    To:
    (SETQ LNFILE (OPEN fil "R"))

    (Repeat (+ 1 (Abs (- A B)))
    (SETQ LN (READ-LINE LNFILE))
    (Setq F (STRCAT "LOT" (Itoa A) "-" LN))
    (Command "_LAYER" "M" F "")
    )
    (CLOSE fil)
    -------------
    Problem was, (while......loop reads entire file..)
    Use Repeat x to read x lines..
    Will read in sequence.

    Bob
     
    ECCAD, Jan 6, 2005
    #17
  18. Tom Hanley

    Tom Hanley Guest

    Bob,
    I tried that and it creates the first set of layers (i.e. Lot1 layers) but
    then errors out with
    bad argument type: streamp "S:/DRAFTING STANDARDS/LOTLAYERS.TXT"
    It still does not create the other lots in the range. I've attached the text
    file so you can see it work.
    Thanks,
    Tom
     
    Tom Hanley, Jan 7, 2005
    #18
  19. Okay, probably a simple question, but how do I get from a variable that is

    "layer1,7,CONTINUOS"

    to

    ("layer1" "7" "CONTINUOUS")

    - I have a comma delimited text file that has Layername,Colornumber,linetype
    all on one line.
     
    Casey Roberts, Jan 7, 2005
    #19
  20. Tom Hanley

    T.Willey Guest

    Tom,

    Try this. Should work as long as the text file is in the search path.

    Tim

    (defun c:CreateLayers (/ FileName FileOpnd LayName cnt1 cnt2 temp1)

    (setq FileName (findfile "LOTLAYERS.txt"))
    (setq cnt1 (getint "\n Starting LOT number: "))
    (setq cnt2 (getint "\n Ending LOT number: "))
    (if (and cnt1 cnt2)
    (progn
    (repeat (1+ (abs (- cnt1 cnt2)))
    (setq FileOpnd (open FileName "r"))
    (while (setq temp1 (read-line FileOpnd))
    (setq LayName (strcat "Lot" (itoa cnt1) "-"temp1))
    (command "_.layer" "_m" LayName "")
    )
    (if (> cnt1 cnt2)
    (setq cnt1 (1- cnt1))
    (setq cnt1 (1+ cnt1))
    )
    (close FileOpnd)
    )
    )
    )
    )
     
    T.Willey, Jan 7, 2005
    #20
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.