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.

149 lines 5.06 kB
var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { AcCmEventManager } from '@mlightcad/common'; /** * Main manager responsible for: * - registry of known system variables * - caching values * - invoking backend getVar/setVar * - dispatching sysvar change events */ var AcDbSysVarManager = /** @class */ (function () { function AcDbSysVarManager() { /** Registered system variable metadata */ this.registry = new Map(); /** Cached current values */ this.cache = new Map(); /** System variable related events */ this.events = { /** * Fired after a system variable is changed directly through the SETVAR command or * by entering the variable name at the command line. */ sysVarChanged: new AcCmEventManager() }; this.registerVar({ name: 'PICKBOX', type: 'number', defaultValue: 0 }); } /** Singleton accessor */ AcDbSysVarManager.instance = function () { if (!this._instance) this._instance = new AcDbSysVarManager(); return this._instance; }; /** * Register one system variable metadata entry. */ AcDbSysVarManager.prototype.registerVar = function (desc) { this.registry.set(desc.name.toUpperCase(), desc); }; /** * Register many system variables. */ AcDbSysVarManager.prototype.registerMany = function (vars) { var _this = this; vars.forEach(function (v) { return _this.registerVar(v); }); }; /** * Get system variable value. */ AcDbSysVarManager.prototype.getVar = function (name) { name = name.toUpperCase(); if (this.cache.has(name)) { return this.cache.get(name); } return undefined; }; /** * Set system variable value. */ AcDbSysVarManager.prototype.setVar = function (name, value) { name = name.toUpperCase(); var descriptor = this.getDescriptor(name); if (descriptor) { var oldVal = this.getVar(name); this.cache.set(name, value); if (descriptor.type !== 'string' && (typeof value === 'string' || value instanceof String)) { if (descriptor.type === 'number') { var num = Number(value); if (Number.isNaN(num)) { throw new Error('Invalid input!'); } value = num; } else if (descriptor.type === 'boolean') { value = this.parseBoolean(value); } } this.events.sysVarChanged.dispatch({ name: name, newVal: value, oldVal: oldVal }); } else { throw new Error("System variable ".concat(name, " not found!")); } }; /** * Get system variable metadata descriptor (if registered). */ AcDbSysVarManager.prototype.getDescriptor = function (name) { return this.registry.get(name.toUpperCase()); }; /** * Get all registered system variable descriptors. */ AcDbSysVarManager.prototype.getAllDescriptors = function () { return __spreadArray([], __read(this.registry.values()), false); }; /** * Parse one string as one boolean value with case-insensitive by ignoring extra spaces * - "true" / "false" * - "t" / "f" * - "1" / "0" * - "yes" / "no" * - "y" / "n" * @param value - One string * @returns - The parsed boolean value */ AcDbSysVarManager.prototype.parseBoolean = function (value) { if (value == null) return false; var v = String(value).trim().toLowerCase(); var trueValues = new Set(['true', 't', '1', 'yes', 'y']); var falseValues = new Set(['false', 'f', '0', 'no', 'n']); if (trueValues.has(v)) return true; if (falseValues.has(v)) return false; return false; }; AcDbSysVarManager._instance = null; return AcDbSysVarManager; }()); export { AcDbSysVarManager }; //# sourceMappingURL=AcDbSysVarManager.js.map