Author Topic: Extract a block from a block to the same location  (Read 10939 times)

0 Members and 1 Guest are viewing this topic.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Extract a block from a block to the same location
« on: March 22, 2012, 02:40:06 AM »
hello everyone .

Is it possible to extract a block from a block and inserted in the same location  ?

Or insert the same block from a block on its location to allow me to copy and paste them in the same location into another drawing .

Thanks in advance for any input .

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #1 on: March 22, 2012, 07:45:30 AM »
Is it possible to extract a block from a block and inserted in the same location  ?

Are you looking to extract a nested block from a block?

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #2 on: March 22, 2012, 09:35:20 AM »
This was an interesting program to write; even if it doesn't suit your task, hopefully someone else will benefit from it  :-)

The program will 'extract' a nested block, converting it to a primary block in the same position, with the same scale and rotation as the selected nested reference.

This has similar functionality to NCOPY, however NCOPY will not work on nested blocks and furthermore this program will remove the nested block from the primary block definition.

Code - Auto/Visual Lisp: [Select]
  1. ;; Extract Nested Block  -  Lee Mac 2012
  2. ;; Converts a selected nested block into a primary block
  3.  
  4. (defun c:enb ( / doc lst sel )    
  5.    (while
  6.        (progn (setvar 'ERRNO 0) (setq sel (nentselp "\nSelect Nested Block: "))
  7.            (cond
  8.                (   (= 7 (getvar 'ERRNO))
  9.                    (princ "\nMissed, try again.")
  10.                )
  11.                (   (or (null sel) (cadr (cadddr sel)))
  12.                    nil
  13.                )
  14.                (   (princ "\nObject is not a Nested Block: "))
  15.            )
  16.        )
  17.    )
  18.    (if sel
  19.        (progn
  20.                  lst (mapcar 'vlax-ename->vla-object (last sel))
  21.            )
  22.            (vla-transformby
  23.                (vla-insertblock
  24.                    (if (vlax-method-applicable-p doc 'objectidtoobject32)
  25.                        (vla-objectidtoobject32 doc (vla-get-ownerid32 (last lst)))
  26.                        (vla-objectidtoobject   doc (vla-get-ownerid   (last lst)))
  27.                    )
  28.                    (vlax-3D-point '(0.0 0.0 0.0))
  29.                    (vla-get-name (car lst)) 1.0 1.0 1.0 0.0
  30.                )
  31.                (vlax-tmatrix (caddr sel))
  32.            )
  33.            (vla-delete (car lst))
  34.            (vla-regen doc acactiveviewport)
  35.        )
  36.    )
  37.    (princ)
  38. )

Example:

In this example, Block1 (green) is nested within Block2 (yellow), which is nested within Block3 (red). To make things a little more complex, at each level of the nesting, the references have been scaled and rotated.

« Last Edit: March 22, 2012, 05:57:13 PM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #3 on: March 22, 2012, 09:45:07 AM »
Here is another version of the above code; although not as concise, this version will retain all properties of the nested block when extracted:

Code - Auto/Visual Lisp: [Select]
  1. ;; Extract Nested Block  -  Lee Mac 2012
  2. ;; Converts a selected nested block into a primary block
  3.  
  4. (defun c:enb ( / doc lst obj sel )    
  5.     (while
  6.         (progn (setvar 'ERRNO 0) (setq sel (nentselp "\nSelect Nested Block: "))
  7.             (cond
  8.                 (   (= 7 (getvar 'ERRNO))
  9.                     (princ "\nMissed, try again.")
  10.                 )
  11.                 (   (or (null sel) (cadr (cadddr sel)))
  12.                     nil
  13.                 )
  14.                 (   (princ "\nObject is not a Nested Block: "))
  15.             )
  16.         )
  17.     )
  18.     (if sel
  19.         (progn
  20.                   lst (mapcar 'vlax-ename->vla-object (last sel))
  21.                   obj (car
  22.                           (vlax-invoke doc 'copyobjects (list (car lst))
  23.                               (if (vlax-method-applicable-p doc 'objectidtoobject32)
  24.                                   (vla-objectidtoobject32 doc (vla-get-ownerid32 (last lst)))
  25.                                   (vla-objectidtoobject   doc (vla-get-ownerid   (last lst)))
  26.                               )
  27.                           )
  28.                       )
  29.             )
  30.             (vla-put-insertionpoint obj (vlax-3D-point '(0.0 0.0 0.0)))
  31.             (vla-put-rotation obj 0.0)
  32.             (vla-put-xscalefactor obj 1.0)
  33.             (vla-put-yscalefactor obj 1.0)
  34.             (vla-put-zscalefactor obj 1.0)
  35.             (vla-put-normal obj (vlax-3D-point '(0.0 0.0 1.0)))
  36.             (vla-transformby obj (vlax-tmatrix (caddr sel)))
  37.             (vla-delete (car lst))
  38.             (vla-regen doc acactiveviewport)
  39.         )
  40.     )
  41.     (princ)
  42. )
« Last Edit: March 22, 2012, 05:57:29 PM by Lee Mac »

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #4 on: March 22, 2012, 12:03:30 PM »
Hi Lee .

Many thanks for your hard work on the thread .  :-)

Both routines gave me the same result (error) ;
Quote
Command: enb
Select Nested Block: ; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on unknown exception

My thoughts were to make a selection set of blocks and check every Block for a specific block name (one or two blocks) and insert
the same block(s) in the same location in Model space to be able later on to make another selection set of these blocks and move
them to another drawing with the same base point .

The hard part of the routine for me is that the translation of coordinates from Block Definition to model Space .

Thanks a lot


Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #5 on: March 22, 2012, 03:51:54 PM »
Both routines gave me the same result (error) ;
Quote
Command: enb
Select Nested Block: ; error: Exception occurred: 0xC0000005 (Access Violation)
; warning: unwind skipped on unknown exception

That's weird since I didn't get any errors when testing them before posting - are you maybe using dynamic blocks or have locked layers?

Anyone else getting errors?

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #6 on: March 22, 2012, 03:57:56 PM »
No , I am not using dynamic block and not on locked layer either , but maybe due to OS is 64 ? just a guess .
because the code stopped at the function vla-get-ownerid when I tried to check where the error took a place .

Thanks

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #7 on: March 22, 2012, 04:16:27 PM »
No , I am not using dynamic block and not on locked layer either , but maybe due to OS is 64 ?

Ah yes! That would explain it  :-)

I have modified the above posts with updated code, please give them a try  :-)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #8 on: March 22, 2012, 04:38:00 PM »
No , I am not using dynamic block and not on locked layer either , but maybe due to OS is 64 ?

Ah yes! That would explain it  :-)

I have modified the above posts with updated code, please give them a try  :-)

OMG , the same result  :oops:

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #9 on: March 22, 2012, 05:00:55 PM »
OMG , the same result  :oops:

Oops! I forgot the corresponding ObjectIDtoObject32 method - above posts updated again  :-)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #10 on: March 22, 2012, 05:09:02 PM »
I am very embarrassed with this error with my cad 2009 .
so I brought a copy of simple block if you interested to take a look .

Many thanks

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #11 on: March 22, 2012, 05:26:03 PM »
lol, don't be embarassed, its my code that failing  :lol:

Your example block works fine on my machine; do the above modified programs still error for you?

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #12 on: March 22, 2012, 05:48:13 PM »
Tried it , but the same . :cry:

Hope you take a look at the attached video and I attached it in winrar folder because I don't know how to convert it to .gif to show it
directly in the forum as well as you do .

Many thanks

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Extract a block from a block to the same location
« Reply #13 on: March 22, 2012, 06:00:03 PM »
My apologies for wasting your time Tharwat, I've just noticed my simple mistake: 'objectidtoobject32' is a method, not a property like 'ownerid32', so I should have used vlax-method-applicable-p over vlax-property-available-p. I think modified things a little too hastily earlier..

Please try the above modified code if you get a chance  :-)

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Extract a block from a block to the same location
« Reply #14 on: March 22, 2012, 06:08:59 PM »
My apologies for wasting your time Tharwat, I've just noticed my simple mistake: 'objectidtoobject32' is a method, not a property like 'ownerid32', so I should have used vlax-method-applicable-p over vlax-property-available-p. I think modified things a little too hastily earlier..

Please try the above modified code if you get a chance  :-)

Yeah ... now it is perfect  :-)

So can we make it with ssget function for multiple selection set and getting them all within one base point ?

Thank you so much .