UNPKG

@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.

264 lines 9.09 kB
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 { AcDbObject } from '../base'; import { AcDbObjectIterator } from '../misc'; /** * A database-resident object dictionary that maintains a map between text strings and database objects. * * An instance of this class represents a single object, such as Drawing Symbol Table, to which objects * derived from AcDbObject may be added, accessed, and removed. Entries in an AcDbDictionary must be unique. * Entries consist of a unique AcDbObject and string, which comprises the entry's key name. The key may be * either a text string, or an asterisk ('*') as the first character in the string to signify an anonymous * entry. An anonymous entry's key will be constructed internally by appending an 'A' plus a unique integer * value to the asterisk; for example, '*A13'. When an object is placed in a dictionary, the dictionary is * established as the object's owner, the lookup key string is associated with the object's object ID, and * the dictionary itself is attached to the object as a persistent reactor so that the dictionary is notified * when the object is erased. * * @template TObjectType - The type of objects stored in this dictionary * * @example * ```typescript * const dictionary = new AcDbDictionary<AcDbLayout>(database); * const layout = new AcDbLayout(); * dictionary.setAt('MyLayout', layout); * const retrievedLayout = dictionary.getAt('MyLayout'); * ``` */ var AcDbDictionary = /** @class */ (function (_super) { __extends(AcDbDictionary, _super); /** * Creates a new AcDbDictionary instance. * * @param db - The database this dictionary belongs to * * @example * ```typescript * const dictionary = new AcDbDictionary(database); * ``` */ function AcDbDictionary(db) { var _this = _super.call(this) || this; _this.database = db; _this._recordsByName = new Map(); _this._recordsById = new Map(); return _this; } Object.defineProperty(AcDbDictionary.prototype, "numEntries", { /** * Gets the number of entries in the dictionary. * * @returns The number of entries in the dictionary * * @example * ```typescript * const count = dictionary.numEntries; * console.log(`Dictionary has ${count} entries`); * ``` */ get: function () { return this._recordsByName.size; }, enumerable: false, configurable: true }); /** * Adds a new entry to the dictionary. * * If an entry with the specified key already exists, the existing entry is erased * and replaced with the new one. * * @param key - String representing the object's search key name * @param value - The new object to add to the dictionary * * @example * ```typescript * const layout = new AcDbLayout(); * dictionary.setAt('MyLayout', layout); * ``` */ AcDbDictionary.prototype.setAt = function (key, value) { value.database = this.database; this._recordsByName.set(key, value); this._recordsById.set(value.objectId, value); this.database.events.dictObjetSet.dispatch({ database: this.database, object: value, key: key }); }; /** * Removes the entry specified by name from the dictionary. * * @param name - String representing the entry's key (or name) * @returns True if the entry was found and removed, false otherwise * * @example * ```typescript * const removed = dictionary.remove('MyLayout'); * if (removed) { * console.log('Layout removed successfully'); * } * ``` */ AcDbDictionary.prototype.remove = function (name) { var object = this.getAt(name); if (object) { this._recordsByName.delete(name.toUpperCase()); this._recordsById.delete(this.objectId); this.database.events.dictObjectErased.dispatch({ database: this.database, object: object, key: name }); return true; } return false; }; /** * Removes the entry specified by object ID from the dictionary. * * @param id - ID of the object to delete * @returns True if the entry was found and removed, false otherwise * * @example * ```typescript * const removed = dictionary.removeId('some-object-id'); * ``` */ AcDbDictionary.prototype.removeId = function (id) { var _this = this; var object = this.getIdAt(id); if (object) { this._recordsById.delete(this.objectId); this._recordsByName.forEach(function (value, key) { if (value === object) { _this._recordsByName.delete(key); _this.database.events.dictObjectErased.dispatch({ database: _this.database, object: object, key: key }); } }); return true; } return false; }; /** * Removes all records from the dictionary. * * @example * ```typescript * dictionary.removeAll(); * ``` */ AcDbDictionary.prototype.removeAll = function () { var _this = this; this._recordsByName.forEach(function (value, key) { _this.database.events.dictObjectErased.dispatch({ database: _this.database, object: value, key: key }); }); this._recordsByName.clear(); this._recordsById.clear(); }; /** * Checks if the dictionary contains an object with the specified name. * * @param name - Name to search for * @returns True if the dictionary contains an object with the specified name, false otherwise * * @example * ```typescript * if (dictionary.has('MyLayout')) { * console.log('Layout exists'); * } * ``` */ AcDbDictionary.prototype.has = function (name) { return this._recordsByName.has(name.toUpperCase()); }; /** * Checks if the dictionary contains an object with the specified ID. * * @param id - ID to search for * @returns True if the dictionary contains an object with the specified ID, false otherwise * * @example * ```typescript * if (dictionary.hasId('some-object-id')) { * console.log('Object exists'); * } * ``` */ AcDbDictionary.prototype.hasId = function (id) { return this._recordsById.has(id); }; /** * Gets the object with the specified name from the dictionary. * * @param name - Name of the object to retrieve * @returns The object with the specified name, or undefined if not found * * @example * ```typescript * const layout = dictionary.getAt('MyLayout'); * if (layout) { * console.log('Layout found:', layout); * } * ``` */ AcDbDictionary.prototype.getAt = function (name) { return this._recordsByName.get(name); }; /** * Gets the object with the specified ID from the dictionary. * * @param id - ID of the object to retrieve * @returns The object with the specified ID, or undefined if not found * * @example * ```typescript * const object = dictionary.getIdAt('some-object-id'); * ``` */ AcDbDictionary.prototype.getIdAt = function (id) { return this._recordsById.get(id); }; /** * Creates a new iterator for traversing the dictionary entries. * * @returns A new AcDbObjectIterator for this dictionary * * @example * ```typescript * const iterator = dictionary.newIterator(); * while (iterator.hasNext()) { * const object = iterator.next(); * console.log('Object:', object); * } * ``` */ AcDbDictionary.prototype.newIterator = function () { return new AcDbObjectIterator(this._recordsByName); }; return AcDbDictionary; }(AcDbObject)); export { AcDbDictionary }; //# sourceMappingURL=AcDbDictionary.js.map