Multiline Text Entity (MTextEnt)
About 2 min
Multiline Text Entity (MTextEnt)
MTextEnt represents multiline text entities in CAD, supporting multi-line text and simple formatting.
Overview
Multiline text can contain multiple lines of text, with support for automatic line wrapping, text styles, and simple format control codes. Suitable for adding longer descriptions, tables, and similar content.
Constructor
import { MTextEnt, Point2D } from 'vjcad';
// Create multiline text
const mtext = new MTextEnt();
mtext.insertionPoint = [100, 100]; // Array format supported
mtext.text = 'First line\nSecond line\nThird line';
mtext.width = 100;
mtext.height = 10;
mtext.setDefaults();Simplified Syntax
The insertionPoint property supports [x, y] array format for coordinates.
Properties
| Property | Type | Description |
|---|---|---|
insertionPoint | Point2D | Insertion point (position determined by attachment) |
text | string | Text content (alias for textString) |
textString | string | Text content (supports newline characters) |
contents | string | Rich text content (includes format codes) |
maxWidth | number | Text box width |
height | number | Text height |
rotation | number | Rotation angle (radians) |
textAttachment | number | Attachment point position |
rowSpcFac | number | Row spacing factor |
styleName | string | Text style name |
textStyle | TextStyle | Text style object (read-only, auto-fetched after setting styleName) |
Attachment Point Positions
1 --- 2 --- 3
| | |
4 --- 5 --- 6
| | |
7 --- 8 --- 9| Value | Position |
|---|---|
| 1 | Top-left |
| 2 | Top-center |
| 3 | Top-right |
| 4 | Middle-left |
| 5 | Center |
| 6 | Middle-right |
| 7 | Bottom-left |
| 8 | Bottom-center |
| 9 | Bottom-right |
Format Control Codes
| Code | Description |
|---|---|
\n | Line break |
\P | Paragraph break |
\\ | Backslash |
\{ | Left curly brace |
\} | Right curly brace |
Examples
Using Fonts
import { Engine, MTextEnt } from 'vjcad';
// Method 1: Use font name directly as styleName (recommended)
// MainView automatically creates a TextStyle with the same name for configured fonts
const mtext1 = new MTextEnt();
mtext1.insertionPoint = [0, 100];
mtext1.text = 'Using Kai font';
mtext1.height = 10;
mtext1.styleName = "simkai"; // Use font name directly
mtext1.setDefaults();
// Using SHX font
const mtext2 = new MTextEnt();
mtext2.insertionPoint = [0, 80];
mtext2.text = 'SHX Font Text';
mtext2.height = 10;
mtext2.styleName = "_default"; // Use font name directly
mtext2.setDefaults();
Engine.addEntities([mtext1, mtext2]);Tips
Fonts must be configured during MainView initialization. See Text Entities - Font Configuration.
Creating Multiline Description Text
import { Engine, MTextEnt, Point2D } from 'vjcad';
const mtext = new MTextEnt();
mtext.position = new Point2D(50, 150);
mtext.text = 'Project: WebCAD Example\n' +
'Date: 2024-01-01\n' +
'Designer: Dev Team';
mtext.width = 120;
mtext.height = 8;
mtext.attachment = 1; // Top-left alignment
mtext.setDefaults();
Engine.addEntities(mtext);Creating Centered Title
import { Engine, MTextEnt, Point2D } from 'vjcad';
const title = new MTextEnt();
title.position = new Point2D(100, 200);
title.text = 'Design Drawing';
title.width = 100;
title.height = 15;
title.attachment = 2; // Top-center alignment
title.setDefaults();
Engine.addEntities(title);Creating Bordered Description
import { Engine, MTextEnt, PolylineEnt, BulgePoints, BulgePoint, Point2D } from 'vjcad';
function drawTextBox(text: string, x: number, y: number, width: number, padding: number = 5) {
// Create text
const mtext = new MTextEnt();
mtext.position = new Point2D(x + padding, y - padding);
mtext.text = text;
mtext.width = width - 2 * padding;
mtext.height = 8;
mtext.attachment = 1; // Top-left
mtext.setDefaults();
// Estimate height (simplified: ~10 units per line)
const lines = text.split('\n').length;
const height = lines * 12 + 2 * padding;
// Create border
const points = new BulgePoints();
points.add(new BulgePoint(new Point2D(x, y), 0));
points.add(new BulgePoint(new Point2D(x + width, y), 0));
points.add(new BulgePoint(new Point2D(x + width, y - height), 0));
points.add(new BulgePoint(new Point2D(x, y - height), 0));
const border = new PolylineEnt(points);
border.isClosed = true;
border.setDefaults();
Engine.addEntities([border, mtext]);
}
drawTextBox('Notes:\n1. All dimensions in millimeters\n2. Unspecified tolerances per GB/T 1804-m', 0, 0, 150);