当前位置:首页 > 帮助文档 > 网页开发 > 自定义实体
简介增加自定义实体对象增加绘制自定义实体1对象响应自定义事件,绘制自定义实体响应自定义事件,动态绘制响应自定义事件,返回自定义实体夹点响应自定义事件,夹点移动后的处理
简介

在cad使用过程中,用户可以绘制自定义实体。点击此处在线演示

增加自定义实体对象

调用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);
}
增加绘制自定义实体1对象

下面代码绘制一个自定义实体1,js代码实现如下:


//绘制自定义实体1
function MyInsertCustomEntity()
{ //清空当前显示内容
    mxOcx.NewFile();
    var spDrawData;
    var getPt1 = mxOcx.NewComObject("IMxDrawUiPrPoint");
    {
        getPt1.message = "点取第一点";
        spDrawData = getPt1.InitUserDraw("DynDrawEnt1");
        if (getPt1.go() != 1) {
            return;
        }
    }
    var getPt2 = mxOcx.NewComObject("IMxDrawUiPrPoint");
    {
        getPt2.message = "点取第二点";
        spDrawData = getPt2.InitUserDraw("DynDrawEnt2");
        spDrawData.SetPoint("vFirst", getPt1.value());
        if (getPt2.go() != 1) {
            return;
        }
    }
    var getPt3 = mxOcx.NewComObject("IMxDrawUiPrPoint");
    {
        getPt3.message = "点取第三点";
        spDrawData = getPt3.InitUserDraw("DynDrawEnt3");
        spDrawData.SetPoint("vFirst", getPt1.value());
        spDrawData.SetPoint("vSecond", getPt2.value());
        if (getPt3.go() != 1) {
            return;
        }
    }
    var getPt4 = mxOcx.NewComObject("IMxDrawUiPrPoint");
    {
        getPt4.message = "点取第四点";
        spDrawData = getPt4.InitUserDraw("DynDrawEnt4");
        spDrawData.SetPoint("vFirst", getPt1.value());
        spDrawData.SetPoint("vSecond", getPt2.value());
        spDrawData.SetPoint("vThird", getPt3.value());
        if (getPt4.go() != 1) {
            return;
        }
    }
    {
        spDrawData = mxOcx.DrawCustomEntity("DrawEnt", "");
        spDrawData.SetPoint("vFirst", getPt1.value());
        spDrawData.SetPoint("vSecond", getPt2.value());
        spDrawData.SetPoint("vThird", getPt3.value());
        spDrawData.SetPoint("vFour", getPt4.value());
    }
}
响应自定义事件,绘制自定义实体

需要响应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);

    }
   

    var dHeigth = mxOcx.ViewLongToDocCoord(5);
    
    // 绘制绘制自定义实体1
    if(sGuid == "DrawEnt"){
        {
            if (!pCustomEntity.IsHave("vFour"))
                return;

            if (!pCustomEntity.IsHave("vThird"))
                return;

            if (!pCustomEntity.IsHave("vSecond"))
                return;

            if (!pCustomEntity.IsHave("vFirst"))
                return;
        }
        {
            var vFourPt = pCustomEntity.GetPoint("vFour");
            var dX = vFourPt.x;
            var dY = vFourPt.y;

            var vThirdPt = pCustomEntity.GetPoint("vThird");
            pWorldDraw.DrawLine(vThirdPt.x, vThirdPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            var dX = vThirdPt.x;
            var dY = vThirdPt.y;

            var vSecondPt = pCustomEntity.GetPoint("vSecond");
            pWorldDraw.DrawLine(vSecondPt.x, vSecondPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            dX = vSecondPt.x;
            dY = vSecondPt.y;

            var vFirstPt = pCustomEntity.GetPoint("vFirst");
            pWorldDraw.DrawLine(vFirstPt.x, vFirstPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            dX = vFirstPt.x;
            dY = vFirstPt.y;

            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
        }
        mxOcx.SetEventRet(1);
    }

}
响应自定义事件,动态绘制

js代码实现如下:


//动态施放绘制事件回调函数指针
//动态拖放时的绘制事件
//参数一为当前鼠标位置X坐标;参数为二当前鼠标位置Y坐标;
// 参数三为IMxDrawWorldDraw对象,用于动态绘制;参数四为IMxDrawCustomEntity对象,动态绘制数据
function DoDynWorldDrawFun(dX,dY,pWorldDraw,pData) {
    //自定义实体的GUID标识符
    var sGuid = pData.Guid;
    //设置自定义事件的返回值
    mxOcx.SetEventRet(0);

    var curPt = mxOcx.NewPoint();
    curPt.x = dX;
    curPt.y = dY;
    
    var dHeigth = mxOcx.ViewLongToDocCoord(5);
    //自定义实体的GUID标识符
    switch (pData.Guid) {
        case "DynDrawEnt4":
        {
            var vThirdPt = pData.GetPoint("vThird");
            pWorldDraw.DrawLine(vThirdPt.x, vThirdPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            dX = vThirdPt.x;
            dY = vThirdPt.y;
        }
        case "DynDrawEnt3":
        {
            var vSecondPt = pData.GetPoint("vSecond");
            pWorldDraw.DrawLine(vSecondPt.x, vSecondPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            dX = vSecondPt.x;
            dY = vSecondPt.y;
        }
        case "DynDrawEnt2":
        {
            var vFirstPt = pData.GetPoint("vFirst");
            pWorldDraw.DrawLine(vFirstPt.x, vFirstPt.y, dX, dY);
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
            dX = vFirstPt.x;
            dY = vFirstPt.y;
        }
        case "DynDrawEnt1":
        {
            pWorldDraw.DrawText(dX, dY, "(" + dX.toFixed(1) + "," + dY.toFixed(1) + ")", dHeigth, 0, 1, 1);
        }
    }
    //设置自定义事件的返回值
    mxOcx.SetEventRet(0);


}
响应自定义事件,返回自定义实体夹点

需要响应_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);
    }
    
    //自定义实体1
    if (sGuid == "DrawEnt") {
        if (!pCustomEntity.IsHave("vFour"))
            return;
        var stp = pCustomEntity.GetPoint("vFirst");
        if (stp == null)
            return;
        var ept = pCustomEntity.GetPoint("vSecond");
        if (ept == null)
            return;
        var tpt = pCustomEntity.GetPoint("vThird");
        if (tpt == null)
            return;
        var fpt = pCustomEntity.GetPoint("vFour");
        if (fpt == null)
            return;
        var ret = mxOcx.NewResbuf();
        ret.AddPoint(stp);
        ret.AddPoint(ept);
        ret.AddPoint(tpt);
        ret.AddPoint(fpt);
        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);
    }
    if (sGuid == "DrawEnt") {
        if (!pCustomEntity.IsHave("vFour"))
            return;
        var stp = pCustomEntity.GetPoint("vFirst");
        if (stp == null)
            return;
        var ept = pCustomEntity.GetPoint("vSecond");
        if (ept == null)
            return;
        var tpt = pCustomEntity.GetPoint("vThird");
        if (tpt == null)
            return;
        var fpt = pCustomEntity.GetPoint("vFour");
        if (fpt == null)
            return;
        {
            stp.x = stp.x + dOffsetX;
            stp.y = stp.y + dOffsetY;
            pCustomEntity.SetPoint("vFirst", stp);
            ept.x = ept.x + dOffsetX;
            ept.y = ept.y + dOffsetY;
            pCustomEntity.SetPoint("vSecond", ept);
            tpt.x = tpt.x + dOffsetX;
            tpt.y = tpt.y + dOffsetY;
            pCustomEntity.SetPoint("vThird", tpt);
            fpt.x = fpt.x + dOffsetX;
            fpt.y = fpt.y + dOffsetY;
            pCustomEntity.SetPoint("vFour", fpt);
        }
        mxOcx.SetEventRet(1);
    }
}
梦想CAD是专业的CAD插件(控件),可轻松在网页、手机及BS/CS程序中浏览编辑DWG文件,不需安装AutoCAD即可运行。经十余年累积已非常稳定可靠,有关键的空间搜索算法,并使用汇编优化,可同时处理50万级实体,有非常高的图形显示和处理效率。
联系我们
TEL:400-888-5703
185-8173-1060
客服I QQ: 3570505660
技术I QQ: 827867134
技术II QQ:6884123
VIP服务购买
销售TEL:156-8136-8971
销售QQ:710714273
用户交流
控件QQ交流群1:73281982
控件QQ交流群2:112199959
MxCAD软件群 1:515771658
技术I:QQ
827867134
客服I:QQ
3570505660
销售QQ
710714273
联系电话400-888-5703