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.

451 lines 15.7 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 __()); }; })(); 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 { AcGeBox2d, AcGeBox3d, AcGePoint2d, AcGePoint3d, AcGeVector2d } from '@mlightcad/geometry-engine'; import { AcDbEntity } from './AcDbEntity'; /** * Defines the clip boundary type for raster images. */ export var AcDbRasterImageClipBoundaryType; (function (AcDbRasterImageClipBoundaryType) { /** Undefined state */ AcDbRasterImageClipBoundaryType[AcDbRasterImageClipBoundaryType["Invalid"] = 0] = "Invalid"; /** Rectangle aligned with the image pixel coordinate system */ AcDbRasterImageClipBoundaryType[AcDbRasterImageClipBoundaryType["Rect"] = 1] = "Rect"; /** Polygon with points entirely within the image boundary */ AcDbRasterImageClipBoundaryType[AcDbRasterImageClipBoundaryType["Poly"] = 2] = "Poly"; })(AcDbRasterImageClipBoundaryType || (AcDbRasterImageClipBoundaryType = {})); /** * Defines the display options for raster images. */ export var AcDbRasterImageImageDisplayOpt; (function (AcDbRasterImageImageDisplayOpt) { /** Show image (or draw frame only) */ AcDbRasterImageImageDisplayOpt[AcDbRasterImageImageDisplayOpt["Show"] = 1] = "Show"; /** Show rotates images (or draw frame only) */ AcDbRasterImageImageDisplayOpt[AcDbRasterImageImageDisplayOpt["ShowUnAligned"] = 2] = "ShowUnAligned"; /** Clip image */ AcDbRasterImageImageDisplayOpt[AcDbRasterImageImageDisplayOpt["Clip"] = 4] = "Clip"; /** Use transparent background for bitonal images (or use opaque background color) */ AcDbRasterImageImageDisplayOpt[AcDbRasterImageImageDisplayOpt["Transparent"] = 8] = "Transparent"; })(AcDbRasterImageImageDisplayOpt || (AcDbRasterImageImageDisplayOpt = {})); /** * Represents a raster image entity in AutoCAD. * * The AcDbRasterImage entity (or "image entity") works with the AcDbRasterImageDef object * (or "image definition object") to implement raster images inside AutoCAD. The relationship * between these two classes is much like the relationship between an AutoCAD block definition * object and a block insert entity. * * Two or more image entities can be linked to a single image definition object. Since each * image entity has its own clip boundary, this is an efficient way to display different * regions of a single raster image at different positions in the drawing. * * @example * ```typescript * // Create a raster image entity * const rasterImage = new AcDbRasterImage(); * rasterImage.position = new AcGePoint3d(0, 0, 0); * rasterImage.width = 100; * rasterImage.height = 75; * rasterImage.scale = new AcGeVector2d(1, 1); * rasterImage.rotation = 0; * rasterImage.brightness = 50; * rasterImage.contrast = 50; * * // Access raster image properties * console.log(`Position: ${rasterImage.position}`); * console.log(`Width: ${rasterImage.width}`); * console.log(`Height: ${rasterImage.height}`); * ``` */ var AcDbRasterImage = /** @class */ (function (_super) { __extends(AcDbRasterImage, _super); /** * Creates a new raster image entity. * * This constructor initializes a raster image with default values. * The brightness and contrast are set to 50, fade to 0, position to origin, * scale to (1,1), rotation to 0, and clip boundary type to Rect. * * @example * ```typescript * const rasterImage = new AcDbRasterImage(); * rasterImage.position = new AcGePoint3d(10, 20, 0); * rasterImage.width = 200; * rasterImage.height = 150; * ``` */ function AcDbRasterImage() { var _this = _super.call(this) || this; _this._brightness = 50; _this._contrast = 50; _this._fade = 0; _this._width = 0; _this._height = 0; _this._position = new AcGePoint3d(); _this._scale = new AcGeVector2d(1, 1); _this._rotation = 0; _this._clipBoundaryType = AcDbRasterImageClipBoundaryType.Rect; _this._clipBoundary = []; _this._isClipped = false; _this._isShownClipped = false; _this._isImageShown = true; _this._isImageTransparent = false; _this._imageDefId = ''; return _this; } Object.defineProperty(AcDbRasterImage.prototype, "brightness", { /** * Gets the current brightness value of the image. * * @returns The brightness value (0-100) * * @example * ```typescript * const brightness = rasterImage.brightness; * console.log(`Image brightness: ${brightness}`); * ``` */ get: function () { return this._brightness; }, set: function (value) { this._brightness = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "contrast", { /** * The current contrast value of the image. */ get: function () { return this._contrast; }, set: function (value) { this._contrast = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "fade", { /** * The current fade value of the image. */ get: function () { return this._fade; }, set: function (value) { this._fade = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "height", { /** * The height of the image. */ get: function () { return this._height; }, set: function (value) { this._height = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "width", { /** * The width of the image. */ get: function () { return this._width; }, set: function (value) { this._width = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "position", { /** * The position of the image in wcs. */ get: function () { return this._position; }, set: function (value) { this._position = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "rotation", { /** * The rotation of the image. */ get: function () { return this._rotation; }, set: function (value) { this._rotation = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "scale", { /** * The scale of the image. */ get: function () { return this._scale; }, set: function (value) { this._scale.copy(value); }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "clipBoundaryType", { /** * The current clip boundary type. */ get: function () { return this._clipBoundaryType; }, set: function (value) { this._clipBoundaryType = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "clipBoundary", { /** * An array of clip boundary vertices in image pixel coordinates. */ get: function () { return this._clipBoundary; }, set: function (value) { var _a; this._clipBoundary = []; (_a = this._clipBoundary).push.apply(_a, __spreadArray([], __read(value), false)); }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "isClipped", { /** * The flag whether the image is clipped. */ get: function () { return this._isClipped; }, set: function (value) { this._isClipped = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "isShownClipped", { /** * The flag whether to use clipping boundary. */ get: function () { return this._isShownClipped; }, set: function (value) { this._isShownClipped = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "isImageShown", { /** * The flag whether the image is shown. */ get: function () { return this._isImageShown; }, set: function (value) { this._isImageShown = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "isImageTransparent", { /** * The flag whether the image is transparent. */ get: function () { return this._isImageTransparent; }, set: function (value) { this._isImageTransparent = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "image", { /** * The image data of this entity. */ get: function () { return this._image; }, set: function (value) { this._image = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "imageDefId", { /** * The object id of an image entity's image definition object. */ get: function () { return this._imageDefId; }, set: function (value) { this._imageDefId = value; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "imageFileName", { /** * The file name of the image. */ get: function () { if (this._imageDefId) { var imageDef = this.database.dictionaries.imageDefs.getIdAt(this._imageDefId); if (imageDef) { return imageDef.sourceFileName; } } return ''; }, enumerable: false, configurable: true }); Object.defineProperty(AcDbRasterImage.prototype, "geometricExtents", { /** * @inheritdoc */ get: function () { var extents = new AcGeBox3d(); extents.min.copy(this._position); extents.max.set(this._position.x + this._width, this._position.y + this._height, 0); return extents; }, enumerable: false, configurable: true }); /** * @inheritdoc */ AcDbRasterImage.prototype.subGetGripPoints = function () { return this.boundaryPath(); }; /** * @inheritdoc */ AcDbRasterImage.prototype.draw = function (renderer) { var points = this.boundaryPath(); if (this._image) { return renderer.image(this._image, { boundary: points, roation: this._rotation }); } else { return renderer.lines(points, this.lineStyle); } }; AcDbRasterImage.prototype.boundaryPath = function () { var _this = this; var points = []; if (this.isClipped && this._clipBoundary.length > 3) { var wcsWidth_1 = this._width; var wcsHeight_1 = this._height; // The left-bottom corner of the boundary is mapped to the insertion point of the image. // So calcuate the translation based on those two points. var ocsBox = new AcGeBox2d(); ocsBox.setFromPoints(this._clipBoundary); var translation_1 = new AcGePoint2d(); translation_1.setX(this._position.x - ocsBox.min.x * wcsWidth_1); translation_1.setY(this._position.y - ocsBox.min.y * wcsHeight_1); this._clipBoundary.forEach(function (point) { // Clip boundary vertices are in image pixel coordinates. So we need to convert their coordniates // from the image pixel coordinate system to the world coordinate system. var x = point.x * wcsWidth_1 + translation_1.x; var y = point.y * wcsHeight_1 + translation_1.y; points.push(new AcGePoint3d(x, y, _this._position.z)); }); } else { points.push(this._position); points.push(this._position.clone().setX(this._position.x + this._width)); points.push(this._position .clone() .set(this._position.x + this._width, this._position.y + this._height, this._position.z)); points.push(this._position.clone().setY(this._position.y + this._height)); if (this._rotation > 0) { _point1.copy(points[1]); for (var index = 1; index < 4; index++) { _point2.copy(points[index]); _point2.rotateAround(_point1, this._rotation); points[index].setX(_point2.x); points[index].setY(_point2.y); } } points.push(points[0]); } return points; }; /** The entity type name */ AcDbRasterImage.typeName = 'RasterImage'; return AcDbRasterImage; }(AcDbEntity)); export { AcDbRasterImage }; var _point1 = /*@__PURE__*/ new AcGePoint2d(); var _point2 = /*@__PURE__*/ new AcGePoint2d(); //# sourceMappingURL=AcDbRasterImage.js.map