如何实现自定义实体?
-
回答:
参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm
1) 增加自定义实体对象
调用DrawCustomEntity函数,绘制一个自定义实体对象
函数说明如下:
JS例子,下面代码绘制一个自定义实体,设置了两个属性,属性名分别” startpoint”,” endpoint”的两个点坐标,
// 插入自定义实体函数
function InsertCustomEntity() {
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);
};
2) 响应自定义事件,绘制自定义实体
需要响应DMxDrawXEvents::CustomEntity_Explode事件
帮助如下:
JS例子,下面例子,得到自实体的数据,根据自定义实体的两个必属,开始点,和结束点绘制一个直线
// 自定义实体绘制函数
function ExplodeFun(pCustomEntity, pWorldDraw) {
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);
}
}
3) 响应自定义事件,返回自定义实体夹点
需要响应_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);
}
}
4) 响应自定义事件,夹点移动后的处理
需要响应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);
}
}