Write attribute and replace block

Write attribute and replace block

Anonymous
Not applicable
2,011 Views
19 Replies
Message 1 of 20

Write attribute and replace block

Anonymous
Not applicable

Hello people
The .lsp attached replaces a polyline with a specific block,
This works perfectly well.
There is the possibility of in addition to replacing them write the attributes based on the closest text.

(defun c:BK_Replace ( / *error* _StartUndo _EndUndo doc spc ss ll ur )
  (vl-load-com)
  ;; Lee Mac 2010 - www.lee-mac.com
  
;;---------------------------------------------------------------;;
  
  (defun *error* ( msg )
    (if doc (_EndUndo doc))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
	(princ (strcat "\n** Error: " msg " **")))
    (princ)
    )
  
;;---------------------------------------------------------------;;

  (defun _StartUndo ( doc ) (_EndUndo doc)
    (vla-StartUndoMark doc)
    )
;;---------------------------------------------------------------;;
  
 (defun _EndUndo ( doc )
   (if (= 8 (logand 8 (getvar 'UNDOCTL)))
     (vla-EndUndoMark doc)
     )
   )
;;---------------------------------------------------------------;;
  
 (LM:ActiveSpace 'doc 'spc)
  (if
    (and
      ; Old Code by LeeMac
      ; (setq *dwg (getfiled "Select Block" (vl-filename-directory (cond ( *dwg ) ( "" ))) "dwg" 16))
      ; EDIT by 3dwannab 15-03-18
      (cond
	(
	 (and
	   (setq *dwg (car (entsel "\nSelect Block:")))
	   (eq (cdr (assoc 0 (entget *dwg))) "INSERT")
	   (setq *dwg (vla-get-effectivename (vlax-ename->vla-object *dwg)))
	   )
	 )
	)
      ; End EDIT
      (ssget "_:L" (list (cons 0 "LWPOLYLINE") (cons 410 (getvar 'CTAB))))
      )
    (progn
      (_StartUndo doc)
      (vlax-for obj (setq ss (vla-get-ActiveSelectionSet doc))
	(vla-getBoundingBox obj 'll 'ur)
	(
	 (lambda ( block )
	   (mapcar
	     (function
	       (lambda ( p )
		 (vlax-put-property block p (vlax-get-property obj p))
		 )
	       )
	     '(Layer Linetype Lineweight)
	     )
	   (
	    (lambda ( hyp )
	      (vlax-for h (vla-get-HyperLinks obj)
		(vla-Add hyp (vla-get-Url h) (vla-get-UrlDescription h) (vla-get-UrlNamedLocation h))
		)
	      )
	     (vla-get-HyperLinks block)
	     )
	   )
	  (vla-InsertBlock spc
	    (vlax-3D-point
	      (apply 'mapcar
		     (cons '(lambda ( a b ) (/ (+ a b) 2.))
			   (mapcar 'vlax-safearray->list (list ll ur))
			   )
		     )
	      )
	    *dwg 1. 1. 1. 0.
	    )
	  )
	(vla-delete obj)
	)
      (vla-delete ss) (_EndUndo doc)
      )
    )

  (princ)

  )

;;--------------------=={ ActiveSpace }==---------------------;;
;;                                                            ;;
;;  Retrieves pointers to the Active Document and Space       ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  *doc - quoted symbol (other than *doc)                    ;;
;;  *spc - quoted symbol (other than *spc)                    ;;
;;------------------------------------------------------------;;

(defun LM:ActiveSpace ( *doc *spc )
  ;; © Lee Mac 2010
  (set *spc (vlax-get-property (set *doc  (vla-get-ActiveDocument (vlax-get-acad-object)))
	      (if (= 1 (getvar 'CVPORT)) 'PaperSpace 'ModelSpace)
	      )
       )
  )
(princ (strcat "\nLoaded 'BK_Replace' "))
(princ)

pic.JPG

Example dwg

 

0 Likes
Accepted solutions (1)
2,012 Views
19 Replies
Replies (19)
Message 2 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:


The .lsp attached replaces a polyline with a specific block,
There is the possibility of in addition to replacing them write the attributes based on the closest text.

...

 

Yes, it is possible. 

 

Message 3 of 20

Anonymous
Not applicable

how to do this?

0 Likes
Message 4 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

how to do this?


 

From the block do this...

 

HeartfeltAcceptableAmericanrobin-small.gif

 

 

 

 

 

 

 

 

 

 

 

Until you hit at least two TEXT object.

Well, not exactly like that but a box getting bigger each "missed" pass.

 

That would be nice to code eh? Smiley Happy

 

 

 

0 Likes
Message 5 of 20

ВeekeeCZ
Consultant
Consultant

Oh boy, where is Nokia gone now.. 

0 Likes
Message 6 of 20

pbejse
Mentor
Mentor

@ВeekeeCZ wrote:

Oh boy, where is Nokia gone now.. 


 

 

No One Knows It Actually  Smiley Wink

0 Likes
Message 7 of 20

Moshe-A
Mentor
Mentor
Accepted solution

@Anonymous  hi,

 

check this one,  on select objects pick the polyline plus the two texts.

 

enjoy

moshe

 

(vl-load-com) ; load activex support

(defun c:bkrpl (/ get_center_point format_text ; local functions
		  BNAME ss0 ss1 ss2 elist p0 p1 attval0 attval1 savAttReq savAttDia)

 (defun get_center_point (ent / AcDbPline MinPoint MaxPoint)
  (setq AcDbPline (vlax-ename->vla-object (ssname ss1 0)))
  (vla-getboundingbox AcDbPline 'MinPoint 'MaxPoint)
  (vlax-release-object AcDbPline)

  (mapcar
    '(lambda (x0 x1)
      (/ (+ x0 x1) 2)
     )
   (vlax-safearray->list MinPoint)
   (vlax-safearray->list MaxPoint)
  )
 ); get_center_point
  
 (defun format_text (str / p)
  (if (setq p (vl-string-search "/" str))
   (strcat (substr str (1+ p)) "/" (substr str 1 p))
   str
  )
 ); format_text
  
 (setvar "cmdecho" 0)
 (command "._undo" "_begin")

 (setq BNAME "Example Block") ; const

 (cond
  ((and
    (null (tblsearch "block" BNAME))
    (not (findfile (strcat BNAME ".dwg")))
   )
   (vlr-beep-reaction)
   (prompt (strcat "\nBlock " BNAME " is not exist."))
  ); case  
  ( t
   (if (setq ss0 (ssget '((0 . "lwpolyline,text"))))
    (progn
     (setq ss1 (ssadd) ss2 (ssadd)) 
     (foreach ename (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss0)))
      (setq elist (entget ename))
      (cond 
       ((eq (cdr (assoc '0 elist)) "LWPOLYLINE")
        (ssadd ename ss1)
       )
       ((eq (cdr (assoc '0 elist)) "TEXT")
        (ssadd ename ss2)
       )
      ); cond
     ); foreach
   
     (cond
      ((= (sslength ss1) 0)
       (vlr-beep-reaction)
       (prompt "\nNo polyline selected.")
      ); case 
      ((> (sslength ss1) 1)
       (vlr-beep-reaction)
       (prompt "\nto many polylines selected.")
      ); case
      ((= (sslength ss2) 0)
       (vlr-beep-reaction)
       (prompt "\nNo text selected.")
      ); case 
      ((= (sslength ss2) 1)
       (vlr-beep-reaction)
       (prompt "\nonly 1 text selected.")
      ); case 
      ((> (sslength ss2) 2)
       (vlr-beep-reaction)
       (prompt "\nto many texts selected.")
      ); case
      ( t
       (setq p0 (get_center_point (ssname ss1 0)))
       (setq attval0 (cdr (assoc '1 (entget (ssname ss2 0)))))
       (setq attval1 (cdr (assoc '1 (entget (ssname ss2 1)))))

       (setq savAttReq (getvar "attreq"))
       (setq savAttDia (getvar "attdia"))

       (setvar "attreq" 1)
       (setvar "attdia" 0)
     
       (command "._insert" BNAME (trans p0 0 1) 1 1 0)
       (if (vl-string-search "/" attval0)
        (command (format_text attval0) attval1)
        (command attval1 (format_text attval0))
       ); if

       (setvar "attdia" savAttReq)
       (setvar "attreq" savAttDia)

       (setq p1 (get_center_point (entlast)))
       (command "._move" "_si" "_last" (trans p1 0 1) (trans p0 0 1))
       
       (command "._erase" ss1 ss2 "") ; remove origin objects
      ); case
     ); cond
    ); progn
   ); if
  ); case 
 ); cond

 (command "._undo" "_end")
 (setvar "cmdecho" 1)

 (princ)  
); c:bkrpl
Message 8 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

how to do this?


 

If your request is for the attached example or drawing of similar condition then its easy.

0 Likes
Message 9 of 20

ВeekeeCZ
Consultant
Consultant

@pbejse wrote:

@ВeekeeCZ wrote:

Oh boy, where is Nokia gone now.. 


No One Knows It Actually  Smiley Wink


 

Yeah, I guess it's sad, but kinda true. 

But you know what... Nice work with words, pBe! I never knew it was an acronym... ever thought of doing that for a living? 😉

0 Likes
Message 10 of 20

ВeekeeCZ
Consultant
Consultant

@Anonymous, OK, let's do not spoil it all... and get back to the topic.

 

Hey Junior, it's not that difficult that you couldn't make in on your own! At least show some trials, that count! I remember some of your programming attempts not that long ago... Where is your enthusiasm gone? Does it have anything to do with that shiny blue badge on the profile of yours?

0 Likes
Message 11 of 20

pbejse
Mentor
Mentor

Try 

 

(defun _ImGroot	(bl lw / textfound)
  (if (and
	(setq atbv (vlax-invoke bl 'Getattributes))
	(setq ss
	       (ssget "_X"
		      (list '(0 . "TEXT")
			    '(-4 . ">=,>=,*")
			    (Cons 10 lw)
			    '(-4 . "<=,<=,*")
			    (Cons 10 (mapcar '+ lw (list 3.75 2.75 0.0)))
		      )
	       )
	)
	(= (setq i (sslength ss)) 2)
      )
    (progn
      (setq textfound
	     ((lambda (lst)
		(mapcar
		  '(lambda (en)
		     (setq str (cdr (assoc 1 (entget en))))
		     (entdel en)
		     (if (setq p (vl-string-position 47 str))
		       (strcat (substr str (+ 2 p)) "/" (substr str 1 p))
		       str
		     )
		   )
		  lst
		)
	      )
	       (vl-remove-if
		 'listp
		 (mapcar 'cadr (ssnamex ss))
	       )
	     )
      )
      (while (not (vl-string-position 47 (Car textfound)))
	(setq textfound (reverse textfound))
      )
      (mapcar 'vla-put-textstring atbv textfound)
    )
  )
)

 

insert it here..

 

     ... (vlax-for obj (setq ss (vla-get-ActiveSelectionSet doc))
	(vla-getBoundingBox obj 'll 'ur)
	(
	 (lambda ( block )
;;;							   
	   (_ImGroot block (vlax-safearray->list ll))  
;;;							   
	   (mapcar
	     (function
	       (lambda ( p )
		 (vlax-put-property block p (vlax-get-property obj p))
		 )
	       )
	     '(Layer Linetype Lineweight)
	     )
	   (...

 

Go ahead and play around with it. it missed some of the TEXT objects not on the same positions as the others. Well you get the general idea

 

 

0 Likes
Message 12 of 20

pbejse
Mentor
Mentor

@ВeekeeCZ wrote:

 

Yeah, I guess it's sad, but kinda true. 

But you know what... Nice work with words, pBe! I never knew it was an acronym... ever thought of doing that for a living? 😉


 

 

Smiley Very Happy  Good times BeeKeeCZ.

 

I missed this, Wish I have more time to engage in this forum.  Fun fun fun..

 

 

 

 

0 Likes
Message 13 of 20

ВeekeeCZ
Consultant
Consultant

@pbejse wrote:

@ВeekeeCZ wrote:

 

Yeah, I guess it's sad, but kinda true. 

But you know what... Nice work with words, pBe! I never knew it was an acronym... ever thought of doing that for a living? 😉


 

Smiley Very Happy  Good times BeeKeeCZ.

 

I missed this, Wish I have more time to engage in this forum.  Fun fun fun..

 


Yeah... just a few folks play this card sometimes... glad to see you around!

0 Likes
Message 14 of 20

Anonymous
Not applicable

Many thanks @Moshe-A  you are fantastic !!! I will try to make a multiple selection.

Thank you too @pbejse  @ВeekeeCZ 

0 Likes
Message 15 of 20

Anonymous
Not applicable

@pbejse 
This is very complex for me, some doubts
works relatively well if it is a small amount of selected objects, when more than 10000 takes many hours to run, is there a possibility of reducing that time?

 

(defun c:BK_Replace ( / *error* _StartUndo _EndUndo doc spc ss ll ur )
  (vl-load-com)
  ;; Lee Mac 2010 - www.lee-mac.com
  
;;---------------------------------------------------------------;;
  
  (defun *error* ( msg )
    (if doc (_EndUndo doc))
    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
	(princ (strcat "\n** Error: " msg " **")))
    (princ)
    )
  
;;---------------------------------------------------------------;;

  (defun _StartUndo ( doc ) (_EndUndo doc)
    (vla-StartUndoMark doc)
    )
;;---------------------------------------------------------------;;
  
 (defun _EndUndo ( doc )
   (if (= 8 (logand 8 (getvar 'UNDOCTL)))
     (vla-EndUndoMark doc)
     )
   )
;;---------------------------------------------------------------;;
  
 (LM:ActiveSpace 'doc 'spc)
  (if
    (and
      ; Old Code by LeeMac
      ; (setq *dwg (getfiled "Select Block" (vl-filename-directory (cond ( *dwg ) ( "" ))) "dwg" 16))
      ; EDIT by 3dwannab 15-03-18
      (cond
	(
	 (and
	   (setq *dwg (car (entsel "\nSelect Block:")))
	   (eq (cdr (assoc 0 (entget *dwg))) "INSERT")
	   (setq *dwg (vla-get-effectivename (vlax-ename->vla-object *dwg)))
	   )
	 )
	)
      ; End EDIT
      (ssget "_:L" (list (cons 0 "LWPOLYLINE") (cons 410 (getvar 'CTAB))))
      )
    (progn
      (_StartUndo doc)
      (vlax-for obj (setq ss (vla-get-ActiveSelectionSet doc))
	(vla-getBoundingBox obj 'll 'ur)
	(
	 (lambda ( block )
	   (_ImGroot block (vlax-safearray->list ll)) 
	   (mapcar
	     (function
	       (lambda ( p )
		 (vlax-put-property block p (vlax-get-property obj p))
		 )
	       )
	     '(Layer Linetype Lineweight)
	     )
	   (
	    (lambda ( hyp )
	      (vlax-for h (vla-get-HyperLinks obj)
		(vla-Add hyp (vla-get-Url h) (vla-get-UrlDescription h) (vla-get-UrlNamedLocation h))
		)
	      )
	     (vla-get-HyperLinks block)
	     )
	   )
	  (vla-InsertBlock spc
	    (vlax-3D-point
	      (apply 'mapcar
		     (cons '(lambda ( a b ) (/ (+ a b) 2.))
			   (mapcar 'vlax-safearray->list (list ll ur))
			   )
		     )
	      )
	    *dwg 1. 1. 1. 0.
	    )
	  )
	(vla-delete obj)
	)
      (vla-delete ss) (_EndUndo doc)
      )
    )

  (princ)
  )

;;--------------------=={ ActiveSpace }==---------------------;;
;;                                                            ;;
;;  Retrieves pointers to the Active Document and Space       ;;
;;------------------------------------------------------------;;
;;  Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  *doc - quoted symbol (other than *doc)                    ;;
;;  *spc - quoted symbol (other than *spc)                    ;;
;;------------------------------------------------------------;;

(defun LM:ActiveSpace ( *doc *spc )
  ;; © Lee Mac 2010
  (set *spc (vlax-get-property (set *doc  (vla-get-ActiveDocument (vlax-get-acad-object)))
	      (if (= 1 (getvar 'CVPORT)) 'PaperSpace 'ModelSpace)
	      )
       )
  )
(princ (strcat "\nLoaded 'BK_Replace' "))
(princ)


;;------------------------------------------------------------;
pbejse
;;------------------------------------------------------------;

(defun _ImGroot	(bl lw / textfound)
  (if (and
	(setq atbv (vlax-invoke bl 'Getattributes))
	(setq ss
	       (ssget "_X"
		      (list '(0 . "TEXT")
			    '(-4 . ">=,>=,*")
			    (Cons 10 lw)
			    '(-4 . "<=,<=,*")
			    (Cons 10 (mapcar '+ lw (list 3.75 2.75 0.0)))
		      )
	       )
	)
	(= (setq i (sslength ss)) 2)
      )
    (progn
      (setq textfound
	     ((lambda (lst)
		(mapcar
		  '(lambda (en)
		     (setq str (cdr (assoc 1 (entget en))))
		     (entdel en)
		     (if (setq p (vl-string-position 47 str))
		       (strcat (substr str (+ 2 p)) "/" (substr str 1 p))
		       str
		     )
		   )
		  lst
		)
	      )
	       (vl-remove-if
		 'listp
		 (mapcar 'cadr (ssnamex ss))
	       )
	     )
      )
      (while (not (vl-string-position 47 (Car textfound)))
	(setq textfound (reverse textfound))
      )
      (mapcar 'vla-put-textstring atbv textfound)
    )
  )
)

;;------------------------------------------------------------;
;;------------------------------------------------------------;
0 Likes
Message 16 of 20

devitg
Advisor
Advisor

One way someone  could help you, if you would help someone  with a true whole .dwg 

And a whole specifications about what you want. 

 

0 Likes
Message 17 of 20

pbejse
Mentor
Mentor

@Anonymous wrote:

@pbejse 
This is very complex for me, some doubts
works relatively well if it is a small amount of selected objects, when more than 10000 takes many hours to run, is there a possibility of reducing that time?

 

 

A measly 10,000+?!? ! 😄

I copied the items on the sample drawing and copied the items making it 15,000 and it took  9:34, maybe you meant 100,000?  like most things, 100,000 will take a long time to process. I will have a look later.

 

 

0 Likes
Message 18 of 20

Anonymous
Not applicable

@devitg  The file is exactly that, it's just cropped.
@pbejse 
I'm being generous in saying 10,000
to be more accurate are 4 parts each with approximately 32,000. No need to bother trying to solve it.

0 Likes
Message 19 of 20

devitg
Advisor
Advisor

Please apologize me . Could you explain how ACAD can work on a IMAGE. ??

 

 

0 Likes
Message 20 of 20

devitg
Advisor
Advisor

Junior , please pardon me . I did no seen your link to download the DWG . I will check it now 

 

0 Likes