Multiple_text_mtext_2

Short description

Sort / Purpose: Convert multiple selected TEXT and MTEXT entities into a single MTEXT object while keeping each original line as a separate paragraph.Scope: Works on standard text objects in the current space and merges them into one formatted MTEXT container.Goal: Simplify editing and formatting by replacing several separate text items with one unified MTEXT that preserves visual layout and underlines.

Command:

Command name: T2M2 How to run: Load the LISP file, then type T2M2 in the command line and press Enter.Selection: When prompted, select any combination of TEXT and MTEXT entities to be converted into one MTEXT object.

Description:

Text collection: Builds an internal list of the text strings from all selected TEXT and MTEXT entities in the selection set.Underline conversion: Scans each string for “%%U” underline markers and replaces them with MTEXT underline codes using L and l so underlining is preserved correctly.Paragraph handling: For plain TEXT entities, prefixes each string with P so every original DText line becomes its own paragraph inside the final MTEXT.Concatenation: Concatenates all processed strings into one combined MTEXT content string in selection order.Placement: Creates a new MTEXT object at the insertion point of the first selected entity, using its text width to define the wrapping width.Property transfer: Copies key visual properties from the first entity to the new MTEXT such as Layer, Color, StyleName, and Height, as well as alignment and rotation.Cleanup: After successfully creating the new MTEXT, deletes all original TEXT and MTEXT entities that were part of the selection.No selection case: If no valid text objects are selected, prints a short message instead of doing any changes.

Helper function: (if any)​

Local helper functions inside the routine:*error*: Custom error handler that cleans up the created MTEXT object and any stored entities if an error or user cancel occurs.align_Mt: Determines a compatible MTEXT attachment point from either an existing MTEXT or a TEXT alignment setting so the new MTEXT aligns like the source.Get_MTOffset_pt: Computes a vertical offset point based on MTEXT height, bounding box, and attachment point, prepared for possible alignment offset logic.GetTextWidth: Calculates an appropriate text width for the new MTEXT, using TEXTBOX on TEXT entities or the Width property on MTEXT objects.ReplaceUnderline: Walks through each text string, converting “%%U” underline markers into MTEXT underline on and off codes L and l, ensuring open underlines are properly closed at the end if needed. Used system and VLA calls:vl-load-com: Enables use of ActiveX and VLA functions within the routine.vla-get-ActiveDocument: Gets the current drawing document object.vla-get-ModelSpace / Paperspace: Chooses the correct space object depending on where the user is working.vla-AddMText: Creates the final MTEXT entity containing the combined text.

Functionalities:

Multi text merge: Merges multiple individual TEXT and MTEXT items into a single MTEXT object for easier global editing.Paragraph preservation: Keeps each original TEXT line as its own paragraph inside MTEXT so line structure is not lost.Underline aware: Translates “%%U” underline codes into proper MTEXT underline formatting that works in modern text styles.Style consistency: Uses the first selected text object as a template for layer, color, text style, and height, ensuring the new MTEXT matches the existing annotation style.Alignment and rotation: Attempts to reproduce the same alignment and rotation of the first entity so the new MTEXT appears in the same visual position and orientation.Space aware: Automatically works in model space or paper space depending on the current active space of the document.Automatic cleanup: Removes all original text entities after creation of the MTEXT, preventing duplicates on screen.

Result:

Result: All selected TEXT and MTEXT objects are replaced by a single MTEXT entity that preserves line order, paragraph separation, and underline formatting while matching the original visual style.Cleaner editing: You can now edit the combined content in one MTEXT editor instead of handling many separate text entities.Consistent appearance: The new MTEXT inherits layer, color, style, height, alignment, and rotation from the first selected text item.Preserved structure: Individual lines from plain TEXT are kept as separate paragraphs, and underline codes are correctly converted to MTEXT format.Non reversible by command: Original text entities are deleted once the MTEXT is created, so the change is final unless you use Undo. Copy code: Copy T2M2 LISP

Images, animations etc.

Pixel

XAML code:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Autodesk.Windows;assembly=AdWindows">
    
<src:RibbonToolTip x:Key="Multiple_text_mtext">
<src:RibbonToolTip.ExpandedContent>
<StackPanel>
<TextBlock Background="AntiqueWhite" TextAlignment="Left">
<Bold>Function Syntax: t2m2</Bold>
<LineBreak/>
<Bold>Version: 1 Date: 05.08.2025</Bold>
<LineBreak/>
<LineBreak/>
<Hyperlink>N/A</Hyperlink>
<LineBreak/>
<Bold>Purpose</Bold><LineBreak/>
This routine converts multiple selected TEXT and MTEXT objects into one new MTEXT object, preserving readable paragraph separation and transferring key properties from the first selected object.<LineBreak/>
<LineBreak/>

<Bold>Primary Command</Bold><LineBreak/>
<Span Foreground="DarkRed"><Bold>T2M2</Bold></Span> creates a single MTEXT object from the selection, then deletes the original text entities.<LineBreak/>
<LineBreak/>

<Bold>User Workflow</Bold><LineBreak/>
1. Run <Span Foreground="DarkRed"><Bold>T2M2</Bold></Span> in the AutoCAD command line.<LineBreak/>
2. Select one or more objects filtered to <Span Foreground="DarkRed"><Bold>TEXT</Bold></Span> and <Span Foreground="DarkRed"><Bold>MTEXT</Bold></Span> only.<LineBreak/>
3. The program reads each selected object string, converts underline markers, concatenates them into one MTEXT content string, then inserts a new MTEXT at the first object insertion point.<LineBreak/>
4. The program deletes the original TEXT and MTEXT objects after creating the combined MTEXT.<LineBreak/>
<LineBreak/>

<Bold>Selection Requirements and Outcome</Bold><LineBreak/>
<ul><LineBreak/>
  <li>If no objects are selected, it prints <Span Foreground="DarkRed"><Bold>No text selected.</Bold></Span> and exits.<LineBreak/></li><LineBreak/>
  <li>If objects are selected, it creates exactly one new <Span Foreground="DarkRed"><Bold>MTEXT</Bold></Span> object and removes the originals using <Span Foreground="DarkRed"><Bold>entdel</Bold></Span>.<LineBreak/></li><LineBreak/>
</ul><LineBreak/>
<LineBreak/>

<Bold>Core Functionalities</Bold><LineBreak/>
<ul><LineBreak/>
  <li><Bold>Multi selection conversion</Bold> collects strings from every selected TEXT or MTEXT and merges them into one MTEXT content stream.<LineBreak/></li><LineBreak/>
  <li><Bold>Paragraph preservation</Bold> ensures that each TEXT object becomes a separate paragraph inside the resulting MTEXT by prefixing TEXT strings with the MTEXT paragraph control <Span Foreground="DarkRed"><Bold>\P</Bold></Span>.<LineBreak/></li><LineBreak/>
  <li><Bold>Underline translation</Bold> converts legacy underline sequences <Span Foreground="DarkRed"><Bold>%%U</Bold></Span> into MTEXT underline toggles <Span Foreground="DarkRed"><Bold>\L</Bold></Span> and <Span Foreground="DarkRed"><Bold>\l</Bold></Span> so underline appears correctly in MTEXT.<LineBreak/></li><LineBreak/>
  <li><Bold>Width calculation</Bold> computes the new MTEXT width based on the first selected object using <Span Foreground="DarkRed"><Bold>GetTextWidth</Bold></Span>.<LineBreak/></li><LineBreak/>
  <li><Bold>Property inheritance</Bold> copies key properties from the first selected object to the new MTEXT including layer, color, style name, and height.<LineBreak/></li><LineBreak/>
  <li><Bold>Alignment and rotation matching</Bold> sets MTEXT attachment point and rotation to match the first object so the result is positioned consistently.<LineBreak/></li><LineBreak/>
  <li><Bold>Undo safety and cleanup</Bold> includes an error handler that attempts to close undo marks and delete partially created objects if an error occurs.<LineBreak/></li><LineBreak/>
</ul><LineBreak/>
<LineBreak/>

<Bold>Important Commands and Aspects</Bold><LineBreak/>
<Span Foreground="DarkRed"><Bold>ssget</Bold></Span> filters selection to TEXT and MTEXT.<LineBreak/>
<Span Foreground="DarkRed"><Bold>vla-get-TextString</Bold></Span> reads the displayed text content from each object.<LineBreak/>
<Span Foreground="DarkRed"><Bold>ReplaceUnderline</Bold></Span> converts %%U underline control codes to MTEXT underline toggles.<LineBreak/>
<Span Foreground="DarkRed"><Bold>\P</Bold></Span> is inserted for TEXT items to force new paragraphs in the final MTEXT content.<LineBreak/>
<Span Foreground="DarkRed"><Bold>vla-AddMText</Bold></Span> creates the combined MTEXT using insertion point and computed width.<LineBreak/>
<Span Foreground="DarkRed"><Bold>entdel</Bold></Span> deletes the original selected text entities after successful creation.<LineBreak/>
<LineBreak/>

<Bold>How the New MTEXT is Built</Bold><LineBreak/>
<ul><LineBreak/>
  <li>The routine iterates through the selection set and builds a list of text fragments called textlist.<LineBreak/></li><LineBreak/>
  <li>Each fragment is post processed by <Span Foreground="DarkRed"><Bold>ReplaceUnderline</Bold></Span> to rewrite underline codes appropriately.<LineBreak/></li><LineBreak/>
  <li>If the source object is TEXT, the fragment is prefixed with <Span Foreground="DarkRed"><Bold>\P</Bold></Span> so it becomes a separate paragraph when combined.<LineBreak/></li><LineBreak/>
  <li>All fragments are concatenated using <Span Foreground="DarkRed"><Bold>apply</Bold></Span> and <Span Foreground="DarkRed"><Bold>strcat</Bold></Span> into one combined string named combined.<LineBreak/></li><LineBreak/>
</ul><LineBreak/>
<LineBreak/>

<Bold>Placement, Size, and Formatting Rules</Bold><LineBreak/>
<ul><LineBreak/>
  <li><Bold>Insertion point</Bold> The new MTEXT insertion point is taken from the first selected object using <Span Foreground="DarkRed"><Bold>vla-get-InsertionPoint</Bold></Span>.<LineBreak/></li><LineBreak/>
  <li><Bold>Width</Bold> The new MTEXT width is derived from the first object via <Span Foreground="DarkRed"><Bold>GetTextWidth</Bold></Span>, which uses a textbox measurement for TEXT and MTEXT width property otherwise.<LineBreak/></li><LineBreak/>
  <li><Bold>Layer, color, style, height</Bold> These properties are copied from the first object to the new MTEXT using <Span Foreground="DarkRed"><Bold>vlax-put-property</Bold></Span> across Layer, Color, StyleName, Height.<LineBreak/></li><LineBreak/>
  <li><Bold>Attachment and rotation</Bold> The routine sets the MTEXT attachment point based on the first object alignment using <Span Foreground="DarkRed"><Bold>align_Mt</Bold></Span> and applies the same rotation value.<LineBreak/></li><LineBreak/>
</ul><LineBreak/>
<LineBreak/>

<Bold>Space Resolution</Bold><LineBreak/>
The routine determines whether to create the MTEXT in model space or paper space by checking the active space and MSpace state, then selects the correct container for <Span Foreground="DarkRed"><Bold>vla-AddMText</Bold></Span>.<LineBreak/>
<LineBreak/>

<Bold>Error Handling Behavior</Bold><LineBreak/>
<ul><LineBreak/>
  <li>If an error occurs, it attempts to end an undo mark on the active document if one was started.<LineBreak/></li><LineBreak/>
  <li>If a new MTEXT object was created but the routine fails, it deletes that MTEXT to avoid leaving partial results.<LineBreak/></li><LineBreak/>
  <li>If temporary entities were tracked in a list, it deletes them as well, then prints an error message unless the error is a user cancel or break.<LineBreak/></li><LineBreak/>
</ul><LineBreak/>
<LineBreak/>

<Bold>Operational Summary</Bold><LineBreak/>
This program will require the user to select one or more TEXT or MTEXT objects, then it will combine their strings into a single new MTEXT object placed at the first object position, it will preserve separate paragraphs for TEXT items using <Span Foreground="DarkRed"><Bold>\P</Bold></Span>, it will convert underline markers <Span Foreground="DarkRed"><Bold>%%U</Bold></Span> into MTEXT underline toggles, it will copy key formatting properties from the first selected object, and finally it will delete the original selected entities so only the combined MTEXT remains.<LineBreak/>


</TextBlock>
            
<Grid>
<Image Source="PNG" Stretch="Uniform"/>
</Grid>

<Grid>
<MediaElement
 Source="GIF"
 Stretch="Uniform"
 Visibility="Visible"/>
 </Grid>

</StackPanel>
</src:RibbonToolTip.ExpandedContent>
</src:RibbonToolTip>
</ResourceDictionary>



Additional info:

Share this page:

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Tags: 🏷️ Autocad Lisps, 🏷️ Text_menu
0
Would love your thoughts, please comment.x
()
x