export all line coordinates left to right or bottom to top

export all line coordinates left to right or bottom to top

vishalghuge2500
Enthusiast Enthusiast
720 Views
8 Replies
Message 1 of 9

export all line coordinates left to right or bottom to top

vishalghuge2500
Enthusiast
Enthusiast

Im looking for a code which can extract coordinates of lines like -

for Horizontal lines - excel's first two columns will have left coordinates (red) and next columns will have right coordinates (green)..

for vertical lines - excel's first two columns will have bottom coordinates (red) and next columns will have top coordinates (green).

during export it should not consider start point and end point concept of lines.

i have drawn some lines by clicking top to bottom  & bottom to top also for horizontal lines - left to right & right to left

Untitled.png

I failed so

please give it a try,

0 Likes
Accepted solutions (2)
721 Views
8 Replies
Replies (8)
Message 2 of 9

daniel_cadext
Advisor
Advisor

Sorry for using the wrong language, but I wanted to play

import PyRx as Rx
import PyGe as Ge
import PyGi as Gi
import PyDb as Db
import PyAp as Ap
import PyEd as Ed

import traceback
import openpyxl as Ex  # using openpyxl


def PyRxCmd_doit():
    try:
        filter = [(Db.DxfCode.kDxfStart, "LINE")]
        ssres = Ed.Editor.select(filter)
        if ssres[0] != Ed.PromptStatus.eNormal:
            return
        
        doubles = [[]]
        vx = Ge.Vector3d.kXAxis
        vy = Ge.Vector3d.kYAxis

        tol = Ge.Tol()
        tol.setEqualVector(0.0001)

        for id in ssres[1].objectIds():
            line = Db.Line(id)
            sp = line.startPoint()
            ep = line.endPoint()
            seg = Ge.LineSeg3d(sp, ep)
            segdir = seg.direction()

            if vx.isParallelTo(segdir, tol):
                if vx.isCodirectionalTo(segdir, tol):
                    doubles.append([sp.x,sp.y,ep.x,ep.y])
                else:
                    doubles.append([ep.x,ep.y,sp.x,sp.y])
            elif vy.isParallelTo(segdir, tol):
                if vy.isCodirectionalTo(segdir, tol):
                    doubles.append([sp.x,sp.y,ep.x,ep.y])
                else:
                    doubles.append([ep.x,ep.y,sp.x,sp.y])
                    
        # create a workbook and get the active sheet
        wb = Ex.Workbook()
        ws = wb.active
        
        for nrow, i in enumerate(doubles):
            for ncol, j in enumerate(i):
                ws.cell(row=nrow+1, column=ncol+1, value=j)
            
        wb.save('e://pyout.xlsx')

    except Exception as err:
        traceback.print_exception(err)

out.png

 

Python for AutoCAD, Python wrappers for ARX https://github.com/CEXT-Dan/PyRx
0 Likes
Message 3 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

Try this

 

(vl-load-com)

(defun c:LineCoordsCSV ( / *error* i f s d b e)
  
  (defun *error* (errmsg)
    (if (not (wcmatch errmsg "Function cancelled,quit / exit abort,console break,end"))
      (princ (strcat "\nError: " errmsg)))
    (if f (close f))
    (princ))
  
  ;------------------------------------------------------------------------------------------------------
  
  (if (and (setq f (open (getfiled "Define name of exported file" (getvar 'DWGPREFIX) "csv" 1) "a"))
	   (setq s (ssget '((0 . "LINE"))))
	   (write-line "sx,sy,ex,ey,,dir" f)
	   )
    (repeat (setq i (sslength s))
      (if (and (setq d (entget (ssname s (setq i (1- i)))))
	       (setq b (cdr (assoc 10 d)))
	       (setq e (cdr (assoc 11 d))))
	(write-line
	  (if (equal (car b) (car e) 1e-6)
	    (if (> (cadr e) (cadr b))
	      (strcat (rtos (car b) 2 3) "," (rtos (cadr b) 2 3) "," (rtos (car e) 2 3) "," (rtos (cadr e) 2 3) ",," "V - LINES")
	      (strcat (rtos (car e) 2 3) "," (rtos (cadr e) 2 3) "," (rtos (car b) 2 3) "," (rtos (cadr b) 2 3) ",," "V - LINES"))
	    (if (> (car e) (car b))
	      (strcat (rtos (car b) 2 3) "," (rtos (cadr b) 2 3) "," (rtos (car e) 2 3) "," (rtos (cadr e) 2 3) ",," "H - LINES")
	      (strcat (rtos (car e) 2 3) "," (rtos (cadr e) 2 3) "," (rtos (car b) 2 3) "," (rtos (cadr b) 2 3) ",," "H - LINES")))
	  f))))
  (*error* "end")
  )

 

Message 4 of 9

komondormrex
Mentor
Mentor
Accepted solution

or this one

(defun get_csv_filename (extension / output_name csv_filename)
	(setq output_name (strcat (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'path)
							  "\\"
							  (vl-filename-base (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'name))
					  )
	)
	(if (type acet-ui-getfile)
			(setq csv_filename (acet-ui-getfile "Enter name of CSV file"
								 output_name
								 extension
								 ""
								 1
					   )
			)
			(setq csv_filename (getfiled "Enter name of CSV file"
							  output_name
							  extension
							  1
					   )
			)
	)
	(if (= "" csv_filename)
		nil
		csv_filename
	)
)

;**************************************************************************************************************************************************

(defun c:lines_lr_bt (/ line_list h_line_list v_line_list csv_filename_full csv_id)
	(setq line_list (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex (ssget '((0 . "line")))))))
		  h_line_list (vl-remove-if-not '(lambda (line) (or (equal 0 (vla-get-angle line) 1e-3)
													  	  	(equal pi (vla-get-angle line) 1e-3)
													  	)
							 		   	 )
								 	   	 line_list
					   )
		  h_line_list (mapcar '(lambda (line) (vl-sort (list (vlax-get line 'startpoint) (vlax-get line 'endpoint))
		  											  '(lambda (start end) (< (car start) (car end)))
											  )
							   )
							   h_line_list
					  )
		  v_line_list (vl-remove-if-not '(lambda (line) (or (equal (* 0.5 pi) (vla-get-angle line) 1e-3)
													    (equal (* 1.5 pi) (vla-get-angle line) 1e-3)
												 )
									     )
									     line_list
				      )
		  v_line_list (mapcar '(lambda (line) (vl-sort (list (vlax-get line 'startpoint) (vlax-get line 'endpoint))
		  											  '(lambda (start end) (< (cadr start) (cadr end)))
											  )
							   )
							   v_line_list
					  )
		  line_list (append (mapcar '(lambda (line) (cons "Hor" line)) h_line_list)
							(mapcar '(lambda (line) (cons "Ver" line)) v_line_list)
					)
	)
	(if (setq csv_filename_full (get_csv_filename "csv"))
		(progn
			 (setq csv_id (open csv_filename_full "w"))
			 (foreach line line_list
			 	(princ (car (nth 1 line)) csv_id)
				(princ "," csv_id)
			 	(princ (cadr (nth 1 line)) csv_id)
				(princ "," csv_id)
			 	(princ (car (nth 2 line)) csv_id)
				(princ "," csv_id)
			 	(princ (cadr (nth 2 line)) csv_id)
				(princ ",," csv_id)
				(princ (car line) csv_id)
				(princ "\n" csv_id)
			)
			(close csv_id)
		)
	)
	(princ)
)

;**************************************************************************************************************************************************
Message 5 of 9

vishalghuge2500
Enthusiast
Enthusiast

@ВeekeeCZ @komondormrex  thank you.

 

one more thing.

can we group these exported lines?

Untitled01.png

0 Likes
Message 6 of 9

komondormrex
Mentor
Mentor

possible way to solve it may be joining all lines into plines with autocad command then explode each pline joined and output coordinates as per each line of exploded pline.

0 Likes
Message 7 of 9

komondormrex
Mentor
Mentor

somewhat like this 

;********************************************************************************************************************************************************************************

(defun get_csv_filename (extension / output_name csv_filename)
	(setq output_name (strcat (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'path)
							  "\\"
							  (vl-filename-base (vlax-get (vla-get-activedocument (vlax-get-acad-object)) 'name))
					  )
	)
	(if (type acet-ui-getfile)
			(setq csv_filename (acet-ui-getfile "Enter name of CSV file"
								 output_name
								 extension
								 ""
								 1
					   )
			)
			(setq csv_filename (getfiled "Enter name of CSV file"
							  output_name
							  extension
							  1
					   )
			)
	)
	(if (= "" csv_filename)
		nil
		csv_filename
	)
)

;********************************************************************************************************************************************************************************

(defun c:lines_lr_bt (/ last_object_number lines_sset pline_list csv_filename_full csv_id line_list line_list_delete)
	(setq last_object_number (vla-get-count (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))))
		  lines_sset (ssget '((0 . "line")))
	)
	(setvar 'cmdecho 0)
	(command "_copy" lines_sset "" '(0 0) '(0 0))
	(command "_pedit" "_m" lines_sset "" "_y" "_j" "" "")
	(setvar 'cmdecho 1)
	(repeat (- (vla-get-count (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))) last_object_number)
		(setq pline_list (append pline_list (list (vla-item (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object)))) last_object_number)))
		      last_object_number (1+ last_object_number)
		)
	)
	(if (setq csv_filename_full (get_csv_filename "csv"))
			(progn
				(setq csv_id (open csv_filename_full "w"))
				(foreach pline pline_list
					(setq line_list (mapcar '(lambda (line) (cond
											(
												(or (equal 0 (vla-get-angle line) 1e-3)
												  	(equal pi (vla-get-angle line) 1e-3)
												)
													(cons "Hor"
														   (vl-sort (list (vlax-get line 'startpoint) (vlax-get line 'endpoint))
	  											  				   '(lambda (start end) (< (car start) (car end)))
														   )
													)
										    )
											(
												(or (equal (* 0.5 pi) (vla-get-angle line) 1e-3)
												  	(equal (* 1.5 pi) (vla-get-angle line) 1e-3)
												)
													(cons "Ver"
														   (vl-sort (list (vlax-get line 'startpoint) (vlax-get line 'endpoint))
	  											  				   '(lambda (start end) (< (cadr start) (cadr end)))
														   )
													)
										    )
												( t )
											)
							 )
							 (setq line_list_delete (vlax-invoke pline 'explode))
									)
					)
					(mapcar 'vla-erase line_list_delete) 
					(foreach line line_list
						(princ (car (nth 1 line)) csv_id)
						(princ "," csv_id)
						(princ (cadr (nth 1 line)) csv_id)
						(princ "," csv_id)
						(princ (car (nth 2 line)) csv_id)
						(princ "," csv_id)
						(princ (cadr (nth 2 line)) csv_id)
						(princ ",," csv_id)
						(princ (car line) csv_id)
						(princ "\n" csv_id)
					)
					(princ ",\n" csv_id)
				  	(vla-erase pline)
				)
				(close csv_id)
			)
	)
	(princ)
)

;********************************************************************************************************************************************************************************

 

0 Likes
Message 8 of 9

vishalghuge2500
Enthusiast
Enthusiast

@komondormrex 

Command: LINES_LR_BT
Select objects: Specify opposite corner: 19 found
Select objects:
; error: bad argument type: consp T

0 Likes
Message 9 of 9

komondormrex
Mentor
Mentor

post a file with error.

0 Likes