Lisp routine: Insert Block with attribute depending of another block attribute

Lisp routine: Insert Block with attribute depending of another block attribute

C.Utzinger
Collaborator Collaborator
1,919 Views
6 Replies
Message 1 of 7

Lisp routine: Insert Block with attribute depending of another block attribute

C.Utzinger
Collaborator
Collaborator

HI, I post it again!

 

I'm looking for a code with the following steps:

 

1- Select the existing block

2- Change the exisitng block to an unprinted layer

2- Insert the new block

3- Get the new block atomatically with the same Attribute value as the existing one (depending on this one).

 

I dont know how to start this...

 

Regards

0 Likes
Accepted solutions (1)
1,920 Views
6 Replies
Replies (6)
Message 2 of 7

DannyNL
Advisor
Advisor
Accepted solution

Ok, here it goes.

 

Quick 'n dirty code tailored for your specific block only. Changing the layer of the original block is not included yet, but to add remove the semicolon before the (vla-put-Layer ....)  line and change the layer name to whatever you need.

 

Use the command TEST and select your existing block on the attribute (!).

 

(defun c:Test (/ ent obj att fieldcode)
   (if
      (and
         (setq ent (entsel "\nSelect block: "))
         (= (vla-get-ObjectName (setq obj (vlax-ename->vla-object (car ent)))) "AcDbBlockReference")
         (= (vla-get-EffectiveName obj) "SPI-Datenextraktionspunkt-CM")
         (setq att (nentselp (cadr ent)))
         (= (vla-get-ObjectName (setq att (vlax-ename->vla-object (car att)))) "AcDbAttribute")
      )
      (progn
         (setvar "ATTDIA" 0)
         ;(vla-put-Layer obj "LayerName")
         (setq fieldcode (strcat "%<\\AcObjProp Object\(%<\\_ObjId " (itoa (vla-get-objectid att)) ">%\).TextString>%"))
         (command "-INSERT" "SPI-Punkttest3" PAUSE "" "" "" fieldcode "")
         (setvar "ATTDIA" 1)
      )
   )
)

block_field.gif

0 Likes
Message 3 of 7

C.Utzinger
Collaborator
Collaborator

Perfect!

 

I have to change the code a little bit for my use, but it's just what i was looking for.

 

 

Thank you!!!

0 Likes
Message 4 of 7

DannyNL
Advisor
Advisor

You're welcome, glad I could help Smiley Happy

Message 5 of 7

joselggalan
Advocate
Advocate

try this:


You can change the global options in the code:

;;global data:
 (setq NameBlkExist "SPI-Datenextraktionspunkt-CM")
 (setq NameBlkNew "SPI-Punkttest3")
 (setq layerNoPrint  "000-layerNoPrint")
 (setq IsField T)
....

Image_08.gif

 

Program:

;;============================= C:test0023 ====================================;;
;; Jose L. García G. -  27/09/17                                               ;;
;;=============================================================================;;
(defun C:test0023 (/ NameBlkExist NameBlkNew layerNoPrint IsField
		     idx ss lstss
		     ;|Functions|; Blk2Blk get-atts
		  )
	;;----------------------- get-atts ----------------------
	(defun get-atts  (blk / lst)
	  (if (and (= (vla-get-hasAttributes blk) :vlax-true)
		   (not	(vl-catch-all-error-p
			 (setq lst (vl-catch-all-apply
				    'vlax-safeArray->list
				    (list (vlax-variant-value (vla-getAttributes blk))))))))
	    (setq lst (mapcar (function (lambda (Att)
		       (list (vla-get-tagstring Att)(vla-get-textstring Att) Att)
		      )) lst))
	    nil
	  );c.if
	);c.defun

	;;------------------------------- Blk2Blk ------------------------------
	(defun Blk2Blk (eBlkExist / oBlk oBlkNew ptIns bX bY bZ bRot bLayer lAtts1 lAtts2 ERRMsg)
	 (setq ActDoc (vla-get-activedocument (vlax-get-acad-object)))
	 (setq ActSpace (vlax-get-property ActDoc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace)))
	 
	 (setq oBlk (vlax-ename->vla-object eBlkExist))
	 (setq ptIns (vla-Get-InsertionPoint oBlk))
	 (setq bX (vla-get-XEffectiveScaleFactor oBlk)
	       bY (vla-get-YEffectiveScaleFactor oBlk)
	       bZ (vla-get-ZEffectiveScaleFactor oBlk))
	 (setq bRot (vla-get-Rotation oBlk))
	 (setq bLayer (vla-get-Layer oBlk))
	 (cond
	  ((vl-catch-all-error-p (vl-catch-all-apply (function vla-Item) (list (vla-get-Blocks ActDoc) NameBlkNew)))
	   (setq ERRMsg (strcat "ERROR: There is no block [" NameBlkNew "] in the document.."))
	  )
	  ((not (setq oBlkNew (vla-InsertBlock ActSpace ptIns NameBlkNew bX bY bZ bRot)))
	   (setq ERRMsg (strcat "ERROR: Could not insert the block: [" NameBlkNew "]."))
	  )
	  (T
	   (vla-add (vla-get-layers ActDoc) layerNoPrint)
	   (vla-put-Layer oBlkNew bLayer)
	   (vla-put-Layer oBlk layerNoPrint)
	   (cond
	    ((not (setq lAtts1 (get-atts oBlk)))
	     (setq ERRMsg (strcat "ALERT: Block [" NameBlkExist "] does not contain attributes."))
	    )
	    ((not (setq lAtts2 (get-atts oBlkNew)))
	     (setq ERRMsg (strcat "ALERT: Block [" NameBlkNew "] does not contain attributes."))
	    )
	    (T
	     ;;(("01" "EG-032" #<VLA-OBJECT IAcadAttributeReference 0000000039685788>)...)
	     (mapcar
	      (function
	       (Lambda (pAtt / pAtt2 fcode)
		(setq fcode (strcat "%<\\AcObjProp Object\(%<\\_ObjId "
				    (itoa (vla-get-objectid (last pAtt)))
				    ">%\).TextString>%"))
		(if (setq pAtt2 (assoc (car pAtt) lAtts2))
		 (vla-put-TextString (last pAtt2)
		  (if IsField fcode (cadr pAtt))
		 )
		)
	       )
	      )
	      lAtts1
	     );c.mapcar
	    )
	   );c.cond
	  )
	 );c.cond
	 ERRMsg
	)
 
 ;;---------------------------- MAIN -------------------------
 (setvar 'cmdecho 0)

 ;;global data:
 (setq NameBlkExist "SPI-Datenextraktionspunkt-CM")
 (setq NameBlkNew "SPI-Punkttest3")
 (setq layerNoPrint  "000-layerNoPrint")
 (setq IsField T)
 ;;______________________________________
 (setq idx 1)
 (prompt (strcat "\nSelect blocks;[" NameBlkExist "]: "))
 (cond
  ((setq ss (ssget (list '(0 . "INSERT")(cons 2 NameBlkExist))))
   (setq lstss  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
   (command-s "_.undo" "_BE")
   (mapcar
    (function
     (lambda (eBlk / lAtts oBlk Result)
      (if (setq Result (Blk2Blk eBlk))
       (prompt (strcat "\n" Result))
      )
      (prompt (strcat "\rModifying Blocks: " (itoa idx)))
      (setq idx (1+ idx))
     )
    )
    lstss
   );c.mapcar
   (command-s "_.undo" "_E")
  )
 );c.cond
 (princ)
);c defun

(princ)
Message 6 of 7

C.Utzinger
Collaborator
Collaborator

Hi

 

For the moment I have it like this! It works perfect.

I just need one more little thing!

I want the original block not only to change the layer, i also want to change the textstyle of the Attribute.

 

Is that possible? 

 

 

 

(defun Punktkopie ( / ent obj att fieldcode gesch)

(defun LM:getattributevalue ( blk tag / enx )
    (if (and (setq blk (entnext blk)) (= "ATTRIB" (cdr (assoc 0 (setq enx (entget blk))))))
        (if (= (strcase tag) (strcase (cdr (assoc 2 enx))))
            (cdr (assoc 1 (reverse enx)))
            (LM:getattributevalue blk tag)
        )
    )
)

  (while
   (if (and (setq ent (entsel "\nGeoreferenzierter Punkt wählen: "))
            (= (vla-get-ObjectName (setq obj (vlax-ename->vla-object (car ent)))) "AcDbBlockReference")
            (= (vla-get-EffectiveName obj) "SPI-Datenextraktionspunkt-CM")
            (setq att (nentselp (cadr ent)))
            (= (vla-get-ObjectName (setq att (vlax-ename->vla-object (car att)))) "AcDbAttribute")
       )
       (progn
         (command "_.-layer" "_m" "-I-Hilfslinien" "_co" "_t" "153,115,0" "-I-Hilfslinien" "_p" "_n" "-I-Hilfslinien" "")
         (vla-put-Layer obj "-I-Hilfslinien")
         (command "_.-LAYER" "_m" "-I-Koordinatenpunkte-Kopie" "_co" "2" "-I-Koordinatenpunkte-Kopie" "")
         (setq fieldcode (strcat "%<\\AcObjProp Object\(%<\\_ObjId " (itoa (vla-get-objectid att)) ">%\).TextString>%"))

         (setq gesch (LM:GetAttributeValue (car ent) "01" ))
         (setq pt (getpoint (strcat "\nEinfügepunkt für Punktkopie von " gesch " angeben: ")))
	 (setq blk (vlax-invoke
		     (vlax-get
		       (vla-get-ActiveLayout
			 (vla-get-activedocument
			   (vlax-get-acad-object)))
			     'Block)
			     'InsertBlock  (trans pt 1 0)
			     "SPI-Punktkopie.dwg" 1 1 1 (- (getvar 'viewtwist))))
         (setq atb  (Car (vlax-invoke blk 'Getattributes)))
         (vla-put-textstring atb fieldcode)
         (princ "\nTextposition angeben: ")	
         (Textmove (trans (vlax-get atb 'TextAlignmentPoint) 0 1) atb)
         (setq inserted (cons blk inserted))
       )
   ) ; end of if
  ) ; end of while
) ; end of defun

 

Thank you in advance...

0 Likes
Message 7 of 7

C.Utzinger
Collaborator
Collaborator
Not necesary, i have got a solution!
0 Likes