@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.
541 lines (540 loc) • 20.9 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 __());
};
})();
import { AcGeMatrix3d, AcGePoint3d, AcGeVector3d } from '@mlightcad/geometry-engine';
import { AcGiArrowType } from '@mlightcad/graphic-interface';
import { AcDbDimStyleTableRecord } from '../../database';
import { AcDbRenderingCache } from '../../misc';
import { AcDbEntity } from '../AcDbEntity';
/**
* Defines the line spacing style for dimension text.
*/
export var AcDbLineSpacingStyle;
(function (AcDbLineSpacingStyle) {
/** At least the specified spacing */
AcDbLineSpacingStyle[AcDbLineSpacingStyle["AtLeast"] = 1] = "AtLeast";
/** Exactly the specified spacing */
AcDbLineSpacingStyle[AcDbLineSpacingStyle["Exactly"] = 2] = "Exactly";
})(AcDbLineSpacingStyle || (AcDbLineSpacingStyle = {}));
/**
* Abstract base class for all dimension entity types in AutoCAD.
*
* This class provides the fundamental functionality for all dimension entities,
* including dimension text, style management, arrow handling, and measurement
* calculations. The appearance of dimensions is controlled by dimension variable
* settings and dimension styles.
*
* @example
* ```typescript
* class MyDimension extends AcDbDimension {
* // Implementation for specific dimension type
* draw(renderer: AcGiRenderer) {
* // Custom drawing logic
* }
* }
* ```
*/
var AcDbDimension = /** @class */ (function (_super) {
__extends(AcDbDimension, _super);
/**
* Creates a new dimension entity.
*
* This constructor initializes a dimension with default values.
* Subclasses should override this constructor to set up dimension-specific properties.
*
* @example
* ```typescript
* const dimension = new MyDimension();
* dimension.dimensionText = "10.0";
* dimension.textPosition = new AcGePoint3d(5, 5, 0);
* ```
*/
function AcDbDimension() {
var _this = _super.call(this) || this;
_this._dimBlockId = null;
_this._dimBlockPosition = new AcGePoint3d();
_this._dimensionStyleName = null;
_this._dimensionText = null;
_this._textLineSpacingFactor = 1.0;
_this._textLineSpacingStyle = AcDbLineSpacingStyle.AtLeast;
_this._textPosition = new AcGePoint3d();
_this._textRotation = 0;
_this._normal = new AcGeVector3d(0, 0, 1);
return _this;
}
Object.defineProperty(AcDbDimension.prototype, "dimBlockId", {
/**
* Gets the block table record ID containing the entities that this dimension displays.
*
* @returns The block table record ID, or null if not set
*
* @example
* ```typescript
* const blockId = dimension.dimBlockId;
* console.log(`Dimension block ID: ${blockId}`);
* ```
*/
get: function () {
return this._dimBlockId;
},
/**
* Sets the block table record ID for this dimension.
*
* @param value - The block table record ID, or null to clear
*
* @example
* ```typescript
* dimension.dimBlockId = "MyDimensionBlock";
* ```
*/
set: function (value) {
this._dimBlockId = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "dimBlockPosition", {
/**
* Gets the relative position point of the block referenced by the dimension (in WCS coordinates).
* The block position is the insertion point of the block referenced by the dimension, relative to
* the primary definition point (DXF group code 10) of the dimension itself.
*
* The block position (in WCS) is added to the dimension primary definition point (also in WCS) to
* get the actual insertion point for the anonymous block that is referenced by the dimension. So,
* for example, changing a dimension's block position from (0,0,0) to (0,0,1) (in WCS) will cause
* the dimension to display as though it has been moved 1 unit along the WCS Z axis.
*
* For a dimension newly created via any dimension command, the block position will be (0,0,0).
* For copies of existing dimensions, or if a dimension is moved, the block position will be the
* offset vector (in WCS) from where the original dimension was located. For example, moving a
* dimension 1 unit down the WCS Y axis will cause AutoCAD to update the block position value from
* (0,0,0) to (0,-1,0) (in WCS coordinates).
*
* The position point is used for DXF group code 12.
*
* @returns The relative position point of the block referenced by the dimension.
*
*/
get: function () {
return this._dimBlockPosition;
},
/**
* Sets the relative position point of the block referenced by the dimension (in WCS coordinates).
* The block position is the insertion point of the block referenced by the dimension, relative to
* the primary definition point (DXF group code 10) of the dimension itself.
*
* The block position (in WCS) is added to the dimension primary definition point (also in WCS) to
* get the actual insertion point for the anonymous block that is referenced by the dimension. So,
* for example, changing a dimension's block position from (0,0,0) to (0,0,1) (in WCS) will cause
* the dimension to display as though it has been moved 1 unit along the WCS Z axis.
*
* For a dimension newly created via any dimension command, the block position will be (0,0,0).
* For copies of existing dimensions, or if a dimension is moved, the block position will be the
* offset vector (in WCS) from where the original dimension was located. For example, moving a
* dimension 1 unit down the WCS Y axis will cause AutoCAD to update the block position value from
* (0,0,0) to (0,-1,0) (in WCS coordinates).
*
* The position point is used for DXF group code 12.
*
* @param value - The relative position point of the block referenced by the dimension.
*/
set: function (value) {
this._dimBlockPosition.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "dimensionStyleName", {
/**
* Gets the dimension style name used by this dimension.
*
* @returns The dimension style name, or null if not set
*
* @example
* ```typescript
* const styleName = dimension.dimensionStyleName;
* console.log(`Dimension style: ${styleName}`);
* ```
*/
get: function () {
return this._dimensionStyleName;
},
/**
* Sets the dimension style name for this dimension.
*
* @param value - The dimension style name, or null to use default
*
* @example
* ```typescript
* dimension.dimensionStyleName = "Standard";
* ```
*/
set: function (value) {
this._dimensionStyleName = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "dimensionStyle", {
/**
* Gets the dimension style used by this dimension.
*
* This method returns the dimension style record associated with this dimension.
* If no style is specified, it returns the default dimension style.
*
* @returns The dimension style record
*
* @example
* ```typescript
* const style = dimension.dimensionStyle;
* console.log(`Style name: ${style.name}`);
* ```
*/
get: function () {
if (this._dimStyle == null) {
var dimStyle = undefined;
if (this.dimensionStyleName) {
dimStyle = this.database.tables.dimStyleTable.getAt(this.dimensionStyleName);
}
if (dimStyle == null)
dimStyle = new AcDbDimStyleTableRecord();
this._dimStyle = dimStyle;
}
return this._dimStyle;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "dimensionText", {
/**
* Gets the user-supplied dimension annotation text string.
*
* This string can contain multiline text formatting characters. The text can be:
* - Empty string ('') for default text only
* - Text with angle brackets for mixed default and user text (e.g., 'This is the default text <>')
* - Period ('.') for no text
* - User-defined text only
*
* @returns The dimension text string
*
* @example
* ```typescript
* const text = dimension.dimensionText;
* console.log(`Dimension text: ${text}`);
* ```
*/
get: function () {
return this._dimensionText;
},
set: function (value) {
this._dimensionText = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "measurement", {
/**
* The current measurement value for this dimension
*/
get: function () {
return this._measurement;
},
set: function (value) {
this._measurement = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "textLineSpacingFactor", {
/**
* The line spacing factor (a value between 0.25 and 4.00).
*/
get: function () {
return this._textLineSpacingFactor;
},
set: function (value) {
this._textLineSpacingFactor = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "textLineSpacingStyle", {
/**
* The line spacing style for the dimension.
*/
get: function () {
return this._textLineSpacingStyle;
},
set: function (value) {
this._textLineSpacingStyle = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "textPosition", {
/**
* The dimension's text position point. This is the middle center point of the text (which is itself an
* MText object with middle-center justification).
*/
get: function () {
return this._textPosition;
},
set: function (value) {
this._textPosition.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "textRotation", {
/**
* The rotation angle (in radians) of the dimension's annotation text. This is the angle from the
* dimension's horizontal axis to the horizontal axis used by the text. The angle is in the dimension's
* OCS X-Y plane with positive angles going counterclockwise when looking down the OCS Z axis towards
* the OCS origin. The value obtained from: (2 * pi) + the dimension's text rotation angle--the
* dimension's horizontal rotation angle
*/
get: function () {
return this._textRotation;
},
set: function (value) {
this._textRotation = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "normal", {
/**
* Gets the normal vector of the plane containing the dimension.
*
* @returns The normal vector
*/
get: function () {
return this._normal;
},
/**
* Sets the normal vector of the plane containing the dimension.
*
* @param value - The new normal vector
*/
set: function (value) {
this._normal.copy(value).normalize();
},
enumerable: false,
configurable: true
});
/**
* @inheritdoc
*/
AcDbDimension.prototype.draw = function (renderer) {
if (this.dimBlockId) {
var blockTableRecord = this.database.tables.blockTable.getAt(this.dimBlockId);
if (blockTableRecord) {
var matrix = new AcGeMatrix3d().makeTranslation(this.dimBlockPosition);
var group_1 = AcDbRenderingCache.instance.draw(renderer, blockTableRecord, this.rgbColor, false, matrix, this.normal);
this.attachEntityInfo(group_1);
return group_1;
}
}
var group = renderer.group([]);
this.attachEntityInfo(group);
return group;
};
AcDbDimension.prototype.drawFirstArrow = function (renderer) {
var blockTableRecord = this.database.tables.blockTable.getAt(this.firstArrowType);
if (blockTableRecord) {
return AcDbRenderingCache.instance.draw(renderer, blockTableRecord, this.rgbColor, false);
}
return undefined;
};
AcDbDimension.prototype.drawSecondArrow = function (renderer) {
var blockTableRecord = this.database.tables.blockTable.getAt(this.secondArrowType);
if (blockTableRecord) {
return AcDbRenderingCache.instance.draw(renderer, blockTableRecord, this.rgbColor, false);
}
return undefined;
};
Object.defineProperty(AcDbDimension.prototype, "arrowScaleFactor", {
get: function () {
var dimStyle = this.dimensionStyle;
// TODO:
// 1. DIMASZ has no effect when DIMTSZ is other than zero.
// 2. Calculate scale factor based on unit
return dimStyle.dimasz;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "firstArrowStyle", {
/**
* Arrow style of the first arrow
*/
get: function () {
return {
type: this.firstArrowType,
scale: this.arrowScaleFactor,
appended: this.isAppendArrow,
visible: this.dimensionStyle.dimse1 == 0
};
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "secondArrowStyle", {
/**
* Arrow style of the second arrow
*/
get: function () {
return {
type: this.secondArrowType,
scale: this.arrowScaleFactor,
appended: this.isAppendArrow,
visible: this.dimensionStyle.dimse2 == 0
};
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "isAppendArrow", {
/**
* The flag to determinate how to attach arrow to endpoint of the line.
* - true: append arrow to endpoint of the line
* - false: overlap arrow with endpoint of the line
*/
get: function () {
return true;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "firstArrowTypeBtrId", {
/**
* The BTR id associated with the first arrow type
*/
get: function () {
var dimStyle = this.dimensionStyle;
return dimStyle.dimsah == 0 ? dimStyle.dimblk : dimStyle.dimblk1;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "firstArrowType", {
/**
* The first arrow type
*/
get: function () {
var btrId = this.firstArrowTypeBtrId;
return this.getArrowName(btrId);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "secondArrowTypeBtrId", {
/**
* The BTR id associated with the second arrow type
*/
get: function () {
var dimStyle = this.dimensionStyle;
return dimStyle.dimsah == 0 ? dimStyle.dimblk : dimStyle.dimblk2;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "secondArrowType", {
/**
* The second arrow type
*/
get: function () {
var btrId = this.secondArrowTypeBtrId;
return this.getArrowName(btrId);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbDimension.prototype, "arrowLineCount", {
/**
* The maximum number of arrow lines of this dimension
*/
get: function () {
return 1;
},
enumerable: false,
configurable: true
});
/**
* Get line arrow style of the specified line according to its type (extension line or dimension
* line). Return undefined if no line arrow style applied.
* @param line Input line to get its line style
* @returns Return the line style of the specified line. Return undefined if no line arrow style applied.
*/
// @ts-expect-error not use '_' prefix so that typedoc can the correct parameter to generate doc
AcDbDimension.prototype.getLineArrowStyle = function (line) {
return undefined;
};
/**
* Find the point `p3` on a line along the line direction at a distance `length` from `p2`.
*
* @param p1 - The start point of the line.
* @param p2 - The end point of the line.
* @param length - The distance from `p2` to `p3`.
* @returns Return the point `p3`.
*/
AcDbDimension.prototype.findPointOnLine1 = function (p1, p2, length) {
// Calculate the direction vector from p1 to p2
var direction = new AcGePoint3d().subVectors(p2, p1).normalize();
// Calculate p3 by moving in the direction from p2 by the specified length
var p3 = new AcGePoint3d(p2).addScaledVector(direction, length);
return p3;
};
/**
* Find the point `p2` on a line starting from `p1` at a specified angle
* and at a distance `length` from `p1`.
*
* @param p1 - The start point of the line.
* @param angle - The angle of the line in radians relative to the x-axis.
* @param length - The distance from `p1` to `p2`.
* @returns Return the point `p2`.
*/
AcDbDimension.prototype.findPointOnLine2 = function (p1, angle, length) {
// Calculate the new point p2
var x = p1.x + length * Math.cos(angle);
var y = p1.y + length * Math.sin(angle);
return { x: x, y: y };
};
/**
* Adjust start point and end point of extension line according current dimension style
* @param extensionLine Input extension line to adjust its start point and end point
*/
AcDbDimension.prototype.adjustExtensionLine = function (extensionLine) {
var dimStyle = this.dimensionStyle;
extensionLine.extend(dimStyle.dimexe);
extensionLine.extend(-dimStyle.dimexo, true);
};
/**
* Get dimension style name by dimension block id
* @param id Input dimension block id
* @returns Return dimension style name by dimension block id
*/
AcDbDimension.prototype.getArrowName = function (id) {
var blockTableRecord = this.database.tables.blockTable.getIdAt(id);
return (blockTableRecord
? blockTableRecord.name.toUpperCase()
: AcGiArrowType.Closed);
};
/** The entity type name */
AcDbDimension.typeName = 'Dimension';
return AcDbDimension;
}(AcDbEntity));
export { AcDbDimension };
//# sourceMappingURL=AcDbDimension.js.map