Jump to content

What's wrong with my code


mit

Recommended Posts

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

Link to comment
Share on other sites

(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 by pBe
move to more
Link to comment
Share on other sites

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

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...