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

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

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


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


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
45
46
47
48
49
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#代码实现如下:


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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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#代码实现如下:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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#代码实现如下:


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
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#代码实现如下:


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
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);
            }
        }
    }
}

posted on   梦想CAD控件  阅读(1371)  评论(0编辑  收藏  举报

编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示