Jump to content

Recommended Posts

Posted

Hi Guys,

 

So I've written a few functions to count all the blocks within a selected polyline, it is working but the I feel there is a much better way to handle the list processing. I'm busy looking into mapcar and lambda functions, but I'm struggling to apply it.

 

What do you guys think?

 

Have a look:

;; Test function

(defun c:test (/ ss bb plist) 
  (if (setq ss (ssget "_+.:S:E:L" '((0 . "*LINE,CIRCLE,ARC,ELLIPSE"))))
    (progn
        (setq bb (ssname ss 0))
        (setq plist (_coordlist bb))
        (_boq plist))))

;; Pline to coordinates

(defun _coordlist (ent / plobj coords num pt ptlist)
    (setq
        plobj (vlax-ename->vla-object ent)
        coords (vlax-get plobj 'Coordinates))
    (setq num (if (= (cdr (assoc 0 (entget ent))) "LWPOLYLINE") 2 3))
    (repeat (/ (length coords) num)
        (repeat num 
        (setq pt (append pt (list (car coords)))
              coords (cdr coords)))
        (setq ptlist (cons pt ptlist)
              pt nil))
    (setq ptlist (reverse ptlist)))

;; Generates output by matching two lists up

(defun _boq (plist / blist clist bline dataext)
  (setq blist (_blklist plist))
  (setq clist (_blkcount blist plist))
  (repeat (length blist)
    (setq bline (list (car blist) (car clist)))
    (setq dataext (cons bline dataext))
    (setq blist (cdr blist))
    (setq clist (cdr clist)))
  dataext)

;; Makes a list of block numbers based on block names

(defun _blkcount ( blist plist / clist ss pblist)
  (setq pblist blist)
  (repeat (length blist)
   (if (setq ss (ssget "_WP" plist (list '(0 . "INSERT")(cons 2 (car blist)))))
    (progn
      (setq clist (cons (sslength ss) clist))
      (setq blist (cdr blist)))))
  (setq clist (reverse clist)))

;; Makes a list of block names

(defun _blklist (plist / ss ctr ent lst)
  (setq ss (ssget "_WP" plist '((0 . "INSERT"))))
  (setq ctr 0)
  (repeat (sslength ss)
    (setq ent (ssname ss ctr))
    (setq lst (cons (cdr (assoc 2 (tblsearch "BLOCK" (cdr (assoc 2 (entget ent)))))) lst))
    (setq ctr (1+ ctr)))
  (setq lst (remove-nil (remove_doubles lst))))

;; Remove duplicates and nil values

(defun remove_doubles (lst)
  (if lst
    (cons (car lst) (remove_doubles (vl-remove (car lst) (cdr lst))))))

(defun remove-nil ( lst ) (vl-remove nil lst))

 

Posted

I have changed from use vl-coordinates this is shorter.

 

(setq plent (entsel "\nPick rectang"))
(if plent (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))))
(princ co-ord)

 

  • Thanks 1
Posted
On 4/23/2021 at 11:51 PM, BIGAL said:

I have changed from use vl-coordinates this is shorter.

 


(setq plent (entsel "\nPick rectang"))
(if plent (setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent))))))
(princ co-ord)

 

Wow this works perfectly! Why did change from vl-coordinates, is it because this method is shorter?

  • 1 year later...
Posted

😜 Hello all,

 

there is another issue with polylines:

- polylines with double vertices and

-self-intersecting/crossing polylines

 

will cause ssget "C/WP" to fail.

 

BTW, you can check if the polyline has arc/bulges.

If so, there are some routines around here that subsitute the args with a series of short line.segments.

The more line-segments, the better the cnverson works.

 

regards

Wolfgang

  • Like 1
Posted

_WP will fail if there are too many points as well.

  • Like 1
Posted

.... And if the selected polyline is off screen along with any of the targeted objects. ;) 

  • Like 1
Posted

Thanks for all the feedback! 

 

I was trying to use this on a much bigger scale and I hit most of these issues, a rethink and rewrite is in order!

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