Jump to content

Lisp for total length of multilines with different scales


mohamedmousad

Recommended Posts

Hello everyone,

 

I am searching for lisp that calculates the total length of multilines with different scales, as I am working in the field of fire fighting and I use the multiline command to draw the pipes, then I change the scale of multiline to change the pipe size, so I need the lisp to calculate the total length of each scale separately.

 

The link below includes a video illustrates what I mean exactly.

 

Thanks a lot.

Link to comment
Share on other sites

Try this

 

 

(defun c:mllength (/ MCNT MLINE MLINES PNTS TOTAL VCNT)

;;function to extract multiple pairs of the same dxf number from one list
(defun massoc (key alist / x nlist)
(foreach x alist
(if (eq key (car x))
(setq nlist (cons (cdr x) nlist))
)
)
(reverse nlist)
)

(setq mlines (ssget '((0 . "mline"))) ; grab mlines
mcnt 0 ;set mline counter
total 0 ;set total to zero
)
(repeat (sslength mlines)
(setq mline (entget (ssname mlines mcnt)) ;get mline entdata
pnts (massoc 11 mline) ; extract all the vertex codes
mcnt (1+ mcnt) ; increment mline counter
vcnt 0 ; set vertex counter
)
;; repeat for each vertex less the last one
(repeat (1- (length pnts))

(setq total (+ total
;; get distance from current vertex to next one
(distance (nth vcnt pnts)
(nth (setq vcnt (1+ vcnt)) pnts)
)
)
)
)
)
(alert (strcat (itoa (sslength mlines))
" mlines total length = "
(rtos total)
)
)
(princ)

)

Link to comment
Share on other sites

dear msirois,

 

Thanks for your reply and support.

 

Actually the code is working properly for calculating multilines lengths with different scales together.

 

What I need is to calculate the length for each scale separately like below:

scale 1= 150 mm

scale 1.25= 220 mm

scale 2= 500 mm

 

I hope it is available and I really appreciate your support.

 

Thanks a lot

Link to comment
Share on other sites

dear msirois,

 

Thanks for your reply and support.

 

Actually the code is working properly for calculating multilines lengths with different scales together.

 

What I need is to calculate the length for each scale separately like below:

scale 1= 150 mm

scale 1.25= 220 mm

scale 2= 500 mm

 

I hope it is available and I really appreciate your support.

 

Thanks a lot

 

I'm not sure how changing the scale of your line in regards to the pipe width affects your pipe length, but I'll assume here that you're using scale to reference the size in which the actual drawing is being drawn at. This number might need to be change, but essentially I'm multiplying your overall length by 1/SCALE. So if your scale was 2, it would be 1/2 = 0.5. So a 10ft pipe would should up a size of 5ft once you run the following:

 

 

(defun c:mlG (/ MCNT MLINE MLINES PNTS TOTAL VCNT S1 SC)

;;function to extract multiple pairs of the same dxf number from one list
(defun massoc (key alist / x nlist)
(foreach x alist
(if (eq key (car x))
(setq nlist (cons (cdr x) nlist))
)
)
(reverse nlist)
)
(setq mlines (ssget '((0 . "mline"))))
(setq S1 (getdist "\nWhat is object scale?: ")
 SC (/ 1 S1)
)
(setq

mcnt 0 ;set mline counter
total 0 ;set total to zero
)
(repeat (sslength mlines)
(setq mline (entget (ssname mlines mcnt)) ;get mline entdata
pnts (massoc 11 mline) ; extract all the vertex codes
mcnt (1+ mcnt) ; increment mline counter
vcnt 0 ; set vertex counter
)
;; repeat for each vertex less the last one
(repeat (1- (length pnts))

(setq total(+ total
;; get distance from current vertex to next one
(distance (nth vcnt pnts)
(nth (setq vcnt (1+ vcnt)) pnts)
)
)
)
)
)
(alert (strcat (itoa (sslength mlines))
" mlines total length = "
(rtos (* total SC))
)
)
(princ)

)

 

Keep in mind you have to manually input the scale of the MLINE, which I could not figure out how to do... so maybe someone else can help you with that.

Link to comment
Share on other sites

Actually It is not exactly what I need, you can get what I mean in the video below:

 

Anyway, thanks a lot, it was nice to contact with you.

 

I'm not sure how changing the scale of your line in regards to the pipe width affects your pipe length, but I'll assume here that you're using scale to reference the size in which the actual drawing is being drawn at. This number might need to be change, but essentially I'm multiplying your overall length by 1/SCALE. So if your scale was 2, it would be 1/2 = 0.5. So a 10ft pipe would should up a size of 5ft once you run the following:

 

 

(defun c:mlG (/ MCNT MLINE MLINES PNTS TOTAL VCNT S1 SC)

;;function to extract multiple pairs of the same dxf number from one list
(defun massoc (key alist / x nlist)
(foreach x alist
(if (eq key (car x))
(setq nlist (cons (cdr x) nlist))
)
)
(reverse nlist)
)
(setq mlines (ssget '((0 . "mline"))))
(setq S1 (getdist "\nWhat is object scale?: ")
 SC (/ 1 S1)
)
(setq

mcnt 0 ;set mline counter
total 0 ;set total to zero
)
(repeat (sslength mlines)
(setq mline (entget (ssname mlines mcnt)) ;get mline entdata
pnts (massoc 11 mline) ; extract all the vertex codes
mcnt (1+ mcnt) ; increment mline counter
vcnt 0 ; set vertex counter
)
;; repeat for each vertex less the last one
(repeat (1- (length pnts))

(setq total(+ total
;; get distance from current vertex to next one
(distance (nth vcnt pnts)
(nth (setq vcnt (1+ vcnt)) pnts)
)
)
)
)
)
(alert (strcat (itoa (sslength mlines))
" mlines total length = "
(rtos (* total SC))
)
)
(princ)

)

 

Keep in mind you have to manually input the scale of the MLINE, which I could not figure out how to do... so maybe someone else can help you with that.

Link to comment
Share on other sites

Hi,

 

I see that you already have the program so what is the need of the rewriting the same program once again?

 

Actually, the video is not mine, I got it form youtube, and I don't have the lisp or its code

Link to comment
Share on other sites

  • 4 months later...
Actually, the video is not mine, I got it form youtube, and I don't have the lisp or its code

 

 

I was needing a similar routine and found it here thanks to msirois

 

I was modifying it in order to work with closed mlines and separating according to mline scale.

 

So far I can just print results from alist in command line, I would like if someone can help to print the results in an alert box.

 

;From de original routine publish by msirois in this thread http://www.cadtutor.net/forum/archive/index.php/t-95773.html?

;23rd Feb 2016, 03:30 pm

;I modifided to identify if is a closed mline so can add the distance between the last and the first point of every mline

;And the final modification was that instead making a total sum I used an association list to make sums according to scale of mlines.

;I used the tutorial by Lee Mac http://www.cadtutor.net/forum/showthread.php?95031-Building-Association-Lists-A-Simple-Block-Counter-AutoLISP-Tutorial

;24th Dec 2015 08:17 pm to learn how to make association list.

 

 

 

(defun c:mll2 (/ MCNT MLINE MLINES PNTS TOTAL VCNT n itm lstesc esc)
;;function to extract multiple pairs of the same dxf number from one list 
(defun massoc (key alist / x nlist)
	(foreach x alist
		(if 
			(eq key (car x))
			(setq nlist (cons (cdr x) nlist))
		)
	)
	(reverse nlist)
)
(setq mlines (ssget '((0 . "mline"))) ; grab mlines
	mcnt 0 ;set mline counter
)
(if
	mlines 
	(repeat	(setq n
			(sslength mlines)
			)
		(setq mline (entget (ssname mlines mcnt)) ;get mline entdata
			pnts (massoc 11 mline) ; extract all the vertex codes
			mcnt (1+ mcnt) ; increment mline counter
			vcnt 0 ; set vertex counter
			total 0 ; set total value
		)
		;;repeat for each vertex less the last one
		(repeat (1- (length pnts)) 
			(setq total (+ total (distance (nth vcnt pnts) (nth (setq vcnt (1+ vcnt)) pnts))))  ;; get distance from current vertex to next one
		)	
		;; Here is the modification to detect if the mline is a closed one, and adds the distance between the last and the first vertex
				(IF (OR (= (CDR (ASSOC 71 MLINE)) 2)
				(= (CDR (ASSOC 71 MLINE)) 3)
				(= (CDR (ASSOC 71 MLINE)) 6)
				(= (CDR (ASSOC 71 MLINE)) 7)
				(= (CDR (ASSOC 71 MLINE)) 10)
				(= (CDR (ASSOC 71 MLINE)) 11)
				(= (CDR (ASSOC 71 MLINE)) 14)
				(= (CDR (ASSOC 71 MLINE)) 15)
			)
			(setq total (+ total (distance (nth (1- (length pnts)) pnts) (nth 0 pnts))))
		)
		;; Here is the construction of the assocciation list according to the scale and the length of each mline.
		(setq	esc 
				(cdr
					(assoc 40 mline)
				)
		)
			(if
				(setq itm
					(assoc esc lstesc)
				)
				;then
				(setq lstesc
					(subst
						(cons esc (+ total (cdr itm))) 
						itm
						lstesc
					)
				)
				;else
				(setq lstesc
					(cons 
						(cons esc total)
						lstesc
					)
				)
			)
	)
)
;printing results
(foreach itm lstesc
	(princ (strcat
				"\n"
				"Cable trays of "
				(rtos (car itm))
				" cms width have a total length of "
				(rtos (cdr itm))
				" cms"
			)
	)
)
(princ)
)

Edited by SLW210
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...