当前位置:   article > 正文

基于C#的AutoCAD二次开发之获取用户输入信息、选择集、访问CAD内部命令_cad二次开发获取点坐标

cad二次开发获取点坐标

基于C#的AutoCAD二次开发之获取用户输入信息、选择集、访问CAD内部命令

    在CAD创建图形对象时,经常需要和用户进行交互,例如:直线创建时需要用户输入起点和终点(或长度)信息,复制对象时需要指定源对象等。
我的开发环境为Visual Studio 2017 & AutoCAD 2014 & AutoCAD 2020。

  1. 点拾取
  • 实现方法

方式一

PromptPointResult retStPoint = ed.GetPoint(("请输入点")); 
  • 1

方式二

PromptPointOptions ppo = new PromptPointOptions("请输入点"); 
PromptPointResult retStPoint = ed.GetPoint(ppo);
  • 1
  • 2
  • 效果展示
  • 实例代码(获取手动交互的某点坐标,并标注)

类库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

代码

[CommandMethod("PointCreated")]
public void ComputePointCreate()
{
    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.
        DocumentManager.MdiActiveDocument.Editor;
    PromptPointResult retStPoint = ed.GetPoint(("获得起点坐标"));
    if (retStPoint.Status != PromptStatus.OK)
        return;
    DBPoint pt = new DBPoint(retStPoint.Value);

    Database db = HostApplicationServices.WorkingDatabase;
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
        BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace],
            OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(pt);
        trans.AddNewlyCreatedDBObject(pt, true);
        trans.Commit();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 其他要素
    其它信息获取方法
  1. 选择集(读取鼠标框选选择集实体)
  • 实现方法
//获取用户框选的选择集对象
PromptSelectionResult pers = ed.GetSelection();
  • 1
  • 2
  • 实例代码(获取框选区的,线和多段线的数量)
    类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

代码

public Entity getEntityByObjId(Database db, ObjectId id)
{
    using (Transaction trans = db.TransactionManager.StartTransaction())
    {
        Entity dbo = trans.GetObject(id, OpenMode.ForRead) as Entity;
        trans.Commit();
        return dbo;
    }
    return null;
}

[CommandMethod("ModelCreated")]
public void ComputeModelCreated()
{
    Database db = HostApplicationServices.WorkingDatabase;
    Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
    //获取用户框选的选择集对象
    PromptSelectionResult pers = ed.GetSelection();
    if (pers.Status != PromptStatus.OK)//判断选择状态
        return;
    if (pers.Value == null)//判断选择集是否为空
        return;
    /*
    PromptEntityResult downEntity = ed.GetEntity("请选择起始线");
    if (downEntity.Status != PromptStatus.OK)
        return;
    */

    List<DBText> textArray = new List<DBText>();//注记
    List<Line> lineArray = new List<Line>();//线段
    List<Polyline> polylineArray = new List<Polyline>();//轻量)多段线
    List<Polyline2d> poly2dArray = new List<Polyline2d>();//拟合曲线
    List<Spline> splineArray = new List<Spline>();//曲线
    List<Point3d> pointArray = new List<Point3d>();//存储线段的点位置

    //获取输入对象的id或实体
    ObjectId[] oids = pers.Value.GetObjectIds();

    for (int ii = 0; ii < oids.Length; ii++)
    {
        ObjectId oid = oids[ii];
        Entity ent = getEntityByObjId(db, oid);
        if (ent == null)
            continue;

        if (ent is DBText)
        {
            textArray.Add(ent as DBText);
        }
        else if (ent is Line)
        {
            lineArray.Add(ent as Line);
        }
        else if (ent is Polyline)
        {
            polylineArray.Add(ent as Polyline);
        }
        else if (ent is Polyline2d)
        {
            poly2dArray.Add(ent as Polyline2d);
        }
        else if (ent is Spline)
        {
            splineArray.Add(ent as Spline);
        }
    }
    MessageBox.Show("线段共有" + lineArray.Count + "多段线有" + polylineArray.Count);
}
  • 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
  • 效果展示
  1. 访问CAD内部命令
  • 实现方式
    1)SendStringToExecute(.Net)
    2)SendCommand(COM方式)
    3)acedCommand( P/Invoke非托管C++函数)

  • 实现代码

[CommandMethod("CadCmdExecute")]
public void CadCmdExecute()
{
    //命令行动态执行语句
    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.
        DocumentManager.MdiActiveDocument;
    //创建一个圆形圆形,并强制缩放视图至图形
    doc.SendStringToExecute("circle\n2,2,0\n4\n", true, false, true);
    doc.SendStringToExecute("zoom e\n", true, false, true);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 总结
    喜欢的朋友们可以点个关注,后续将持续更新,精彩无限^ - ^
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/114312
推荐阅读
相关标签
  

闽ICP备14008679号