Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get (find) a block insert by its block name using VL function, not ssget.

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
dmfrazier
2630 Views, 8 Replies

Get (find) a block insert by its block name using VL function, not ssget.

In this thread...

 

http://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/block-name-to-vla-object/m-p/3786880#...

 

...HMSilva offers a function "GetVLA_BName", which uses the classic ssget "x" method to find an insert based on block name:

 

(ssget "_X" (list '(0 . "INSERT") (cons 2 BName)))

 

Is there an equivalent "VL" (vl, vla, etc.) method to accomplish the same thing?  (Something like "vla-get-objectname", but more like "vla-get-blockobjectname".)

 

What I'm hoping is that there is a way to find a particular block insert (by its block name) in a dwg that is open but not the current dwg.

8 REPLIES 8
Message 2 of 9
dgorsman
in reply to: dmfrazier

You need to iterate the appropriate layout, inspecting each entity for object type, and for those that pass inspect for Name and/or EffectiveName properties.

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 3 of 9
dmfrazier
in reply to: dgorsman

Thanks for your response.  Sounds simple...

 

Okay, let's take each piece in order:

 

1. iterate the appropriate layout - This is always "model", so this is where I am so far:

 

(vlax-for doc (vla-get-documents (vlax-get-acad-object))
  (vlax-for ent (vla-get-modelspace doc)...

 

(Unfortunately, I haven't figured out a way to test this part, yet, but I've seen it used in code samples.)

 

2. inspecting each entity for object type - this is where I fall down. I want to find the first instance of a specific block insert (by block name) among all objects/entities in the model space of each open doc, but I cannot use ssget because I want this function to loop through all open docs (not just the active doc).

 

3. for those that pass, inspect for Name and/or EffectiveName properties - this is what I have (so far) to get me started (I hope):

 

(vlax-get-property ent
  (if (vlax-property-available-p ent 'EffectiveName) 'EffectiveName 'Name)
)

 

Sure could use a hand (or two).

Message 4 of 9
Lee_Mac
in reply to: dmfrazier

Here's a quick example demonstrating how to iterate over all open documents and retrieve a list of all block references with a given name; the function below will return a list of VLA block reference objects, grouped by document name:

 

(defun getblocks ( blk / lst tmp )
    (setq blk (strcase blk))
    (vlax-for doc (vla-get-documents (vlax-get-acad-object))
        (vlax-for obj (vla-get-modelspace doc)
            (if
                (and
                    (= "AcDbBlockReference" (vla-get-objectname obj))
                    (= blk (strcase (LM:blockname obj)))
                )
                (setq tmp (cons obj tmp))
            )
        )
        (if tmp
            (setq lst (cons (cons (vla-get-name doc) tmp) lst)
                  tmp nil
            )
        )
    )
    lst
)

;; Block Name  -  Lee Mac
;; Returns the true (effective) name of a supplied block reference
                        
(defun LM:blockname ( obj )
    (if (vlax-property-available-p obj 'effectivename)
        (defun LM:blockname ( obj ) (vla-get-effectivename obj))
        (defun LM:blockname ( obj ) (vla-get-name obj))
    )
    (LM:blockname obj)
)

(vl-load-com) (princ)

 

Call the function with the name of the block to be retrieved.

 

Message 5 of 9
dgorsman
in reply to: dmfrazier

Track down the acadauto.chm help file - this contains the COM Object model map along with listings for methods and properties.  This can really help understand the why's of the VLA- functions.

 

Entities in model space are members of a collection.  The collection is just a holding bag and doesn't have a means of returning all entities of type "X" or having property value of "Y", so you have to iterate each one in turn.

 

 

 

----------------------------------
If you are going to fly by the seat of your pants, expect friction burns.
"I don't know" is the beginning of knowledge, not the end.


Message 6 of 9
dmfrazier
in reply to: Lee_Mac

Thanks very much, Lee.  That helped a lot, particularly this part:

 

(if (and (= "AcDbBlockReference" (vla-get-objectname obj))
             (= blk (strcase (LM:blockname obj)))
    )...

 

(I modified it some: since none of the blocks I will be looking for are dynamic, I actually don't need to use the LM:blockname function.)

 

I still have quite a bit of other stuff to figure out for the overall function, but getting over this "hurdle" is a relief.

Message 7 of 9
dmfrazier
in reply to: dgorsman

I do use that Help file from time to time, but I'm still on a steep part of the learning curve with some of this stuff.

 

Thanks again for your input.

Message 8 of 9
Lee_Mac
in reply to: dmfrazier

You're most welcome dmfrazier - if you have any questions about the posted code, feel free to ask. Smiley Happy

 

Cheers,

 

Lee

Message 9 of 9
Migooz
in reply to: Lee_Mac

i'm trying to recall the function with the block name but still it gives me unknown command

really appreciated, as im having 2 blocks with same xdata so i want to recall both as it was not duplicated over each other.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost