Routine for Creating Equipment Tags with EQ and # Attributes.

Routine for Creating Equipment Tags with EQ and # Attributes.

LDShaw
Collaborator Collaborator
845 Views
10 Replies
Message 1 of 11

Routine for Creating Equipment Tags with EQ and # Attributes.

LDShaw
Collaborator
Collaborator
  1. Select two MTEXT entities interactively in AutoCAD.
  2. Create a single block called "equiptag" with two attributes: "EQ" and "#."
  3. Assign the text content from the first selected MTEXT entity to the "EQ" attribute.
  4. Assign the text content from the second selected MTEXT entity to the "#" attribute.
  5. Erase the selected MTEXT entities after extracting their content.
(defun c:f11 (/ sel int ent att spc ang eqText noteText)
  ;; Tharwat - Date: 19.Jun.2017	;;
  (setq Size1 (getvar "dimscale"))
  (if
    (and
      (or (tblsearch "BLOCK" "equiptag")
          (alert "Attributed Block <equiptag> is not found in drawing <!>")
      )
      (setq sel (ssget)) ; Allow the user to select MTEXT entities interactively
    )
    (progn
      (defun unformatmtext (string / text str)
        ;; ASMI - sub-function			;;
        ;; Get string from Formatted Mtext string	;;
        (setq text "")
        (while (/= string "")
          (cond ((wcmatch (strcase (setq str (substr string 1 2)))
                          "\\[\\{}`~]"
                 )
                 (setq string (substr string 3)
                       text   (strcat text str)
                 )
                )
                ((wcmatch (substr string 1 1) "[{}]")
                 (setq string (substr string 2))
                )
                ((and (wcmatch (strcase (substr string 1 2)) "\\P")
                      (/= (substr string 3 1) " ")
                 )
                 (setq string (substr string 3)
                       text   (strcat text " ")
                 )
                )
                ((wcmatch (strcase (substr string 1 2)) "\\[LOP]")
                 (setq string (substr string 3))
                )
                ((wcmatch (strcase (substr string 1 2)) "\\[ACFHQTW]")
                 (setq string (substr string
                                      (+ 2 (vl-string-search ";" string))
                              )
                 )
                )
                ((wcmatch (strcase (substr string 1 2)) "\\S")
                 (setq str    (substr string 3 (- (vl-string-search ";" string) 2))
                       text   (strcat text (vl-string-translate "#^\\" " " str))
                       string (substr string (+ 4 (strlen str)))
                 )
                 (print str)
                )
                (t
                 (setq text   (strcat text (substr string 1 1))
                       string (substr string 2)
                 )
                )
          )
        )
        text
      )
      (setq spc
            (vlax-get (vla-get-activelayout
                        (vla-get-activedocument (vlax-get-acad-object))
                      )
                      'block
            )
      )
      (repeat (setq int (sslength sel))
        (setq ent (ssname sel (setq int (1- int))))
        (setq ang (cdr (assoc 50 (entget ent)))) ;; get the Mtext Angle (in radians) and set it to the variable ang

        ;; Prompt for EQ and # values
        (setq eqText (unformatmtext (cdr (assoc 1 (entget ent)))))
        (setq noteText "")

        (if (= int 1)
          (setq eqText (unformatmtext (cdr (assoc 1 (entget ent)))))
          (setq noteText (unformatmtext (cdr (assoc 1 (entget ent)))))
        )

        (and (setq att (vla-insertblock
                        spc
                        (vlax-3d-point (cdr (assoc 10 (entget ent))))
                        "equiptag"
                        Size1
                        Size1
                        Size1
                        ang ;; the block rotation from the Mtext rotation
                      )
            )
            (vl-some
              '(lambda (x)
                 (if (or (eq (strcase (vla-get-tagstring x)) "EQ")
                         (eq (strcase (vla-get-tagstring x)) "#"))
                   (progn (vla-put-textstring
                            x
                            (if (eq (strcase (vla-get-tagstring x)) "EQ")
                              eqText ; Set the EQ attribute to eqText
                              noteText ; Set the # attribute to noteText
                            )
                          )
                          t
                   )
                 )
               )
              (vlax-invoke att 'getattributes)
            )
            (progn (vla-put-layer att (cdr (assoc 8 (entget ent)))) t)
            (entdel ent)
        )
      )
    )
  )
)

When I run this I get two blocks one with the value for "EQ " and one with the value for "#"  in the place of "EQ"
(SEE JPG)
Can someone help me out?



0 Likes
846 Views
10 Replies
Replies (10)
Message 2 of 11

komondormrex
Mentor
Mentor

hey,

check the following

;**************************************************************************************************************************************************

(defun c:insert_equiptag (/ equipment_mtext number_mtext equiptag_insert)
	(if (null (vl-catch-all-error-p 
					(vl-catch-all-apply 
						'vla-item (list (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
								  	 	"equiptag"
								  )
					)
			  )
		)
			(while (setq equipment_mtext (car (entsel "\nPick equipment text (RMB to stop command): ")))
				(progn
					(sssetfirst nil (ssadd equipment_mtext)) 
					(setq number_mtext (car (entsel (strcat "\nPick number text for equipment \"" 
															(getpropertyvalue equipment_mtext "text") 
															"\": "
													)
											)
										)
						  equiptag_insert (vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
														   (vlax-3d-point (mapcar '* 
														   						  '(0.5 0.5) 
														   						  (mapcar '+ 
																					 	  (cdr (assoc 10 (entget equipment_mtext))) 
																						  (cdr (assoc 10 (entget number_mtext)))
																				  )
																		 )
														   )
														   "equiptag"
														    1 1 1 0
										  )
					)
					(mapcar '(lambda (attribute) (cond
													((= "EQ" (vla-get-tagstring attribute))
														(vla-put-textstring attribute (getpropertyvalue equipment_mtext "text"))
													)
													((= "#" (vla-get-tagstring attribute))
														(vla-put-textstring attribute (getpropertyvalue number_mtext "text"))
													)
													(nil)
												 )
							 )
							 (vlax-invoke equiptag_insert 'getattributes)
					)
					(mapcar 'entdel (list equipment_mtext number_mtext)) 
				)
			)
			(alert "\"Equiptag\" block definition has not been found")
	)
	(princ)
)

;**************************************************************************************************************************************************
Message 3 of 11

LDShaw
Collaborator
Collaborator

THANKS!!
You got me pass my block. I just have to make it work with my scales and such. 


(setq Size1 (getvar "dimscale"))
And bring it in if it's not there. 
(if (= (tblsearch "BLOCK" ) nil) (command "-INSERT" "*//dce/designdata/cadd/DC ACAD/DEFAULT/General/Symbols/equiptag.DWG" "0,0" "1" ""))

Or something similar. 


0 Likes
Message 4 of 11

Sea-Haven
Mentor
Mentor

 (tblsearch "BLOCK" Blockname) 

0 Likes
Message 5 of 11

komondormrex
Mentor
Mentor

change YOUR_PATH in lin 15 to actual path of 'equiptag block. texts should be mtexts strictly.

;**************************************************************************************************************************************************

(defun c:insert_equiptag (/ equipment_mtext number_mtext equiptag_insert)
	(setq insert_size (getvar 'dimscale))
  	(if (vl-catch-all-error-p 
		(vl-catch-all-apply 
			'vla-item (list (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
					"equiptag"
			 	  )
		)
	    )
		(vla-erase
			(vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
					 (vlax-3d-point '(0 0))
					 "YOUR_PATH\\equiptag.dwg"
					  1 1 1 0
			)
		)
  	)
	(while (setq equipment_mtext (car (entsel "\nPick equipment MText (RMB to stop command): ")))
		(progn
			(sssetfirst nil (ssadd equipment_mtext)) 
			(setq number_mtext (car (entsel (strcat "\nPick number MText for equipment \"" 
								(getpropertyvalue equipment_mtext "text") 
								"\": "
							)
						)
					   )
				  equiptag_insert (vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
								   (vlax-3d-point (mapcar '* 
					   						  '(0.5 0.5) 
					   						  (mapcar '+ 
												 	  (cdr (assoc 10 (entget equipment_mtext))) 
													  (cdr (assoc 10 (entget number_mtext)))
											  )
									 )
								   )
								   "equiptag"
								   insert_size insert_size insert_size 0
						  )
			)
			(mapcar '(lambda (attribute) (cond
							((= "EQ" (vla-get-tagstring attribute))
								(vla-put-textstring attribute (getpropertyvalue equipment_mtext "text"))
							)
							((= "#" (vla-get-tagstring attribute))
								(vla-put-textstring attribute (getpropertyvalue number_mtext "text"))
							)
							(nil)
						 )
					 )
					 (vlax-invoke equiptag_insert 'getattributes)
			)
			(mapcar 'entdel (list equipment_mtext number_mtext)) 
		)
	)
	(princ)
)

;**************************************************************************************************************************************************

 

0 Likes
Message 6 of 11

LDShaw
Collaborator
Collaborator

Thanks for all the help. I am still having a problem placing the block if it does not exists in the dwg. My path is
"//dce/designdata/cadd/DC ACAD/DEFAULT/General/Symbols/ES_equiptag.dwg"
I would put up what I've tried but since nothing works it would be a waste. 
Here is what I have 

 

(defun c:f11 (/ equipment_mtext number_mtext equiptag_insert)
  ; Set the scale to the current DIMSCALE
  (setq Size1 (getvar "dimscale"))

  ; Check if the "ES_equiptag" block definition exists
  (if (null (vl-catch-all-error-p
             (vl-catch-all-apply
              'vla-item
              (list (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object)))
                    "ES_equiptag"
                    )
              )
             )
          )
      (while (setq equipment_mtext (car (entsel "\nPick equipment text (RMB to stop command): ")))
        (progn
          (sssetfirst nil (ssadd equipment_mtext))
          (setq number_mtext (car (entsel (strcat "\nPick number text for equipment \""
                                                   (getpropertyvalue equipment_mtext "text")
                                                   "\": "
                                                   )
                                       )
                                 )
                equiptag_insert (vla-insertblock (vla-get-block (vla-get-activelayout (vla-get-activedocument (vlax-get-acad-object))))
                                                  (vlax-3d-point (mapcar '*
                                                                         '(0.5 0.5)
                                                                         (mapcar '+
                                                                                   (cdr (assoc 10 (entget equipment_mtext)))
                                                                                   (cdr (assoc 10 (entget number_mtext)))
                                                                                   )
                                                                         )
                                                  )
                                                  "ES_equiptag"
                                                  Size1 Size1 Size1 0 ; Use Size1 for scale values
                                                  )
                )
          ; Update attribute values based on equipment and number text
          (mapcar '(lambda (attribute)
                     (cond
                       ((= "EQ" (vla-get-tagstring attribute))
                        (vla-put-textstring attribute (getpropertyvalue equipment_mtext "text"))
                        )
                       ((= "#" (vla-get-tagstring attribute))
                        (vla-put-textstring attribute (getpropertyvalue number_mtext "text"))
                        )
                       (t nil) ; Optional: Handle other cases
                       )
                     )
                  (vlax-invoke equiptag_insert 'getattributes)
                  )
          ; Delete the selected equipment and number text
          (mapcar 'entdel (list equipment_mtext number_mtext))
          )
        )
    (alert "\"ES_equiptag\" block definition has not been found")
    )
  (princ)
  )

 

 komondormrex I tried what you asked and it failed out also. 

 

0 Likes
Message 7 of 11

komondormrex
Mentor
Mentor

hey, 

to use relative path like "//dce//mdesigndata//cadd//DC ACAD//DEFAULT//General//Symbols//ES_equiptag.dwg“ your current directory should be a parent to this one. i suggest you use absolute path, the one with drive letter. 

0 Likes
Message 8 of 11

komondormrex
Mentor
Mentor

anyway, that is not a problem to make a not dynamic block right in the drawing using lisp if you might be interested in that. 

0 Likes
Message 9 of 11

LDShaw
Collaborator
Collaborator

We use unc pathing so no drive letters. 

The lisp works how we want. I just need it to add that block if it's not loaded. 

We are having to import pdf's to dwg's. Once it's in we are updating the equipment tags for new schedule information. 
Since all the information is already text this routine allows us to just pick the parts and move on. Right now I am dragging the  ES_equiptag.dwg block into the dwg then using this routine. I'd like to cut out that one step. 

0 Likes
Message 10 of 11

komondormrex
Mentor
Mentor

oh, sure, right, slashes, you are absolutely correct. my fault.

0 Likes
Message 11 of 11

komondormrex
Mentor
Mentor

hey,

i tried unc in backslash way and it worked just fine. check it this way:

 "\\\\dce\\designdata\\cadd\\DC ACAD\\DEFAULT\\General\\Symbols\\ES_equiptag.dwg"