Jump to content

Need help creating an AutoLISP script


Farrt

Recommended Posts

I have a text file that contains a set of points on each line in the form "x, y, z". I am trying to write a .LSP that can read the first line as point 1, then read the next line as point 2 and draw a line between the two. Then it will save point 1 as point 2, and define a new point 2 from line 3 and so on until the end of the text file. 

My biggest problem is getting autoCAD to read the line as a set of x,y,z points. If the text file line reads "214,1296,0" then either autoCAD will read it as a string or if I use the function atoi it just reads 214 and saves that number ignoring the other 2 points.

 

I can adjust the text file to any format that would work best with the .LSP function. It is just that each text file has over 4000 points and so over 4000 lines. And I have over 3000 text files to read. So having automation for doing this would be a great help to my project. I included the first text file below for reference.

 

Thanks for the support. 

1.txt

Link to comment
Share on other sites

3 hours ago, Farrt said:

I have a text file that contains a set of points on each line in the form "x, y, z". I am trying to write a .LSP that can read the first line as point 1, then read the next line as point 2 and draw a line between the two. Then it will save point 1 as point 2, and define a new point 2 from line 3 and so on until the end of the text file. 

My biggest problem is getting autoCAD to read the line as a set of x,y,z points. If the text file line reads "214,1296,0" then either autoCAD will read it as a string or if I use the function atoi it just reads 214 and saves that number ignoring the other 2 points.

 

I can adjust the text file to any format that would work best with the .LSP function. It is just that each text file has over 4000 points and so over 4000 lines. And I have over 3000 text files to read. So having automation for doing this would be a great help to my project. I included the first text file below for reference.

 

Thanks for the support. 

1.txt 49.46 kB · 0 downloads

Please show us how far you went on LISP . 

 

To be clear. please upload your LISP , and the DWG where you apply  such lisp 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

Same result as devitg with these few lines of code

(defun c:readTXT2line ( / input f_open l_read x_data y_data z_data new_pt previous_pt)
  (setq
    input (getfiled "Select a TXT file" "" "txt" 2)
    f_open (open input "r")
  )
  (while (setq l_read (read-line f_open))
    (setq
      x_data (atoi (substr l_read 1 (vl-string-position 44 l_read)))
      l_read (substr l_read (+ 2 (vl-string-position 44 l_read)))
      y_data (atof (substr l_read 1 (vl-string-position 44 l_read)))
      l_read (substr l_read (+ 2 (vl-string-position 44 l_read)))
      z_data (atof (substr l_read 1 (vl-string-position 44 l_read)))
      new_pt (list x_data y_data z_data)
    )
    (if previous_pt
      (entmake
        (list
          '(0 . "LINE")
          '(100 . "AcDbEntity")
          (cons 8 (getvar "CLAYER"))
          (cons 10 previous_pt)
          (cons 11 new_pt)
          '(210 0.0 0.0 1.0)
        )
      )
    )
    (setq previous_pt new_pt)
  )
  (close f_open)
  (prin1)
)

 

Link to comment
Share on other sites

If your looking for a multi join seperate lines then this is like CIV3D functions that join strings via a code, all you would do is add a "CODE" to the txt "X,Y,Z,CODE"  "123.45,456.78,0.0,str1" when the code changes you start a new string. 

 

Any way a pline answer.

 

(defun c:joinpts ( / fo str ptstr lstpts csv->lst lwpoly)
; thanks to Lee-mac for this defun
; 32 is space 44 is comma

(defun csv->lst ( str / pos )
(if (setq pos (vl-string-position 44 str))
    (cons (substr str 1 pos) (csv->lst (substr str (+ pos 2))))
    (list str)
    )
)

(defun LWPoly (lst cls)
 (entmakex (append (list (cons 0 "LWPOLYLINE")
                         (cons 100 "AcDbEntity")
                         (cons 100 "AcDbPolyline")
                         (cons 90 (length lst))
                         (cons 70 cls))
                   (mapcar (function (lambda (p) (cons 10 p))) lst)))
)

; starts here

(setvar 'ctab "Model")
(setq fo (open (getfiled "the data file " "C:" "txt" 0) "R"))
(setq lstpts '())
(while (setq str (read-line fo))
  (if (= (strlen str) 0) ; if blank line on end of file
  (princ)
  (progn
    (setq ptstr (csv->lst str))
    (setq lstpts (cons  (list (atof (car ptstr)) (atof (cadr ptstr)) (atof (caddr ptstr))) lstpts))
  )
  )
)

(close fo)

(LWPoly lstpts 1)

(command "zoom" "E")

(princ)
)
(c:joinpts)

 

Link to comment
Share on other sites

So if it is x,y,z you could look at Lee Mac string to list function for each line, using your line as the string and ',' as the deliminator - makes a list, and I think you'd need to atof each item in the list to make them numbers

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...