指南
- 开始
- 指南
- 基础绘图
- 交互绘图
- 图纸编辑
- 数据库
- 最佳实践
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
- 接口
- 模块
4. 参数化交互绘图
mxcad参数化绘图
mxcad 提供了参数化绘制的功能, 我们可以通过查看继承自McDbEntity的类的所有实例
它们都可以进行参数化的绘图
首先我们应该在页面上显示一张图纸, 请根据入门文档的说明
或者通过查看 github | https://www.wenjiangs.com/wp-content/uploads/2024/07/examples.zip 存储的初始化各种示例项目查看代码
来实现显示图纸的页面。
参数化绘制
绘制一个点
import { MxCpp, McDbPoint, McCmColor } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const point = new McDbPoint()
const color = new McCmColor()
color.setRGB(0, 255, 255)
point.trueColor = color
point.setPosition(200, 200)
mxcad.drawEntity(point)
绘制多行文字
import { MxCpp, McGePoint3d, McCmColor, McDb, McDbMText } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const mText = new McDbMText()
const textId = mxcad.drawEntity(mText)
const text = textId.getMcDbEntity() as McDbMText
text.attachment = McDb.AttachmentPoint.kTopLeft
text.contents = "内容 \\P 内容"
text.location = new McGePoint3d(10, 20)
text.trueColor = new McCmColor(255, 0, 255)
text.textHeight = 10
mxcad.updateDisplay()
绘制单行文本
import { MxCpp, McGePoint3d, McCmColor, McDb, McDbText } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const text = new McDbText()
text.widthFactor = 1
text.horizontalMode = McDb.TextHorzMode.kTextCenter
text.verticalMode = McDb.TextVertMode.kTextBottom
text.textString = "内容"
text.position = new McGePoint3d(-10, -20)
text.trueColor = new McCmColor(255, 0, 255)
text.height = 10
mxcad.drawEntity(text)
mxcad.updateDisplay()
绘制对齐标注尺寸
import { MxCpp, McGePoint3d, McCmColor, McDb, McDbAlignedDimension } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const mDimension = new McDbAlignedDimension()
const dimensionId = mxcad.drawEntity(mDimension)
const dimension = dimensionId.getMcDbEntity() as McDbAlignedDimension
dimension.xLine1Point = new McGePoint3d(0, 255)
dimension.xLine2Point = new McGePoint3d(30, 60)
dimension.dimLinePoint = new McGePoint3d(88, 88)
dimension.textAttachment = McDb.AttachmentPoint.kTopLeft
dimension.trueColor = new McCmColor(200, 255, 0)
dimension.oblique = 0
mxcad.updateDisplay()
绘制旋转标注尺寸
import { MxCpp, McGePoint3d, McCmColor, McDb, McDbRotatedDimension } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const mDimension = new McDbRotatedDimension()
const dimensionId = mxcad.drawEntity(mDimension)
const dimension = dimensionId.getMcDbEntity() as McDbRotatedDimension
dimension.xLine1Point = new McGePoint3d(100, -137)
dimension.xLine2Point = new McGePoint3d(161,30)
dimension.dimLinePoint = new McGePoint3d(80, -60)
dimension.textAttachment = McDb.AttachmentPoint.kTopLeft
dimension.textRotation = 0.23
dimension.trueColor = new McCmColor(200, 255, 0)
dimension.oblique = 0
dimension.rotation = 0
mxcad.updateDisplay()
绘制直线
import { MxCpp, McCmColor, McDbLine } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const line = new McDbLine(0, 0, 0, -80, -80, 0)
line.trueColor = new McCmColor(255, 0, 0)
mxcad.drawEntity(line)
绘制圆
import { MxCpp, McCmColor, McDbCircle } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const circle = new McDbCircle(-100, 300, 0, 20)
circle.trueColor = new McCmColor(255, 0, 0)
mxcad.drawEntity(circle)
绘制多义线
import { MxCpp, McGePoint3d, McDbPolyline } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const polyline = new McDbPolyline()
polyline.isClosed = false
polyline.constantWidth = 10
polyline.addVertexAt(new McGePoint3d(100, 100))
polyline.addVertexAt(new McGePoint3d(200, 100), 0.2, 1, 5, 1)
polyline.addVertexAt(new McGePoint3d(100, 200), 0.2, 5, 1, 2)
mxcad.drawEntity(polyline)
绘制圆弧
import { MxCpp, McGePoint3d, McDbArc, McCmColor } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const arc = new McDbArc()
arc.center = new McGePoint3d(-100, -100),
arc.radius = 20
arc.startAngle = Math.PI / 2
arc.endAngle = Math.PI * 3 / 2
arc.trueColor = new McCmColor(255, 233, 0)
mxcad.drawEntity(arc)
绘制椭圆
import { MxCpp, McGePoint3d, McDbEllipse, McCmColor, McGeVector3d } from "mxcad"
const mxcad = MxCpp.App.getCurrentMxCAD()
const ellipse = new McDbEllipse()
ellipse.center = new McGePoint3d(-200, -200),
ellipse.majorAxis = new McGeVector3d(0, 300, 0)
ellipse.minorAxis = new McGeVector3d(33, 0, 0)
ellipse.radiusRatio = 0.5
ellipse.startAngle = Math.PI / 2
ellipse.endAngle = Math.PI * 3 / 2
ellipse.trueColor = new McCmColor(255, 233, 0)
mxcad.drawEntity(ellipse)
更多图形的绘制,请自行参考上述代码和API文档完成。
交互绘制
在上面参数化绘制代码
一些属性都是固定写死的, 比如位置属性应该是用户通过点击或者输入框输入得到。
mxcad提供了这样一套用于获取用户输入并在绘制中得到用户输入的绘制流程。
最频繁的应该是鼠标点击输入, 如果通过鼠标点击获取CAD图纸中的坐标位置:
import { MxCADUiPrPoint } from "mxcad"
const getPoint = new MxCADUiPrPoint()
const point = await getPoint.go()
console.log(point)
上述代码打印的就是一个坐标点了, 其坐标点是用户通过鼠标点击获取到的对应的图纸坐标位置。
而用户输入也可能是通过输入框输入值, 来确定除了坐标以外的其他参数:
import { MxFun } from "mxdraw"
const input = document.createElement("input")
input.addEventListener("keydown", (e: KeyboardEvent) => {
// 设置传输命令行消息数据
MxFun.setCommandLineInputData((e.target as HTMLInputElement).value, e.keyCode);
})
document.body.appendChild(input)
mxcad中引用了mxdraw 下载依赖时会自动下载, 所有我们只要安装了mxcad就可以使用mxdraw
如上代码所示, 我们传入用户输入的内容和对应按键的keyCode值
const getInt = new MxCADUiPrInt()
const getKey = new MxCADUiPrKeyWord
const getStr = new MxCADUiPrString()
getInt.setMessage("提示用户输入数字:")
const intVal = await getInt.go()
console.log(intVal)
getKey.setMessage("提示用户关键词 A、 B、 C:")
getKey.setKeyWords("A B C")
const keyVal = await getKey.go()
console.log(keyVal)
getStr.setMessage("提示用户输入字符串:")
const strVal = await getStr.go()
console.log(strVal)
上述代码会在用户输入对应类型的数据后按下回车键(Enter或者Esc)才会往下执行, 通过setMessage设置提示
最终得到用户输入的数据, 通过这些数据进行参数化绘图。
最后这些设置的用户提示通过下面代码获得:
import { MxFun } from "mxdraw"
MxFun.listenForCommandLineInput(({ msCmdTip, msCmdDisplay, msCmdText }) => {
console.log(msCmdTip, msCmdDisplay, msCmdText)
}
);
如果你无法理解上述某个函数的意思 可以在mxdraw API文档或者mxcad API文档中搜索查看对应的APi说明
绘制文字的流程
举例一个参数化绘制文字的流程代码, 绘制其他图形也都推荐参考下面这段代码这样实现:
import { MxFun } from "mxdraw"
import { MxCADUiPrInt, MxCADUiPrKeyWord, MxCADUiPrString, MxCADUiPrPoint, McDbText, MxCpp } from "mxcad"
MxFun.addCommand("Mx_draw_Text", async ()=> {
const getInt = new MxCADUiPrInt()
const getKey = new MxCADUiPrKeyWord()
const getStr = new MxCADUiPrString()
const getPoint = new MxCADUiPrPoint()
const text = new McDbText()
getPoint.setMessage("请点击确定文字位置")
const position = await getPoint.go()
if(!position) return
text.position = position
getInt.setMessage("请输入文字高度")
const height = await getInt.go()
if(!height) return
text.height = height
getKey.setMessage("选择水平对齐方式 快捷键 L: 左对齐 C: 居中对齐 R: 右对齐 A: 水平对齐 M: 垂直中间对齐 F: 自适应")
getKey.setKeyWords("L C R A M F")
await getKey.go()
if(getKey.isKeyWordPicked("L")) text.horizontalMode = McDb.TextHorzMode.kTextLeft
if(getKey.isKeyWordPicked("C")) text.horizontalMode = McDb.TextHorzMode.kTextCenter
if(getKey.isKeyWordPicked("R")) text.horizontalMode = McDb.TextHorzMode.kTextRight
if(getKey.isKeyWordPicked("A")) text.horizontalMode = McDb.TextHorzMode.kTextAlign
if(getKey.isKeyWordPicked("M")) text.horizontalMode = McDb.TextHorzMode.kTextMid
if(getKey.isKeyWordPicked("F")) text.horizontalMode = McDb.TextHorzMode.kTextFit
getStr.setMessage("请输入文字内容")
const str = await getStr.go()
if(!str) return
text.textString = str
const mxcad = MxCpp.App.getCurrentMxCAD()
mxcad.drawEntity(text)
})
上述展示的完整项目源码下载地址: https://gitee.com/mxcadx/mxdraw-article/tree/master/mxcad参数化绘图/demo.zip
选择图形
通过鼠标点击选择选择使用MxCADUiPrEntity
import { MxCADUiPrEntity, MxCpp } from "mxcad"
// 得到当前控件
const mxcad = MxCpp.App.getCurrentMxCAD();
// 实例化一个mxcad提供的通过鼠标点击获得图形对象ID的类
let getEnt = new MxCADUiPrEntity();
let id = await getEnt.go();
// 通过ID对象得到图形数据对象
let ent = id.getMcDbEntity();
if(ent) {
const color = ent.trueColor.clone()
color.setRGB(255, 0, 0)
ent.trueColor = color
mxcad.updateDisplay()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论