Author Topic: Block trim and bounding box  (Read 8359 times)

0 Members and 1 Guest are viewing this topic.

jaydee

  • Guest
Block trim and bounding box
« on: July 29, 2011, 02:18:02 PM »
Apppologise, feel like i ask to much lately in this forum.

I  want to update a block trim lisp from MelFranks. Mels code works a treat in most cases, except it does not trim properly when attributes located outside the block entities, which from my understanding is that the way this boundingbox calculated is to include attributes as the extend of the block.
And then i read about this code from HighflyingBird link below
http://www.theswamp.org/index.php?topic=39013.new#new

I spend a night trying to figure out how to merge Mel's and HighflyingBird's codes so it could trim blocks with attributes (but exclude the attributes from boundingbox). My knowledge is limited and heading no where with these code.

If anyone able to help me to modify these codes so it could trim *LINE,ARC,CIRCLE over any blocks with or without attributes

Make sure to delete these code from HB to get the boundingbox of block entities only
Code: [Select]
     (if (= (cdr (assoc 66 dxf)) 1)
(setq BoxPts (AttBox ent BoxPts))
      )

Here MelFranks codes
Code: [Select]
(defun c:BLKTRIM ( / *error* trim-blk rotate-bbox sv-cmd
                                sv-osm acad-doc ss cnt)
 (defun *error* (msg)
  (vla-EndUndoMark acad-doc)
  (setvar 'cmdecho sv-cmd)
  (setvar 'osmode sv-osm)
 )

 (defun trim-blk (pts / ln-set ln-obj sp ep tmp int-lst ct)
  (if(setq ln-set(ssget "cp" pts '((0 . "*LINE,ARC"))))
   (progn
    (setq pts(reverse(cons(car pts)(reverse pts))))
    (repeat(setq ct(sslength ln-set))
     (setq ln-obj  (vlax-ename->vla-object
                    (ssname ln-set
                      (setq ct(1- ct))
                    )
                   )
           sp      (vlax-curve-getStartPoint ln-obj)
           ep      (vlax-curve-getEndPoint ln-obj)
           tmp      pts
           int-lst '()
     )
     (while(>(length tmp)1)
      (if(setq int(inters sp ep(car tmp)(cadr tmp)nil))
       (if(inters sp int (car tmp)(cadr tmp))
        (setq int-lst(cons int int-lst))
       )
      )
      (setq tmp(cdr tmp))
     )
     (if(=(length int-lst)2)
      (vl-cmdf "_.break"
               (list
                (vlax-vla-object->ename ln-obj)
                (car int-lst)
               )
               (cadr int-lst)
      )
     )
    )
   )
  )
 )

 (defun rotate-bbox (blk-obj / mspace blk-rot blk-pt blk-bbox
                                            p1 p2 p3 p4 x tmp)

  (setq mspace   (vla-get-modelspace acad-doc)
        blk-rot  (vlax-get-property blk-obj 'Rotation)
        blk-pt   (vlax-get-property blk-obj 'InsertionPoint)
  )
  (vlax-put-property blk-obj 'Rotation 0.0)
  (setq blk-bbox (vla-getBoundingBox blk-obj 'p1 'p3)
        p1       (vlax-safearray->list p1)
        p3       (vlax-safearray->list p3)
        p1       (list(car p1)(cadr p1))
        p3       (list(car p3)(cadr p3))
        p2       (list(car p1)(cadr p3))
        p4       (list(car p3)(cadr p1))
  )
  (vlax-put-property blk-obj 'Rotation blk-rot)
  (foreach x '(p1 p2 p3 p4)
   (vla-rotate
    (vla-addpoint mspace(vlax-3d-point(eval x)))
     blk-pt
     blk-rot
   )
   (set x(vlax-ename->vla-object(entlast)))
  )
  (foreach x '(p1 p2 p3 p4)
   (setq tmp(eval x))
   (set x
    (vlax-safearray->list
     (vlax-variant-value
      (vlax-get-property(eval x)'Coordinates)
     )
    )
   )
   (vla-delete tmp)
  )
  (list p1 p2 p3 p4)
 )
 
 (setq sv-cmd   (getvar "cmdecho")
       sv-osm   (getvar "osmode")
       acad-doc (vla-get-activedocument
                 (vlax-get-Acad-Object)
                )
 )
 (setvar 'cmdecho 0)
 (setvar 'osmode 0)
 (vla-StartUndoMark acad-doc)
 (if(setq ss(ssget '((0 . "INSERT"))))
  (repeat(setq cnt(sslength ss))
   (trim-blk
    (rotate-bbox
     (vlax-ename->vla-object
      (ssname ss
       (setq cnt(1- cnt))
      )
     )
    )
   )
  )
 )
 (vla-EndUndoMark acad-doc)
 (setvar 'cmdecho sv-cmd)
 (setvar 'osmode sv-osm)
 (princ)
)


Thankyou in advance verymuch appriciated



Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Block trim and bounding box
« Reply #1 on: July 29, 2011, 06:06:25 PM »
Does this help at all?

jaydee

  • Guest
Re: Block trim and bounding box
« Reply #2 on: July 30, 2011, 04:49:21 AM »
Thanks Lee

i already use your blocktrim routine, but not for the purpose that i want to trim pipes over sprinklers.
We often draw our sprinkler heads layout way before we draw connecting pipes to the sprinklers, and similarly we layout all the light fittings on the ceiling plan ahead of providing any cable circuits connecting the lights. Hense the block trim routine comes in handy.

The attach drawing file demonstrate what MelFranks codes able to trim and unable to trim blocks with attributes located outside the block.

Cheers. have a good wk

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #3 on: August 02, 2011, 04:05:56 AM »
jaydee,

Here's a solution, assuming I understand the question. Change the Invisible
property of the attributes to invisible -1. The attributes are ignored when
the bounding box of the block is calculated. Then change the Invisible
property back to zero.

Code: [Select]
;; Replace this:
 (if(setq ss(ssget '((0 . "INSERT"))))
  (repeat(setq cnt(sslength ss))
   (trim-blk
    (rotate-bbox
     (vlax-ename->vla-object
      (ssname ss
       (setq cnt(1- cnt))
      )
     )
    )
   )
  )
 )

;; With this:
 (if (setq ss (ssget '((0 . "INSERT"))))
   (foreach x (SSVLAList ss)
     (setq atts (vlax-invoke x 'GetAttributes))
     (foreach a atts
       (vlax-put a 'Invisible -1)
     )
     (trim-blk (rotate-bbox x))
     (foreach a atts
       (vlax-put a 'Invisible 0)
     )
   )
 )
 
;; Add this function
(defun SSVLAList (ss / obj lst i)
  (setq i 0)
  (if ss
    (repeat (sslength ss)
      (setq obj (vlax-ename->vla-object (ssname ss i))
            lst (cons obj lst)
            i (1+ i)
      )
    )
  )
  (reverse lst)
) ;end
« Last Edit: August 02, 2011, 04:16:10 AM by Joe Burke »

jaydee

  • Guest
Re: Block trim and bounding box
« Reply #4 on: August 02, 2011, 05:39:33 AM »
Thankyou Joe.

Its a great idea to put the attribute in the invisible mode.
Unfortunately this boundingbox method still sees the inv attributes and the bounding box still put the boundary over the invisible attributes.

I have try swap the boundingbox with this one from Lee Mac and HighflyingBird, link below
http://www.theswamp.org/index.php?topic=39013.0

Im still leaning the codes.

Thankyou for your help.

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #5 on: August 02, 2011, 07:01:08 AM »
jaydee,

I tested the revised code I posted using your example file. Using 2008, the attributes are ignored as I said, and a line passing through the selected blocks is correctly trimmed/broken at the bounding box of each block with attributes ignored.

Example file attached.

If you are not seeing the same result, I suspect you've not modified the BLKTRIM routine as needed.

Regards

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #6 on: August 02, 2011, 07:20:46 AM »
jaydee,

Complete revised BLKTRIM code attached for your review.
« Last Edit: August 02, 2011, 07:27:47 AM by Joe Burke »

jaydee

  • Guest
Re: Block trim and bounding box
« Reply #7 on: August 02, 2011, 08:37:07 PM »
Thankyou verymuch Joe
and appologise for the hassle.

Everything is perfect when i tested on 2011.
The one not working was tested on 2006(home), will test it agian on 2006 to confirm, could be fault on my behalf,
as I would not expect the boundingbox to be any different to version 2006 vs 2011.

Cheers.

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #8 on: August 03, 2011, 03:31:21 AM »
jaydee,

You're welcome. I'm glad to hear it's working.

BTW, no hassle. I probably should have posted the complete revised routine in the first place.

You should rename the routine if you want to post it elsewhere so it's clear it's not the same as the original. Also give credit to the original author in header comments.

Regards

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #9 on: August 03, 2011, 04:26:38 AM »
One other thought. The revised portion of the code was intended as proof-of-concept, rather than complete code.

What it should do is check the Invisible property of each attribute. If the property is 0, change it to -1 and add that attribute object to a list. Thn after the bounding box/break operation, step through the list changing the -1 property back to 0.

That way any attribute which was invisible before running the routine will not be modified. IOW, changed to visible when it shouldn't be.

jaydee

  • Guest
Re: Block trim and bounding box
« Reply #10 on: August 03, 2011, 05:26:29 AM »
Thanks Joe for the advice.
I tested it again on 2006 and its not working, as i noted before the bbox still covers the invisible attributes.
What is important is it runs on my current version 2011.

Joe Burke

  • Guest
Re: Block trim and bounding box
« Reply #11 on: August 03, 2011, 06:34:09 AM »
jaydee,

The fact the code does not work in 2006 is interesting. I don't have 2006 for testing.

Here's a routine which sets the Invisible property of the selected attribute to -1. Try it with your example file. When you pick an attribute it should disappear.

Code: [Select]
;; Change the selected attribute to invisible.
(defun c:InvAtt ( / e att obj)
  (if
    (and
      (setq e (car (nentsel "\nSelect attribute: ")))
      (eq "ATTRIB" (cdr (assoc 0 (entget e))))
      (setq obj (vlax-ename->vla-object e))
    )
    (vlax-put obj 'Invisible -1)
  )
  (princ)
) ; end

After running that code in 2006, select the block where the attribute was set invisible. Do the block handles appear to include the invisible attribute? When I do this in 2008 the block handles do not include the invisible attribute.

jaydee

  • Guest
Re: Block trim and bounding box
« Reply #12 on: August 03, 2011, 06:27:43 PM »
I think its just the way the boundingbox box apply to blocks with attributes in acad2006.
I purposely made some blocks with the attributes being invisible and run the your mod codes and the original codes, the result are the same in 2006.

Thankyou