Author Topic: Help to code this lisp  (Read 2718 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Help to code this lisp
« on: November 27, 2012, 01:15:30 AM »
I want to code this lisp. So can some help me just the steps and commands i'll do the rest

The problem which I face is, In some drawings which come from the client, there are a lot of blocks.
I want to detect the blocks which has no sub-entities (but attribute) and add a rectangle inside this block, If it is an attribute add a line from 0,0, to insertpoint of attribute.

Thanks

kojacek

  • Mosquito
  • Posts: 14
Re: Help to code this lisp
« Reply #1 on: November 27, 2012, 04:33:54 AM »
Try it:
Function jk:BLK_GetInsertAtts gets a list of attributes in block reference, and
function cd:BLK_GetEntity gets objects in block definition.
The command for test named TEST-INS gets list of two lists: attribs enames, and list of other objects into block definition (but not ATTDEFs), for your selected block.
Sorry for my English ;)

Code - Auto/Visual Lisp: [Select]
  1. ; =========================================================================================== ;
  2. ; Lista obiektow w definicji bloku / List of objects in block definition                      ;
  3. ;  Name   [STR] - nazwa bloku / block name                                                    ;
  4. ;  Entity [STR] - nazwa entycji / entity name                                                 ;
  5. ; ------------------------------------------------------------------------------------------- ;
  6. ; (cd:BLK_GetEntity "*Model_space" nil), (cd:BLK_GetEntity "NAZWA" "*LINE")                   ;
  7. ; =========================================================================================== ;
  8. (defun cd:BLK_GetEntity (Name Entity / en dt res)
  9.   (setq en (tblobjname "BLOCK" Name))
  10.   (while
  11.     (and
  12.       en
  13.       (setq en (entnext en))
  14.       (setq dt (entget en))
  15.       (/= "ENDBLK" (cdr (assoc 0 dt)))
  16.     )
  17.     (if
  18.       (if Entity
  19.         (wcmatch (cdr (assoc 0 dt))(strcase Entity))
  20.         (cdr (assoc 0 dt))
  21.       )
  22.       (setq res
  23.         (cons
  24.           (cdr (assoc -1 dt))
  25.           res
  26.         )
  27.       )
  28.     )
  29.   )
  30.   (reverse res)
  31. )
  32. ; =========================================================================================== ;
  33. ; List of <ename> attribs in block reference
  34. ; =========================================================================================== ;
  35. (defun jk:BLK_GetInsertAtts (Ename / d a n o res)
  36.   (if
  37.     (and
  38.       (setq a (cdr (assoc 66 (setq d (entget Ename)))))
  39.       (not (zerop a))
  40.     )
  41.     (progn
  42.       (setq n T)
  43.       (while n
  44.         (setq Ename (entnext Ename)
  45.               d (entget Ename)
  46.               n (/= "SEQEND" (setq o (cdr (assoc 0 d))))
  47.         )
  48.         (if
  49.           (= o "ATTRIB")
  50.           (setq res (cons (cdr (assoc -1 d)) res))
  51.         )
  52.       )
  53.     )
  54.   )
  55.   (reverse res)
  56. )
  57. ; =========================================================================================== ;
  58. (defun C:TEST-INS (/ s d)
  59.   (if
  60.     (and
  61.       (setq s (entsel "\nSelect block: "))
  62.       (= "INSERT" (cdr (assoc 0 (setq d (entget (car s))))))
  63.     )
  64.     (list
  65.       (jk:BLK_GetInsertAtts (car s))
  66.       (vl-remove-if
  67.         '(lambda (%)
  68.           (= "ATTDEF" (cdr (assoc 0 (entget %))))
  69.         )
  70.         (cd:BLK_GetEntity (cdr (assoc 2 d)) nil)
  71.       )
  72.     )
  73.   )
  74. )



HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Help to code this lisp
« Reply #2 on: November 27, 2012, 06:35:20 AM »
Thanks kojacek for your quick reply
 
PLease find attached a file has sa sample of 2 blocks
one has no objects this blocks i want to draw a rectangle inside - (lets say 1000x1000)
another one has attribute only
 
If no objects draw a rectangle inside the block
if att only draw a line from 0,0 of the block to inspoint of any att inside the block too
 
Regards

kojacek

  • Mosquito
  • Posts: 14
Re: Help to code this lisp
« Reply #3 on: November 27, 2012, 06:57:59 AM »
Thanks kojacek for your quick reply
 
PLease find attached a file has sa sample of 2 blocks
one has no objects this blocks i want to draw a rectangle inside - (lets say 1000x1000)
another one has attribute only
 
If no objects draw a rectangle inside the block
if att only draw a line from 0,0 of the block to inspoint of any att inside the block too
 
Regards
For make rectangle in "empty" block:
Load this code:
Code - Auto/Visual Lisp: [Select]
  1. ; =========================================================================================== ;
  2. ; =========================================================================================== ;
  3. ; Tworzy obiekt typu LWPOLYLINE / Creates a LWPOLYLINE object                                 ;
  4. ;  Space  [VLA-Object] - kolekcja / collection | Model/Paper + Block Object                   ;
  5. ;  Pts    [LIST]  - lista wierzcholkow polilinii / list of polyline vertex                    ;
  6. ;  Closed [T/nil] - nil = otwarta / open                                                      ;
  7. ;                   T   = zamknieta / closed                                                  ;
  8. ; ------------------------------------------------------------------------------------------- ;
  9. ; (cd:ACX_AddLWPolyline (cd:ACX_ASpace) (list '(5 5) '(15 5) '(15 10) '(10 10)) nil)          ;
  10. ; =========================================================================================== ;
  11. (defun cd:ACX_AddLWPolyline (Space Pts Closed / obj)
  12.   (setq Pts
  13.     (apply
  14.       (quote append)
  15.       (mapcar
  16.         (function
  17.           (lambda (%)
  18.             (list (car %) (cadr %))
  19.           )
  20.         )
  21.         (mapcar
  22.           (function
  23.             (lambda (%)
  24.               (trans % 1 (trans '(0 0 1) 1 0 T))
  25.             )
  26.           )
  27.           Pts
  28.         )
  29.       )
  30.     )
  31.   )
  32.   (setq obj
  33.       (vlax-make-variant
  34.         (vlax-safearray-fill
  35.           (vlax-make-safearray vlax-vbdouble (cons 0 (1- (length Pts))))
  36.           Pts
  37.         )
  38.       )
  39.     )
  40.   )
  41.   (if Closed (vla-put-closed obj Closed))
  42.   obj
  43. )
and next:
Code - Auto/Visual Lisp: [Select]
  1. (vlax-for % *cd-Blocks*
  2.   (if
  3.     (not (cd:BLK_GetEntity (vla-get-Name %) nil))
  4.     (cd:ACX_AddLWPolyline
  5.       %
  6.       (list (list 0 0)(list 1000 0)(list 1000 1000)(list 0 1000))
  7.       T
  8.     )
  9.   )
  10. )
... and REGEN command...

kojacek

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help to code this lisp
« Reply #4 on: November 27, 2012, 07:15:31 AM »
Consider the following example:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:revealblocks ( / doc lst )
  2.         (if
  3.             (and
  4.                 (= :vlax-false (vla-get-islayout blk))
  5.                 (= :vlax-false (vla-get-isxref blk))
  6.                 (null (wcmatch (vla-get-name blk) "`**"))
  7.             )
  8.             (progn
  9.                 (setq lst nil)
  10.                 (vlax-for obj blk (setq lst (cons obj lst)))
  11.                 (cond
  12.                     (   (null lst)
  13.                         (vla-put-closed
  14.                             (vlax-invoke blk 'addlightweightpolyline
  15.                                '(0.0 0.0 1000.0 0.0 1000.0 1000.0 0.0 1000.0)
  16.                             )
  17.                             :vlax-true
  18.                         )
  19.                     )
  20.                     (   (vl-every '(lambda ( x ) (= "AcDbAttributeDefinition" (vla-get-objectname x))) lst)
  21.                         (foreach att lst
  22.                             (vlax-invoke blk 'addline '(0.0 0.0 0.0) (vlax-get att 'insertionpoint))
  23.                         )
  24.                     )
  25.                 )
  26.             )
  27.         )
  28.     )
  29.     (vla-regen doc acallviewports)
  30.     (princ)
  31. )

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Help to code this lisp
« Reply #5 on: November 27, 2012, 11:51:05 PM »
kojacek
Thnaks working perfect,

LEE
As usual, perfect, I faced the that case when the block is in  Xref I was searching to solve it.
but one thing after running the lisp and zoom Extents the block is out of view.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help to code this lisp
« Reply #6 on: November 28, 2012, 07:12:55 AM »
but one thing after running the lisp and zoom Extents the block is out of view.

Are all drawing layers thawed?

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Help to code this lisp
« Reply #7 on: November 28, 2012, 01:33:45 PM »
but one thing after running the lisp and zoom Extents the block is out of view.

Are all drawing layers thawed?

YES