Results 1 to 10 of 10

Thread: export line coordinates

  1. #1
    Member
    Join Date
    2011-10
    Posts
    4

    Exclamation export line coordinates

    Hello everyone,

    i tried to make this autolisp code to export all line endpoints into a file, but i have a problem.
    It seems to workl for the first +-50 lines, but then I keep getting the same repeating line endpoint results. I checked the repeating coordinates in autocad, and there is no such thing as a line even near those 2 defined points.
    here is what i mean:

    lispcode, containing the code.
    test2, containing an example points calculation
    onderlegger, containg the file of shich to extract points.

    any help would be appreciated!

    Thanks!
    Attached Files Attached Files

  2. #2
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    478

    Default Re: export line coordinates

    Would this help you with it .... ?

    Code:
    (defun c:TesT (/ fileName selectionset)
    ;;; Tharwat 04. Dec. 2011 ;;;
      (if (and (setq fileName (getfiled "Name of File >>..." "" "txt" 1))
               (setq selectionset (ssget '((0 . "LINE"))))
          )
        (progn (setq filename (open filename "w"))
               ((lambda (i / namess entlist pt1 pt2)
                  (while (setq namess (ssname selectionset (setq i (1+ i))))
                    (setq entlist (entget namess))
                    (setq pt1 (cdr (assoc 10 entlist)))
                    (setq pt2 (cdr (assoc 11 entlist)))
                    (write-line (strcat (rtos (car pt1) 2) "\t" (rtos (cadr pt1) 2) "\t" (rtos (caddr pt1) 2)) fileName)
                    (write-line (strcat (rtos (car pt2) 2) "\t" (rtos (cadr pt2) 2) "\t" (rtos (caddr pt2) 2)) fileName)
                  )
                )
                 -1
               )
               (close fileName)
        )
        (princ)
      )
      (princ)
    )

  3. #3
    I could stop if I wanted to pbejse's Avatar
    Join Date
    2010-10
    Posts
    398

    Default Re: export line coordinates

    Quote Originally Posted by spamkiller433238 View Post
    Hello everyone,

    iI checked the repeating coordinates in autocad, and there is no such thing as a line even near those 2 defined points.

    Thanks!
    With the way your code is written right now. it will still write to file if the object is not a line:

    Code:
    (if (= "LINE" (cdr (assoc 0 (entget (ssname points i)))))
          (setq c (cdr (assoc 10 (entget (ssname points i))))
         
          )
        )
        (if (= "LINE" (cdr (assoc 0 (entget (ssname points i)))))
          (setq cbert (cdr (assoc 11 (entget (ssname points i))))
         i (1+ i)
          )
        )
    if nil your next line still write the previous data c and cbert hence the repeat, now imagine that if the first entity is not a line then c and cbert is nil. then you'll get

    ; error: bad argument type: numberp: nil

    to avoid this, instead of
    (setq points (ssget) i 0)
    use
    (setq points (ssget '((0 . "LINE"))) i 0)
    then you dont need to test if the entity is line:

    Your edited code so you may understand it better yourself
    Code:
    (defun c:PO2TXT (/ file points c cbert i)
      (setq file (open (getfiled "specify output file" "c:/" "TXT" 1) "w"))
      (if (setq points (ssget '((0 . "LINE"))) i 0)
        	(progn
    	  (repeat (sslength points)
    	    	(setq c (cdr (assoc 10 (entget (ssname points i))))
    	    	  cbert (cdr (assoc 11 (entget (ssname points i))))
    	     		i (1+ i)
    	      		)
    	(write-line
    	      (strcat (rtos (car c)) " ; "
    	       (rtos (cadr c)) " ; "
    	       (rtos (caddr c))
    	      ) file)
    	(write-line
    	      (strcat (rtos (car cbert)) " ; "
    	       (rtos (cadr cbert)) " ; "
    	       (rtos (caddr cbert))
    	      ) file)
    		)
              );progn
        );if
      (close file)
      (Princ)
    )
    Last edited by pbejse; 2011-12-04 at 11:19 AM.

  4. #4
    I could stop if I wanted to Lee Mac's Avatar
    Join Date
    2009-03
    Location
    London, England
    Posts
    280

    Default Re: export line coordinates

    After a quick look at your program, I notice that you are only incrementing the variable i if the entity encountered is a LINE entity.

    Since you haven't used a filter list with the ssget selection, there is a good chance that the user could inadvertently select an object which is not a line.

    In this case, the if statements within your repeat loop would not evaluate to T, and so the counter variable i would never be incremented.

    Hence, in the next iteration of the repeat loop, the same entity is processed (since i has not been incremented), and again i will not be incremented, and so on and so forth.

    Meanwhile, the program is still writing the values of variables c and cbert to the file, and, since the values of these variables have not been altered since the previous iteration, the same values are written over and over - that is, if a LINE has been encountered first, since otherwise these variables would be nil and you would receive an error from supplying a null value to the rtos function.

    Here is an alternative approach for you to study, I have included comments to help you see what the code is doing at each stage:


    Code:
    (defun c:pt2txt ( / counter endpoint entitydata filename openfile selection startpoint )
    
        ;; Define the function and localise the variables.
    
        (if
            ;; If the following expression returns a non-nil value...
            
            (and
    
                ;; All of the enclosed expressions must return a non-nil
                ;; value for the AND function to return T.
    
                (setq selection (ssget '((0 . "LINE"))))
    
                ;; Provide the SSGET function with a filter list so
                ;; that only LINEs may be selected.
                
                (setq filename (getfiled "Specify Output File" "" "txt" 1))
    
                ;; The IF statement is required since the user could press
                ;; 'cancel' when prompted to specify an output file.
                ;;
                ;; If 'cancel' is pressed, the GETFILED function would
                ;; return nil.
                ;;
                ;; (open nil "w") will result in an error.
    
                (setq openfile (open filename "w"))
    
                ;; A check that the file was able to be opened for writing.
                ;; This is to account for the possibility that the user
                ;; has specified an output file in a location for which they
                ;; do not have write permissions.
                ;;
                ;; In such a case (open <filename> "w") will return nil.
                
            ) ;; End AND
    
            (progn
    
                ;; The IF function can only accept at most three parameters:
                ;; a TEST expression, a THEN expression to be evaluated if the
                ;; TEST expression evaluates to a non-nil value, and an ELSE expression
                ;; to be evaluated if TEST evaluates to nil.
                ;;
                ;; Hence, if we wish to evaluate multiple expressions within the THEN
                ;; parameter, we need to wrap these expressions into a single PROGN
                ;; expression to be passed to the IF function.
                ;;
                ;; The PROGN function does nothing more than evaluate the expressions
                ;; passed to it, then return the result of the last expression evaluated.
                
                (setq counter 0)
    
                ;; Initialise the counter variable at zero
                ;; (since the Selection Set index is zero based).
    
                (repeat (sslength selection)
    
                    ;; Repeat evaluation of the following expressions a number
                    ;; of times equal to the number of objects in the selection set.
    
                    (setq entitydata (entget (ssname selection counter)))
    
                    ;; The entity at index 'counter' in the selection set is returned by
                    ;; the SSNAME function, and its corresponding DXF entity data is returned
                    ;; by the ENTGET function.
    
                    (setq startpoint (cdr (assoc 10 entitydata))
                          endpoint   (cdr (assoc 11 entitydata))
                    )
    
                    ;; Collect the start and end points of the Line entity from the DXF data list
                    ;;
                    ;; We know that is must be a LINE entity since the SSGET filter list has
                    ;; only allowed the user to select lines.
    
                    (write-line
                        (strcat
                            (rtos (car   startpoint)) ";"
                            (rtos (cadr  startpoint)) ";"
                            (rtos (caddr startpoint))
                        )
                        openfile
                    )
    
                    (write-line
                        (strcat
                            (rtos (car   endpoint)) ";"
                            (rtos (cadr  endpoint)) ";"
                            (rtos (caddr endpoint))
                        )
                        openfile
                    )
    
                    ;; Write the x;y;z values of the start and end points of the Line entity
                    ;; to the open file.
                    ;;
                    ;; RTOS is not supplied with units or precision parameters, so the values
                    ;; of the LUNITS and LUPREC System Variables will be used.
    
                    (setq counter (1+ counter))
    
                    ;; Increment the counter to move on to the next line in the Selection Set
    
                ) ;; End REPEAT
    
                (close openfile)
    
                ;; Close the open file.
    
                (princ (strcat "\nThe data from " (itoa counter) " line(s) has been written to file."))
    
                ;; Print a message to inform the user that the data has been written successfully.
    
            ) ;; End PROGN
    
        ) ;; End IF
    
        (princ)
    
        ;; (princ) is used to 'exit cleanly', since otherwise the
        ;; result of the last expression evaluated (the IF expression) would
        ;; be returned.
    
    ) ;; End DEFUN
    Ask if you have any questions about any part of the above code.

    Lee
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?
    Just another Swamper

  5. #5
    I could stop if I wanted to pbejse's Avatar
    Join Date
    2010-10
    Posts
    398

    Default Re: export line coordinates

    it look as if my explanation comes form a pocket reference and Lee's as Encyclopedia version

    Lock, stock and barrel

    Very detailed explanation Lee.

    Cheers

  6. #6
    I could stop if I wanted to Lee Mac's Avatar
    Join Date
    2009-03
    Location
    London, England
    Posts
    280

    Default Re: export line coordinates

    lol thanks pbejse
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?
    Just another Swamper

  7. #7
    Member
    Join Date
    2011-10
    Posts
    4

    Default Re: export line coordinates

    thank you all for your replies, ill try it and let you know if it works

  8. #8
    Member
    Join Date
    2011-10
    Posts
    4

    Default Re: export line coordinates

    Quote Originally Posted by Lee Mac View Post
    After a quick look at your program, I notice that you are only incrementing the variable i if the entity encountered is a LINE entity.

    Since you haven't used a filter list with the ssget selection, there is a good chance that the user could inadvertently select an object which is not a line.

    In this case, the if statements within your repeat loop would not evaluate to T, and so the counter variable i would never be incremented.

    Hence, in the next iteration of the repeat loop, the same entity is processed (since i has not been incremented), and again i will not be incremented, and so on and so forth.

    Meanwhile, the program is still writing the values of variables c and cbert to the file, and, since the values of these variables have not been altered since the previous iteration, the same values are written over and over - that is, if a LINE has been encountered first, since otherwise these variables would be nil and you would receive an error from supplying a null value to the rtos function.


    Lee
    Thank you, now it worked. i had to get this points to calculate the stresses in the structure with some maple toolbox.

    The lisp code i wrote was actually and adapted version of something i found on the internet and that i tried to decrypt and adapt to use for lines instead of points.

    anyway, im not a really good programmer

    Thanks again!

  9. #9
    I could stop if I wanted to Tharwat's Avatar
    Join Date
    2010-06
    Posts
    478

    Default Re: export line coordinates

    no comment.

  10. #10
    100 Club
    Join Date
    2005-06
    Location
    CORDOBA-ARGENTINA
    Posts
    151

    Default Re: export line coordinates

    Quote Originally Posted by spamkiller433238 View Post
    Hello everyone,

    i tried to make this autolisp code to export all line endpoints into a file, but i have a problem.
    It seems to workl for the first +-50 lines, but then I keep getting the same repeating line endpoint results. I checked the repeating coordinates in autocad, and there is no such thing as a line even near those 2 defined points.
    here is what i mean:

    lispcode, containing the code.
    test2, containing an example points calculation
    onderlegger, containg the file of shich to extract points.

    any help would be appreciated!

    Thanks!
    You can use DATAEXTRACTION , it will give you the req´d data. in a xls file

Similar Threads

  1. Export Coordinates
    By Maastricht in forum AutoCAD Map 3D - General
    Replies: 3
    Last Post: 2009-08-19, 04:45 PM
  2. Replies: 1
    Last Post: 2007-05-29, 10:37 AM
  3. Project coordinates not in synch with cad export
    By jhd in forum Revit Architecture - General
    Replies: 1
    Last Post: 2007-05-18, 09:04 PM
  4. Export coordinates to Excel
    By rputhenv in forum AutoCAD General
    Replies: 4
    Last Post: 2006-07-26, 12:24 PM
  5. Export coordinates - shared
    By sbrown in forum Revit Architecture - General
    Replies: 2
    Last Post: 2005-07-13, 02:58 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •