var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
import { AcCmTransparency, AcDb2dPolyline, AcDb3dPolyline, AcDb3PointAngularDimension, AcDbAlignedDimension, AcDbArc, AcDbAttribute, AcDbAttributeDefinition, AcDbAttributeFlags, AcDbAttributeMTextFlag, AcDbBlockReference, AcDbCircle, AcDbDiametricDimension, AcDbEllipse, AcDbFace, AcDbHatch, AcDbLeader, AcDbLine, AcDbMText, AcDbOrdinateDimension, AcDbPoint, AcDbPoly2dType, AcDbPoly3dType, AcDbPolyline, AcDbRadialDimension, AcDbRasterImage, AcDbRay, AcDbSpline, AcDbTable, AcDbText, AcDbTrace, AcDbViewport, AcDbWipeout, AcDbXline, AcGeCircArc2d, AcGeEllipseArc2d, AcGeLine2d, AcGeLoop2d, AcGePoint2d, AcGePoint3d, AcGePolyline2d, AcGeSpline3d, AcGeVector2d, AcGeVector3d } from '@mlightcad/data-model';
var AcDbEntityConverter = /** @class */ (function () {
    function AcDbEntityConverter() {
    }
    AcDbEntityConverter.prototype.convert = function (entity) {
        var dbEntity = this.createEntity(entity);
        if (dbEntity) {
            this.processCommonAttrs(entity, dbEntity);
        }
        return dbEntity;
    };
    /**
     * Create the corresponding drawing database entity from data in dxf format
     * @param entity Input entity data in dxf format
     * @returns Return the converted drawing database entity
     */
    AcDbEntityConverter.prototype.createEntity = function (entity) {
        if (entity.type == '3DFACE') {
            return this.convertFace(entity);
        }
        else if (entity.type == 'ARC') {
            return this.convertArc(entity);
        }
        else if (entity.type == 'ATTDEF') {
            return this.convertAttributeDefinition(entity);
        }
        else if (entity.type == 'CIRCLE') {
            return this.convertCirle(entity);
        }
        else if (entity.type == 'DIMENSION') {
            return this.convertDimension(entity);
        }
        else if (entity.type == 'ELLIPSE') {
            return this.convertEllipse(entity);
        }
        else if (entity.type == 'HATCH') {
            return this.convertHatch(entity);
        }
        else if (entity.type == 'IMAGE') {
            return this.convertImage(entity);
        }
        else if (entity.type == 'LEADER') {
            return this.convertLeader(entity);
        }
        else if (entity.type == 'LINE') {
            return this.convertLine(entity);
        }
        else if (entity.type == 'LWPOLYLINE') {
            return this.convertLWPolyline(entity);
        }
        else if (entity.type == 'MTEXT') {
            return this.convertMText(entity);
        }
        else if (entity.type == 'POINT') {
            return this.convertPoint(entity);
        }
        else if (entity.type == 'POLYLINE2D') {
            return this.convertPolyline2d(entity);
        }
        else if (entity.type == 'POLYLINE3D') {
            return this.convertPolyline3d(entity);
        }
        else if (entity.type == 'RAY') {
            return this.convertRay(entity);
        }
        else if (entity.type == 'SPLINE') {
            return this.convertSpline(entity);
        }
        else if (entity.type == 'ACAD_TABLE') {
            return this.convertTable(entity);
        }
        else if (entity.type == 'TEXT') {
            return this.convertText(entity);
        }
        else if (entity.type == 'SOLID') {
            return this.convertSolid(entity);
        }
        else if (entity.type == 'VIEWPORT') {
            return this.convertViewport(entity);
        }
        else if (entity.type == 'WIPEOUT') {
            return this.convertWipeout(entity);
        }
        else if (entity.type == 'XLINE') {
            return this.convertXline(entity);
        }
        else if (entity.type == 'INSERT') {
            return this.convertBlockReference(entity);
        }
        return null;
    };
    AcDbEntityConverter.prototype.convertFace = function (face) {
        var dbEntity = new AcDbFace();
        if (face.corner1)
            dbEntity.setVertexAt(0, face.corner1);
        if (face.corner2)
            dbEntity.setVertexAt(1, face.corner2);
        if (face.corner3)
            dbEntity.setVertexAt(2, face.corner3);
        if (face.corner4)
            dbEntity.setVertexAt(3, face.corner4);
        dbEntity.setEdgeInvisibilities(face.flag);
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertArc = function (arc) {
        var _a;
        var dbEntity = new AcDbArc(arc.center, arc.radius, arc.startAngle, arc.endAngle, (_a = arc.extrusionDirection) !== null && _a !== void 0 ? _a : AcGeVector3d.Z_AXIS);
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertCirle = function (circle) {
        var _a;
        var dbEntity = new AcDbCircle(circle.center, circle.radius, (_a = circle.extrusionDirection) !== null && _a !== void 0 ? _a : AcGeVector3d.Z_AXIS);
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertEllipse = function (ellipse) {
        var _a;
        var majorAxis = new AcGeVector3d(ellipse.majorAxisEndPoint);
        var majorAxisRadius = majorAxis.length();
        var dbEntity = new AcDbEllipse(ellipse.center, (_a = ellipse.extrusionDirection) !== null && _a !== void 0 ? _a : AcGeVector3d.Z_AXIS, majorAxis, majorAxisRadius, majorAxisRadius * ellipse.axisRatio, ellipse.startAngle, ellipse.endAngle);
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertLine = function (line) {
        var start = line.startPoint;
        var end = line.endPoint;
        var dbEntity = new AcDbLine(new AcGePoint3d(start.x, start.y, start.z), new AcGePoint3d(end.x, end.y, end.z));
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertSpline = function (spline) {
        // Catch error to construct spline because it maybe one spline in one block.
        // If don't catch the error, the block conversion may be interruptted.
        try {
            if (spline.numberOfControlPoints > 0 && spline.numberOfKnots > 0) {
                return new AcDbSpline(spline.controlPoints, spline.knots, spline.weights, spline.degree, !!(spline.flag & 0x01));
            }
            else if (spline.numberOfFitPoints > 0) {
                return new AcDbSpline(spline.fitPoints, 'Uniform', spline.degree, !!(spline.flag & 0x01));
            }
        }
        catch (error) {
            console.log("Failed to convert spline with error: ".concat(error));
        }
        return null;
    };
    AcDbEntityConverter.prototype.convertPoint = function (point) {
        var dbEntity = new AcDbPoint();
        dbEntity.position = point.position;
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertSolid = function (solid) {
        var dbEntity = new AcDbTrace();
        dbEntity.setPointAt(0, __assign(__assign({}, solid.corner1), { z: 0 }));
        dbEntity.setPointAt(1, __assign(__assign({}, solid.corner2), { z: 0 }));
        dbEntity.setPointAt(2, __assign(__assign({}, solid.corner3), { z: 0 }));
        dbEntity.setPointAt(3, solid.corner4 ? __assign(__assign({}, solid.corner4), { z: 0 }) : __assign(__assign({}, solid.corner3), { z: 0 }));
        dbEntity.thickness = solid.thickness;
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertLWPolyline = function (polyline) {
        // Libredwg changes meaning of the 'flag' field. '512' means closed.
        var dbEntity = new AcDbPolyline();
        dbEntity.closed = !!(polyline.flag & 0x200);
        polyline.vertices.forEach(function (vertex, index) {
            dbEntity.addVertexAt(index, new AcGePoint2d(vertex.x, vertex.y), vertex.bulge, vertex.startWidth, vertex.endWidth);
        });
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertPolyline2d = function (polyline) {
        // Polyline flag (bit-coded; default = 0):
        // https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-ABF6B778-BE20-4B49-9B58-A94E64CEFFF3
        //
        // 1 = This is a closed polyline (or a polygon mesh closed in the M direction)
        // 2 = Curve-fit vertices have been added
        // 4 = Spline-fit vertices have been added
        // 8 = This is a 3D polyline
        // 16 = This is a 3D polygon mesh
        // 32 = The polygon mesh is closed in the N direction
        // 64 = The polyline is a polyface mesh
        // 128 = The linetype pattern is generated continuously around the vertices of this polyline
        var isClosed = !!(polyline.flag & 0x01);
        // Filter out spline control points
        var vertices = [];
        var bulges = [];
        polyline.vertices.map(function (vertex) {
            var _a;
            // Check whether it is one spline control point
            if (!(vertex.flag & 0x10)) {
                vertices.push({
                    x: vertex.x,
                    y: vertex.y,
                    z: vertex.z
                });
                bulges.push((_a = vertex.bulge) !== null && _a !== void 0 ? _a : 0);
            }
        });
        var polyType = AcDbPoly2dType.SimplePoly;
        if (polyline.flag & 0x02) {
            polyType = AcDbPoly2dType.FitCurvePoly;
        }
        else if (polyline.flag & 0x04) {
            // Please don't use enum DwgSmoothType value here.
            // It will result in libredwg-web bundled in this package.
            if (polyline.smoothType == 6) {
                // DwgSmoothType.CUBIC
                polyType = AcDbPoly2dType.CubicSplinePoly;
            }
            else if (polyline.smoothType == 5) {
                // DwgSmoothType.QUADRATIC
                polyType = AcDbPoly2dType.QuadSplinePoly;
            }
        }
        return new AcDb2dPolyline(polyType, vertices, 0, isClosed, polyline.startWidth, polyline.endWidth, bulges);
    };
    AcDbEntityConverter.prototype.convertPolyline3d = function (polyline) {
        // Polyline flag (bit-coded; default = 0):
        // https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-ABF6B778-BE20-4B49-9B58-A94E64CEFFF3
        //
        // 1 = This is a closed polyline (or a polygon mesh closed in the M direction)
        // 2 = Curve-fit vertices have been added
        // 4 = Spline-fit vertices have been added
        // 8 = This is a 3D polyline
        // 16 = This is a 3D polygon mesh
        // 32 = The polygon mesh is closed in the N direction
        // 64 = The polyline is a polyface mesh
        // 128 = The linetype pattern is generated continuously around the vertices of this polyline
        var isClosed = !!(polyline.flag & 0x01);
        // Filter out spline control points
        var vertices = [];
        polyline.vertices.map(function (vertex) {
            // Check whether it is one spline control point
            if (!(vertex.flag & 0x10)) {
                vertices.push({
                    x: vertex.x,
                    y: vertex.y,
                    z: vertex.z
                });
            }
        });
        var polyType = AcDbPoly3dType.SimplePoly;
        if (polyline.flag & 0x04) {
            // Please don't use enum DwgSmoothType value here.
            // It will result in libredwg-web bundled in this package.
            if (polyline.smoothType == 6) {
                // DwgSmoothType.CUBIC
                polyType = AcDbPoly3dType.CubicSplinePoly;
            }
            else if (polyline.smoothType == 5) {
                // DwgSmoothType.QUADRATIC
                polyType = AcDbPoly3dType.QuadSplinePoly;
            }
        }
        return new AcDb3dPolyline(polyType, vertices, isClosed);
    };
    AcDbEntityConverter.prototype.convertHatch = function (hatch) {
        var _a;
        var dbEntity = new AcDbHatch();
        (_a = hatch.definitionLines) === null || _a === void 0 ? void 0 : _a.forEach(function (item) {
            dbEntity.definitionLines.push({
                angle: item.angle,
                base: item.base,
                offset: item.offset,
                dashLengths: item.numberOfDashLengths > 0 ? item.dashLengths : []
            });
        });
        // Important: Don't use DwgHatchSolidFill.SolidFill to avoid bundling libredwg-web into libredeg-converter
        dbEntity.isSolidFill = hatch.solidFill == 1;
        dbEntity.hatchStyle = hatch.hatchStyle;
        dbEntity.patternName = hatch.patternName;
        dbEntity.patternType = hatch.patternType;
        dbEntity.patternAngle = hatch.patternAngle == null ? 0 : hatch.patternAngle;
        dbEntity.patternScale = hatch.patternScale == null ? 0 : hatch.patternScale;
        var paths = hatch.boundaryPaths;
        paths.forEach(function (path) {
            var flag = path.boundaryPathTypeFlag;
            // Check whether it is a polyline
            if (flag & 0x02) {
                var polylinePath = path;
                var polyline_1 = new AcGePolyline2d();
                polyline_1.closed = polylinePath.isClosed;
                polylinePath.vertices.forEach(function (vertex, index) {
                    polyline_1.addVertexAt(index, {
                        x: vertex.x,
                        y: vertex.y,
                        bulge: vertex.bulge
                    });
                });
                dbEntity.add(polyline_1);
            }
            else {
                var edgePath = path;
                var loop_1 = new AcGeLoop2d();
                edgePath.edges.forEach(function (edge) {
                    // TODO: It seems there are some issue on libredwg. Sometimes 'undefined' edges are added.
                    if (edge == null)
                        return;
                    if (edge.type == 1) {
                        var line = edge;
                        loop_1.add(new AcGeLine2d(line.start, line.end));
                    }
                    else if (edge.type == 2) {
                        var arc = edge;
                        loop_1.add(new AcGeCircArc2d(arc.center, arc.radius, arc.startAngle, arc.endAngle, !arc.isCCW));
                    }
                    else if (edge.type == 3) {
                        var ellipse = edge;
                        var majorAxis = new AcGeVector2d();
                        majorAxis.subVectors(ellipse.end, ellipse.center);
                        var majorAxisRadius = Math.sqrt(Math.pow(ellipse.end.x, 2) + Math.pow(ellipse.end.y, 2));
                        // Property name 'lengthOfMinorAxis' is really confusing.
                        // Actually length of minor axis means percentage of major axis length.
                        var minorAxisRadius = majorAxisRadius * ellipse.lengthOfMinorAxis;
                        var startAngle = ellipse.startAngle;
                        var endAngle = ellipse.endAngle;
                        var rotation = Math.atan2(ellipse.end.y, ellipse.end.x);
                        if (!ellipse.isCCW) {
                            // when clockwise, need to handle start/end angles
                            startAngle = Math.PI * 2 - startAngle;
                            endAngle = Math.PI * 2 - endAngle;
                        }
                        loop_1.add(new AcGeEllipseArc2d(__assign(__assign({}, ellipse.center), { z: 0 }), majorAxisRadius, minorAxisRadius, startAngle, endAngle, !ellipse.isCCW, rotation));
                    }
                    else if (edge.type == 4) {
                        var spline = edge;
                        if (spline.numberOfControlPoints > 0 && spline.numberOfKnots > 0) {
                            var controlPoints = spline.controlPoints.map(function (item) {
                                return {
                                    x: item.x,
                                    y: item.y,
                                    z: 0
                                };
                            });
                            var hasWeights_1 = true;
                            var weights = spline.controlPoints.map(function (item) {
                                if (item.weight == null)
                                    hasWeights_1 = false;
                                return item.weight || 1;
                            });
                            loop_1.add(new AcGeSpline3d(controlPoints, spline.knots, hasWeights_1 ? weights : undefined));
                        }
                        else if (spline.numberOfFitData > 0) {
                            var fitPoints = spline.fitDatum.map(function (item) {
                                return {
                                    x: item.x,
                                    y: item.y,
                                    z: 0
                                };
                            });
                            loop_1.add(new AcGeSpline3d(fitPoints, 'Uniform'));
                        }
                    }
                });
                dbEntity.add(loop_1);
            }
        });
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertTable = function (table) {
        var dbEntity = new AcDbTable(table.name, table.rowCount, table.columnCount);
        dbEntity.attachmentPoint =
            table.attachmentPoint;
        dbEntity.position.copy(table.startPoint);
        table.columnWidthArr.forEach(function (width, index) {
            return dbEntity.setColumnWidth(index, width);
        });
        table.rowHeightArr.forEach(function (height, index) {
            return dbEntity.setRowHeight(index, height);
        });
        table.cells.forEach(function (cell, index) {
            dbEntity.setCell(index, cell);
        });
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertText = function (text) {
        var _a, _b;
        var dbEntity = new AcDbText();
        dbEntity.textString = text.text;
        dbEntity.styleName = text.styleName;
        dbEntity.height = text.textHeight;
        dbEntity.position.copy(text.startPoint);
        dbEntity.rotation = text.rotation;
        dbEntity.oblique = (_a = text.obliqueAngle) !== null && _a !== void 0 ? _a : 0;
        dbEntity.thickness = text.thickness;
        dbEntity.horizontalMode = text.halign;
        dbEntity.verticalMode = text.valign;
        dbEntity.widthFactor = (_b = text.xScale) !== null && _b !== void 0 ? _b : 1;
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertMText = function (mtext) {
        var dbEntity = new AcDbMText();
        dbEntity.contents = mtext.text;
        if (mtext.styleName != null) {
            dbEntity.styleName = mtext.styleName;
        }
        dbEntity.height = mtext.textHeight;
        dbEntity.width = mtext.rectWidth;
        dbEntity.rotation = mtext.rotation || 0;
        dbEntity.location = mtext.insertionPoint;
        dbEntity.attachmentPoint =
            mtext.attachmentPoint;
        if (mtext.direction) {
            dbEntity.direction = new AcGeVector3d(mtext.direction);
        }
        dbEntity.drawingDirection =
            mtext.drawingDirection;
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertLeader = function (leader) {
        var dbEntity = new AcDbLeader();
        leader.vertices.forEach(function (point) {
            dbEntity.appendVertex(point);
        });
        dbEntity.hasArrowHead = leader.isArrowheadEnabled;
        dbEntity.hasHookLine = leader.isHooklineExists;
        dbEntity.isSplined = leader.isSpline;
        dbEntity.dimensionStyle = leader.styleName;
        dbEntity.annoType =
            leader.leaderCreationFlag;
        return dbEntity;
    };
    AcDbEntityConverter.prototype.convertDimension = function (dimension) {
        if (dimension.subclassMarker == 'AcDbAlignedDimension' ||
            dimension.subclassMarker == 'AcDbRotatedDimension') {
            var entity = dimension;
            var dbEntity = new AcDbAlignedDimension(entity.subDefinitionPoint1, entity.subDefinitionPoint2, entity.definitionPoint);
            if (entity.insertionPoint) {
                dbEntity.dimBlockPosition = {
                    x: entity.insertionPoint.x,
                    y: entity.insertionPoint.y,
                    z: 0
                };
            }
            dbEntity.rotation = entity.rotationAngle;
            this.processDimensionCommonAttrs(dimension, dbEntity);
            return dbEntity;
        }
        else if (dimension.subclassMarker == 'AcDb3PointAngularDimension') {
            var entity = dimension;
            var dbEntity = new AcDb3PointAngularDimension(entity.centerPoint, entity.subDefinitionPoint1, entity.subDefinitionPoint2, entity.definitionPoint);
            this.processDimensionCommonAttrs(dimension, dbEntity);
            return dbEntity;
        }
        else if (dimension.subclassMarker == 'AcDbOrdinateDimension') {
            var entity = dimension;
            var dbEntity = new AcDbOrdinateDimension(entity.subDefinitionPoint1, entity.subDefinitionPoint2);
            this.processDimensionCommonAttrs(dimension, dbEntity);
            return dbEntity;
        }
        else if (dimension.subclassMarker == 'AcDbRadialDimension') {
            var entity = dimension;
            var dbEntity = new AcDbRadialDimension(entity.definitionPoint, entity.centerPoint, entity.leaderLength);
            this.processDimensionCommonAttrs(dimension, dbEntity);
            return dbEntity;
        }
        else if (dimension.subclassMarker == 'AcDbDiametricDimension') {
            var entity = dimension;
            var dbEntity = new AcDbDiametricDimension(entity.definitionPoint, entity.centerPoint, entity.leaderLength);
            this.processDimensionCommonAttrs(dimension, dbEntity);
            return dbEntity;
        }
        return null;
    };
    AcDbEntityConverter.prototype.processImage = function (image, dbImage) {
        dbImage.position.copy(image.position);
        dbImage.brightness = image.brightness;
        dbImage.contrast = image.contrast;
        dbImage.fade = image.fade;
        dbImage.imageDefId = image.imageDefHandle;
        dbImage.isClipped = image.clipping > 0;
        dbImage.isShownClipped = (image.flags | 0x0004) > 0;
        dbImage.isImageShown = (image.flags | 0x0003) > 0;
        dbImage.isImageTransparent = (image.flags | 0x0008) > 0;
        image.clippingBoundaryPath.forEach(function (point) {
            dbImage.clipBoundary.push(new AcGePoint2d(point));
        });
        dbImage.clipBoundaryType =
            image.clippingBoundaryType;
        // Calculate the scale factors
        dbImage.width =
            Math.sqrt(Math.pow(image.uPixel.x, 2) + Math.pow(image.uPixel.y, 2) + Math.pow(image.uPixel.z, 2)) * image.imageSize.x;
        dbImage.height =
            Math.sqrt(Math.pow(image.vPixel.x, 2) + Math.pow(image.vPixel.y, 2) + Math.pow(image.vPixel.z, 2)) * image.imageSize.y;
        // Calculate the rotation angle
        // Rotation is determined by the angle of the U-vector relative to the X-axis
        dbImage.rotation = Math.atan2(image.uPixel.y, image.uPixel.x);
    };
    AcDbEntityConverter.prototype.convertImage = function (image) {
        var dbImage = new AcDbRasterImage();
        this.processImage(image, dbImage);
        return dbImage;
    };
    AcDbEntityConverter.prototype.convertWipeout = function (wipeout) {
        var dbWipeout = new AcDbWipeout();
        this.processImage(wipeout, dbWipeout);
        return dbWipeout;
    };
    AcDbEntityConverter.prototype.convertViewport = function (viewport) {
        var dbViewport = new AcDbViewport();
        dbViewport.number = viewport.viewportId;
        dbViewport.centerPoint.copy(viewport.viewportCenter);
        dbViewport.height = viewport.height;
        dbViewport.width = viewport.width;
        dbViewport.viewCenter.copy(viewport.displayCenter);
        dbViewport.viewHeight = viewport.viewHeight;
        return dbViewport;
    };
    AcDbEntityConverter.prototype.convertRay = function (ray) {
        var dbRay = new AcDbRay();
        dbRay.basePoint.copy(ray.firstPoint);
        dbRay.unitDir.copy(ray.unitDirection);
        return dbRay;
    };
    AcDbEntityConverter.prototype.convertXline = function (xline) {
        var dbXline = new AcDbXline();
        dbXline.basePoint.copy(xline.firstPoint);
        dbXline.unitDir.copy(xline.unitDirection);
        return dbXline;
    };
    AcDbEntityConverter.prototype.convertAttributeCommon = function (attrib, dbAttrib) {
        var _a, _b;
        var text = attrib.text;
        dbAttrib.textString = text.text;
        dbAttrib.styleName = text.styleName;
        dbAttrib.height = text.textHeight;
        dbAttrib.position.copy(text.startPoint);
        dbAttrib.rotation = text.rotation;
        dbAttrib.oblique = (_a = text.obliqueAngle) !== null && _a !== void 0 ? _a : 0;
        dbAttrib.thickness = text.thickness;
        dbAttrib.horizontalMode = text.halign;
        dbAttrib.verticalMode = text.valign;
        dbAttrib.widthFactor = (_b = text.xScale) !== null && _b !== void 0 ? _b : 1;
        dbAttrib.tag = attrib.tag;
        dbAttrib.fieldLength = attrib.fieldLength;
        dbAttrib.isInvisible = (attrib.flags & AcDbAttributeFlags.Invisible) !== 0;
        dbAttrib.isConst = (attrib.flags & AcDbAttributeFlags.Const) !== 0;
        dbAttrib.isVerifiable = (attrib.flags & AcDbAttributeFlags.Verifiable) !== 0;
        dbAttrib.isPreset = (attrib.flags & AcDbAttributeFlags.Preset) !== 0;
        dbAttrib.lockPositionInBlock = attrib.lockPositionFlag;
        dbAttrib.isReallyLocked = attrib.isReallyLocked;
        dbAttrib.isMTextAttribute =
            (attrib.mtextFlag & AcDbAttributeMTextFlag.MultiLine) !== 0;
        dbAttrib.isConstMTextAttribute =
            (attrib.mtextFlag & AcDbAttributeMTextFlag.ConstMultiLine) !== 0;
    };
    AcDbEntityConverter.prototype.convertAttribute = function (attrib) {
        var dbAttrib = new AcDbAttribute();
        this.convertAttributeCommon(attrib, dbAttrib);
        return dbAttrib;
    };
    AcDbEntityConverter.prototype.convertAttributeDefinition = function (attrib) {
        var dbAttDef = new AcDbAttributeDefinition();
        this.convertAttributeCommon(attrib, dbAttDef);
        dbAttDef.prompt = attrib.prompt;
        return dbAttDef;
    };
    AcDbEntityConverter.prototype.convertBlockReference = function (blockReference) {
        var _this = this;
        var dbBlockReference = new AcDbBlockReference(blockReference.name);
        if (blockReference.insertionPoint)
            dbBlockReference.position.copy(blockReference.insertionPoint);
        dbBlockReference.scaleFactors.x = blockReference.xScale;
        dbBlockReference.scaleFactors.y = blockReference.yScale;
        dbBlockReference.scaleFactors.z = blockReference.zScale;
        dbBlockReference.rotation = blockReference.rotation;
        dbBlockReference.normal.copy(blockReference.extrusionDirection);
        if (blockReference.attribs) {
            blockReference.attribs.forEach(function (attrib) {
                var dbAttrib = _this.convertAttribute(attrib);
                dbBlockReference.appendAttributes(dbAttrib);
            });
        }
        return dbBlockReference;
    };
    AcDbEntityConverter.prototype.processDimensionCommonAttrs = function (entity, dbEntity) {
        dbEntity.dimBlockId = entity.name;
        dbEntity.textPosition.copy(entity.textPoint);
        dbEntity.textRotation = entity.textRotation || 0;
        if (entity.textLineSpacingFactor) {
            dbEntity.textLineSpacingFactor = entity.textLineSpacingFactor;
        }
        if (entity.textLineSpacingStyle) {
            dbEntity.textLineSpacingStyle =
                entity.textLineSpacingStyle;
        }
        dbEntity.dimensionStyleName = entity.styleName;
        dbEntity.dimensionText = entity.text || '';
        dbEntity.measurement = entity.measurement;
    };
    AcDbEntityConverter.prototype.processCommonAttrs = function (entity, dbEntity) {
        dbEntity.layer = entity.layer || '0';
        dbEntity.objectId = entity.handle;
        dbEntity.ownerId = entity.ownerBlockRecordSoftId;
        if (entity.lineType != null) {
            dbEntity.lineType = entity.lineType;
        }
        if (entity.lineweight != null) {
            dbEntity.lineWeight = entity.lineweight;
        }
        if (entity.lineTypeScale != null) {
            dbEntity.linetypeScale = entity.lineTypeScale;
        }
        if (entity.color != null) {
            dbEntity.color.setRGBValue(entity.color);
        }
        if (entity.colorIndex != null) {
            dbEntity.color.colorIndex = entity.colorIndex;
        }
        if (entity.colorName) {
            dbEntity.color.colorName = entity.colorName;
        }
        if (entity.isVisible != null) {
            dbEntity.visibility = entity.isVisible;
        }
        if (entity.transparency != null) {
            var transparency = new AcCmTransparency();
            transparency.method = entity.transparencyType;
            if (transparency.isByBlock || transparency.isByBlock) {
                transparency.alpha = entity.transparency;
            }
            dbEntity.transparency = transparency;
        }
    };
    return AcDbEntityConverter;
}());
export { AcDbEntityConverter };
//# sourceMappingURL=AcDbEntitiyConverter.js.map