@mlightcad/data-model
Version:
The data-model package provides the core classes for interacting with AutoCAD's database and entities. This package mimics AutoCAD ObjectARX's AcDb (Database) classes and implements the drawing database structure that AutoCAD developers are familiar with.
619 lines • 23.7 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
import { AcGeBox3d, AcGePoint3d, AcGeVector3d } from '@mlightcad/geometry-engine';
import { AcGiMTextAttachmentPoint, AcGiMTextFlowDirection } from '@mlightcad/graphic-interface';
import { AcDbEntity } from './AcDbEntity';
/**
* Represents a multiline text (mtext) entity in AutoCAD.
*
* A multiline text entity is a 2D geometric object that displays formatted text
* with support for multiple lines, word wrapping, and rich text formatting.
* MText entities are more advanced than regular text entities and support
* features like background fills, line spacing, and attachment points.
*
* @example
* ```typescript
* // Create a multiline text entity
* const mtext = new AcDbMText();
* mtext.contents = "This is a\nmultiline text\nwith formatting";
* mtext.height = 2.5;
* mtext.width = 20;
* mtext.location = new AcGePoint3d(0, 0, 0);
* mtext.attachmentPoint = AcGiMTextAttachmentPoint.TopLeft;
*
* // Access mtext properties
* console.log(`Contents: ${mtext.contents}`);
* console.log(`Height: ${mtext.height}`);
* console.log(`Width: ${mtext.width}`);
* ```
*/
var AcDbMText = /** @class */ (function (_super) {
__extends(AcDbMText, _super);
/**
* Creates a new multiline text entity.
*
* This constructor initializes an mtext entity with default values.
* The contents are empty, height and width are 0, and the location is at the origin.
*
* @example
* ```typescript
* const mtext = new AcDbMText();
* mtext.contents = "Sample multiline text";
* mtext.height = 3.0;
* mtext.width = 15;
* ```
*/
function AcDbMText() {
var _this = _super.call(this) || this;
_this._contents = '';
_this._height = 0;
_this._width = 0;
_this._lineSpacingFactor = 0.25;
_this._lineSpacingStyle = 0;
_this._backgroundFill = false;
_this._backgroundFillColor = 0xc8c8c8;
_this._backgroundFillTransparency = 1;
_this._backgroundScaleFactor = 1;
_this._rotation = 0;
_this._styleName = '';
_this._location = new AcGePoint3d();
_this._attachmentPoint = AcGiMTextAttachmentPoint.TopLeft;
_this._direction = new AcGeVector3d(1, 0, 0);
_this._drawingDirection = AcGiMTextFlowDirection.LEFT_TO_RIGHT;
return _this;
}
Object.defineProperty(AcDbMText.prototype, "contents", {
/**
* Gets the contents of the mtext object.
*
* This returns a string that contains the contents of the mtext object.
* Formatting data used for word wrap calculations is removed.
*
* @returns The text contents
*
* @example
* ```typescript
* const contents = mtext.contents;
* console.log(`Text contents: ${contents}`);
* ```
*/
get: function () {
return this._contents;
},
/**
* Sets the contents of the mtext object.
*
* @param value - The new text contents
*
* @example
* ```typescript
* mtext.contents = "New multiline\ntext content";
* ```
*/
set: function (value) {
this._contents = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "height", {
/**
* Gets the height of the text.
*
* @returns The text height
*
* @example
* ```typescript
* const height = mtext.height;
* console.log(`Text height: ${height}`);
* ```
*/
get: function () {
return this._height;
},
/**
* Sets the height of the text.
*
* @param value - The new text height
*
* @example
* ```typescript
* mtext.height = 5.0;
* ```
*/
set: function (value) {
this._height = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "width", {
/**
* Gets the maximum width setting used by the MText object for word wrap formatting.
*
* It is possible that none of the lines resulting from word wrap formatting will
* reach this width value. Words which exceed this width value will not be broken,
* but will extend beyond the given width.
*
* @returns The maximum width for word wrap
*
* @example
* ```typescript
* const width = mtext.width;
* console.log(`Text width: ${width}`);
* ```
*/
get: function () {
return this._width;
},
/**
* Sets the maximum width setting used by the MText object for word wrap formatting.
*
* @param value - The new maximum width for word wrap
*
* @example
* ```typescript
* mtext.width = 25;
* ```
*/
set: function (value) {
this._width = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "rotation", {
/**
* Gets the rotation angle of the text.
*
* The rotation angle is relative to the X axis of the text's OCS, with positive
* angles going counterclockwise when looking down the Z axis toward the origin.
*
* @returns The rotation angle in radians
*
* @example
* ```typescript
* const rotation = mtext.rotation;
* console.log(`Rotation: ${rotation} radians (${rotation * 180 / Math.PI} degrees)`);
* ```
*/
get: function () {
return this._rotation;
},
set: function (value) {
this._rotation = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "lineSpacingFactor", {
/**
* The line spacing factor (a value between 0.25 and 4.00).
*/
get: function () {
return this._lineSpacingFactor;
},
set: function (value) {
this._lineSpacingFactor = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "lineSpacingStyle", {
/**
* The line spacing style.
*/
get: function () {
return this._lineSpacingStyle;
},
set: function (value) {
this._lineSpacingStyle = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "backgroundFill", {
/**
* Toggle the background fill on or off. If it is true, background color is turned off, and no
* background fill color has been specified, this function sets the background fill color to
* an RGB value of 200,200,200.
*/
get: function () {
return this._backgroundFill;
},
set: function (value) {
this._backgroundFill = value;
this._backgroundFillColor = 0xc8c8c8;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "backgroundFillColor", {
/**
* The background fill color. This property is valid only if background fill is enable.
*/
get: function () {
return this._backgroundFillColor;
},
set: function (value) {
this._backgroundFillColor = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "backgroundFillTransparency", {
/**
* The background fill transparency. This property is valid only if background fill is enable.
*/
get: function () {
return this._backgroundFillTransparency;
},
set: function (value) {
this._backgroundFillTransparency = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "backgroundScaleFactor", {
/**
* The background scale factor.
*/
get: function () {
return this._backgroundScaleFactor;
},
set: function (value) {
this._backgroundScaleFactor = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "styleName", {
/**
* The style name stored in text ttyle table record and used by this text entity
*/
get: function () {
return this._styleName;
},
set: function (value) {
this._styleName = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "location", {
/**
* The insertion point of this mtext entity.
*/
get: function () {
return this._location;
},
set: function (value) {
this._location.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "attachmentPoint", {
/**
* The attachment point value which determines how the text will be oriented around the insertion point
* of the mtext object. For example, if the attachment point is AcGiAttachmentPoint.MiddleCenter, then
* the text body will be displayed such that the insertion point appears at the geometric center of the
* text body.
*/
get: function () {
return this._attachmentPoint;
},
set: function (value) {
this._attachmentPoint = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "direction", {
/**
* Represent the X axis ("horizontal") for the text. This direction vector is used to determine the text
* flow direction.
*/
get: function () {
return this._direction;
},
set: function (value) {
this._direction.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "drawingDirection", {
get: function () {
return this._drawingDirection;
},
set: function (value) {
this._drawingDirection = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "geometricExtents", {
/**
* @inheritdoc
*/
get: function () {
// TODO: Implement it correctly
return new AcGeBox3d();
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbMText.prototype, "properties", {
/**
* Returns the full property definition for this mtext entity, including
* general group and geometry group.
*
* The geometry group exposes editable start/end coordinates via
* {@link AcDbPropertyAccessor} so the property palette can update
* the mtext in real-time.
*
* Each property is an {@link AcDbEntityRuntimeProperty}.
*/
get: function () {
var _this = this;
return {
type: this.type,
groups: [
this.getGeneralProperties(),
{
groupName: 'text',
properties: [
{
name: 'contents',
type: 'string',
editable: true,
accessor: {
get: function () { return _this.contents; },
set: function (v) {
_this.contents = v;
}
}
},
{
name: 'styleName',
type: 'string',
editable: true,
accessor: {
get: function () { return _this.styleName; },
set: function (v) {
_this.styleName = v;
}
}
},
{
name: 'attachmentPoint',
type: 'enum',
editable: true,
options: [
{ label: AcGiMTextAttachmentPoint[1], value: 1 },
{ label: AcGiMTextAttachmentPoint[2], value: 2 },
{ label: AcGiMTextAttachmentPoint[3], value: 3 },
{ label: AcGiMTextAttachmentPoint[4], value: 4 },
{ label: AcGiMTextAttachmentPoint[5], value: 5 },
{ label: AcGiMTextAttachmentPoint[6], value: 6 },
{ label: AcGiMTextAttachmentPoint[7], value: 7 },
{ label: AcGiMTextAttachmentPoint[8], value: 8 },
{ label: AcGiMTextAttachmentPoint[9], value: 9 }
],
accessor: {
get: function () { return _this.attachmentPoint; },
set: function (v) {
_this.attachmentPoint = v;
}
}
},
{
name: 'drawingDirection',
type: 'enum',
editable: true,
options: [
{ label: AcGiMTextFlowDirection[1], value: 1 },
{ label: AcGiMTextFlowDirection[2], value: 2 },
{ label: AcGiMTextFlowDirection[3], value: 3 },
{ label: AcGiMTextFlowDirection[4], value: 4 },
{ label: AcGiMTextFlowDirection[5], value: 5 }
],
accessor: {
get: function () { return _this.drawingDirection; },
set: function (v) {
_this.drawingDirection = v;
}
}
},
{
name: 'textHeight',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.height; },
set: function (v) {
_this.height = v;
}
}
},
{
name: 'rotation',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.rotation; },
set: function (v) {
_this.rotation = v;
}
}
},
{
name: 'lineSpacingFactor',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.lineSpacingFactor; },
set: function (v) {
_this.lineSpacingFactor = v;
}
}
},
{
name: 'definedWidth',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.width; },
set: function (v) {
_this.width = v;
}
}
},
{
name: 'directionX',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.direction.x; },
set: function (v) {
_this.direction.x = v;
}
}
},
{
name: 'directionY',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.direction.y; },
set: function (v) {
_this.direction.y = v;
}
}
},
{
name: 'directionZ',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.direction.z; },
set: function (v) {
_this.direction.z = v;
}
}
}
]
},
{
groupName: 'geometry',
properties: [
{
name: 'locationX',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.location.x; },
set: function (v) {
_this.location.x = v;
}
}
},
{
name: 'locationY',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.location.y; },
set: function (v) {
_this.location.y = v;
}
}
},
{
name: 'locationZ',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.location.z; },
set: function (v) {
_this.location.z = v;
}
}
}
]
}
]
};
},
enumerable: false,
configurable: true
});
AcDbMText.prototype.getTextStyle = function () {
var textStyleTable = this.database.tables.textStyleTable;
var style = textStyleTable.getAt(this.styleName);
if (!style) {
style = (textStyleTable.getAt('STANDARD') ||
textStyleTable.getAt('Standard'));
}
return style.textStyle;
};
/**
* Draws this entity using the specified renderer.
*
* @param renderer - The renderer to use for drawing
* @param delay - The flag to delay creating one rendered entity and just create one dummy
* entity. Renderer can delay heavy calculation operation to avoid blocking UI when this flag
* is true.
* @returns The rendered entity, or undefined if drawing failed
*
* @example
* ```typescript
* const renderedEntity = entity.draw(renderer);
* ```
*/
AcDbMText.prototype.draw = function (renderer, delay) {
var mtextData = {
text: this.contents,
height: this.height,
width: this.width,
position: this.location,
rotation: this.rotation,
directionVector: this.direction,
attachmentPoint: this.attachmentPoint,
drawingDirection: this.drawingDirection,
lineSpaceFactor: this.lineSpacingFactor
};
var textStyle = __assign(__assign({}, this.getTextStyle()), { color: this.rgbColor });
return renderer.mtext(mtextData, textStyle, delay);
};
/** The entity type name */
AcDbMText.typeName = 'MText';
return AcDbMText;
}(AcDbEntity));
export { AcDbMText };
//# sourceMappingURL=AcDbMText.js.map