Jump to content

LISP to count MTEXT & Polylines


andy_06

Recommended Posts

Does the diameter column include the description [mm PE]? Please clarify, in another post its

 

EN EN Length Diameter

1 2 10 90mm PE

2 3 25 63mm PE

2 4 18 63mm PE

 

Hi pBe,

 

It doesn't have to no. The main info I need is the number before the mm PE (i.e. 63, 90, 125, 180 etc). The pipe sizing software that I use doesn't require the mm PE.

 

Thanks for your help.

Link to comment
Share on other sites

  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

  • andy_06

    32

  • pBe

    17

  • BIGAL

    10

  • Grrr

    5

Top Posters In This Topic

It doesn't have to no. The main info I need is the number before the mm PE (i.e. 63, 90, 125, 180 etc). The pipe sizing software that I use doesn't require the mm PE.

 

Ok, hang on....

 

(defun c:labelToCSV  (/ _FindNextGN ss end val sn pl_list mt_list gn_list data gnv data
                     	csvfile f ip2pl openfile nearest)
(defun _FindNextGN (p lst glst / pl gn f)
     (setq factor 0.1)
     (if (and 
      (setq pl (vl-some (function (lambda (v / w)
			 (if (setq f (vl-some '(lambda (u)
                            	(if (equal p u factor) u)) (caddr v)))
                             		(list (car v)
                                              (cadr v)
                                              (car (vl-remove f (last v))) v)))) lst))
            (setq gn (vl-some (function (lambda (w)
                                    (if (equal (caddr pl) (car w) factor) (cadr w)))) (cdr glst)))
           )
         (list  gn pl )          
     	)
     )

(foreach v '( pl_list mt_list gn_list data)(set v nil))
     
(if (setq ss (ssget 
	       '((410 . "Model")(8 . "0gas*")(62 . 256)
	         (-4 . "<OR")
               	 (-4 . "<AND")(0 . "MTEXT")(1 . "#*[Pp][Ee]")(-4 . "AND>")
		 (0 . "LWPOLYLINE")
                        (-4 . "<AND")(0 . "INSERT")(66 . 1)(2 . "GN")(-4 . "AND>")
                        (-4 . "OR>")))
        )
	(progn          
		(repeat (setq i (sslength ss))
	              (setq val nil sn (ssname ss (setq i (1- i))))
			(cond
                                     (	(eq (cdr (assoc 0 (setq ent (entget  sn)))) "LWPOLYLINE")
                                       (setq pl_list (cons (list (vlax-curve-getDistAtParam sn
	                                                        (vlax-curve-getendparam sn)) sn
                                                                 	(list (vlax-curve-getStartPoint sn)
                                                                             (vlax-curve-getEndPoint sn))
                                                                 ) pl_list))
                                      )
                                     (	(eq (cdr (assoc 0 ent)) "MTEXT")
                                      	(setq mt_list (cons (list (cdr (assoc 10 ent)) (atoi (cdr (assoc 1 ent)))) mt_list))
                                      )
                                     (	T (while
				        (and
				            (null val)
				            (setq sn (entnext sn))
				            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget sn)))))
				        )
				        (if (= "1" (cdr (assoc 2 enx)))
				            (setq val (cdr (assoc 1 (reverse enx))))
				        )
                                            )
						    
                                     	(setq gn_list (cons (list (cdr (assoc 10 ent)) val)	gn_list))
                                     )
	                    )
                     		)
                     

	(and
                     mt_list
                     pl_list
               (setq gn_list (vl-sort gn_list '(lambda (n m)
                                                     (< (atoi (cadr n))(atoi (cadr m))))))
	(progn
		(While gn_list
                             (setq gnv (Car gn_list))
                         (while
                               (setq f (_FindNextGN (car gnv) pl_list gn_list))
			(setq ip2pl
                               	(mapcar '(lambda (a)
					(list   (distance (car a)                                            
					(vlax-curve-getClosestPointTo (cadadr f) (car a))) (cadr a) a))
                                              			mt_list))
                               (setq _nearest (car (vl-sort ip2pl '(lambda ( d e )
	                                                            (< (car d) (car e))))))
                               (setq mt_list (vl-remove (last _nearest) mt_list))
                               
                               (setq data (cons (strcat  (cadr gnv) "," (Car f) ","
                                                       [color="blue"](rtos (caadr f) 2 0) [/color]"," [color="blue"](itoa (cadr _nearest))[/color]) data))
                               
                               (setq pl_list (vl-remove (last (cadr f)) pl_list))
                               )
                         (setq gn_list (cdr gn_list))
                         )
                     data
                     )
                     
	(setq csvfile (getfiled "Save CSV file" (strcat (getvar 'dwgprefix) 
                                                   (vl-filename-base (getvar 'dwgname)) ".csv")
                                                   "CSV" 1))
	(setq openfile (open csvfile "w"))
               (write-line "Node,Node,Diameter,Length" openfile)
               (foreach itm (reverse data)
                     (write-line itm openfile))
	(not (close  openfile))
               (startapp "notepad"    csvfile)  
                     )
	)
)
(princ)
   )

 

Resulting CSV file content:

 

Node,Node,Diameter,Length

1,3,63,14

3,12,125,10

3,25,63,20

5,25,90,14

 

I don't have enough drawing samples to do a thorough test, try it on your end and tell me how it goes.

 

pBe

Edited by pBe
Link to comment
Share on other sites

Ok, hang on....

 

(defun c:labelToCSV  (/ _FindNextGN ss end val sn pl_list mt_list gn_list data gnv data
                     	csvfile f ip2pl openfile nearest)
(defun _FindNextGN (p lst glst / pl gn f)
     (setq factor 0.1)
     (if (and 
      (setq pl (vl-some (function (lambda (v / w)
			 (if (setq f (vl-some '(lambda (u)
                            	(if (equal p u factor) u)) (caddr v)))
                             		(list (car v)
                                              (cadr v)
                                              (car (vl-remove f (last v))) v)))) lst))
            (setq gn (vl-some (function (lambda (w)
                                    (if (equal (caddr pl) (car w) factor) (cadr w)))) (cdr glst)))
           )
         (list  gn pl )          
     	)
     )

(foreach v '( pl_list mt_list gn_list data)(set v nil))
     
(if (setq ss (ssget 
	       '((410 . "Model")(8 . "0gas*")(62 . 256)
	         (-4 . "<OR")
               	 (-4 . "<AND")(0 . "MTEXT")(1 . "#*[Pp][Ee]")(-4 . "AND>")
		 (0 . "LWPOLYLINE")
                        (-4 . "<AND")(0 . "INSERT")(66 . 1)(2 . "GN")(-4 . "AND>")
                        (-4 . "OR>")))
        )
	(progn          
		(repeat (setq i (sslength ss))
	              (setq val nil sn (ssname ss (setq i (1- i))))
			(cond
                                     (	(eq (cdr (assoc 0 (setq ent (entget  sn)))) "LWPOLYLINE")
                                       (setq pl_list (cons (list (vlax-curve-getDistAtParam sn
	                                                        (vlax-curve-getendparam sn)) sn
                                                                 	(list (vlax-curve-getStartPoint sn)
                                                                             (vlax-curve-getEndPoint sn))
                                                                 ) pl_list))
                                      )
                                     (	(eq (cdr (assoc 0 ent)) "MTEXT")
                                      	(setq mt_list (cons (list (cdr (assoc 10 ent)) (atoi (cdr (assoc 1 ent)))) mt_list))
                                      )
                                     (	T (while
				        (and
				            (null val)
				            (setq sn (entnext sn))
				            (= "ATTRIB" (cdr (assoc 0 (setq enx (entget sn)))))
				        )
				        (if (= "1" (cdr (assoc 2 enx)))
				            (setq val (cdr (assoc 1 (reverse enx))))
				        )
                                            )
						    
                                     	(setq gn_list (cons (list (cdr (assoc 10 ent)) val)	gn_list))
                                     )
	                    )
                     		)
                     

	(and
                     mt_list
                     pl_list
               (setq gn_list (vl-sort gn_list '(lambda (n m)
                                                     (< (atoi (cadr n))(atoi (cadr m))))))
	(progn
		(While gn_list
                             (setq gnv (Car gn_list))
                         (while
                               (setq f (_FindNextGN (car gnv) pl_list gn_list))
			(setq ip2pl
                               	(mapcar '(lambda (a)
					(list   (distance (car a)                                            
					(vlax-curve-getClosestPointTo (cadadr f) (car a))) (cadr a) a))
                                              			mt_list))
                               (setq _nearest (car (vl-sort ip2pl '(lambda ( d e )
	                                                            (< (car d) (car e))))))
                               (setq mt_list (vl-remove (last _nearest) mt_list))
                               
                               (setq data (cons (strcat  (cadr gnv) "," (Car f) ","
                                                      (itoa (cadr _nearest)) "," (rtos (caadr f) 2 0)) data))
                               
                               (setq pl_list (vl-remove (last (cadr f)) pl_list))
                               )
                         (setq gn_list (cdr gn_list))
                         )
                     data
                     )
                     
	(setq csvfile (getfiled "Save CSV file" (strcat (getvar 'dwgprefix) 
                                                   (vl-filename-base (getvar 'dwgname)) ".csv")
                                                   "CSV" 1))
	(setq openfile (open csvfile "w"))
               (write-line "Node,Node,Diameter,Length" openfile)
               (foreach itm (reverse data)
                     (write-line itm openfile))
	(not (close  openfile))
               (startapp "notepad"    csvfile)  
                     )
	)
)
(princ)
   )

 

Resulting CSV file content:

 

Node,Node,Diameter,Length

1,3,63,14

3,12,125,10

3,25,63,20

5,25,90,14

 

I don't have enough drawing samples to do a thorough test, try it on your end and tell me how it goes.

 

pBe

 

Wow I have just tested it and looks like it does exactly what I want! I will test it more over the next couple of days and let you know if I pick anything up. Just a minor thing that is my fault as I asked for it incorrectly but would it be possible to swap the Diameter & Length column?

Link to comment
Share on other sites

From

 

(setq data (cons (strcat (cadr gnv)
                        ","
                        (Car f)
                        ","
                        (itoa (cadr _nearest))
                        ","
                        (rtos (caadr f) 2 0))
                data))

 

To

(setq data (cons (strcat (cadr gnv)
                        ","
                        (Car f)
                        ","
                        [color="blue"](rtos (caadr f) 2 0)[/color]
                        ","
                       [color="blue"] (itoa (cadr _nearest))[/color])
                data))

 

CODE UPDATED at post # 62

Link to comment
Share on other sites

From

 

(setq data (cons (strcat (cadr gnv)
                        ","
                        (Car f)
                        ","
                        (itoa (cadr _nearest))
                        ","
                        (rtos (caadr f) 2 0))
                data))

 

To

(setq data (cons (strcat (cadr gnv)
                        ","
                        (Car f)
                        ","
                        [color="blue"](rtos (caadr f) 2 0)[/color]
                        ","
                       [color="blue"] (itoa (cadr _nearest))[/color])
                data))

 

CODE UPDATED at post # 62

 

Thank you so much, this will help me out loads! I have just tested this on a big network and couldn't find any faults so that's great.

Link to comment
Share on other sites

  • 3 weeks later...
You are welcome, Glad I was able to help.

 

Now go and practice some coding. :)

 

Hi pBe,

 

I have been using the code and it works brilliantly. I just have one query, is it possible for the code to still work if the polylines aren't touching at each end?

 

As there are some instances where the polylines aren't always touching so when this is the case the code misses the polyline/GN block/label.

 

Thanks

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