Jump to content

Replace text with block and transfer value into attribute


Recommended Posts

Posted

Dear all,

 

I need a lisp routine which basicly performs the action below:

 

I have only room number as mtext objects. What I want to achieve is:

 

I defined a block with attribute inside named ROOM_NO,

I want to replace each mtext object with this block while getting the value into the attribute.

 

The issue comes from migrating the drawing from Revit to AutoCad. Revit exports the room tags as mtext objects and I need to covert them to proper blocks with attibute. I have over 3000 rooms which is why a lisp routing comes in handy.

 

Regards,

 

MA

Posted

Hi,

 

If you are using AutoCAD LT as shown in your profile so you have no luck to run any Lisp codes with it.

Posted

Dear Tharwat,

 

I joined this forum in 2012 and totally missed updating my profile in this period. Thanks for warning.

 

I am currently using AutoCad 2015 (not LT) in the office.

Posted

No worries.

 

Did you create the Attributed Block?

Then what's the name of this Block?

Do you have a list of these Room names?

Is there any of these Mtexts formatted?

Posted

Let me explain as per your questions:

 

Did you create the Attributed Block?

- Yes.

Then what's the name of this Block?

-The block is named as ROOMTAG

Do you have a list of these Room names?

-Room numbers are distributed over the DWG

Is there any of these Mtexts formatted?

-All room numbers are placed in Mtext objects

 

I shared a file with this post. Please check out the file as working drawing.

REPLACE-TEXT-W-BLOCK_TRANSFER-CONTENT.dwg

Posted

Give this a try.

(defun c:Test (/ sel int ent att spc)
 ;; Tharwat - Date: 19.Jun.2017	;;
 (if
   (and
     (or (tblsearch "BLOCK" "ROOMTAG")
         (alert "Attributed Block <ROOMTAG> is not found in drawing <!>")
     )
     (princ "\nSelect Mtexts to be replaced with Attributed Block <ROOMTAG> :")
     (setq sel (ssget "_:L" '((0 . "MTEXT"))))
   )
    (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))))
        (and (setq att (vla-insertblock
                         spc
                         (vlax-3d-point (cdr (assoc 10 (entget ent))))
                         "ROOMTAG"
                         1.0
                         1.0
                         1.0
                         0.
                       )
             )
             (vl-some
               '(lambda (x)
                  (if (eq (strcase (vla-get-tagstring x)) "ROOMNO")
                    (progn (vla-put-textstring
                             x
                             (unformatmtext (cdr (assoc 1 (entget ent))))
                           )
                           t
                    )
                  )
                )
               (vlax-invoke att 'getattributes)
             )
             (entdel ent)
        )
      )
    )
 )
 (princ)
)(vl-load-com)

  • Like 3
Posted

Dear Tharwat,

 

It works like a charm! I really appreciate! You can't know how much time you saved me!

 

Hopefully I will have the chance to help you back soon.

 

Thank you very much again.

 

Regards,

 

MA

Posted
Dear Tharwat,

 

It works like a charm! I really appreciate! You can't know how much time you saved me!

 

Hopefully I will have the chance to help you back soon.

 

Thank you very much again.

 

Regards,

 

MA

 

Great to hear. You are most welcome.

  • 5 months later...
Posted

I was looking for the same routine. Thank you very much.

 

Also i need the block to be inserted in the source layer. Currently its placing in "0" layer. please update this lisp to place in source object layer.

 

 

 

Thank you

Nanjappa

Posted
I was looking for the same routine. Thank you very much.

 

Also i need the block to be inserted in the source layer. Currently its placing in "0" layer. please update this lisp to place in source object layer.

 

Thank you

Nanjappa

 

Good to hear that. :)

 

Please add the following codes.

(progn (vla-put-layer att (cdr (assoc 8 (entget ent)))) t)

Before the following codes in the routine.

(entdel ent)

Like this:

(progn (vla-put-layer att (cdr (assoc 8 (entget ent)))) t)
(entdel ent)

  • Like 1
  • 1 month later...
Posted

I hope u help me to replaace tendon id and number of strand in two circle as drawing attached with

thank u :)

NEW.dwg

Posted

Bookmarked. This is useful. Thanks Tharwat

  • 3 months later...
Posted

Hi There, I loaded the lisp you created and opened my drawing which contains multiple mtext which are height levels (e.g. 3.580). I created an attributed text with the 'tag' roomtag and then created a block of the attributed text called 'roomtag' when i use the lisp command it inserts the block but the value on the attributed text within the block remains empty. Am i doing something incorrectly?

  • 1 month later...
Posted

Thank you thank you thank you!!! You saved my life!

Can you keep the attribute block angle same as the text before conversion? Before replacing, the text may be having different angle.

Thanks a lot.

Posted

Hi, You have to change the attribute tag name or using the tag name in the lisp.

Posted
Hi There, I loaded the lisp you created and opened my drawing which contains multiple mtext which are height levels (e.g. 3.580). I created an attributed text with the 'tag' roomtag and then created a block of the attributed text called 'roomtag' when i use the lisp command it inserts the block but the value on the attributed text within the block remains empty. Am i doing something incorrectly?

Hi, You have to change the attribute tag name or using the tag name in the lisp.

  • 3 years later...
Posted
On 1/29/2018 at 4:37 AM, CAD_Noob said:

Bookmarked. This is useful. Thanks Tharwat

 

@Tharwat

Wow!

Definitely Bookmarked!

22 hours ago, Danish said:

what is the command to use this lisp?

if you didn't change anything so after you load it (use appload command) - type Test in the command line.

make shure you have a block named ROOMTAG that contains a visible attribute named ROOMNO inserted in to your drawing first.

something like the block I have attached.

ROOMTAG.dwg

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