Getfiled to read file and make a pline

Discussion in 'AutoCAD' started by Rogerio_Brazil, Dec 27, 2004.

  1. Hello,

    How to use a function getfiled to read a .txt file and draw a pline with the points?

    Sample getfiled function:

    (set fname (getfiled "Points file to read:" "C:\\" "txt" 4))

    Sample .txt file:
    2 coordinates

    1,4
    2,5
    3,1

    or
    3 coordinates:

    1,4,0
    2,5,0
    3,1,0

    Thanks in advance,

    Rogerio
     
    Rogerio_Brazil, Dec 27, 2004
    #1
  2. Rogerio_Brazil

    pablorico21 Guest

    Tomorrow I will bring some idea for you.

    Pablo
     
    pablorico21, Dec 27, 2004
    #2
  3. Rogerio_Brazil

    T.Willey Guest

    Use these to read your file once you have it.

    (defun PointsFromFile (FileName / PtList temp1 temp2)

    (setq Opned (open FileName "r"))
    (while (setq temp1 (read-line Opned))
    (setq temp2 (mapcar 'read (StrParse temp1 ",")))
    (setq PtList (cons temp2 PtList))
    )
    (close Opned)
    (reverse PtList)
    )

    (defun StrParse (String Seperator / Pos1 Pos2 NewStrList)
    ;|
    Seperator a string (making a list of stings) at a given
    string value
    ie: (StrParse "1,1,0" ",")
    returns: ("1" "1" "0")
    Written when I couldn't find it on the web
    By: Tim Willey 11/15/2004
    |;

    (setq Pos2 1)
    (while (setq Pos1 (vl-string-search Seperator String Pos1))
    (if (= Pos2 1)
    (setq NewStrList (cons (substr String Pos2 Pos1) NewStrList))
    (setq NewStrList (cons (substr String Pos2 (- (1+ Pos1) Pos2)) NewStrList))
    )
    (setq Pos2 (1+ (+ (strlen Seperator) Pos1)))
    (setq Pos1 (+ Pos1 (strlen Seperator)))
    )
    (reverse (setq NewStrList (cons (substr String Pos2) NewStrList)))
    )

    Then you can issue the pline command like
    (command "_.pline")
    (mapcar 'command temp1)
    ; where temp1 is the list returned by PointsFromFile
    (command "")

    Hope that helps.
    Tim
     
    T.Willey, Dec 27, 2004
    #3
  4. Rogerio_Brazil

    T.Willey Guest

    Or just issue the command like

    (command "pline" (mapcar 'command temp1))

    Tim
     
    T.Willey, Dec 27, 2004
    #4
  5. Thank very much, Pablo and Tim.

    Rogerio
     
    Rogerio_Brazil, Dec 27, 2004
    #5
  6. Rogerio_Brazil

    T.Willey Guest

    Glad it works for you.
    Happy to help.

    Tim
     
    T.Willey, Dec 27, 2004
    #6
  7. Rogerio_Brazil

    pablorico21 Guest

    ;;MAKE PLINE FROM TXT POINTS
    ;;By Pablo Barbosa
    ;;e-mail:p
    ;;date:27/12/04


    ;|OBS.:
    TAKE WITH YOUR COORDINATE FORMAT.
    THE "," SEPARATE X Y Z, and "." SEPARATE DECIMALS
    ex.: 1.5,2.6,0.0 -> correct format
    1,5,2,6,0,0 -> wrong format
    |;
    ;;TASTE THIS ROUTINE, IT WORK FINE.
    (defun C:MKPLINE (/ OPENFILE LINEINFILE LENVERTICE filename ENDLIST2P ENDLIST2 ENDLIST)
    (SETQ ECHOMKPLINE (GETVAR "CMDECHO"))
    (SETVAR "CMDECHO" 0)

    ;GET FILENAME
    (SETQ FILENAME (getfiled "Points file to read:" "c:\\" "txt" 4))
    (IF FILENAME
    (PROGN
    ;;OPEN FILE
    (SETQ ENDLIST NIL)
    (SETQ OPENFILE (OPEN FILENAME "R"))
    ;;WHILE HAVE LINES FOR READ..
    (WHILE (SETQ LINEINFILE (READ-LINE OPENFILE))
    (SETQ ENDLIST (APPEND (LIST LINEINFILE) ENDLIST))
    )
    ;;CLOSE FILE
    (CLOSE OPENFILE)
    ;|
    CONVERT STRINGLIST IN LIST
    SEE GETCONVERTLIST FUNCTION AFTER THIS ROUTINE FOR MORE DETAILS...
    |;
    (SETQ ENDLIST2P (MAPCAR 'GETCONVERTLIST ENDLIST))
    ;;ADD 10 DXF CODE FOR MAKE PLINE LIST CORDINATES
    (SETQ ENDLIST2 (MAPCAR '(LAMBDA (X) (APPEND '(10) X)) ENDLIST2P))
    ;; VERTICE NUMBER
    (SETQ LENVERTICE (LENGTH ENDLIST2))
    ;;IF YOU NEED, CHANGE FOR 3DPOLY...
    ;;FINALLY...MAKING PLINE
    (entmake
    (APPEND
    (LIST (cons 0 "lwpolyline"))
    ;;2dpoly
    (LIST (cons 100 "AcDbEntity"))
    (LIST (cons 100 "AcDbPolyline"))
    (LIST (cons 90 LENVERTICE))
    ENDLIST2

    )
    )

    (princ "\nBy Pablo Barbosa - - 27/12/04."))
    (progn
    ;;IF THE USER CANCEL THE GETFILED
    (princ "\nMKPLINE COMMAND WAS CANCELED!")
    (SETVAR "CMDECHO" ECHOMKPLINE)
    (PRINC)
    (VL-EXIT-WITH-VALUE nil))
    )
    (SETVAR "CMDECHO" ECHOMKPLINE)
    (PRINC)
    )



    ;;convert list originating from of the strings
    ;;EX:
    ;;2d (mapcar 'GETCONVERTLIST '("0,1" "1,2" "0,3"))->((0 1) (1 2) (0 3))
    ;;or 3d (mapcar 'GETCONVERTLIST '("0,1,1" "1,2,1" "0,3,1"))->((0 1 1) (1 2 1) (0 3 1))
    ;;IF You need, substitute the "," for other character.

    ;;and so, the magic function...
    (DEFUN GETCONVERTLIST (PSEUDOLISTT / RESULT)
    ;;{(STRCAT "("...} make a string result: "0,2,0" -> "(0,2,0)"
    (SETQ RESULT
    (STRCAT "("(VL-STRING-TRANSLATE "," " " PSEUDOLISTT)")")
    )
    ;;...andprint list result
    (READ RESULT)
    )


    ;;excuse for my poor English, I promise improve it!,But my lisp...
    ;;I will post it in Autolisp.com.br too.



    ;|«Visual LISP© Format Options»
    (90 2 1 0 nil "end of " 90 9 0 0 2 nil T nil T)
    ;*** DO NOT add text below the comment! ***|;
     
    pablorico21, Dec 28, 2004
    #7
  8. In portuguese language:

    Pablo, como o mundo é pequeno.

    Sou eu, o Rogério do www.autolisp.com.br.

    Postei aqui porque sempre tem boas alternativas.

    Valeu pela ajuda. De verdade.

    Um abraço. Thank you!

    Rogério
     
    Rogerio_Brazil, Dec 28, 2004
    #8
  9. Rogerio_Brazil

    pablorico21 Guest

    Oi Rogerio, coloquei essa rotina no Autolisp.com.br
    Tchau. o Fórum aqui é muito bom.
     
    pablorico21, Dec 28, 2004
    #9
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.