@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.
261 lines • 9.62 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 { AcGePoint3d } from '@mlightcad/geometry-engine';
import { AcDbObjectIterator } from '../misc/AcDbObjectIterator';
import { AcDbSymbolTableRecord } from './AcDbSymbolTableRecord';
/**
* Block table record that serves as a container for entities within drawing databases.
*
* Block table records (BTRs) are used to organize and group entities together.
* There are two special BTRs that are always present in every database:
* - *MODEL_SPACE: Contains entities in model space
* - *PAPER_SPACE: Contains entities in paper space
*
* Each block table record has an origin point and can contain multiple entities.
*
* @example
* ```typescript
* const blockRecord = new AcDbBlockTableRecord();
* blockRecord.name = 'MyBlock';
* blockRecord.origin = new AcGePoint3d(0, 0, 0);
* blockRecord.appendEntity(new AcDbLine());
* ```
*/
var AcDbBlockTableRecord = /** @class */ (function (_super) {
__extends(AcDbBlockTableRecord, _super);
/**
* Creates a new AcDbBlockTableRecord instance.
*
* @example
* ```typescript
* const blockRecord = new AcDbBlockTableRecord();
* ```
*/
function AcDbBlockTableRecord() {
var _this = _super.call(this) || this;
_this._origin = new AcGePoint3d();
_this._layoutId = '';
_this._entities = new Map();
return _this;
}
/**
* Returns true if the specified name is the name of the model space block table record.
*
* Model space is the primary drawing area where most entities are created.
*
* @param name - The name of one block table record.
* @returns True if the specified name is the name of the model space block table record.
*
* @example
* ```typescript
* if (AcDbBlockTableRecord.isModelSapceName('*Model_Space')) {
* console.log('This is the name of the model space block table record.');
* }
* ```
*/
AcDbBlockTableRecord.isModelSapceName = function (name) {
return (name.toLowerCase() == AcDbBlockTableRecord.MODEL_SPACE_NAME.toLowerCase());
};
/**
* Returns true if the specified name is the name of a paper space block table record.
*
* Paper space is used for creating layouts for printing and plotting.
*
* @param name - The name of one block table record.
* @returns True if the specified name is the name of a paper space block table record.
*
* @example
* ```typescript
* if (AcDbBlockTableRecord.isPaperSapceName('*Paper_Space1')) {
* console.log('This is the name of the paper space block table record.');
* }
* ```
*/
AcDbBlockTableRecord.isPaperSapceName = function (name) {
return name
.toLowerCase()
.startsWith(AcDbBlockTableRecord.PAPER_SPACE_NAME_PREFIX.toLowerCase());
};
Object.defineProperty(AcDbBlockTableRecord.prototype, "isModelSapce", {
/**
* Returns true if this is a model space block table record.
*
* Model space is the primary drawing area where most entities are created.
*
* @returns True if this is a model space block table record
*
* @example
* ```typescript
* if (blockRecord.isModelSapce) {
* console.log('This is model space');
* }
* ```
*/
get: function () {
return AcDbBlockTableRecord.isModelSapceName(this.name);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockTableRecord.prototype, "isPaperSapce", {
/**
* Returns true if this is a paper space block table record.
*
* Paper space is used for creating layouts for printing and plotting.
*
* @returns True if this is a paper space block table record
*
* @example
* ```typescript
* if (blockRecord.isPaperSapce) {
* console.log('This is paper space');
* }
* ```
*/
get: function () {
return AcDbBlockTableRecord.isPaperSapceName(this.name);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockTableRecord.prototype, "origin", {
/**
* Gets or sets the base point of the block in WCS coordinates.
*
* This point is the origin of the MCS (Model Coordinate System), which is the
* local WCS for the entities within the block table record.
*
* @returns The origin point of the block
*
* @example
* ```typescript
* const origin = blockRecord.origin;
* blockRecord.origin = new AcGePoint3d(10, 20, 0);
* ```
*/
get: function () {
return this._origin;
},
set: function (value) {
this._origin.copy(value);
},
enumerable: false,
configurable: true
});
Object.defineProperty(AcDbBlockTableRecord.prototype, "layoutId", {
/**
* Gets or sets the object ID of the associated AcDbLayout object in the Layouts dictionary.
*
* This property links the block table record to its corresponding layout object,
* which defines the viewport configuration and display settings for the block.
* For model space blocks, this is typically empty, while paper space blocks
* have a corresponding layout ID.
*
* @returns The object ID of the associated layout
*
* @example
* ```typescript
* const layoutId = blockRecord.layoutId;
* blockRecord.layoutId = 'some-layout-object-id';
* ```
*/
get: function () {
return this._layoutId;
},
set: function (value) {
this._layoutId = value;
},
enumerable: false,
configurable: true
});
/**
* Appends the specified entity or entities to this block table record.
*
* This method adds an entity to the block and sets up the necessary
* relationships between the entity and the block table record.
*
* @param entity - The entity or entities to append to this block table record
*
* @example
* ```typescript
* const line = new AcDbLine();
* blockRecord.appendEntity(line);
* ```
*/
AcDbBlockTableRecord.prototype.appendEntity = function (entity) {
if (Array.isArray(entity)) {
for (var i = 0; i < entity.length; ++i) {
var item = entity[i];
item.database = this.database;
item.ownerId = this.objectId;
this._entities.set(item.objectId, item);
}
}
else {
entity.database = this.database;
entity.ownerId = this.objectId;
this._entities.set(entity.objectId, entity);
}
// When creating one block, it will also go to this function. But we don't want `entityAppended` event
// tiggered in this case. So check whether the block name is name of the model space.
if (this.isModelSapce || this.isPaperSapce) {
this.database.events.entityAppended.dispatch({
database: this.database,
entity: entity
});
}
};
/**
* Creates an iterator object that can be used to iterate over the entities in the block table record.
*
* @returns An iterator object that can be used to iterate over the entities
*
* @example
* ```typescript
* const iterator = blockRecord.newIterator();
* for (const entity of iterator) {
* console.log('Entity:', entity.type);
* }
* ```
*/
AcDbBlockTableRecord.prototype.newIterator = function () {
return new AcDbObjectIterator(this._entities);
};
/**
* Searches for an entity in this block table record with the specified ID.
*
* @param id - The entity ID to search for
* @returns The entity with the specified ID, or undefined if not found
*
* @example
* ```typescript
* const entity = blockRecord.getIdAt('some-entity-id');
* if (entity) {
* console.log('Found entity:', entity.type);
* }
* ```
*/
AcDbBlockTableRecord.prototype.getIdAt = function (id) {
return this._entities.get(id);
};
/** Name constant for model space block table record */
AcDbBlockTableRecord.MODEL_SPACE_NAME = '*MODEL_SPACE';
/** Name prefix for paper space block table records */
AcDbBlockTableRecord.PAPER_SPACE_NAME_PREFIX = '*PAPER_SPACE';
return AcDbBlockTableRecord;
}(AcDbSymbolTableRecord));
export { AcDbBlockTableRecord };
//# sourceMappingURL=AcDbBlockTableRecord.js.map