@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.
447 lines • 18 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 __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
import { AcGeBox3d, AcGeEuler, AcGeMatrix3d, AcGePoint3d, AcGeQuaternion, AcGeVector3d } from '@mlightcad/geometry-engine';
import { AcDbRenderingCache } from '../misc';
import { AcDbEntity } from './AcDbEntity';
/**
* Represents a block reference entity in AutoCAD.
*
* A block reference is used to place, size, and display an instance of the collection
* of entities within the block table record that it references. Block references allow
* you to reuse complex geometry by referencing a block definition multiple times with
* different positions, rotations, and scales.
*
* @example
* ```typescript
* // Create a block reference
* const blockRef = new AcDbBlockReference("MyBlock");
* blockRef.position = new AcGePoint3d(10, 20, 0);
* blockRef.rotation = Math.PI / 4; // 45 degrees
* blockRef.scaleFactors = new AcGePoint3d(2, 2, 1); // 2x scale
*
* // Access block reference properties
* console.log(`Block name: ${blockRef.blockTableRecord?.name}`);
* console.log(`Position: ${blockRef.position}`);
* console.log(`Rotation: ${blockRef.rotation}`);
* ```
*/
var AcDbBlockReference = /** @class */ (function (_super) {
__extends(AcDbBlockReference, _super);
/**
* Creates a new block reference entity.
*
* This constructor initializes a block reference with the specified block name.
* The position is set to the origin, rotation to 0, normal to Z-axis, and scale factors to 1.
*
* @param blockName - The name of the block table record to reference
*
* @example
* ```typescript
* const blockRef = new AcDbBlockReference("MyBlock");
* blockRef.position = new AcGePoint3d(5, 10, 0);
* blockRef.rotation = Math.PI / 6; // 30 degrees
* ```
*/
function AcDbBlockReference(blockName) {
var _this = _super.call(this) || this;
_this._blockName = blockName;
_this._position = new AcGePoint3d();
_this._rotation = 0.0;
_this._normal = new AcGeVector3d(0, 0, 1);
_this._scaleFactors = new AcGePoint3d(1, 1, 1);
return _this;
}
Object.defineProperty(AcDbBlockReference.prototype, "position", {
/**
* Gets the WCS position point (insertion point) of the block reference.
*
* @returns The position point in WCS coordinates
*
* @example
* ```typescript
* const position = blockRef.position;
* console.log(`Block position: ${position.x}, ${position.y}, ${position.z}`);
* ```
*/
get: function () {
return this._position;
},
/**
* Sets the WCS position point (insertion point) of the block reference.
*
* @param value - The new position point
*
* @example
* ```typescript
* blockRef.position = new AcGePoint3d(15, 25, 0);
* ```
*/
set: function (value) {
this._position.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "rotation", {
/**
* Gets the rotation value of the block reference.
*
* The rotation value is relative to the X axis of a coordinate system that is parallel
* to the OCS of the block reference, but has its origin at the position point of the
* block reference. The rotation axis is the Z axis of this coordinate system with
* positive rotations going counterclockwise when looking down the Z axis towards the origin.
*
* @returns The rotation value in radians
*
* @example
* ```typescript
* const rotation = blockRef.rotation;
* console.log(`Rotation: ${rotation} radians (${rotation * 180 / Math.PI} degrees)`);
* ```
*/
get: function () {
return this._rotation;
},
/**
* Sets the rotation value of the block reference.
*
* @param value - The new rotation value in radians
*
* @example
* ```typescript
* blockRef.rotation = Math.PI / 4; // 45 degrees
* ```
*/
set: function (value) {
this._rotation = value;
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "scaleFactors", {
/**
* Gets the X, Y, and Z scale factors for the block reference.
*
* @returns The scale factors as a 3D point
*
* @example
* ```typescript
* const scaleFactors = blockRef.scaleFactors;
* console.log(`Scale factors: ${scaleFactors.x}, ${scaleFactors.y}, ${scaleFactors.z}`);
* ```
*/
get: function () {
return this._scaleFactors;
},
/**
* Sets the X, Y, and Z scale factors for the block reference.
*
* @param value - The new scale factors
*
* @example
* ```typescript
* blockRef.scaleFactors = new AcGePoint3d(2, 1.5, 1); // 2x X scale, 1.5x Y scale
* ```
*/
set: function (value) {
this._scaleFactors.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "normal", {
/**
* Gets the normal vector of the plane containing the block reference.
*
* @returns The normal vector
*
* @example
* ```typescript
* const normal = blockRef.normal;
* console.log(`Normal: ${normal.x}, ${normal.y}, ${normal.z}`);
* ```
*/
get: function () {
return this._normal;
},
/**
* Sets the normal vector of the plane containing the block reference.
*
* @param value - The new normal vector
*
* @example
* ```typescript
* blockRef.normal = new AcGeVector3d(0, 0, 1);
* ```
*/
set: function (value) {
this._normal.copy(value).normalize();
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "blockTableRecord", {
/**
* Gets the block table record referenced by this block reference.
*
* The referenced block table record contains the entities that the block reference will display.
*
* @returns The block table record, or undefined if not found
*
* @example
* ```typescript
* const blockRecord = blockRef.blockTableRecord;
* if (blockRecord) {
* console.log(`Block name: ${blockRecord.name}`);
* }
* ```
*/
get: function () {
return this.database.tables.blockTable.getAt(this._blockName);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "properties", {
/**
* Returns the full property definition for this block reference entity, including
* general group and geometry group.
*
* The geometry group exposes editable properties via {@link AcDbPropertyAccessor}
* so the property palette can update the block reference in real-time.
*
* Each property is an {@link AcDbEntityRuntimeProperty}.
*/
get: function () {
var _this = this;
return {
type: this.type,
groups: [
this.getGeneralProperties(),
{
groupName: 'geometry',
properties: [
{
name: 'blockName',
type: 'float',
editable: false,
accessor: {
get: function () { return _this._blockName; }
}
},
{
name: 'positionX',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.position.x; },
set: function (v) {
_this.position.x = v;
}
}
},
{
name: 'positionY',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.position.y; },
set: function (v) {
_this.position.y = v;
}
}
},
{
name: 'positionZ',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.position.z; },
set: function (v) {
_this.position.z = v;
}
}
},
{
name: 'rotation',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.rotation; },
set: function (v) {
_this.rotation = v;
}
}
},
{
name: 'scaleFactorsX',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.scaleFactors.x; },
set: function (v) {
_this.scaleFactors.x = v;
}
}
},
{
name: 'scaleFactorsY',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.scaleFactors.y; },
set: function (v) {
_this.scaleFactors.y = v;
}
}
},
{
name: 'scaleFactorsZ',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.scaleFactors.z; },
set: function (v) {
_this.scaleFactors.z = v;
}
}
},
{
name: 'normalX',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.normal.x; },
set: function (v) {
_this.normal.x = v;
}
}
},
{
name: 'normalY',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.normal.y; },
set: function (v) {
_this.normal.y = v;
}
}
},
{
name: 'normalZ',
type: 'float',
editable: true,
accessor: {
get: function () { return _this.normal.z; },
set: function (v) {
_this.normal.z = v;
}
}
}
]
}
]
};
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockReference.prototype, "geometricExtents", {
/**
* Gets the geometric extents (bounding box) of this block reference.
*
* This method calculates the bounding box by transforming the geometric extents
* of all entities in the referenced block according to the block reference's
* position, rotation, and scale factors.
*
* @returns The bounding box that encompasses the entire block reference
*
* @example
* ```typescript
* const extents = blockRef.geometricExtents;
* console.log(`Block bounds: ${extents.minPoint} to ${extents.maxPoint}`);
* ```
*/
get: function () {
var e_1, _a;
var box = new AcGeBox3d();
var blockTableRecord = this.blockTableRecord;
if (blockTableRecord != null) {
var entities = blockTableRecord.newIterator();
try {
for (var entities_1 = __values(entities), entities_1_1 = entities_1.next(); !entities_1_1.done; entities_1_1 = entities_1.next()) {
var entity = entities_1_1.value;
box.union(entity.geometricExtents);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (entities_1_1 && !entities_1_1.done && (_a = entities_1.return)) _a.call(entities_1);
}
finally { if (e_1) throw e_1.error; }
}
}
var quaternion = new AcGeQuaternion().setFromEuler(new AcGeEuler(this.rotation, 0, 0));
var matrix = new AcGeMatrix3d();
matrix.compose(this.position, quaternion, this.scaleFactors);
box.applyMatrix4(matrix);
return box;
},
enumerable: false,
configurable: true
});
/**
* @inheritdoc
*/
AcDbBlockReference.prototype.draw = function (renderer) {
var results = [];
var blockTableRecord = this.blockTableRecord;
if (blockTableRecord != null) {
var matrix = this.computeTransformMatrix();
var block = AcDbRenderingCache.instance.draw(renderer, blockTableRecord, this.rgbColor, true, matrix, this.normal);
this.attachEntityInfo(block);
return block;
}
else {
var block = renderer.group(results);
this.attachEntityInfo(block);
return block;
}
};
AcDbBlockReference.prototype.computeTransformMatrix = function () {
var quaternion = new AcGeQuaternion();
quaternion.setFromAxisAngle(AcGeVector3d.Z_AXIS, this.rotation);
return new AcGeMatrix3d().compose(this._position, quaternion, this._scaleFactors);
};
/** The entity type name */
AcDbBlockReference.typeName = 'BlockReference';
return AcDbBlockReference;
}(AcDbEntity));
export { AcDbBlockReference };
//# sourceMappingURL=AcDbBlockReference.js.map