Visual LISP, AutoLISP and General Customization

Reply
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 1 of 15 (824 Views)
Accepted Solution

Block Name to VLA-object

824 Views, 14 Replies
02-21-2013 09:24 PM

Hi,

I need to get VLA-Object from block name.

here there is a code to get block name from VLA-Object:

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

 but I need the inverse of this code exactly:

 

blockname(as a string)==>VLA-object

(defun GetVLA_BName (BName).......

 

so, does anybody know how can I do it?

 

Thanks

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 2 of 15 (804 Views)

Re: Block Name to VLA-object

02-21-2013 10:24 PM in reply to: aqdam1978

aqdam1978 wrote:

 

 but I need the inverse of this code exactly:

 

blockname(as a string)==>VLA-object

(defun GetVLA_BName (BName).......

 

so, does anybody know how can I do it?

 

Thanks


Are you wanting the get a selection set by supplying the block name?

 

*Expert Elite*
hmsilva
Posts: 2,649
Registered: ‎12-17-2004
Message 3 of 15 (791 Views)

Re: Block Name to VLA-object

02-22-2013 04:22 AM in reply to: aqdam1978

aqdam,
if I understood you correctly
perhaps something like

 

;; usage (GetVLA_BName "test")
;; Given a block name will return as a VlaObject the 1st
;; element of a selection set, if one exists, otherwise
;; return nil.
(defun GetVLA_BName (BName / ss)
  (if (setq ss (ssget "_X" (list '(0 . "INSERT") (cons 2 BName))))
    (vlax-ename->vla-object (ssname ss 0)))
  )

 

Hope that helps

Henrique

*Expert Elite*
Kent1Cooper
Posts: 5,243
Registered: ‎09-13-2004
Message 4 of 15 (783 Views)

Re: Block Name to VLA-object

02-22-2013 06:22 AM in reply to: aqdam1978

aqdam1978 wrote:

....I need to get VLA-Object from block name.

.... 

blockname(as a string)==>VLA-object

(defun GetVLA_BName (BName).......


There are two possibilities here:

 

1.  A Block reference [or insertion] can be a VLA object, for example an insertion can be made into one by something like:
 

(setq BlkInsObj (vlax-ename->vla-object BlockInsertionEntityName)); [not the Block name]

 

Using something like hmsilva's routine will make one, but of course there can be more than one insertion of the same Block name.  That code will identify only the first one in the selection set, which if I remember right will be the last one inserted.  If you're looking for a particular one, you would need to do something like add additional filtering for Layer, and/or insertion point, and/or some other characteristic(s).  If you know there will be only one such Block in the drawing, then a routine like that should find it as is.

 

However, it might be more accurate to name that function MakeVLA_BName, because it will not "get" an existing VLA object if that's what you want to do, but will make one of an insertion of the Block.  If you already have such a VLA object made, and by "get" you mean that you want to find it, I'm not sure how you would go about it, except by somehow stepping through all VLA objects [if there's even a way to do so -- that's beyond my experience] and looking for one with the correct "Name" property.  Someone else probably knows some (vla...) function that will find it, if there is one.

 

2.  A Block's definition in the Block Table can be a VLA object, for example made into one by something like:

 

(setq BlkDefObj (vlax-ename->vla-object (tblobjname "block" BlockName)))

 

There will be only one Block definition of a given name in a drawing, no matter how many insertions there are of that Block name.  [But it is possible to assign a VLA object of the same Block definition to more than one variable....]  If you have already placed a Block definition as a VLA object into a variable with something like the above, and you want to find the name of that variable by giving the Block name, I'm not sure how you would go about that, either.  The Block name, oddly enough, is not among the VLA properties of the object made that way.

Kent Cooper
Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 5 of 15 (776 Views)

Re: Block Name to VLA-object

02-22-2013 07:13 AM in reply to: Kent1Cooper

Hi Kent,

 

Thank you for your good information.

in my case there is a one block in every dwg file and its name is "TittleBlock".

I just want to change a value of a tag of an attribute!

 

(defun SetAttVal (obj tag value)
  (foreach a (vlax-invoke obj 'getattributes)
    (if (= (vlax-get a 'TagString) tag)
        (vlax-put a 'textstring value))))

 

BlockName: "TittleBlock"

 

(SetAttVal (vlax-ename->vla-object (tblobjname "block" "TittleBlock")) "REV" "Z")

 

----------------------------------------

But you pointed to good issue!, if there are more than one block and I want to change a tag of an attribute inside the ALL blocks, how can I do it?

 

Thanks

 

*Expert Elite*
hmsilva
Posts: 2,649
Registered: ‎12-17-2004
Message 6 of 15 (771 Views)

Re: Block Name to VLA-object

02-22-2013 07:45 AM in reply to: aqdam1978

aqdam,

try this

 

;;;Posted by Jeff Mishler
;;;Discussion Groups - Visual LISP, AutoLISP and General Customization
;;;06-24-2004
(defun edit_att	(blkname att_tag str)
  (vl-load-com)
  (ssget "x"
	 (list '(0 . "INSERT") (cons 2 blkname) '(66 . 1))
  )
  (vlax-for item (vla-get-activeselectionset
		   (vla-get-activedocument (vlax-get-acad-object))
		 )
    (foreach att (vlax-safearray->list
		   (vlax-variant-value (vla-getattributes item))
		 )
      (if (= att_tag (vla-get-tagstring att))
	(vla-put-textstring att str)
      )
    )
  )
)
;;; Usage: (edit_att "blockname" "tag of attribute to edit" "new text")
;;; is Case sensitive

 

Hope that helps,

 

Henrique

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 7 of 15 (764 Views)

Re: Block Name to VLA-object

02-22-2013 08:21 AM in reply to: hmsilva

Thank you Henrique,

 

Abbas

 

*Expert Elite*
hmsilva
Posts: 2,649
Registered: ‎12-17-2004
Message 8 of 15 (762 Views)

Re: Block Name to VLA-object

02-22-2013 08:24 AM in reply to: aqdam1978

You're welcome, Abbas
Glad I could help

 

Henrique

*Expert Elite*
pbejse
Posts: 2,406
Registered: ‎11-24-2009
Message 9 of 15 (744 Views)

Re: Block Name to VLA-object

02-22-2013 07:23 PM in reply to: hmsilva

With the introduction of DB you may still want the snip that check for EffectiveName 

To make it more generic:

 

(defun edit_att	(blkname att_tag str)
  (vl-load-com)
  (ssget "x"
	 (list '(0 . "INSERT") (cons 2 (strcat blkname ",`*U*")) '(66 . 1))
  )
  (vlax-for item (vla-get-activeselectionset
		   (vla-get-activedocument (vlax-get-acad-object))
		 )
    (if (eq (strcase blkname)
               (strcase (vla-get-effectivename item)))
    (foreach att (vlax-safearray->list
		   (vlax-variant-value (vla-getattributes item))
		 )
      (if (= (strcase att_tag) (vla-get-tagstring att))
	(vla-put-textstring att str)
      )
    )
      )
  )
)

 

HTH

 

Mentor
aqdam1978
Posts: 265
Registered: ‎11-05-2008
Message 10 of 15 (730 Views)

Re: Block Name to VLA-object

02-23-2013 10:22 AM in reply to: pbejse

With the introduction of DB you may still want the snip that check for EffectiveName 

To make it more generic:

 

 

 

Hi Pbejse,

Can you explain more  please? what does mean "DB"?

what is the advantages of this code vs previous code prepared by Henrique and Jeff Mishler?

 

Thanks,

 

Abbas

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community