+ Reply to Thread
Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 26
  1. #1
    Super Member
    Using
    AutoCAD 2010
    Join Date
    Dec 2010
    Posts
    1,246

    Default File handling : reading text file

    Registered forum members do not see this ad.

    Hi,
    Its been a while since I lisp, trying to go back to track
    The lisp should acquire the drawing database for specific layers and ones they are found it should set a specific color to them.

    The lisp should read an external txt file containing the layer name and color to set, in this format (layerName,11).

    Ive saved the text file, lisp,dwg at the same folder, and use this code

    Code:
    (setq file (open "map.txt" "r"))
    (read-line file)
    (close file)
    ; error: bad argument type: FILE nil

    it seems like lisp cannot find the file , even if i use absolute file path it wont be found

    Help please
    shay

  2. #2
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,106

    Default

    Code:
     (if (and (setq fn (findfile "map.txt")) 
              (setq fp (open fn "r"))
         )
       (progn (setq data (read-line fp))(close fp))
       (princ "\nUnable to find or open map.txt")
    )

  3. #3
    Super Member
    Using
    AutoCAD 2010
    Join Date
    Dec 2010
    Posts
    1,246

    Default

    Quote Originally Posted by rlx View Post
    Code:
     (if (and (setq fn (findfile "map.txt")) 
              (setq fp (open fn "r"))
         )
       (progn (setq data (read-line fp))(close fp))
       (princ "\nUnable to find or open map.txt")
    )
    it return nil

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,567

    Default

    What does this return for you?

    Code:
    (defun c:test ( / des str txt )
        (cond
            (   (not (setq txt (findfile "map.txt")))
                (princ "\nmap.txt not found.")
            )
            (   (not (setq des (open txt "r")))
                (princ "\nmap.txt could not be opened for reading.")
            )
            (   (princ "\nmap.txt found and opened for reading.")
                (setq str (read-line des))
                (princ "\nRead 1 line from map.txt: ")
                (princ str)
                (close des)
                (princ "\nmap.txt closed.")
            )
        )
        (textpage) (princ)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  5. #5
    Super Member
    Using
    AutoCAD 2010
    Join Date
    Dec 2010
    Posts
    1,246

    Default

    Hi

    Im writing a program that manipulate layers by a given text file
    The text file might look something like :
    Code:
    window,11
    wall,144
    my program will do the following:
    1. Open the file
    2. Read until it get to “,” (as <layer name>)
    3. Read after the “,”(as <color>)
    4. Look layers for <layer name>
    5. Set <color> as the layer color
    6. Loop til the end of file

    how do i perform number 2 and 3?

  6. #6
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Win 7 Pro / Win 10 Pro / Linux Mint
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2018
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,567

    Default

    Quote Originally Posted by samifox View Post
    2. Read until it get to “,” (as <layer name>)
    3. Read after the “,”(as <color>)

    how do i perform number 2 and 3?
    Typically, you might use a function such as vl-string-position or vl-string-search to determine the position of the comma within the string, and then use substr with the appropriate index arguments to obtain the string either side of the comma; or alternatively, use list functions such as nth/car/cadr on the list returned by a function such as my String to List function.

    However, since you are using comma delimiters, you could also use DIESEL to obtain the data either side of the comma:
    Code:
    _$ (setq str "MyLayer,MyColour")
    "MyLayer,MyColour"
    _$ (menucmd (strcat "m=$(index,0,\"" str "\")"))
    "MyLayer"
    _$ (menucmd (strcat "m=$(index,1,\"" str "\")"))
    "MyColour"
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  7. #7
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,106

    Default

    Quote Originally Posted by samifox View Post
    it return nil

    Does your file actually exist and has it the data you describe? If so
    Code:
     (princ data)
    should return the first line in the file.

  8. #8
    Super Member
    Using
    AutoCAD 2010
    Join Date
    Dec 2010
    Posts
    1,246

    Default

    this is the code ive come to so far,
    its trow an error

    please help

    Code:
    (defun tst (/ txt des str layer color l)
    
      ;; reading...
      (setq txt (findfile "map.txt"))
      (setq des (open txt "r"))
      (setq str (read-line des))
      (close des)
    
      ;; divide string by comma
      (setq layer (menucmd (strcat "m=$(index,0,\"" str "\")")))
      (setq color (menucmd (strcat "m=$(index,1,\"" str "\")")))
    
    ;;set layer color
      (setq l (tblobjname "LAYER" layer))
      (setq l (vlax-ename->vla-object l))
      (vlax-put-property l 'Color color)
    
      (vl-load-com)
    )

  9. #9
    Luminous Being Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draughtsman
    Discipline
    Mechanical
    Details
    HVAC, Drainage, Water Supply, Fire Fighting and a little about Electricity.
    Using
    AutoCAD 2017
    Join Date
    Oct 2009
    Location
    Great Syria , Living in Abu Dhabi
    Posts
    6,261

    Default

    Hi,

    You can use my function below to separate a string with delimiter.
    Code:
    (defun _peelstring (string del / str pos lst)
      ;; Tharwat - 03.Mar.2015	;;
      (while (setq pos (vl-string-search del string 0))
        (setq str    (substr string 1 pos)
              string (substr string (+ pos (1+ (strlen del))))
        )
        (and str (/= str "") (setq lst (cons str lst)))
      )
      (and string (/= string "") (setq lst (cons string lst)))
      (reverse lst)
    )
    usage f the above function:

    Code:
    _$ (_PEELSTRING "Layer1,Color,Ltype,LineWeight" ",")
    ("Layer1" "Color" "Ltype" "LineWeight")

  10. #10
    Super Member rlx's Avatar
    Computer Details
    rlx's Computer Details
    Operating System:
    W10
    Computer:
    i74ghz/ssd500/2tbhdd
    Discipline
    Electrical
    rlx's Discipline Details
    Occupation
    electrical designer dragon
    Discipline
    Electrical
    Details
    I dont excel in anything but I rearly give up
    Using
    AutoCAD 2016
    Join Date
    Nov 2014
    Location
    Bergen op Zoom , Netherlands
    Posts
    1,106

    Default

    Registered forum members do not see this ad.

    just quickly written , change path to match and make sure names and colors in your data file are correct

    Code:
    (defun c:samifox ( / actDoc fn-data-file fp-data-file data-list )
      (vl-load-com)
      (setq actDoc (vla-get-activedocument (vlax-get-acad-object))
            fn-data-file (findfile "d:\\Temp\\Lisp\\Cad-tutor\\samifox\\map.txt"))
      (cond
        ((null fn-data-file)
         (princ "\nData file map.txt not found"))
        ((not (setq fp-data-file (open fn-data-file "r")))
         (princ "\nUnable to read from map.txt"))
        (t
         (while (setq data (read-line fp-data-file))
           (if (setq pos (vl-string-position (ascii ",") data))
             (setq data-list (cons (cons (substr data 1 pos) (substr data (+ 2 pos))) data-list))
           )
         )
        )
      )
      ; data integrity test
      (cond
        ((not (vl-consp data-list))
         (princ "\nNo usable layer data was found"))
        (t (mapcar 'fox-it data-list))
      )
      (princ)
    )
    
    
    (defun _AddLayer ($lay)
      (if (vl-catch-all-error-p (setq $lay (vl-catch-all-apply 'vla-add (list (vla-get-layers actDoc) $lay)))) nil $lay))
    
    (defun fox-it ( x / lay col l)
      (setq lay (car x) col (cdr x))
      (if (not (tblsearch "layer" lay)) (_AddLayer lay))
      (if (setq l (tblobjname "LAYER" lay))
        (progn (setq l (vlax-ename->vla-object l)) (vlax-put-property l 'Color col))
      )
    )

Similar Threads

  1. Reading .DWG file with vb.NET
    By DB007 in forum .NET, ObjectARX & VBA
    Replies: 0
    Last Post: 25th Sep 2016, 12:50 pm
  2. reading text file with characters (~, ',')
    By rodrigo_sjc_sp in forum AutoLISP, Visual LISP & DCL
    Replies: 11
    Last Post: 5th Sep 2013, 08:36 pm
  3. Handling Drawing (DWG) File Format Changes with AutoCAD
    By HyperPics in forum AutoCAD RSS Feeds
    Replies: 0
    Last Post: 5th Jul 2012, 09:40 pm
  4. Need help in reading data file
    By T2L in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 26th Jun 2009, 09:55 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts