Jump to content

Draw polyline from file coords excel


nghiahuu

Recommended Posts

I wrote this a while back - will extract from a txt file. Assumes points are comma delimited.

 


(defun c:ptpoly (/ doc spc file nl ptlst)
 (vl-load-com)
 
 (setq doc (vla-get-ActiveDocument
             (vlax-get-Acad-Object))
       spc (if (zerop (vla-get-activespace doc))
             (if (= (vla-get-mspace doc) :vlax-true)
               (vla-get-modelspace doc)
               (vla-get-paperspace doc))
             (vla-get-modelspace doc)))
 
 (if (setq file (getfiled "Select File" "" "txt" )
   (progn
     (setq file (open file "r"))
     (while (setq nl (read-line file))
       (setq ptlst (cons (StrBrk nl 44) ptlst)))
     (close file)
     (if ptlst
       (progn
         (setq ptlst
                (apply 'append
                       (mapcar
                         (function
                           (lambda (x)
                             (list (car x) (cadr x))))
                         (reverse
                           (mapcar
                             (function
                               (lambda (x)
                                 (mapcar 'distof x))) ptlst)))))
         (vla-addLightweightpolyline spc
           (vlax-make-variant
             (vlax-safearray-fill
               (vlax-make-safearray
                 vlax-vbdouble
                 (cons 0 (1- (length ptlst)))) ptlst))))
       (princ "\n<< No Points Found in File >>")))
   (princ "\n<< No File Selected >>"))
 (princ))
         

(defun StrBrk (str chrc / pos lst)
 (while (setq pos (vl-string-position chrc str))
   (setq lst (cons (substr str 1 pos) lst)
         str (substr str (+ pos 2))))
 (reverse (cons str lst)))

Link to comment
Share on other sites

Don't zip it with RAR because that is not a commonly (for free) available format. Try saving your Excel file as a CSV file, and copy ten lines or so into your post.

Link to comment
Share on other sites

Can you help me??. Thanks.

 

Try this one, just select the diapazone with numbers only

Tested on A2008, Excel 2003

 

(vl-load-com)

(defun C:PX (/ acapp acsp adoc aexc cel col cols coords csht
       item nwb points poly rang result row rows sht tmp wbks)
(setq aexc (vlax-get-or-create-object "Excel.Application")
     wbks (vlax-get-property aexc "Workbooks")
     nwb  (vlax-invoke-method wbks "Open" "C:\\File excel.xls");;<--change file name here
     sht  (vlax-get-property nwb "Sheets")
     csht (vlax-get-property sht "Item" 1)
     )

(vla-put-visible aexc :vlax-true)
(setq rang (vlax-invoke-method
     (vlax-get-property aexc "Application")
     "InputBox"
     "Select Diapazone To Get Coordinates"
     "Let you get a points"
     nil
     nil
     nil
     nil
     nil
     (vlax-make-variant 8 3))
     )
(setq rang (vlax-variant-value rang)
     )
(setq coords (vlax-get-property rang "Value2")
     )
(setq rows (vlax-get-property (vlax-get-property rang "Rows") "Count")
     )
(setq cols (vlax-get-property
     (vlax-get-property rang "Columns")
     "Count")
     )
(setq row 1)
(repeat	rows
 (setq col 1)
 (repeat cols
   (setq cel (vlax-variant-value
	(vlax-get-property
	  (vlax-get-property rang "Cells")
	  "Item"
	  (vlax-make-variant row vlax-vbLong)
	  (vlax-make-variant col vlax-vbLong))))
   (setq item (vlax-variant-value
	 (vlax-get-property cel "Value2"))
  )
   (setq tmp (cons item tmp)
  )
   (setq col (1+ col)
  )
   )
 (setq	points (cons (reverse tmp) points)
tmp    nil
row    (1+ row)
)
 )
(setq points (reverse points)
     points (apply 'append points)
     )

(vl-catch-all-apply
 (function (lambda ()
      (vlax-invoke-method
	nwb
	"Close" :vlax-false)))
 )

(vl-catch-all-apply
 (function (lambda ()
      (vlax-invoke-method
	aexc
	"Quit")))
 )
(mapcar	(function (lambda (x)
	    (vl-catch-all-apply
	      (function	(lambda	()
			  (progn
			    (vlax-release-object x)
			    (setq x nil)))))))
(list rang csht nwb wbks aexc)
)

(setq
 adoc (vla-get-activedocument
 (setq acapp (vlax-get-acad-object))
 )
 )
(if (= 1 (vlax-get-property adoc "Activespace"))
 (setq acsp (vla-get-modelspace adoc))
 (setq acsp (vla-get-paperspace adoc))
 )

(setq poly (vlax-invoke acsp "Add3DPoly" points)
     )
(vla-eval
 acapp
 (strcat
   "ThisDrawing.SetVariable \"USERI1\","
   "MsgBox (\"Close Polyline?\","
   "vbYesNo"
   ",\""
   "Answer this question:"
   "\")"
   )
 )
(if (= 6 (setq result (getvar "USERI1")))
 (vlax-put-property poly "Closed" :vlax-true)
 )
(vla-zoomextents acapp)
 
(gc)
(gc)
(princ)
 )
(prompt "\t\t***\t\nType PX to run program\t***")
(prin1)

 

~'J'~

Link to comment
Share on other sites

Try this one, just select the diapazone with numbers only

Tested on A2008, Excel 2003

 

Fixo, I could learn a lot from your code - I like the way you merge VBA and VL :thumbsup: - didn't know you could do that like that.

 

Lee

Link to comment
Share on other sites

I downloaded the file, and saved it in csv format, deleted some columns and a row, and saved it in csv format again so that it looked like this:-

0,0,0

0.05,0.002,0.05

0.1,0.01,0.1

0.148,0.022,0.15

0.196,0.04,0.2

0.242,0.062,0.25

0.287,0.089,0.3

0.329,0.12,0.35

0.368,0.156,0.4

Then I opened the text file, selected all and copied, went to AutoCAD, started the 3D polyline command, and when it wanted a startpoint, I clicked on the command line and pasted, and here is what I got (without using Lisp).

polyline.jpg

Link to comment
Share on other sites

Fixo, I could learn a lot from your code - I like the way you merge VBA and VL :thumbsup: - didn't know you could do that like that.

 

Lee

 

Thanks for the flowers Lee :))

I was just grabbing all these things from Excel VBA Help

and partially from my old experiences

Regards,

 

Oleg

 

~'J'~

Link to comment
Share on other sites

eldon

 

Fill in the point coordenate in the Excel file, then select the yellow cells next to the points you entered then copy (Ctrl+C) the selected area then paste in AutoCAD command line.

Link to comment
Share on other sites

eldon

 

Fill in the point coordenate in the Excel file, then select the yellow cells next to the points you entered then copy (Ctrl+C) the selected area then paste in AutoCAD command line.

 

I am not quite sure why you are telling me this, because I have already plotted your points as shown in the picture. I did not in fact use the yellow cells, but saved the file as a csv format, and worked from there. :)

Link to comment
Share on other sites

I used it my way, and it worked. Why should I change?

 

I am used to ASCII text data, and see no reason to complicate matters with further formatting.

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