@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.
255 lines • 9.07 kB
JavaScript
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 { AcCmColor } from '@mlightcad/common';
import { AcDbBlockTableRecord } from '../database';
/**
* Internal class used to cache rendered results to avoid duplicated rendering.
*
* This class can be used to improve performance when rendering block references.
* Because different colors will result in different materials, the block name and
* color are used together to create the cache key.
*
* @internal
*
* @example
* ```typescript
* const cache = AcDbRenderingCache.instance;
* const key = cache.createKey('MyBlock', 0xFF0000);
* const renderedEntity = cache.draw(renderer, blockRecord, 0xFF0000);
* ```
*/
var AcDbRenderingCache = /** @class */ (function () {
/**
* Creates a new AcDbRenderingCache instance.
*
* @example
* ```typescript
* const cache = new AcDbRenderingCache();
* ```
*/
function AcDbRenderingCache() {
this._blocks = new Map();
}
Object.defineProperty(AcDbRenderingCache, "instance", {
/**
* Gets the singleton instance of the rendering cache.
*
* @returns The singleton instance of AcDbRenderingCache
*
* @example
* ```typescript
* const cache = AcDbRenderingCache.instance;
* ```
*/
get: function () {
if (!this._instance) {
this._instance = new AcDbRenderingCache();
}
return this._instance;
},
enumerable: false,
configurable: true
});
/**
* Creates a cache key by combining the block name and color.
*
* @param name - The block name
* @param color - The color value
* @returns A unique key for the cache entry
*
* @example
* ```typescript
* const key = cache.createKey('MyBlock', 0xFF0000);
* // Returns: "MyBlock_16711680"
* ```
*/
AcDbRenderingCache.prototype.createKey = function (name, color) {
return "".concat(name, "_").concat(color);
};
/**
* Stores rendering results of a block in the cache.
*
* @param key - The key for the rendering results
* @param group - The rendering results to store
* @returns The stored rendering results (deep cloned)
*
* @example
* ```typescript
* const renderedEntity = cache.set(key, entity);
* ```
*/
AcDbRenderingCache.prototype.set = function (key, group) {
group = group.fastDeepClone();
this._blocks.set(key, group);
return group;
};
/**
* Gets rendering results with the specified key.
*
* @param name - The key of the rendering results
* @returns The rendering results with the specified key, or undefined if not found
*
* @example
* ```typescript
* const cachedEntity = cache.get('MyBlock_16711680');
* if (cachedEntity) {
* // Use cached entity
* }
* ```
*/
AcDbRenderingCache.prototype.get = function (name) {
var block = this._blocks.get(name);
if (block) {
block = block.fastDeepClone();
}
return block;
};
/**
* Checks if rendering results with the specified key exist in the cache.
*
* @param name - The key to check
* @returns True if the key exists in the cache, false otherwise
*
* @example
* ```typescript
* if (cache.has('MyBlock_16711680')) {
* console.log('Cached result found');
* }
* ```
*/
AcDbRenderingCache.prototype.has = function (name) {
return this._blocks.has(name);
};
/**
* Clears all cached rendering results.
*
* @example
* ```typescript
* cache.clear();
* console.log('Cache cleared');
* ```
*/
AcDbRenderingCache.prototype.clear = function () {
this._blocks.clear();
};
/**
* Draws a block table record and optionally caches the result.
*
* This method renders the block table record using the specified renderer
* and color, and optionally stores the result in the cache for future use.
*
* @param renderer - The renderer to use for drawing
* @param blockTableRecord - The block table record to draw
* @param color - The color to use for rendering
* @param cache - Whether to cache the rendering result (default: true)
* @param transform - Optional transformation matrix to apply
* @param normal - Optional normal vector
* @returns The rendered entity
*
* @example
* ```typescript
* const renderedEntity = cache.draw(
* renderer,
* blockRecord,
* 0xFF0000,
* true,
* transform,
* normal
* );
* ```
*/
AcDbRenderingCache.prototype.draw = function (renderer, blockTableRecord, color, cache, transform, normal) {
var e_1, _a;
var _b;
if (cache === void 0) { cache = true; }
var results = [];
if (blockTableRecord != null) {
var key = this.createKey(blockTableRecord.name, color);
var block = void 0;
if (this.has(key)) {
block = this.get(key);
}
else {
var basePoint = (_b = renderer.basePoint) === null || _b === void 0 ? void 0 : _b.clone();
renderer.basePoint = undefined;
var entities = blockTableRecord.newIterator();
var isFirstEntity = true;
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;
// If the color of this entity is 'byBlock', then store the original color of this entity color
// and set the color of this entity to block's color. After renderering this entity, restore
// its original color
if (entity.color.isByBlock && color) {
_tmpColor.copy(entity.color);
entity.color.color = color;
this.addEntity(entity, results, renderer);
entity.color.copy(_tmpColor);
}
else {
this.addEntity(entity, results, renderer);
}
if (isFirstEntity) {
var firstEntity = results[0];
renderer.basePoint = firstEntity.basePoint;
isFirstEntity = false;
}
}
}
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; }
}
block = renderer.group(results);
if (block && cache) {
// If one block is one transient block whose name starts with '*U', don't cache it
if (AcDbBlockTableRecord.name &&
!AcDbBlockTableRecord.name.startsWith('*U')) {
this.set(key, block);
}
}
renderer.basePoint = basePoint;
}
if (block && transform) {
block.applyMatrix(transform);
if (normal && (normal.x != 0 || normal.y != 0 || normal.z != 1)) {
transform.setFromExtrusionDirection(normal);
block.applyMatrix(transform);
}
}
return block;
}
else {
return renderer.group(results);
}
};
AcDbRenderingCache.prototype.addEntity = function (entity, results, renderer) {
var object = entity.draw(renderer);
if (object) {
this.attachEntityInfo(object, entity);
results.push(object);
}
};
AcDbRenderingCache.prototype.attachEntityInfo = function (target, source) {
target.objectId = source.objectId;
target.ownerId = source.ownerId;
target.layerName = source.layer;
target.visible = source.visibility;
};
return AcDbRenderingCache;
}());
export { AcDbRenderingCache };
var _tmpColor = /*@__PURE__*/ new AcCmColor();
//# sourceMappingURL=AcDbRenderingCache.js.map