Jump to content

How can I prefix text consistently??


Recommended Posts

Posted

This is one way to do it with the Buttons holding the Prefix or Suffix.

Note that the layer current will remain current and the 'TEXT' layer will be created

if it doesn't exist. Also the Text size and style will remain current and not be changed.

 

Create your buttons with this in them

^C^C(Pre_Text_Suf "CH" "")

^C^C(Pre_Text_Suf "FL_" "")

^C^C(if (setq str (getstring t "\nEnter Prefix:"))(Pre_Text_Suf str ""))

^C^C(if (setq str (getstring t "\nEnter Suffix:"))(Pre_Text_Suf "" str))

^C^C(if (and (setq pre (getstring t "\nEnter Prefix:"))(setq suf (getstring t "\nEnter Suffix:")))(Pre_Text_Suf pre suf))

 

;;  CAB 09.20.08
(defun Pre_Text_Suf (Prefix Suffix / p1 str)
 (or Prefix (setq Prefix "")) ; if nil set to empty string
 (or Suffix (setq Suffix ""))
 (while
   (and
     (setq p1 (getpoint "\nSpecify start point of text:"))
     (setq str (getstring t "\nEnter text:"))
     (/= str "") ; user pressed Enter only with no text
   )
    (entmake
      (list
        '(0 . "TEXT")
        (cons 10 p1)
        (cons 40 2.2) ; Text height
        '(50 . 0) ; Text rotation 
        '(51 . 0) ; Oblique angle 
        '(7 . "STANDARD")
        '(8 . "TEXT") ; layer will make if it doesn't exist
        (cons 1 (strcat Prefix str Suffix)) ; Text String
      )
    )
 )       ; end while
 (princ)
)         ; end defun


;|
Create your buttons with this in them
^C^C(Pre_Text_Suf "CH" "")
^C^C(Pre_Text_Suf "FL_" "")
^C^C(if (setq str (getstring t "\nEnter Prefix:"))(Pre_Text_Suf str ""))
^C^C(if (setq str (getstring t "\nEnter Suffix:"))(Pre_Text_Suf "" str))
^C^C(if (and (setq pre (getstring t "\nEnter Prefix:"))(setq suf (getstring t "\nEnter Suffix:")))(Pre_Text_Suf pre suf))
|;

  • Replies 46
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    18

  • coombsie11

    10

  • jamesfear

    6

  • ML0940

    4

Posted

Hi Coombsie,

 

The above LISP is a hell of a lot more advanced than mine, but I modified mine using CAB's knowledge of the entmake and how to represent the table of DXF codes - (think thats what they're called! :P).

 

(defun c:chprefix (/ obj txtpos txt1 txtval)
   (setvar "cmdecho" 0)
   (setq obj "CH") ; <---<< CHANGE "CH" TO WHATEVER PREFIX
   (setq txtpos (getpoint "\nSelect Point for Text"))
   (while (/= txtpos nil)
       (setq txt1 (getstring "\nType Text: "))
       (setq txtval (strcat obj " " txt1)) ; <---<< SWITCH THE ORDER OF "OBJ" & "TXT1" FOR SUFFIX
       (entmake    
           (list   '(0 . "TEXT") 
                   (cons 10 txtpos)
                   '(40 . 2.5) 
                   '(50 . 0.0) 
                   '(51 . 0.0)
                   '(7 . "STANDARD") 
                   '(8 . "TEXT")
                   (cons 1 txtval) 
           ) ; end list
       ) ; end entmake
       (setq txtpos (getpoint "\nSelect Point for Text"))
   ) ; end while
   (setvar "cmdecho" 1)
   (prompt "\nFunction Complete")
   (princ)
)

Posted

Lee Mac,

You're learning fast, good job.

I would make a few changes if it were me.

 

"cmdecho" only comes into play if you use a (COMMAND call. So not needed here.

You can put the getpoint in the WHILE test and eliminate the second one in the loop.

Also include the test of the empty string from the user.

Using T with getstring will allow a space in the user input (getstring t

otherwise a space will act as ENTER key.

(defun c:chprefix (/ obj txtpos txt1 txtval)
   (setq obj "CH") ; <---<< CHANGE "CH" TO WHATEVER PREFIX
   (while (and (setq txtpos (getpoint "\nSelect Point for Text"))
               (/= (setq txt1 (getstring t "\nType Text: ")) ""))
       (setq txtval (strcat obj " " txt1)) ; <---<< SWITCH THE ORDER OF "OBJ" & "TXT1" FOR SUFFIX
       (entmake    
           (list   '(0 . "TEXT") 
                   (cons 10 txtpos)
                   '(40 . 2.5) 
                   '(50 . 0.0) 
                   '(51 . 0.0)
                   '(7 . "STANDARD") 
                   '(8 . "TEXT")
                   (cons 1 txtval) 
           ) ; end list
       ) ; end entmake
   ) ; end while
   (prompt "\nFunction Complete")
   (princ)
)

Posted

Cheers Charles,

 

Thanks for the help in refining my LISP,

 

Good idea using the "and" with the "while" - I tried using the "while" just with the "getstring" command but found that the user could not exit the loop, as the getstring would seem to always have a value... :oops:

 

Lets hope coombsie is satisfied! :D

 

Cheers

 

Lee 8)

Posted

Sorry to rattle on with this LISP, but here is one final ammendment. :P

 

(defun c:pstxt (/ pos obj txtpos txt txtval)
   (initget 1 "Prefix Suffix")
   (setq pos (getkword "\nSelect Position [Prefix/Suffix]"))
   (setq obj (getstring t "\nType Prefix/Suffix Text: "))
   (setq obj (strcase obj))
   (while
       (and    (setq txtpos (getpoint "\nSelect Point for Text"))
               (/= (setq txt (getstring t "\nType Text: ")) "")
       ) ; end and
       (cond
           ((= pos "Prefix")
           (setq txtval (strcat obj " " txt))
           ) ; end condition 1
           (( = pos "Suffix")
           (setq txtval (strcat txt " " obj))
           ) ; end condition 2
           ) ; end cond
       (entmake    
       (list   '(0 . "TEXT") 
               (cons 10 txtpos)
               '(40 . 2.5) 
               '(50 . 0.0) 
               '(51 . 0.0)
               '(7 . "STANDARD") 
               '(8 . "TEXT")
               (cons 1 txtval) 
       ) ; end list
       ) ; end entmake
   ) ; end while
   (prompt "\nFunction Complete.")
   (princ)
)

Once again, thanks for all your help guys - much appreciated :D

Posted

Thanks guys..

 

Tried both Lee Macs' and CABs and they work just great.:thumbsup:

 

Good to know that I can create a few buttons now with set prefixes.

 

Will make the survey plans I'm drawing up that bit quicker in the future.

 

I used to be able to adapt and customise a few DEISEL 2000LT commands, but am lost when it comes to LISP.

 

Are there any good books or websites out there for beginners wanting to learn a few tricks?

Posted

I must admit coombsie, I have only been using AutoLISP (or any type of programming for that matter) for about 6 months now :geek:. I tried to learn it from an old R.11 Programmer's Reference Manual (listing all the functions and a brief description on how to use them).

 

I suppose if you search "LISP" or "AutoLISP" on Amazon.co.uk, you are bound to find some good books for beginners. :)

Posted

I have VBA code that will prefix text, if you are interested?

 

ML

Posted

ML0940

 

I am most certainly interested. :shock:

 

I really want a routine that will prefix new text, rather than existing text, with a specified layer.

 

Thanks ..

Posted

Hi coombsie11

You happened to catch me at my PC :)

 

OK,

Here is the code as it stands, currently

Where is says layer name, type in the layer name that you want the code to filter on. It will search all existing Text and Mtext on that layer. Also change the prefix to what you would like.

 

And, you will see these two methods:

Sset.Select acSelectionSetAll, , , grpcode, dataval

Sset.SelectOnScreen grpcode, dataval

 

acSelectionSetAll: will select all text in the drawing

SelectOnScreen: will prompt the user to pick specific text

You can select whichever method that you prefer and comment out the other.

 

This one will only prefix the existing text, so give it a try

If I get time, I will look at doing it the way you'd like

Can you explain how you'd like it to work, specifically, step by step?

 

Help this helps

 

ML

 

Also, thanks to Dave H. for help with this one

 

Sub ChangeTextPrefixByEntType() 'wGroupCodes

Dim Sset As AcadSelectionSet
Dim Ent As AcadEntity
Dim grpcode(0 To 1) As Integer
Dim dataval(0 To 1) As Variant

grpcode(0) = 0                  'dxf entity group code for entity types
dataval(0) = "TEXT,MTEXT" 'entity type (s)
grpcode(1) = 8                  'dxf entity group code for layername
dataval(1) = UCase("layername") 'layername (s)

On Error Resume Next
ThisDrawing.SelectionSets.Item("gettext").Delete 'If selection set exists, delete it


Set Sset = ThisDrawing.SelectionSets.Add("gettext")
sset.Select acSelectionSetAll, , , grpcode, dataval
'Sset.SelectOnScreen grpcode, dataval

For Each Ent In Sset
 Ent.TextString = "G-" & Ent.TextString 'Change G- to your prefix
 'Debug.Print ent.TextString
Next

Sset.Delete
Set Sset = Nothing

End Sub

Posted

Here is another version w/o using the group codes, same result

Same thing, choose the selection type that you prefer, fill in the layer name of your choice and change the prefix to your liking.

ML

 

Sub ChangeTextPrefixByEntType()
Dim Sset As AcadSelectionSet
Dim Ent As AcadEntity

On Error Resume Next 'If selection set exists, delete it
ThisDrawing.SelectionSets.Item("gettext").Delete

Set Sset = ThisDrawing.SelectionSets.Add("gettext")
Sset.Select acSelectionSetAll 'Sset.SelectOnScreen

For Each Ent In Sset
 If Ent.ObjectName = "AcDbText" Or Ent.ObjectName = "AcDbMText" Then
  If UCase(Ent.Layer) = "layername" Then 'layername
   Ent.TextString = "G-" & Ent.TextString
   'Debug.Print ent.TextString
  End If
 End If
Next

Sset.Delete
Set Sset = Nothing
End Sub

  • 3 months later...
Posted

Sorry to revive an old thread, but....

 

Can someone please help me edit ASMI's LISP here:

____________________________________________________

(defun c:ftxt(/ cMod cStr tSet)

(vl-load-com)

(initget 1 "Prefix Suffix")

(setq cMod(getkword "\nAdd [Prefix/Suffix]: "))

(if(and

(setq cStr(getstring T "\nSpecify string: "))

(setq tSet(ssget '((0 . "TEXT,MTEXT"))))

); and

(foreach tx(mapcar 'vlax-ename->vla-object

(vl-remove-if 'listp

(mapcar 'cadr(ssnamex tSet))))

(if(= "Prefix" cMod)

(vla-put-TextString tx

(strcat cStr(vla-get-TextString Tx)))

(vla-put-TextString tx

(strcat(vla-get-TextString Tx)cStr))

); end if

); end foreach

); end if

(princ)

); end of c:ftxt

____________________________________________________

 

What I want is to remove the option for prefix/suffix and for the LISP to just choose prefix automatically. I then also want to use the same prefix each time, so it would be great for the LISP to also keep this, rather than ask me. What I want to end up doing is to begin the LISP, pick the text I want, and for it to then be prefixed with "DK:" but I'm afraid I'm not experienced enough with LISP to edit the above.....Thanks

Posted

This should work;

 

(defun c:ftxt (/ tSet)
   (vl-load-com)
   (if    (setq tSet (ssget '((0 . "TEXT,MTEXT"))))
   (foreach tx (mapcar 'vlax-ename->vla-object
               (vl-remove-if
               'listp
               (mapcar 'cadr (ssnamex tSet))
               ) ;_  end vl-remove-if
           ) ;_  end mapcar
       (vla-put-TextString
       tx
       (strcat "DK:" (vla-get-TextString Tx))
       ) ;_  end vla-put-TextString
   ) ; end foreach
   ) ; end if
   (princ)
) ; end of c:ftxt

Posted

Thanks a lot LeeMac - that works perfectly! And such a quick response! I love this site, lol :)

  • 1 year later...
Posted
This should work;

 

(defun c:ftxt (/ tSet)
   (vl-load-com)
   (if    (setq tSet (ssget '((0 . "TEXT,MTEXT"))))
   (foreach tx (mapcar 'vlax-ename->vla-object
               (vl-remove-if
               'listp
               (mapcar 'cadr (ssnamex tSet))
               ) ;_  end vl-remove-if
           ) ;_  end mapcar
       (vla-put-TextString
       tx
       (strcat "DK:" (vla-get-TextString Tx))
       ) ;_  end vla-put-TextString
   ) ; end foreach
   ) ; end if
   (princ)
) ; end of c:ftxt

 

 

Hey Lee,

 

If I wanted to use a wild-card character, like "?" (question mark) to match any 1, 2 or 3 characters how would you do that in your lisp?

 

I tried myself to edit your routine but keep failing...

 

Also I really like your T2M and got an idea that I can't write lol I was also wondering if you could do a like a combine 2 or 3 texts in one line without moving its postion if you know what I mean? I'll show you an example with the other thing I am having problems with.

 

I'll attach both the lisp and a picture example hopfully lol. and yeah I had trouble with loading the pic thats why its in a word doc lol.

 

Also would it be rude to ask where you learnt your awesome lisp writting skills from??

 

James

ftxt2.lsp

eg.docx

Posted
Hey Lee,

 

If I wanted to use a wild-card character, like "?" (question mark) to match any 1, 2 or 3 characters how would you do that in your lisp?

 

I tried myself to edit your routine but keep failing...

 

Also I really like your T2M and got an idea that I can't write lol I was also wondering if you could do a like a combine 2 or 3 texts in one line without moving its postion if you know what I mean? I'll show you an example with the other thing I am having problems with.

 

I'll attach both the lisp and a picture example hopfully lol. and yeah I had trouble with loading the pic thats why its in a word doc lol.

 

Also would it be rude to ask where you learnt your awesome lisp writting skills from??

 

James

 

 

opps sent the wrong lisp...

fronttxt.lsp

Posted
opps sent the wrong lisp...

 

 

I should of read the whole thread I think you already done what I asked for. Well the first thing I asked for can you still have a look? give us some advice too lol

fronttxt.lsp

Posted
Also would it be rude to ask where you learnt your awesome lisp writting skills from??

 

Doing a Maths Degree does help - the logic/thought process is very similar, but I learnt LISP mainly from the examples of others. :)

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