在网页中如何绘制自定义实体?
MxDraw网页开发|黄洪辉|2018-05-07 16:51
-
回答:
调用DrawCustomEntity函数,绘制一个自定义实体对象。
下面代码绘制一个自定义实体,设置了两个属性,属性名分别“startpoint”,“endpoint”的两个点坐标,js代码实现如下:
//绘制自定义实体 function DyInsertCustomEntity() { //清空当前显示内容 mxOcx.NewFile(); var getPt = mxOcx.NewComObject("IMxDrawUiPrPoint"); getPt.message = "点取第一点"; if (getPt.go() != 1) return; var frstPt = getPt.value(); if (frstPt == null) return; var getSecondPt = mxOcx.NewComObject("IMxDrawUiPrPoint"); getSecondPt.message = "点取第二点"; getSecondPt.basePoint = frstPt; getSecondPt.setUseBasePt(true); if (getSecondPt.go() != 1) return; var secondPt = getSecondPt.value(); if (secondPt == null) return; var ent = mxOcx.DrawCustomEntity("TestMxCustomEntity", ""); ent.SetPoint("spt", frstPt); ent.SetPoint("ept", secondPt); }
需要响应DMxDrawXEvents::CustomEntity_Explode事件。
下面例子,得到自实体的数据,js代码实现如下:
// 自定义实体绘制函数 function ExplodeFun(pCustomEntity, pWorldDraw, txt) { var sGuid = pCustomEntity.Guid; if (sGuid == "TestMxCustomEntity") { if (!pCustomEntity.IsHave("ept")) return; var stp = pCustomEntity.GetPoint("spt"); if (stp == null) return; var ept = pCustomEntity.GetPoint("ept"); if (ept == null) return; var mxUtility = mxOcx.NewUtility(); var vec = ept.SumVector(stp); vec.Mult(0.5); var midPt = mxOcx.NewPoint(); midPt.x = stp.x; midPt.y = stp.y; midPt.Add(vec); var dAng = vec.Angle(); //计算一个标注角度,使用文字对象始终头朝上 dAng = mxUtility.GetDimAngle(dAng); var dDis = 0.0; dDis = stp.DistanceTo(ept); var sTxt = "L=" + formatNumber(dDis, '#.##'); dAng = dAng * 180.0 / 3.14159265; vec.RotateByXyPlan(3.14159265 / 2.0); vec.Normalize(); vec.Mult(10); stp.Add(vec); ept.Add(vec); pWorldDraw.DrawLine(stp.x, stp.y, ept.x, ept.y); vec.Mult(2); stp.Sum(vec); ept.Sum(vec); pWorldDraw.DrawLine(stp.x, stp.y, ept.x, ept.y); pWorldDraw.SetColorIndex(1); pWorldDraw.DrawText(midPt.x, midPt.y, sTxt, 5, dAng, 1, 2); mxOcx.SetEventRet(1); } }
需要响应_DMxDrawXEvents::CustomEntity_getGripPoints事件,js代码实现如下:
// 返回自定义实体夹点 function GetGripPointsFun(pCustomEntity) { var sGuid = pCustomEntity.Guid; //自定义实体绘制两条直线 if (sGuid == "TestMxCustomEntity") { if (!pCustomEntity.IsHave("ept")) return; var stp = pCustomEntity.GetPoint("spt"); if (stp == null) return; var ept = pCustomEntity.GetPoint("ept"); if (ept == null) return; var ret = mxOcx.NewResbuf(); ret.AddPoint(stp); ret.AddPoint(ept); mxOcx.SetEventRetEx(ret); } }
需要响应CustomEntity_moveGripPointsAt事件。
下面例子,夹点移动后,修改自定义实体的属性,js代码实现如下:
// 移动自定义实体夹点 function MoveGripPointsFun(pCustomEntity, lGridIndex, dOffsetX, dOffsetY) { var sGuid = pCustomEntity.Guid; if (sGuid == "TestMxCustomEntity") { if (!pCustomEntity.IsHave("ept")) return; var stp = pCustomEntity.GetPoint("spt"); if (stp == null) return; var ept = pCustomEntity.GetPoint("ept"); if (ept == null) return; if (lGridIndex == 0) { stp.x = stp.x + dOffsetX; stp.y = stp.y + dOffsetY; pCustomEntity.SetPoint("spt", stp); } else { ept.x = ept.x + dOffsetX; ept.y = ept.y + dOffsetY; pCustomEntity.SetPoint("ept", ept); } mxOcx.SetEventRet(1); } }