mit Posted November 4, 2015 Share Posted November 4, 2015 Hello everyone I want to import data from text file but, I have some problem about my code Cloud you please help me? first type: (defun c:P_Import () (setq SourceFile (open "C:\\test\\01010101.txt" "r")) (setq coords (read-line SourceFile)) (close SourceFile) (command "_pLine" (foreach pt coords ;(command "_pLine" (foreach pt XY (command XY))) (command pt)) ;(foreach pt XY ) ;(command "_pLine" ) ;close defun Second type: (defun c:P_Import () (setq SourceFile (open "C:\\test\\01010101.txt" "r")) (setq coords (read-line SourceFile)) (close SourceFile) (while (/= coords (setq coords (vl-string-subst ")(" " " coords)))) (setq coords (read (strcat "((" (vl-string-translate "," " " coords) "))"))) (command "_pLine" (foreach pt coords ;(command "_pLine" (foreach pt XY (command XY))) (command pt)) ;(foreach pt XY ) ;(command "_pLine" ) ;close defun 01010101.txt Quote Link to comment Share on other sites More sharing options...
pBe Posted November 4, 2015 Share Posted November 4, 2015 (edited) (defun c:P_Import () (setq SourceFile (open "C:\\test\\01010101.txt" "r")) (setq coords (read-line SourceFile)) (close SourceFile) (command "_pLine" (foreach pt (read (Strcat "(" coords ")")) (command pt) ) ) ) Now try and figure out the next one based on the code above [ if there are more than one set of point list that is ] Edited November 5, 2015 by pBe move to more Quote Link to comment Share on other sites More sharing options...
BIGAL Posted November 5, 2015 Share Posted November 5, 2015 I think we need to see what 01010101.txt looks like. Nice foreach Pbe Quote Link to comment Share on other sites More sharing options...
kpblc Posted November 5, 2015 Share Posted November 5, 2015 Another way: (vl-load-com) (defun test (/ _kpblc-conv-string-to-list file handle adoc coords) (defun _kpblc-conv-string-to-list (string separator / i) (cond ((= string "") nil) ((vl-string-search separator string) ((lambda (/ pos res) (while (setq pos (vl-string-search separator string)) (setq res (cons (substr string 1 pos) res) string (substr string (+ (strlen separator) 1 pos)) ) ;_ end of setq ) ;_ end of while (reverse (cons string res)) ) ;_ end of lambda ) ) ((wcmatch (strcase string) (strcat "*" (strcase separator) "*")) ((lambda (/ pos res _str prev) (setq pos 1 prev 1 _str (substr string pos) ) ;_ end of setq (while (<= pos (1+ (- (strlen string) (strlen separator)))) (if (wcmatch (strcase (substr string pos (strlen separator))) (strcase separator)) (setq res (cons (substr string 1 (1- pos)) res) string (substr string (+ (strlen separator) pos)) pos 0 ) ;_ end of setq ) ;_ end of if (setq pos (1+ pos)) ) ;_ end of while (if (< (strlen string) (strlen separator)) (setq res (cons string res)) ) ;_ end of if (if (or (not res) (= _str string)) (setq res (list string)) (reverse res) ) ;_ end of if ) ;_ end of lambda ) ) (t (list string)) ) ;_ end of cond ) ;_ end of defun (if (and (setq file (getfiled "Coords file" "" "txt" 4)) (findfile file) ) ;_ end of and (progn (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object)))) (setq handle (open file "r") coords (read-line handle) ) ;_ end of setq (close handle) (setq coords (mapcar (function (lambda (x) (atof (vl-string-subst "." "," (vl-string-trim "()" x))))) (_kpblc-conv-string-to-list coords " ") ) ;_ end of mapcar ) ;_ end of setq (vla-addlightweightpolyline (vla-get-modelspace adoc) (vlax-make-variant (vlax-safearray-fill (vlax-make-safearray vlax-vbdouble (cons 0 (1- (length coords))) ) ;_ end of vlax-make-safearray coords ) ;_ end of vlax-safearray-fill ) ;_ end of vlax-make-variant ) ;_ end of vla-AddLightWeightPolyline (vla-endundomark adoc) ) ;_ end of progn ) ;_ end of if ) ;_ end of defun Quote Link to comment Share on other sites More sharing options...
mit Posted November 5, 2015 Author Share Posted November 5, 2015 Thank you so much pBe and kpblc Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.