Jump to content

Recommended Posts

Posted
Hi guys,

I have a block reference which has tags and their values and a location symbol, that is, a circle symbol, two texts, one is steel_1 (tag is material) and the other one is 3.25m (tag is depth). I want to select those blocks which have "steel_1" text and change to "steel_2" with lisp routine.
it seems we can't access the components of block references with Autolisp.
Maybe we can do this in other way, however I prefer lisp routine.
Any help or idea, lots of thanks!
San

Posted

You can access the block attributes using either Vanilla or Visual LISP, perhaps these functions will get you started:

 

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

 

If the 'components' that you describe are Text Objects and not Attributes, you will instead need to iterate through the block definition.

 

Lee

Posted
You can access the block attributes using either Vanilla or Visual LISP, perhaps these functions will get you started:

 

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

 

If the 'components' that you describe are Text Objects and not Attributes, you will instead need to iterate through the block definition.

 

Lee

 

Thank you very much for the reply.

I will start with the website. Do you have any clue to iterate through the block definition? or any directions to guide my study?

Thanks again,

 

San

Posted

Are you looking to modify text within a block, or attributes?

Posted
Are you looking to modify text within a block, or attributes?

 

to modify text within a block.

Thank you.

 

San

Posted

Consider the following function to iterate through the objects in the block definition and apply an arbitrary function to each object in turn:

 

[color=GREEN];;---------------=={ Apply to Block Objects }==---------------;;[/color]
[color=GREEN];;                                                            ;;[/color]
[color=GREEN];;  Applies a supplied function to all objects in a block     ;;[/color]
[color=GREEN];;  definition.                                               ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Arguments:                                                ;;[/color]
[color=GREEN];;  _acblocks  - block collection in which block resides      ;;[/color]
[color=GREEN];;  _blockname - name of block to apply function              ;;[/color]
[color=GREEN];;  _function  - function to apply to all objects in block    ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]
[color=GREEN];;  Returns: List of results of evaluating function, else nil ;;[/color]
[color=GREEN];;------------------------------------------------------------;;[/color]

([color=BLUE]defun[/color] LM:ApplytoBlockObjects ( _acblocks _blockname _function [color=BLUE]/[/color] result )
 (
   ([color=BLUE]lambda[/color] ( _function [color=BLUE]/[/color] def )  
     ([color=BLUE]if[/color]
       ([color=BLUE]not[/color]
         ([color=BLUE]vl-catch-all-error-p[/color]
           ([color=BLUE]setq[/color] def
             ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-item[/color] ([color=BLUE]list[/color] _acblocks  _blockname))
           )
         )
       )
       ([color=BLUE]vlax-for[/color] obj def ([color=BLUE]setq[/color] result ([color=BLUE]cons[/color] (_function obj) result)))
     )
   )
   ([color=BLUE]eval[/color] _function)
 )
 ([color=BLUE]reverse[/color] result)
)

Here is an example test function which will move all objects in a block to layer "0":

 

[color=GREEN];; Test Function to Move all Objects in a Block to Layer "0"[/color]

([color=BLUE]defun[/color] c:test ( [color=BLUE]/[/color] acdoc acblk ss l )

 ([color=BLUE]setq[/color] acdoc ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))
       acblk ([color=BLUE]vla-get-blocks[/color] acdoc)
 )

 ([color=BLUE]if[/color] ([color=BLUE]setq[/color] ss ([color=BLUE]ssget[/color] [color=MAROON]"_+.:E:S"[/color] '((0 . [color=MAROON]"INSERT"[/color]))))
   ([color=BLUE]progn[/color]    
     (LM:ApplytoBlockObjects acblk ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 ([color=BLUE]setq[/color] l ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] ss 0)))))
       ([color=BLUE]function[/color]
         ([color=BLUE]lambda[/color] ( obj ) ([color=BLUE]vla-put-Layer[/color] obj [color=MAROON]"0"[/color]))
       )
     )
   )
 )
 ([color=BLUE]princ[/color])
)

 

[ Requires Regen after block definition modification ]

 

Lee

Posted
Consider the following function to iterate through the objects in the block definition and apply an arbitrary function to each object in turn:

 

[color=green];;---------------=={ Apply to Block Objects }==---------------;;[/color]
[color=green];;                                                            ;;[/color]
[color=green];;  Applies a supplied function to all objects in a block     ;;[/color]
[color=green];;  definition.                                               ;;[/color]
[color=green];;------------------------------------------------------------;;[/color]
[color=green];;  Author: Lee Mac, Copyright © 2011 - www.lee-mac.com       ;;[/color]
[color=green];;------------------------------------------------------------;;[/color]
[color=green];;  Arguments:                                                ;;[/color]
[color=green];;  _acblocks  - block collection in which block resides      ;;[/color]
[color=green];;  _blockname - name of block to apply function              ;;[/color]
[color=green];;  _function  - function to apply to all objects in block    ;;[/color]
[color=green];;------------------------------------------------------------;;[/color]
[color=green];;  Returns: List of results of evaluating function, else nil ;;[/color]
[color=green];;------------------------------------------------------------;;[/color]

([color=blue]defun[/color] LM:ApplytoBlockObjects ( _acblocks _blockname _function [color=blue]/[/color] result )
 (
   ([color=blue]lambda[/color] ( _function [color=blue]/[/color] def )  
     ([color=blue]if[/color]
       ([color=blue]not[/color]
         ([color=blue]vl-catch-all-error-p[/color]
           ([color=blue]setq[/color] def
             ([color=blue]vl-catch-all-apply[/color] '[color=blue]vla-item[/color] ([color=blue]list[/color] _acblocks  _blockname))
           )
         )
       )
       ([color=blue]vlax-for[/color] obj def ([color=blue]setq[/color] result ([color=blue]cons[/color] (_function obj) result)))
     )
   )
   ([color=blue]eval[/color] _function)
 )
 ([color=blue]reverse[/color] result)
)

Here is an example test function which will move all objects in a block to layer "0":

 

[color=green];; Test Function to Move all Objects in a Block to Layer "0"[/color]

([color=blue]defun[/color] c:test ( [color=blue]/[/color] acdoc acblk ss l )

 ([color=blue]setq[/color] acdoc ([color=blue]vla-get-activedocument[/color] ([color=blue]vlax-get-acad-object[/color]))
       acblk ([color=blue]vla-get-blocks[/color] acdoc)
 )

 ([color=blue]if[/color] ([color=blue]setq[/color] ss ([color=blue]ssget[/color] [color=maroon]"_+.:E:S"[/color] '((0 . [color=maroon]"INSERT"[/color]))))
   ([color=blue]progn[/color]    
     (LM:ApplytoBlockObjects acblk ([color=blue]cdr[/color] ([color=blue]assoc[/color] 2 ([color=blue]setq[/color] l ([color=blue]entget[/color] ([color=blue]ssname[/color] ss 0)))))
       ([color=blue]function[/color]
         ([color=blue]lambda[/color] ( obj ) ([color=blue]vla-put-Layer[/color] obj [color=maroon]"0"[/color]))
       )
     )
   )
 )
 ([color=blue]princ[/color])
)

 

[ Requires Regen after block definition modification ]

 

Lee

 

Thank you a lot, Lee.

It's hard to understand you code, but I keep trying to.

Posted
Thank you a lot, Lee.

It's hard to understand you code, but I keep trying to.

 

If it helps you needn't understand the 'LM:ApplytoBlockObjects' function, just know how to call it.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...