TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on November 29, 2010, 06:08:24 AM

Title: Codes for adding an Object to a Block
Post by: Coder 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.
Title: Re: Codes for adding an Object to a Block
Post by: gile 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.
)
Title: Re: Codes for adding an Object to a Block
Post by: kpblc 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
Title: Re: Codes for adding an Object to a Block
Post by: Joe Burke on November 29, 2010, 06:25:04 AM
The other option is "bedit," if you are using a version which supports that command.
Title: Re: Codes for adding an Object to a Block
Post by: Coder 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.
Title: Re: Codes for adding an Object to a Block
Post by: kpblc on November 29, 2010, 06:41:32 AM
Check an Original property of block definition
Title: Re: Codes for adding an Object to a Block
Post by: HasanCAD on November 29, 2010, 06:56:49 AM
Try this

http://lee-mac.com/addobjectstoblock.html
Title: Re: Codes for adding an Object to a Block
Post by: Coder 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
Title: Re: Codes for adding an Object to a Block
Post by: kpblc 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)
Title: Re: Codes for adding an Object to a Block
Post by: Coder 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.
Title: Re: Codes for adding an Object to a Block
Post by: kpblc 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 :)
Title: Re: Codes for adding an Object to a Block
Post by: Coder 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.
Title: Re: Codes for adding an Object to a Block
Post by: Lee Mac 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)  :-)
Title: Re: Codes for adding an Object to a Block
Post by: HasanCAD on November 29, 2010, 12:14:19 PM
...
(Thanks Hasan)  :-)
:wink:
Title: Re: Codes for adding an Object to a Block
Post by: Coder 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
Title: Re: Codes for adding an Object to a Block
Post by: Lee Mac on November 29, 2010, 01:04:59 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

There isn't just one line of code to add objects to the block - else I would have just used one line myself. The objects need to be properly transformed before adding them to the block definition, the methods I use (CopyObjects) have already been discussed in this thead.
Title: Re: Codes for adding an Object to a Block
Post by: Coder on November 29, 2010, 01:20:51 PM

The objects needs to be properly transformed before adding them to the block definition.

Yeah... what is that transform ( from and where to  :-) ) ?

Thanks
Title: Re: Codes for adding an Object to a Block
Post by: Lee Mac on November 29, 2010, 01:26:15 PM

The objects needs to be properly transformed before adding them to the block definition.

Yeah... what is that transform ( from and where to  :-) ) ?

A translation from the object position relative to the selected block in Model/Paperspace, and rotation/scaling to account for block scale & rotation, to the Block Definition Geometry.
Title: Re: Codes for adding an Object to a Block
Post by: Coder on November 29, 2010, 01:34:15 PM
The objects needs to be properly transformed before adding them to the block definition.
Yeah... what is that transform ( from and where to  :-) ) ?

A translation from the object position relative to the selected block in Model/Paperspace, and rotation/scaling to account for block scale & rotation, to the Block Definition Geometry.

Exactly and defintely .  :wink:

That's what I would like to know and that's why I started my thread for.

Hope you show me these codes ?
if you do not mind of course .

Appreciated.

Title: Re: Codes for adding an Object to a Block
Post by: Lee Mac on November 29, 2010, 02:16:34 PM
The objects needs to be properly transformed before adding them to the block definition.
Yeah... what is that transform ( from and where to  :-) ) ?

A translation from the object position relative to the selected block in Model/Paperspace, and rotation/scaling to account for block scale & rotation, to the Block Definition Geometry.

Exactly and defintely .  :wink:

That's what I would like to know and that's why I started my thread for.

Hope you show me these codes ?
if you do not mind of course .

Appreciated.



I have already shown you the code - it is downloadable from the link provided.