Display full version of the post: select several mtext contains same letters

Sandervp
17.02.2015, 10:58
Hello everybody,I have got a drawing with a lot of mtext objects. These objects are also different layers.But the most of these mtext objects have something in common. They are containg the same letter combination. For example: "example 001", "example 149", "example 572" etc.How can I select all those objects containing these letter combination?I want to change these into one layer.Thanks!

John Connor
17.02.2015, 11:20
Could possibly be done using a custom lisp routine.

Kent Cooper
17.02.2015, 16:19
Using (ssget) to find a selection set, searching the entire drawing with a selection filter including wildcards [read about the (wcmatch) function] should do it:
 
(command
  "_.chprop"
  (ssget "_X" '((0 . "MTEXT") (1 . "*example 001*")))
  ""
  "_layer" "YourDesiredLayer"
  ""
)
 
If they might not all be in the current space, you would need to do it differently [the CHPROP command won't "find" those not in the current space], but it can be done -- write back.Kent Cooper2015-02-17 16:22:30

Sandervp
18.02.2015, 09:07
Thank you Kent Cooper.Your solution works!Is it also possible to create a toolbar button with this command string, and after putting this button, I need to put a letter combination in my commandbar? So I can select different mtext objects time after time. And change the layer time after time?The reason why;The drawing that I have is from a huge industirial area. This area contains a lot of buildings. Every building has got his own letter. And every building has got a lot of details. These details are other dwg's. All the windows, doors and several other objects in building A are having their own (detail)letter combination. These letter combination starts with "A__".It is the same in the other building, only the first letter is the letter from the building.Thank you

Kent Cooper
18.02.2015, 15:04
A routine can ask the User for input of a variety of different kinds, and then use it appropriately.  For example, to change all Mtext starting with a letter followed by two underscore characters to a specific Layer, where the User specifies the first letter and the Layer, something like:



 

(defun C:YourCommandName (/ first lay)
  (setq
    first (getstring "\nInitial letter: ")
    lay (getstring "\nLayer to move to: ")
  ); setq
  (command
    "_.chprop"
    (ssget "_X" (list '(0 . "MTEXT") (cons 1 (strcat first "__*"))))
    ""
    "_layer" lay
    ""
  ); command
); defun
 
A menu button to call that command would contain:

(C:YourCommandName)
 
or you could type the command name in.
 
That's case-sensitive about the initial letter, but it's possible to have it allow them to designate the letter either way.  It could also be made to do other fancy things, such as ask again if they type in more than one letter or a non-alphabetic character, or a Layer name that doesn't exist in the drawing.  If the Layer to move things to is always the same except for that same initial letter, it could construct that itself, rather than ask the User for a Layer name.
 
It sounds like you may be talking about doing something with Blocks or Xrefs, rather than Mtext, which would of course be a little different, but the approach would be similar.
 
Read up on the (list) and (quote) functions to understand the different construction of the (ssget) filter list from the earlier version.

Sandervp
24.02.2015, 09:41
Hello Kent,Sadly, your solution didn't work.I've made a lisp file, called "Test", which contains your routine.I'd load this lisp with apppload.The intention was to move every mtext object containing the 0, to the layer 0.The first 2 steps were oké, but after that, it went wrong.You can see the consequence below: Command: testInitial letter: 0Layer to move to: 0_.chpropSelect objects:Command: TESTUnknown command "TEST".  Press F1 for help.Command: _layerCurrent layer:  "OWT"Enter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]: 0Invalid option keyword.Function cancelledEnter an option [?/Make/Set/New/Rename/ON/OFF/Color/Ltype/LWeight/TRansparency/MATerial/Plot/Freeze/Thaw/LOck/Unlock/stAte/Description/rEconcile]:What went wrong, during this routine?Thank yoyu

Kent Cooper
24.02.2015, 15:26
[QUOTE=Sandervp]
...Sadly, your solution didn't work.
....
The intention was to move every mtext object containing the 0, to the layer 0.
....

Layer to move to: 0
_.chprop
Select objects:
Command: TEST

Unknown command "TEST".  Press F1 for help.
Command: _layer....
[/QUOTE]
 
The routine is written to find [if you give it "0" for the initial letter] any Mtext not merely containing "0", but specifically beginning with "0" followed by two underscore characters, in keeping with your original description [' These letter combination starts with "A__". '].  It works for me.
 
The fact that it returns to the Command: prompt after after only one Select objects: prompt suggests to me that there are no Mtext objects in the drawing that begin with "0__", that is, the (ssget) function returns nil.  The "" [Enter] that is there to complete the selection [which should be fed in at a second Select objects: prompt] then instead recalls the last command entered at the Command: prompt ["TEST"], but inside a (command) function it won't recognize a (defun)'d command name, only native AutoCAD command names, which is why it gives the Unknown command message.  Then the _layer that is there to be an option within the CHPROP command coincidentally happens to also be a command name that it recognizes, so it inappropriately begins that command.
 
Ensure that there is at least one Mtext object whose text content begins with "0__", and see whether it works.  It could easily be made to check whether it finds any Mtext that starts with the right sub-string before proceeding, if you like.  It could also be made to find any Mtext that begins with the initial character without necessarily having "__" following it, if that's appropriate, or with the initial character + "__" but not necessarily at the beginning.  [It could also be made to find all Mtext that merely contains the initial character anywhere in it, if that's what you really want, but that seems likely to change more Mtext than you want to a different Layer, especially if the initial character is a letter].Kent Cooper2015-02-24 15:33:41