Author Topic: how to get insert point of block based on a number associated with it  (Read 2629 times)

0 Members and 1 Guest are viewing this topic.

andrew_nao

  • Guest
attached is a sample BOM and a pic if you dont want to download the sample.

im attempting to get the insert point of the bmline (name of the block) based on the item number associated with it.

for example: when prompted, i type in "item 1" it would give me the insert point of the bmline that is associated with item #1 so i can do my thing.

i started to use following code, but it only gives me the insert point of the last block inserted


Code: [Select]
  (setq elist
(entget
   (ssname (ssget "x" (list '(0 . "INSERT") '(2 . "bmline*")))
   0
   )
)
  )
  (setq ip (cdr (assoc 10 elist)))

maybe count all the blocks and count backwards, to get to the bmline i need?

appreciate any guidance

edit: the insert point would be the top right corner of the block (bmline)

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: how to get insert point of block based on a number associated with it
« Reply #1 on: September 23, 2014, 01:45:24 PM »
It's a shame that the data is not in the form of attributes - the task would otherwise be very simple  :|

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: how to get insert point of block based on a number associated with it
« Reply #2 on: September 23, 2014, 02:48:20 PM »
Here is my attempt:
Code: [Select]
(defun getbmiinsertion ( a / e i l m p q r s )
    (if (setq s
            (ssget "_X"
               '(   (-4 . "<OR")
                        (-4 . "<AND")
                            (0 . "TEXT")
                            (1 . "~*[~0-9]*")
                            (72 . 1)
                            (73 . 0)
                        (-4 . "AND>")
                        (-4 . "<AND")
                            (0 . "INSERT")
                            (2 . "BMLINE")
                        (-4 . "AND>")
                    (-4 . "OR>")
                )
            )
        )
        (progn
            (repeat (setq i (sslength s))
                (setq e (entget (ssname s (setq i (1- i)))))
                (if (= "INSERT" (cdr (assoc 0 e)))
                    (setq l (cons (list (cdr (assoc 10 e)) (cdr (assoc 5 e))) l))
                    (setq m (cons (list (cdr (assoc 11 e)) (cdr (assoc 1 e))) m))
                )
            )
            (foreach x m
                (if (vl-some '(lambda ( y ) (if (apply 'and (mapcar '<= (mapcar '- (car y) '(33.0 1.5)) (car x) (car y))) (setq p y))) l)
                    (if (setq q (assoc (cadr p) r))
                        (if (< (caar x) (caaddr q))
                            (setq r (subst (append (reverse p) x) q r))
                        )
                        (setq r (cons (append (reverse p) x) r))
                    )
                )
            )
            (caddr (assoc a (mapcar 'reverse r)))
        )
    )
)

Call with the item number, e.g.:
Code: [Select]
_$ (getbmiinsertion "2")
(98.625 63.5462 0.0)

ronjonp

  • Needs a day job
  • Posts: 7531
Re: how to get insert point of block based on a number associated with it
« Reply #3 on: September 23, 2014, 03:58:12 PM »
It's a shame that the data is not in the form of attributes - the task would otherwise be very simple  :|
Agreed.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: how to get insert point of block based on a number associated with it
« Reply #4 on: September 24, 2014, 08:27:18 AM »
My 5 cent
Converted to blocks

RC

  • Guest
Re: how to get insert point of block based on a number associated with it
« Reply #5 on: September 24, 2014, 01:41:14 PM »
It's a shame that the data is not in the form of attributes - the task would otherwise be very simple  :|
Agreed.
Or embedded in xdata related to component data

andrew_nao

  • Guest
Re: how to get insert point of block based on a number associated with it
« Reply #6 on: September 24, 2014, 03:38:18 PM »
Here is my attempt:
Code: [Select]
(defun getbmiinsertion ( a / e i l m p q r s )
    (if (setq s
            (ssget "_X"
               '(   (-4 . "<OR")
                        (-4 . "<AND")
                            (0 . "TEXT")
                            (1 . "~*[~0-9]*")
                            (72 . 1)
                            (73 . 0)
                        (-4 . "AND>")
                        (-4 . "<AND")
                            (0 . "INSERT")
                            (2 . "BMLINE")
                        (-4 . "AND>")
                    (-4 . "OR>")
                )
            )
        )
        (progn
            (repeat (setq i (sslength s))
                (setq e (entget (ssname s (setq i (1- i)))))
                (if (= "INSERT" (cdr (assoc 0 e)))
                    (setq l (cons (list (cdr (assoc 10 e)) (cdr (assoc 5 e))) l))
                    (setq m (cons (list (cdr (assoc 11 e)) (cdr (assoc 1 e))) m))
                )
            )
            (foreach x m
                (if (vl-some '(lambda ( y ) (if (apply 'and (mapcar '<= (mapcar '- (car y) '(33.0 1.5)) (car x) (car y))) (setq p y))) l)
                    (if (setq q (assoc (cadr p) r))
                        (if (< (caar x) (caaddr q))
                            (setq r (subst (append (reverse p) x) q r))
                        )
                        (setq r (cons (append (reverse p) x) r))
                    )
                )
            )
            (caddr (assoc a (mapcar 'reverse r)))
        )
    )
)

Call with the item number, e.g.:
Code: [Select]
_$ (getbmiinsertion "2")
(98.625 63.5462 0.0)

works a charm. thank you

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: how to get insert point of block based on a number associated with it
« Reply #7 on: September 24, 2014, 04:36:44 PM »
You're welcome  8-)