Jump to content

How can i find text in a block..


Arin9916

Recommended Posts

You can start with something like this .... :)

 

 
(vlax-for block
                 (vla-get-blocks
                   (vla-get-activedocument
                     (vlax-get-acad-object)
                   )
                 )
   (vlax-for obj block
     (if (and (eq "AcDbText" (vla-get-objectname obj))
              (eq (vla-get-TextString obj) "abc")
              )
               ;;;;;;;; ....................
                     ;;;;;;;;;;;;;;;;;; ............................
      ;;;;;;;;;;;;;; ..  SO ON .....

Tharwat

Link to comment
Share on other sites

Another, verbose example demonstrating the general concept:

 

([color=BLUE]defun[/color] GetBlockText

 ( blockcollection blockname [color=BLUE]/[/color] _GetIteminCollection _GetItemsinCollectionIf blockdef )

 ([color=BLUE]defun[/color] _GetIteminCollection ( collection item [color=BLUE]/[/color] result )
   ([color=BLUE]if[/color]
     ([color=BLUE]not[/color]
       ([color=BLUE]vl-catch-all-error-p[/color]
         ([color=BLUE]setq[/color] result
           ([color=BLUE]vl-catch-all-apply[/color] '[color=BLUE]vla-item[/color] ([color=BLUE]list[/color] collection item))
         )
       )
     )
     result
   )
 )

 ([color=BLUE]defun[/color] _GetItemsinCollectionIf ( collection condition [color=BLUE]/[/color] result )
   (
     ([color=BLUE]lambda[/color] ( condition )    
       ([color=BLUE]vlax-for[/color] item collection
         ([color=BLUE]if[/color] (condition item) ([color=BLUE]setq[/color] result ([color=BLUE]cons[/color] item result)))
       )
       ([color=BLUE]reverse[/color] result)
     )
     ([color=BLUE]eval[/color] condition)
   )
 )

 ([color=BLUE]if[/color] ([color=BLUE]setq[/color] blockdef (_GetItemInCollection blockcollection blockname))
   ([color=BLUE]mapcar[/color] '[color=BLUE]vla-get-textstring[/color]
     (_GetItemsinCollectionIf blockdef
      '([color=BLUE]lambda[/color] ( x ) ([color=BLUE]wcmatch[/color] ([color=BLUE]vla-get-objectname[/color] x) [color=MAROON]"AcDb*Text"[/color]))
     )
   )
 )
)

([color=blue]vl-load-com[/color])

([color=BLUE]defun[/color] c:test [color=BLUE]nil[/color]
 (GetBlockText
   ([color=BLUE]vla-get-blocks[/color]
     ([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))
   )
   ([color=BLUE]getstring[/color] [color=MAROON]"\nSpecify Block Name: "[/color])
 )
)

Link to comment
Share on other sites

Yes, using recursion:

 

(defun GetBlockText ( blockcollection blockname / _GetIteminCollection blockdef result )

 (defun _GetIteminCollection ( collection item / result )
   (if
     (not
       (vl-catch-all-error-p
         (setq result
           (vl-catch-all-apply 'vla-item (list collection item))
         )
       )
     )
     result
   )
 )

 (if (setq blockdef (_GetIteminCollection blockcollection blockname))
   (vlax-for item blockdef
     (cond
       ( (eq "AcDbBlockReference" (vla-get-objectname item))
         (setq result (append (GetBlockText blockcollection (vla-get-name item)) result))
       )
       ( (wcmatch (vla-get-objectname item) "AcDb*Text")
         (setq result (cons (vla-get-textstring item) result))
       )
     )
   )
 )
 (reverse result)
)

(vl-load-com)

(defun c:test nil
 (GetBlockText
   (vla-get-blocks
     (vla-get-activedocument (vlax-get-acad-object))
   )
   (getstring "\nSpecify Block Name: ")
 )
)

Link to comment
Share on other sites

I need to help again... I`m sorry.....but it`s last...;

 

There are attext in block.. in block... I try to revise myself.... but i can`t not understand use souce... i need to have a time....TT

Link to comment
Share on other sites

  • 1 month later...

Lee,

 

Following on from Arin's query. I'm looking to search a particular block, for a particular string. From what I can gather vlax-for requires a collection, and if I'm not mistaken, you cannot create your own collection, so using vlax-for means I search all blocks on the drawing, or nothing. So, Instead of using vlax-for, How would I iterate through a selection set containing blocks to check each block for my string? And don't spoil it for me... just a few keywords would be great, I want to figure this out myself (as much as possible)....

 

Cheers

Link to comment
Share on other sites

You needn't iterate through the whole Block Collection using vlax-for, but rather use vla-item to retrieve a specific block definition. This block definition is itself a collection of all objects that make it up, so you can then use vlax-for on the block definition.

 

A complete example of this method can be found in post#5 of this thread.

Link to comment
Share on other sites

Lee, Where would I find help on the vla-item command? Or any of the vla- commands for that matter? The AutoCAD help doesn't show them, but I'm sure I've seen them somewhere before...

Link to comment
Share on other sites

Lee, Where would I find help on the vla-item command? Or any of the vla- commands for that matter? The AutoCAD help doesn't show them, but I'm sure I've seen them somewhere before...

 

There was never any specific Visual LISP documentation for the vla-* commands, since these functions are merely properties and methods of the ActiveX Object Model, exposed by the relevant type library (tlb). In versions up to 2010, VBA documentation was provided for these properties and methods, however, since VBA is no longer supported, this documentation was unfortunately removed from 2011 onwards.

Link to comment
Share on other sites

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...