指南
- 开始
- 指南
- 基础绘图
- 交互绘图
- 图纸编辑
- 数据库
- 最佳实践
MxCAD API 文档
- 类
- 2d.Color
- 2d.McAppType
- 2d.McCmColor
- 2d.McDbAlignedDimension
- 2d.McDbArc
- 2d.McDbAttribute
- 2d.McDbAttributeDefinition
- 2d.McDbBlockReference
- 2d.McDbBlockTable
- 2d.McDbBlockTableRecord
- 2d.McDbCircle
- 2d.McDbCurve
- 2d.McDbDatabase
- 2d.McDbDimension
- 2d.McDbEllipse
- 2d.McDbEntity
- 2d.McDbHatch
- 2d.McDbLayerTable
- 2d.McDbLayerTableRecord
- 2d.McDbLine
- 2d.McDbLinetypeTable
- 2d.McDbLinetypeTableRecord
- 2d.McDbMText
- 2d.McDbObject
- 2d.McDbObjectArray
- 2d.McDbPoint
- 2d.McDbPolyline
- 2d.McDbProxyEntity
- 2d.McDbRasterImage
- 2d.McDbRasterImageDef
- 2d.McDbRotatedDimension
- 2d.McDbText
- 2d.McDbTextStyleTable
- 2d.McDbTextStyleTableRecord
- 2d.McGeLongArray
- 2d.McGeMatrix3d
- 2d.McGePoint3d
- 2d.McGePoint3dArray
- 2d.McGeStringArray
- 2d.McGeVector3d
- 2d.McObject
- 2d.McObjectId
- 2d.McRxObject
- 2d.MdGeDoubleArray
- 2d.MxCADResbuf
- 2d.MxCADSelectionSet
- 2d.MxCADUiPrAngle
- 2d.MxCADUiPrBase
- 2d.MxCADUiPrDist
- 2d.MxCADUiPrEntity
- 2d.MxCADUiPrInt
- 2d.MxCADUiPrKeyWord
- 2d.MxCADUiPrPoint
- 2d.MxCADUiPrString
- 2d.MxCADUtilityClass
- 2d.MxCppType
- 2d.MxPropertiesWindowCustom
- 2d.MxPropertiesWindowCustomValue
- 2d.ObjectInheritance
- 3d.MdGeAngleDim
- 3d.MdGeArray1OfInteger
- 3d.MdGeArray1OfPnt
- 3d.MdGeArray1OfReal
- 3d.MdGeArray2OfPnt
- 3d.MdGeArray2OfReal
- 3d.MdGeArrowAspect
- 3d.MdGeAxis
- 3d.MdGeBezierCurve
- 3d.MdGeBezierSurface
- 3d.MdGeBox
- 3d.MdGeBRep
- 3d.MdGeBSplineCurve
- 3d.MdGeBSplineSurface
- 3d.MdGeChamfer
- 3d.MdGeCircle
- 3d.MdGeColor
- 3d.MdGeCompound
- 3d.MdGeCompSolid
- 3d.MdGeCone
- 3d.MdGeCSYS
- 3d.MdGeCSYSR
- 3d.MdGeCylinder
- 3d.MdGeDiameterDim
- 3d.MdGeDimAspect
- 3d.MdGeDir
- 3d.MdGeEdge
- 3d.MdGeEllipse
- 3d.MdGeExplorer
- 3d.MdGeFace
- 3d.MdGeFillet
- 3d.MdGeHGeomPlane
- 3d.MdGeHGeomSurface
- 3d.MdGeHypr
- 3d.MdGeInterpolateBSpl
- 3d.MdGeLengthDim
- 3d.MdGeLight
- 3d.MdGeLine
- 3d.MdGeLineAspect
- 3d.MdGeListIteratorOfListOfShape
- 3d.MdGeListOfShape
- 3d.MdGeLoft
- 3d.MdGeMakeArcOfCircle
- 3d.MdGeMakeFace
- 3d.MdGeMakeThickSolid
- 3d.MdGeMakeWires
- 3d.MdGeMat
- 3d.MdGeMaterialAspect
- 3d.MdGeObject
- 3d.MdGeParab
- 3d.MdGePipe
- 3d.MdGePlane
- 3d.MdGePoint
- 3d.MdGePointsToBSpl
- 3d.MdGePointsToBSplSurface
- 3d.MdGePrism
- 3d.MdGeRadiusDim
- 3d.MdGeRect
- 3d.MdGeRevol
- 3d.MdGeSequenceIteratorOfSequenceOfShape
- 3d.MdGeSequenceOfShape
- 3d.MdGeShape
- 3d.MdGeShell
- 3d.MdGeSolid
- 3d.MdGeSphere
- 3d.MdGeText
- 3d.MdGeTextAspect
- 3d.MdGeTextLabel
- 3d.MdGeTopo
- 3d.MdGeTorus
- 3d.MdGeTransform
- 3d.MdGeTrsf
- 3d.MdGeVec
- 3d.MdGeVertex
- 3d.MdGeWedge
- 3d.MdGeWire
- 3d.MdGeXYZ
- 3d.MxDraw3d
- Color
- McAppType
- McCmColor
- McDbAlignedDimension
- McDbArc
- McDbAttribute
- McDbAttributeDefinition
- McDbBlockReference
- McDbBlockTable
- McDbBlockTableRecord
- McDbCircle
- McDbCurve
- McDbDatabase
- McDbDimension
- McDbEllipse
- McDbEntity
- McDbHatch
- McDbLayerTable
- McDbLayerTableRecord
- McDbLine
- McDbLinetypeTable
- McDbLinetypeTableRecord
- McDbMText
- McDbObject
- McDbObjectArray
- McDbPoint
- McDbPolyline
- McDbProxyEntity
- McDbRasterImage
- McDbRasterImageDef
- McDbRotatedDimension
- McDbText
- McDbTextStyleTable
- McDbTextStyleTableRecord
- McGeLongArray
- McGeMatrix3d
- McGePoint3d
- McGePoint3dArray
- McGeStringArray
- McGeVector3d
- McObject
- McObjectId
- McRxObject
- MdGeAngleDim
- MdGeArray1OfInteger
- MdGeArray1OfPnt
- MdGeArray1OfReal
- MdGeArray2OfPnt
- MdGeArray2OfReal
- MdGeArrowAspect
- MdGeAxis
- MdGeBezierCurve
- MdGeBezierSurface
- MdGeBox
- MdGeBRep
- MdGeBSplineCurve
- MdGeBSplineSurface
- MdGeChamfer
- MdGeCircle
- MdGeColor
- MdGeCompound
- MdGeCompSolid
- MdGeCone
- MdGeCSYS
- MdGeCSYSR
- MdGeCylinder
- MdGeDiameterDim
- MdGeDimAspect
- MdGeDir
- MdGeDoubleArray
- MdGeEdge
- MdGeEllipse
- MdGeExplorer
- MdGeFace
- MdGeFillet
- MdGeHGeomPlane
- MdGeHGeomSurface
- MdGeHypr
- MdGeInterpolateBSpl
- MdGeLengthDim
- MdGeLight
- MdGeLine
- MdGeLineAspect
- MdGeListIteratorOfListOfShape
- MdGeListOfShape
- MdGeLoft
- MdGeMakeArcOfCircle
- MdGeMakeFace
- MdGeMakeThickSolid
- MdGeMakeWires
- MdGeMat
- MdGeMaterialAspect
- MdGeObject
- MdGeParab
- MdGePipe
- MdGePlane
- MdGePoint
- MdGePointsToBSpl
- MdGePointsToBSplSurface
- MdGePrism
- MdGeRadiusDim
- MdGeRect
- MdGeRevol
- MdGeSequenceIteratorOfSequenceOfShape
- MdGeSequenceOfShape
- MdGeShape
- MdGeShell
- MdGeSolid
- MdGeSphere
- MdGeText
- MdGeTextAspect
- MdGeTextLabel
- MdGeTopo
- MdGeTorus
- MdGeTransform
- MdGeTrsf
- MdGeVec
- MdGeVertex
- MdGeWedge
- MdGeWire
- MdGeXYZ
- MxCADResbuf
- MxCADSelectionSet
- MxCADUiPrAngle
- MxCADUiPrBase
- MxCADUiPrDist
- MxCADUiPrEntity
- MxCADUiPrInt
- MxCADUiPrKeyWord
- MxCADUiPrPoint
- MxCADUiPrString
- MxCADUtilityClass
- MxCppType
- MxDraw3d
- MxPropertiesWindowCustom
- MxPropertiesWindowCustomValue
- ObjectInheritance
- 2d.ColorIndexType
- 2d.ColorMethod
- 2d.FetchAttributes
- 2d.McDb.AttachmentPoint
- 2d.McDb.ClipBoundaryType
- 2d.McDb.HatchEdgeType
- 2d.McDb.HatchLoopType
- 2d.McDb.HatchPatternType
- 2d.McDb.HatchStyle
- 2d.McDb.Intersect
- 2d.McDb.LineWeight
- 2d.McDb.TextHorzMode
- 2d.McDb.TextVertMode
- 2d.McObjectIdType
- 2d.MxCADResbufDataType
- 2d.MxCADSelectionSetStatus
- 2d.MxPropertiesWindowCustomValueType
- 3d.MdGe.MxCameraProjection
- 3d.MdGe.MxCF3dFilletShapeEnum
- 3d.MdGe.MxCFDSChamfMode
- 3d.MdGe.MxDimensionArrowOrientation
- 3d.MdGe.MxDimensionTextHorizontalPosition
- 3d.MdGe.MxDimensionTextVerticalPosition
- 3d.MdGe.MxDisplayMode
- 3d.MdGe.MxFontAspect
- 3d.MdGe.MxGAJoinTypeEnum
- 3d.MdGe.MxGAShapeEnum
- 3d.MdGe.MxGFTrihedron
- 3d.MdGe.MxHorizontalTextAlignment
- 3d.MdGe.MxKindOfDimension
- 3d.MdGe.MxNameOfColor
- 3d.MdGe.MxNameOfMaterial
- 3d.MdGe.MxOffsetModeEnum
- 3d.MdGe.MxShapeEnum
- 3d.MdGe.MxTextPath
- 3d.MdGe.MxTypeOfAngle
- 3d.MdGe.MxTypeOfAngleArrowVisibility
- 3d.MdGe.MxTypeOfColor
- 3d.MdGe.MxTypeOfDisplayText
- 3d.MdGe.MxTypeOfLightSource
- 3d.MdGe.MxTypeOfLine
- 3d.MdGe.MxTypeOfMaterial
- 3d.MdGe.MxTypeOfReflection
- 3d.MdGe.MxV3dTypeOfOrientation
- 3d.MdGe.MxVerticalTextAlignment
- 枚举
- ColorIndexType
- ColorMethod
- FetchAttributes
- McDb.AttachmentPoint
- McDb.ClipBoundaryType
- McDb.HatchEdgeType
- McDb.HatchLoopType
- McDb.HatchPatternType
- McDb.HatchStyle
- McDb.Intersect
- McDb.LineWeight
- McDb.TextHorzMode
- McDb.TextVertMode
- McObjectIdType
- MdGe.MxCameraProjection
- MdGe.MxCF3dFilletShapeEnum
- MdGe.MxCFDSChamfMode
- MdGe.MxDimensionArrowOrientation
- MdGe.MxDimensionTextHorizontalPosition
- MdGe.MxDimensionTextVerticalPosition
- MdGe.MxDisplayMode
- MdGe.MxFontAspect
- MdGe.MxGAJoinTypeEnum
- MdGe.MxGAShapeEnum
- MdGe.MxGFTrihedron
- MdGe.MxHorizontalTextAlignment
- MdGe.MxKindOfDimension
- MdGe.MxNameOfColor
- MdGe.MxNameOfMaterial
- MdGe.MxOffsetModeEnum
- MdGe.MxShapeEnum
- MdGe.MxTextPath
- MdGe.MxTypeOfAngle
- MdGe.MxTypeOfAngleArrowVisibility
- MdGe.MxTypeOfColor
- MdGe.MxTypeOfDisplayText
- MdGe.MxTypeOfLightSource
- MdGe.MxTypeOfLine
- MdGe.MxTypeOfMaterial
- MdGe.MxTypeOfReflection
- MdGe.MxV3dTypeOfOrientation
- MdGe.MxVerticalTextAlignment
- MxCADResbufDataType
- MxCADSelectionSetStatus
- MxPropertiesWindowCustomValueType
- 2d.MxCadConfig
- 2d.MxCADUI
- 3d.MxDraw3dConfig
- 3d.WasmConfig
- 接口
- 模块
2. 图层表
图层表
在DWG数据库中,图层存放在图层层表 McDbLayerTable() 中,层表中每一条记录称为图层表记录对象 McDbLayerTableRecord() 。每一个图层表记录对象都对应一个图层,而且可以设置颜色、线型、关闭/打开、冻结等属性,其中DWG数据库默认总是有一个"0"层,该层不能被删除。
我们可以通过调用 mxcad 中的 MxCpp.getCurrentMxCAD()
得到当前的控件, 然后调用控件实例的 getDatabase() 方法得到数据库实例 McDbDatabase() ,在该数据库实例中调用 getLayerTable() 方法我们就能获取到层表 McDbLayerTable() 。
点击 数据库层表 McDbLayerTable()、 图层表记录对象 McDbLayerTableRecord 、 数据库实例 McDbDatabase() 查看详细属性和方法说明。
import { MxCpp} from "mxcad"
// 得到当前控件
const mxcad = MxCpp.getCurrentMxCAD();
// 拿到当前控件的数据库图层表
const layerTable = mxcad.getDatabase().getLayerTable();
添加图层
我们可以通过调用 mxcad 实例对象中的 addLayer() 方法添加自定义图层,并设置 drawLayer 属性将添加图层为绘制图层。
点击 McObject.addLayer() 查看详细属性和方法说明。
import { McApp } from "mxcad"
const mxcad = McApp.getCurrentMxCAD()
mxcad.addLayer("图层名称")
mxcad.drawLayer = "图层名称"
我们还可以通过实例化一个图层表记录对象 McDbLayerTableRecord() ,设置该图层颜色,线型,关闭/打开,冻结等属性后调用 add() 方法添加到图层层表中。
import { McCmColor, MxCpp, McDbLayerTableRecord, McDb } from "mxcad"
// 得到当前控件
const mxcad = MxCpp.getCurrentMxCAD();
// 实例化一个图层数据对象并设置这个图层的一些属性
const layer = new McDbLayerTableRecord()
layer.color = new McCmColor(0, 0, 0)
layer.isFrozen = true
layer.isLocked = true
layer.isOff = true
layer.lineWeight = McDb.LineWeight.kLnWt018
layer.name = "图层名称"
// 拿到当前控件的数据库图层表
const layerTable = mxcad.getDatabase().getLayerTable();
// 将图层数据对象添加到图层表中会得到一个标识该图层数据的对象ID
const objId = layerTable.add(layer)
// 更新显示
mxcad.updateDisplay()
遍历所有图层
我们可以通过调用数据库层表 McDbLayerTable() 中的 getAllRecordId() 方法获取所有图层id,再调用 getMcDbLayerTableRecord() 方法返回图层表记录对象 McDbLayerTableRecord() ,得到所有图层数据。
import { MxCpp } from "mxcad"
let layerTable = MxCpp.getCurrentMxCAD().getDatabase().getLayerTable();
let aryId = layerTable.getAllRecordId();
aryId.forEach((id) => {
let layerRec = id.getMcDbLayerTableRecord();
if (layerRec === null) return;
console.log(layerRec);
console.log("layerRec.color:" + layerRec.color.getColorString());
console.log("layerRec.name:" + layerRec.name);
});
删除图层
我们得到目标图层表记录对象 McDbLayerTableRecord 后可调用该对象实例的 erase() 方法删除对象。
import { MxCpp } from "mxcad"
let layerTable = MxCpp.getCurrentMxCAD().getDatabase().getLayerTable()
let layerId = layerTable.get("图层名字")
let layerRec = layerId.getMcDbLayerTableRecord()
layerRec.erase()
// 更新显示
mxcad.updateDisplay()
此外,我们还可以通过图层表的JSON序列化和反序列化来删除图层。
import { McCmColor, MxCpp, McDbLayerTableRecord, McDb } from "mxcad"
const layerTable = mxcad.getDatabase().getLayerTable();
const layerJsonString = layerTable.getJson()
const layerJson = JSON.parse(layerJsonString)
// 只要保留以下几个名称的图层
const keepLayerNames = ["0", "排水", "testLayer1"]
const keepLayers = layerJson.filter((layerJsonObj)=> {
return keepLayerNames.includes(layerJsonObj.name)
})
const keepLayersJsonString = JSON.stringify(keepLayers)
layerTable.setJson(keepLayersJsonString)
// 最后我们可以通过has方法检查图层是否存在 get方法传入图层名来得到对应的对象ID
console.log("testLayer1", layerTable.has("testLayer1"))
console.log("testLayer1", layerTable.get("testLayer1"))
:::tip 提示 在删除图层前,最好判断一个该图层上是否有CAD绘图对象,避免误删图层内容。 :::
修改图层
mxcad 中修改图层的基础操作为得到数据库层表对象,然后根据层名得到层表记录对象,设置层表记录对应属性值。下面以让用户在CAD图上选择一个对象,然后操作对象所在的图层为例:
- 获取目标图层
import { MxCADSelectionSet, MxCADUiPrPoint } from "mxcad"
const ss = new MxCADSelectionSet();
const getPoint = new MxCADUiPrPoint()
getPoint.setMessage("请选择目标对象")
const point = await getPoint.go()
if (!point) return
const index = ss.pointSelect(point.x, point.y)
const ent = ss.item(index).getMcDbEntity()
if(!ent) return
const layerId = ent.layerId
let layerRec = layerId.getMcDbLayerTableRecord()
console.log(layerRec)
- 获取或设置图层是否被关闭:被关闭上的图层实体无法参与操作编辑。
const offVal = layerRec.isOff
// 关闭图层
layerRec.isOff = true
// 更新显示
mxcad.updateDisplay()
- 获取或设置图层是否被冻结:被冻结上的图层实体,不会参显示数据的生成,这样可以加快显示速度,和打开图纸的速度,它与关闭属性相比,冻结后的图层不但看不见,并且在内存中也不参加显示。
const frozenVal = layerRec.isFrozen
// 冻结图层
layerRec.isFrozen = true
// 更新显示
mxcad.updateDisplay()
- 获取或设置图层是否被锁定:锁定后的图层上的实体不能编辑,但可以选择。
const lockVal = layerRec.isLocked
// 锁定图层
layerRec.isLocked = true
// 更新显示
mxcad.updateDisplay()
- 获取或设置图层颜色。
const colorVal = layerRec.color
// 设置图层颜色为红色
layerRec.color = new McCmColor(255,0,0)
// 更新显示
mxcad.updateDisplay()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论