Author Topic: Codes for adding an Object to a Block  (Read 5556 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
Codes for adding an Object to a Block
« on: November 29, 2010, 06:08:24 AM »
Hello everybody . :-)

Hope that someone could tell me what are the codes for inserting an object to a block.

I am not looking for a complete routine.


Thanks a lot Guys.

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: Codes for adding an Object to a Block
« Reply #1 on: November 29, 2010, 06:14:19 AM »
Hi,

Just consider you block definition as the ModelSpace:

Code: [Select]
(vla-AddCircle
  (vla-Item (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-acad-object))) "BlockName")
  (vlax-3d-point '(0. 0. 0.))
  10.
)
Speaking English as a French Frog

kpblc

  • Bull Frog
  • Posts: 396
Re: Codes for adding an Object to a Block
« Reply #2 on: November 29, 2010, 06:17:37 AM »
Do you like create object in block definition or copy an object to block definition?
If you have block name:
Code: [Select]
; Get a pointer to block definition by block name
(setq blk-def (vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))))
; Add a new AcDbLine object to block definition
(vla-addline blk-def (vlax-3d-point '(0. 0. 0.)) (vlax-3d-point '(10. 10. 10.)))
If you want to copy object, use vla-CopyObjects method
Sorry for my English.

Joe Burke

  • Guest
Re: Codes for adding an Object to a Block
« Reply #3 on: November 29, 2010, 06:25:04 AM »
The other option is "bedit," if you are using a version which supports that command.

Coder

  • Swamp Rat
  • Posts: 827
Re: Codes for adding an Object to a Block
« Reply #4 on: November 29, 2010, 06:35:03 AM »
Great codes GUYs.

But what about specifying a point at the block and selecting an entity to be inserted to added to it ?  :-)

Many thanks for all.

kpblc

  • Bull Frog
  • Posts: 396
Re: Codes for adding an Object to a Block
« Reply #5 on: November 29, 2010, 06:41:32 AM »
Check an Original property of block definition
Sorry for my English.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Codes for adding an Object to a Block
« Reply #6 on: November 29, 2010, 06:56:49 AM »

Coder

  • Swamp Rat
  • Posts: 827
Re: Codes for adding an Object to a Block
« Reply #7 on: November 29, 2010, 07:12:31 AM »
Check an Original property of block definition

Yeah .... This is the main issue of the thread . So how to dig the BD and touring inside ? :-)

Thanks

kpblc

  • Bull Frog
  • Posts: 396
Re: Codes for adding an Object to a Block
« Reply #8 on: November 29, 2010, 07:23:28 AM »
First of all:
Code: [Select]
(setq
  blk_def (vla-item
            (vla-get-blocks
              (vla-get-activedocument (vlax-get-acad-object))
              ) ;_ end of vla-get-blocks
            ((lambda (/ ent)
               (setq
                 ent (vlax-ename->vla-object
                       (car (entsel "\nSelect a block reference : "))
                       ) ;_ end of vlax-ename->vla-object
                 ) ;_ end of setq
               (if (vlax-property-available-p ent 'effectivename)
                 (vla-get-effectivename ent)
                 (vla-get-name ent)
                 ) ;_ end of if
               ) ;_ end of lambda
             )
            ) ;_ end of vla-item
  ) ;_ end of setq
And then:
Code: [Select]
(vlax-dump-object blk_def t)
Sorry for my English.

Coder

  • Swamp Rat
  • Posts: 827
Re: Codes for adding an Object to a Block
« Reply #9 on: November 29, 2010, 07:46:30 AM »
So nice .

My main problem is that how to insert the selected entity to the Block at the same location that it is existed in dwg.

Thanks  a lot.

kpblc

  • Bull Frog
  • Posts: 396
Re: Codes for adding an Object to a Block
« Reply #10 on: November 29, 2010, 08:10:10 AM »
You can use CopyObjects method:
Code: [Select]
(vl-load-com)

(defun copy-objects-to-block-definition (block-name obj)
                                        ;|
*    Copy objects (obj) to a block definition (block-name)
*    Call parameters:
block-name block name
obj vla-pointer to object added to block definition
|;
  (cond
    ((and obj (listp obj))
     (foreach item obj
       (copy-objects-to-block-definition block-name item)
       ) ;_ end of foreach
     )
    ((= (type obj) 'ename)
     (copy-objects-to-block-definition
       block-name
       (vlax-ename->vla-object item)
       ) ;_ end of copy-objects-to-block-definition
     )
    ((= (type obj) 'vla-object)
     (vla-copyobjects
       (vla-get-activedocument (vlax-get-acad-object))
       (vlax-make-variant
         (vlax-safearray-fill
           (vlax-make-safearray vlax-vbobject '(0 . 0))
           (list obj)
           ) ;_ end of vlax-safearray-fill
         ) ;_ end of vlax-make-variant
       (vla-item (vla-get-blocks
                   (vla-get-activedocument (vlax-get-acad-object))
                   ) ;_ end of vla-get-blocks
                 block-name
                 ) ;_ end of vla-item
       ) ;_ end of vla-copyobjects
     )
    ) ;_ end of cond
  ) ;_ end of defun
But this code could return unexpected result :)
Sorry for my English.

Coder

  • Swamp Rat
  • Posts: 827
Re: Codes for adding an Object to a Block
« Reply #11 on: November 29, 2010, 10:44:28 AM »
Thanks a lot.

I am still in need to know how to insert an object to a block by codes ?

Regards.

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Codes for adding an Object to a Block
« Reply #12 on: November 29, 2010, 11:44:32 AM »
My main problem is that how to insert the selected entity to the Block at the same location that it is existed in dwg.

http://lee-mac.com/addobjectstoblock.html

^^ Should do what you want

(Thanks Hasan)  :-)

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Codes for adding an Object to a Block
« Reply #13 on: November 29, 2010, 12:14:19 PM »

Coder

  • Swamp Rat
  • Posts: 827
Re: Codes for adding an Object to a Block
« Reply #14 on: November 29, 2010, 12:59:27 PM »
My main problem is that how to insert the selected entity to the Block at the same location that it is existed in dwg.

^^ Should do what you want

Hi Lee.

Your routine is doing very well , but in your routine you used lots of subentities that it needs a long time to make things clear to me to understand.
And that's why I did mention in the begining I am not looking for a complete routine.

So could you please enlighten at the codes that cover my needs over the thread ?

Thanks