@group

@group

john.uhden
Mentor Mentor
894 Views
6 Replies
Message 1 of 7

@group

john.uhden
Mentor
Mentor

I finally got around to it.  I used to use my old functions @cv_double_up and @cv_triple_up to turn flat lists as returned by (vlax-get plineobj 'coordinates) into a list of points.

 

But this is more compact and easier...

 

(defun @group (old n / item new)
  (while old
    (while (< (length item) n)
      (setq item (cons (car old) item) old (cdr old))
    )
    (setq new (cons (reverse item) new) item nil)
  )
  (reverse new)
)

which can be used as follows:

 

Command: (setq coords '(1 2 3 4 5 6 7 8 9)
Command: (@group coords 3)
((1 2 3) (4 5 6) (7 8 9))

or

Command: (setq coords '(1 2 3 4 5 6 7 8)
Command: (@group coords 2)
((1 2)(3 4)(5 6)(7 8))

Remember that lightweight polylines return just x and y,

but heavies and 3Ds return x, y, and z.

I don't remember what other objects have a coordinates property.

John F. Uhden

Accepted solutions (2)
895 Views
6 Replies
Replies (6)
Message 2 of 7

john.uhden
Mentor
Mentor

According to the 2002 help, the following objects have a coordinates property:

 

Coordinates property [ActiveX and VBA Reference: AAR]

3DFace, 3DPolyline , Leader, LightweightPolyline, MLine, Point, PolyfaceMesh, PolygonMesh, Polyline, Solid, Trace

 

It's up to you to determine if the coordinates are 2D or 3D,

John F. Uhden

0 Likes
Message 3 of 7

Kent1Cooper
Consultant
Consultant

So an interesting question arises in my mind, vis-a-vis generic use of something like this [that is, not always necessarily related to breaking coordinates lists into point lists, since it could be used on lists of anything, not just numbers -- maybe you'd want to divide a list of something like entity names into groups of, say, 6 or something]:  What should such a list-subdivider do when the group size doesn't divide equally into the length of the original list?  For instance, dividing your list of 9 numbers into groups of 4 with that routine fills out the end with nil's to make a last group that's 4 items long like the others:

 

Command: (setq test '(1 2 3 4 5 6 7 8 9))
(1 2 3 4 5 6 7 8 9)

 

Command: (@group test 4)
((1 2 3 4) (5 6 7 8) (9 nil nil nil))

 

I can imagine there could be circumstances in which one would want the last group left with just what's in the end of the original list, without paddng it out with nil's, like this:

((1 2 3 4) (5 6 7 8) (9))

 

so as to, for example, process that last list in some different way depending on how many items are in it, or maybe depending on simply whether or not it has the same number as the other lists.  That could certainly be done, if that kind of result would serve some purpose better.

Kent Cooper, AIA
0 Likes
Message 4 of 7

john.uhden
Mentor
Mentor

I was expecting you you figure that out.  Thank you for even reading, let alone responding.

 

That's what I meant by indicating that the user should know what with which he/she is dealing, generically speaking of course.

 

Yes, of course it could be modified to eliminate nils.  Anyone may do that if desired; it's public domain.

John F. Uhden

0 Likes
Message 5 of 7

SeeMSixty7
Advisor
Advisor

@john.uhden

 

This is not as compact as your original code, but does clean up the last part of the list when someone does send extra data. Keep in mind they would need to be aware of the side affect when calling the code, so they would know how to handle the extra data element that does not conform to the pattern.

 

(defun @group (old n / item new extra)
	(setq old (reverse old)
		  extra (list)
	)
	(repeat (rem (length old) n)
		(setq extra (append extra (list (car old)))
			  old (cdr old)
		)
	)
	(setq old (reverse old))
   (while old
     (while (< (length item) n)
       (setq item (cons (car old) item) old (cdr old))
     )
     (setq new (cons (reverse item) new) item nil)
   )
   (if extra
      (setq new (append (reverse new) (list extra)))
      (setq new (reverse new))
   )
)
0 Likes
Message 6 of 7

Ranjit_Singh
Advisor
Advisor
Accepted solution

Here is another way to modify john's code to remove the unnecessary nil

(defun @group (old n / item new)
  (while old
    (while (< (length item) n)
      (setq item (cons (car old) item) old (cdr old))
    )
    (setq new (cons (vl-remove nil (reverse item)) new) item nil)
  )
  (reverse new)
)
Message 7 of 7

SeeMSixty7
Advisor
Advisor
Accepted solution

Nice!

0 Likes