CAD绘制自定义实体(com接口)

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

调用DrawCustomEntity函数,绘制一个自定义实体对象。

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

private void DrawMlineCommand()
{
    MxDrawUiPrPoint getPt = new MxDrawUiPrPoint();
    getPt.message = "点取第一点";
    if (getPt.go() != MCAD_McUiPrStatus.mcOk)
    {
        return;
    }

    var frstPt = getPt.value();
    if (frstPt == null)
    {
        return;
    }

    MxDrawUiPrPoint getSecondPt = new MxDrawUiPrPoint();

    getSecondPt.message = "点取第二点";
    getSecondPt.basePoint = frstPt;

    getSecondPt.setUseBasePt(false);

    MxDrawCustomEntity spDrawData = getSecondPt.InitUserDraw("DrawCustEntity");
    spDrawData.SetDouble("Width", 30);

    spDrawData.SetPoint("Point1", frstPt);

    Int32 lCount = 1;
    spDrawData.SetLong("Count", 1);

    while (true)
    {
        if (getSecondPt.go() != MCAD_McUiPrStatus.mcOk)
            break;

        var secondPt = getSecondPt.value();
        if (secondPt == null)
            break;

        lCount++;

        String sPointName = "Point" + lCount.ToString();

        spDrawData.SetPoint(sPointName, secondPt);
        spDrawData.SetLong("Count", lCount);
    }
    if (lCount > 1)
        axMxDrawX1.DrawEntity(spDrawData);
}

需要响应DMxDrawXEvents::CustomEntity_Explode事件。

下面例子,得到自实体的数据,C#代码实现如下:

private void axMxDrawX1_CustomEntity_Explode(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_ExplodeEvent e)
        {
            MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
            var sGuid = pCustomEntity.Guid;

            MxDrawWorldDraw pWorldDraw = (MxDrawWorldDraw)e.pDraw;

            if (sGuid == "DrawCustEntity")
            {
                MyDrawMline(pWorldDraw, pCustomEntity, null);
                e.pRet = 1;
            }
        }

private void MyDrawMline(MxDrawWorldDraw pWorldDraw, MxDrawCustomEntity pCustomEntity, MxDrawPoint curPt)
{
    // 取自定义实体的端点数目,属性。
    if (!pCustomEntity.IsHave("Count"))
        return;
    long lCount = pCustomEntity.GetLong("Count");
    MxDrawPolyline tmpPl = new MxDrawPolyline();
    for (long i = 0; i < lCount; i++)
    {
        String sName;
        sName = "Point" + (i + 1).ToString();
        if (!pCustomEntity.IsHave(sName))
            break;

        // 取自定义实体的端点坐标。
        MxDrawPoint pt = pCustomEntity.GetPoint(sName);

        // 把端点坐标,传给pl线,用于生成双线。
        tmpPl.AddVertexAt(pt);
    }
    if (curPt != null)
        tmpPl.AddVertexAt(curPt);

    if (tmpPl.NumVerts < 2)
    {
        // 端点数少于2就,不构成直线,就不需要显示。
        return;
    }
    // 求pl线,开始点的导数.
    MxDrawVector3d vecFx;
    if (!tmpPl.GetFirstDeriv(tmpPl.GetStartParam(), out vecFx))
        return;
    if (vecFx.IsZeroLength())
        return;
    // 把向量旋转90度.
    vecFx.RotateByXyPlan(3.14159265 / 2.0);
    vecFx.Normalize();
    //  得到双线的宽度属性。
    double dWidth = pCustomEntity.GetDouble("Width");
    vecFx.Mult(dWidth);
    MxDrawPoint startPt = tmpPl.GetStartPoint();

    // 向pl线,两个方向偏移,
    MxDrawPoint offsetPt1 = new MxDrawPoint();
    offsetPt1.x = startPt.x;
    offsetPt1.y = startPt.y;
    offsetPt1.Add(vecFx);
    MxDrawPoint offsetPt2 = new MxDrawPoint();
    offsetPt2.x = startPt.x;
    offsetPt2.y = startPt.y;
    offsetPt2.Sum(vecFx);
    MxDrawText text = new MxDrawText();
    text.TextString = "Test";
    text.Height = 100;
    text.Position = startPt;
    text.AlignmentPoint = startPt;
    MxDrawPoint pt1, pt2;
    text.GetBoundingBox(out pt1, out pt2);
    MxDrawPoint pt3 = new MxDrawPoint();
    pt3.x = pt1.x;
    pt3.y = pt2.y;
    MxDrawPoint pt4 = new MxDrawPoint();
    pt4.x = pt2.x;
    pt4.y = pt1.y;
    MxDrawPoints pts = new MxDrawPoints();
    pts.Add(pt1.x, pt1.y, 0);
    pts.Add(pt3.x, pt3.y, 0);
    pts.Add(pt2.x, pt2.y, 0);
    pts.Add(pt4.x, pt4.y, 0);

    Int32 lDraworder = pWorldDraw.Draworder;
    pWorldDraw.Draworder = lDraworder + 1;
    pWorldDraw.DrawWipeout(pts);
    pWorldDraw.Draworder = lDraworder + 2;
    pWorldDraw.DrawEntity((MxDrawEntity)text);
    pWorldDraw.Draworder = lDraworder;
    //    pWorldDraw->
    {
        MxDrawResbuf newobj;
        if (tmpPl.OffsetCurves(dWidth, offsetPt1, out newobj))
        {
            for (Int32 j = 0; j < newobj.Count; j++)
            {
                MxDrawEntity tmpObj = (MxDrawEntity)newobj.AtObject(j);
                if (tmpObj == null)
                    continue;

                pWorldDraw.DrawEntity(tmpObj);
            }
            newobj.RemoveAll();
        }
    }
    {
        MxDrawResbuf newobj;
        if (tmpPl.OffsetCurves(dWidth, offsetPt2, out newobj))
        {
            for (Int32 j = 0; j < newobj.Count; j++)
            {
                MxDrawEntity tmpObj = (MxDrawEntity)newobj.AtObject(j);
                if (tmpObj == null)
                    continue;

                pWorldDraw.DrawEntity(tmpObj);
            }
            // 这不使用newobj,需要显示调用RemoveAll函数清楚内存。
            // 不然这个可能就会程序退出时才释放,这时它会去释放控件对象指针,有可能会出错。
            newobj.RemoveAll();
        }
    }
}

C#代码实现如下:

private void axMxDrawX1_DynWorldDraw(object sender, AxMxDrawXLib._DMxDrawXEvents_DynWorldDrawEvent e)
{
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pData;
    String sGuid = pCustomEntity.Guid;
    e.pRet = 0;

    MxDrawWorldDraw pWorldDraw = (MxDrawWorldDraw)e.pWorldDraw;
    MxDrawPoint curPt = new MxDrawPoint();
    curPt.x = e.dX;
    curPt.y = e.dY;

    if (sGuid == "DrawCustEntity")
    {
        MyDrawMline(pWorldDraw, pCustomEntity, curPt);
    }
}

需要响应_DMxDrawXEvents::CustomEntity_getGripPoints事件,C#代码实现如下:

private void axMxDrawX1_CustomEntity_getGripPoints(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_getGripPointsEvent e)
{
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
    var sGuid = pCustomEntity.Guid;
    e.pOk = 0;
    if (sGuid == "DrawCustEntity")
    {
        if (!pCustomEntity.IsHave("Count"))
            return;

        long lCount = pCustomEntity.GetLong("Count");

        MxDrawResbuf ret = (MxDrawResbuf)axMxDrawX1.NewResbuf();

        for (long i = 0; i < lCount; i++)
        {
            String sName;
            sName = "Point" + (i + 1).ToString();
            if (!pCustomEntity.IsHave(sName))
                break;

            // 取自定义实体的端点坐标。
            MxDrawPoint pt = pCustomEntity.GetPoint(sName);

            ret.AddPoint(pt);
        }
        e.pOk = 1;
        axMxDrawX1.SetEventRetEx(ret);
    }

}

需要响应CustomEntity_moveGripPointsAt事件。

下面例子,夹点移动后,修改自定义实体的属性,C#代码实现如下:

private void axMxDrawX1_CustomEntity_moveGripPointsAt(object sender, AxMxDrawXLib._DMxDrawXEvents_CustomEntity_moveGripPointsAtEvent e)
{
    e.pRet = 1;
    MxDrawCustomEntity pCustomEntity = (MxDrawCustomEntity)e.pCustomEntity;
    var sGuid = pCustomEntity.Guid;

    if (sGuid == "DrawCustEntity")
    {
        if (!pCustomEntity.IsHave("Count"))
            return;

        long lCount = pCustomEntity.GetLong("Count");

        for (long i = 0; i < lCount; i++)
        {
            String sName;
            sName = "Point" + (i + 1).ToString();
            if (!pCustomEntity.IsHave(sName))
                break;

            // 取自定义实体的端点坐标。
            MxDrawPoint pt = pCustomEntity.GetPoint(sName);

            if (i == e.lGridIndex)
            {
                pt.x = pt.x + e.dOffsetX;
                pt.y = pt.y + e.dOffsetY;

                pCustomEntity.SetPoint(sName, pt);
            }
        }
    }
}

原文地址:https://www.cnblogs.com/yzy0224/p/10955635.html

时间: 2024-11-29 09:27:04

CAD绘制自定义实体(com接口)的相关文章

CAD实现自定义实体夹点移动(com接口VB语言)

主要用到函数说明: MxDrawXCustomEvent::MxDrawXCustomEntity::moveGripPointsAt 自定义实体事件,自定义实体夹点被移动,详细说明如下: 参数 说明 LONGLONG lEntity 自定义实体id IN LONG lGridIndex 夹点索引号 IN McGePoint3d vec 夹点移动的向量 VB代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

【CAD】自定义实体的步骤(转)

本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h","acadstrc.h", "geassign.h". 2.在该类头文件的类声明中添加宏:ACRX_DECLARE_MEMBERS(EntTest); 3.在该类的cpp文件中,类的前面添加宏: ACRX_DXF_DEFINE_MEMBERS(EntTest, AcDbEntity

CAD控件:COM接口实现自定义实体的方法

1. 实现步骤: 3 1. 实现步骤: 参考例子 :Src\MxDraw5.2\samples\ie\iedemoTest.htm 1) 增加自定义实体对象 调用DrawCustomEntity函数,绘制一个自定义实体对象 函数说明如下: JS例子,下面代码绘制一个自定义实体,设置了两个属性,属性名分别" startpoint"," endpoint"的两个点坐标, // 插入自定义实体函数 function InsertCustomEntity() { var g

CAD控件使用教程 自定义实体的实现

自定义实体的实现 1 .       自定义实体... 3 1.1      说明... 3 1.2      类的类型信息... 3 1.3      worldDraw.. 4 1.4      getGripPoints 4 1.5      moveGripPointsAt 5 1.6      getGeomExtents 6 1.7      getOsnapPoints 6 1.8      explode. 7 1.9      dwgInFields 8 1.10    dwg

梦想CAD控件自定义实体实现

一.增加自定义实体对象 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private void DrawMlineCommand() {     MxDrawUiPrPoint getPt = new MxDrawUiPrPoint();     getPt.message = "点取第一点";     if (getPt.go() != MCAD_McUiPrStatus.mcOk)     {         r

CAD由一个自定义实体事件中的id得到自定义实体对象(com接口VB语言)

由一个自定义实体事件中的id得到自定义实体对象.该函数只能在自定义实体事件中调用. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 If sEventName = "MxDrawXCustomEntity::explode" Then             Dim param66 As M

CAD参数绘制圆(com接口)

CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以设置圆的圆心位置及半径属性. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下: 参数 说明 DOUBLE dCenterX 圆的中心X值 DOUBLE dCenterY 圆的中心Y值 DOUBLE dRadius 圆的半径 C#中实现代码说明: private void DrawCircle() { //清空当前显示内容 axMxDrawX1.NewFile(); //把颜色改回黑白色 axMxDr

Android中使用ListView绘制自定义表格(2)

上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 一.功能: 1.支持列合并 2.考虑了界面刷新优化 3.预留部分接口 4.支持左右滚动 1.枚举类:CellTypeEnum package csdn.danielinbiti.custometableview.item; public enum CellTypeEnum { STRING //字符 ,DI

用ARX自定义实体

本文介绍了构造自定义实体的步骤.必须继承的函数和必须注意的事项 1.新建一个从AcDbEntity继承的类,如EntTest,必须添加的头文件: "stdarx.h","acadstrc.h", "geassign.h". 2.在该类头文件的类声明中添加宏:ACRX_DECLARE_MEMBERS(EntTest); 3.在该类的cpp文件中,类的前面添加宏: ACRX_DXF_DEFINE_MEMBERS(EntTest, AcDbEntity