Jump to content

Lisp not working properly in AutoCAD 2020


AbdRF

Recommended Posts

Hi,
I am using a block switch lisp which is working properly in the AutoCAD 2019 but when I tried this on the same drawing in the AutoCAD 2020, it does not preserve the scale of the block after switching.
Can somebody advise why it is so ?

I have attach the lisp file and sample drawing below
Thanks 

bs.lsp

BlkSwitch sample.dwg

Link to comment
Share on other sites

Seems to work fine here.

 

If your blocks are not dynamic or attributed this could be simplified quite a bit. Not much error checking on selection.

(defun c:bs (/ a b)
  ;; RJP » 2019-07-23
  (if (and (setq a (car (entsel "\nPick block to replace with new: ")))
	   (setq b (car (entsel "\nPick block to be used for replace: ")))
	   (setq a (assoc 2 (entget a)))
	   (setq b (assoc 2 (entget b)))
      )
    (foreach x (mapcar 'cadr (ssnamex (ssget "_x" (list '(0 . "insert") a))))
      (entmod (subst b a (entget x)))
    )
  )
  (princ)
)

 

Edited by ronjonp
  • Thanks 1
Link to comment
Share on other sites

@ronjonp  which version of AutoCAD you are using?

Btw thanks, your code is working for 2020.
But I also have to deal with dynamic and attributed blocks.
What to do you suggest in order to replace them preserving the properties such as scale,rotation and insertion point (blocks which needed to be switched are almost similar) .
 

Link to comment
Share on other sites

I'm using AutoCAD 2020. Give this version a try for dynamic and attributed blocks.

(defun c:bs (/ a b n)
  ;; RJP » 2019-07-23
  (if (and (setq a (car (entsel "\nPick block to replace with new: ")))
	   (setq b (car (entsel "\nPick block to be used for replace: ")))
	   (= "INSERT" (cdr (assoc 0 (entget a))) (cdr (assoc 0 (entget b))))
	   (setq n (vla-get-effectivename (vlax-ename->vla-object a)))
	   (setq b (vlax-ename->vla-object b))
      )
    (foreach bl	(mapcar 'cadr (ssnamex (ssget "_x" (list '(0 . "insert")))))
      (cond
	((= n (vla-get-effectivename (vlax-ename->vla-object bl)))
	 (entmod (append (entget (vlax-vla-object->ename (vla-copy b)))
			 (vl-remove-if-not '(lambda (x) (member (car x) '(10 41 42 43))) (entget bl))
		 )
	 )
	 (entdel bl)
	)
      )
    )
  )
  (princ)
)
(vl-load-com)

 

Edited by ronjonp
  • Thanks 1
Link to comment
Share on other sites

@ronjonp Thanks this is working perfectly for dynamic blocks but not for attributed block (properly).
Basic requirement is that the attribute values of old blocks should be preserved after swapping.

Sorry I should  have attached the attributed block sample dwg .
So here I am attaching the file for your reference.

attributed block sample.dwg

Thanks for your effort and help.Cheers
 

Link to comment
Share on other sites

7 hours ago, AbdRF said:

@ronjonp Thanks this is working perfectly for dynamic blocks but not for attributed block (properly).
Basic requirement is that the attribute values of old blocks should be preserved after swapping.

Sorry I should  have attached the attributed block sample dwg .
So here I am attaching the file for your reference.

attributed block sample.dwg

Thanks for your effort and help.Cheers
 

Those blocks all have different tag names (NRS NDS NCS) ? What happens when you swap a block that has one attribute with at block that has 2 attributes  or visa versa ?

Link to comment
Share on other sites

@ronjonp Yes all the blocks will have different tag (NRS NDS NCS etc) .
Currently I am dealing only with block having one attribute.So both the blocks which need to be interchanged will have only one attribute (as mention previously, blocks will have almost similar properties)

In case if I need to deal with block having two attributes, the switching will be with another block having two attributes only although tags will be different.

Hoping I am clear.
Thanks 
 

Link to comment
Share on other sites

8 hours ago, AbdRF said:

@ronjonp Yes all the blocks will have different tag (NRS NDS NCS etc) .
Currently I am dealing only with block having one attribute.So both the blocks which need to be interchanged will have only one attribute (as mention previously, blocks will have almost similar properties)

In case if I need to deal with block having two attributes, the switching will be with another block having two attributes only although tags will be different.

Hoping I am clear.
Thanks 
 

Try this .. will only keep the attribute values if the source and destination have the same amount of attributes .. if there are multiple the order of values is unknown!

(defun c:bs (/ a at b co fl j n o r)
  ;; RJP » 2019-07-28
  (cond	((and (setq a (car (entsel "\nPick block to replace with new: ")))
	      (setq b (car (entsel "\nPick block to be used for replace: ")))
	      (= "INSERT" (cdr (assoc 0 (entget a))) (cdr (assoc 0 (entget b))))
	      (setq n (vla-get-effectivename (vlax-ename->vla-object a)))
	      (setq b (vlax-ename->vla-object b))
	      (/= n (vla-get-effectivename b))
	 )
	 ;; Check if the blocks have the same amount of attributes
	 (setq fl (and (setq at (vlax-invoke (vlax-ename->vla-object a) 'getattributes))
		       (= (length at) (length (vlax-invoke b 'getattributes)))
		  )
	 )
	 (foreach bl (mapcar 'cadr (ssnamex (ssget "_x" (list '(0 . "insert")))))
	   (cond ((= n (vla-get-effectivename (setq o (vlax-ename->vla-object bl))))
		  (setq co (vla-copy b))
		  (foreach p '(insertionpoint rotation xscalefactor yscalefactor zscalefactor)
		    (vlax-put co p (vlax-get o p))
		  )
		  (if fl
		    (mapcar '(lambda (r j) (vla-put-textstring r (vla-get-textstring j)))
			    (vlax-invoke co 'getattributes)
			    (vlax-invoke o 'getattributes)
		    )
		  )
		  (entdel bl)
		 )
	   )
	 )
	)
  )
  (princ)
)
(vl-load-com)

 

  • Thanks 1
Link to comment
Share on other sites

ronjonp the multi attribute is about the problem I mentioned in another post using attribute order is easier and can have different number of attributes in each block, need to build the dcl selection form 2 lists maybe with tag names filled in 2nd list allows up down of order. I think lee has something. something like this but 1 dcl and  better.

 

image.png.683ef7a8c2f21e3264fec00b5c59d286.png

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