如何使用 .Net Api c# 获取 AutoCad 几何属性

问题描述

您好,如何使用 .NET API 获取 AutoCAD 图形对象属性(几何)。 如果,我需要在我的数据库中存储属性值之后获得属性

  1. 我不知道如何在 sql 数据库中存储 Autocad 对象信息。

  2. 我需要 Autocad 到 .net API 示例视频链接

  3. 我需要 Autocad 到数据库连接示例视频链接

我的 API 代码

public class Class1
    {
        [CommandMethod("ListLayers")]

        #region Test
        public static void ListLayers()
        {
            Document document = Application.DocumentManager.MdiActiveDocument;
            Database database = document.Database;
            using (Transaction transaction = database.TransactionManager.StartTransaction())
            {
                BlockTable blockTable = transaction.Getobject(database.BlockTableId,OpenMode.ForRead) as BlockTable;
                foreach (ObjectId blockTableObjectId in blockTable)
                {
                    BlockTableRecord blockTableRecord = transaction.Getobject(blockTableObjectId,OpenMode.ForRead) as BlockTableRecord;
                    document.Editor.WriteMessage("\n Block Name:" + blockTableRecord.BlockEndId);
                    object acadobj = blockTableRecord.Acadobject;
                    Type type = acadobj.GetType();
                    PropertyInfo[] propertyInfos = type.GetProperties();
                    foreach (var propertyInfo in propertyInfos)
                    {

                    }
                }
                LayerTable layerTable = transaction.Getobject(database.LayerTableId,OpenMode.ForRead) as LayerTable;
                foreach (ObjectId layerObjectId in layerTable)
                {
                    LayerTableRecord layerTableRecord = transaction.Getobject(layerObjectId,OpenMode.ForRead) as LayerTableRecord;
                    document.Editor.WriteMessage("\n Layer Name: " + layerTableRecord.Name);
                }
                transaction.Commit();
            }
        }
        #endregion

    }

我需要获取属性并存储到我的 C# 对象。

AutoCcad_Object_PropertyImage

解决方法

您可以使用反射来获取实体的所有属性。在以下示例中,PrintDump 方法在 AutCAD 文本屏幕中打印结果,但您可以更改此设置以满足您的需要。

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Reflection;
using AcAp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace DumpEntityProperties
{
   public class Commands
   {
       [CommandMethod("Dump")]
       public void Dump()
       {
           var doc = AcAp.DocumentManager.MdiActiveDocument;
           var db = doc.Database;
           var ed = doc.Editor;
           var result = ed.GetEntity("\nSelect entity: ");
           if (result.Status == PromptStatus.OK)
               PrintDump(result.ObjectId,ed);
           AcAp.DisplayTextScreen = true;
       }

       private void PrintDump(ObjectId id,Editor ed)
       {
           var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;

           using (var tr = id.Database.TransactionManager.StartTransaction())
           {
               var dbObj = tr.GetObject(id,OpenMode.ForRead);
               var types = new List<Type>();
               types.Add(dbObj.GetType());
               while (true)
               {
                   var type = types[0].BaseType;
                   types.Insert(0,type);
                   if (type == typeof(RXObject))
                       break;
               }
               foreach (Type t in types)
               {
                   ed.WriteMessage($"\n\n - {t.Name} -");
                   foreach (var prop in t.GetProperties(flags))
                   {
                       ed.WriteMessage("\n{0,-40}: ",prop.Name);
                       try
                       {
                           ed.WriteMessage("{0}",prop.GetValue(dbObj,null));
                       }
                       catch (System.Exception e)
                       {
                           ed.WriteMessage(e.Message);
                       }
                   }
               }
               tr.Commit();
           }
       }
   }
}
,

我编写了一些代码来获取模型空间中的所有实体。就像在 Autocad 绘图文件中包含一个圆形、矩形、三角形或一些图像一样。将获得所有绘图属性。

public static class SingleEntitySelection
    {
        [CommandMethod("Dump")]
        public static void Dump()
        {
            var doc = AcAp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var opts = new PromptSelectionOptions();
            opts.AllowSubSelections = true;
            opts.SelectEverythingInAperture = true;
            var result = ed.SelectAll();
            if (result.Status == PromptStatus.OK)
            {
                for (int i = 0; i < result.Value.Count; i++)
                {
                    PrintDump(result.Value[i].ObjectId,ed);
                    AcAp.DisplayTextScreen = true;
                }
            }
            AcAp.DisplayTextScreen = true;
        }

        public static void PrintDump(ObjectId id,Editor ed)
        {
            var flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;

            using (var tr = id.Database.TransactionManager.StartTransaction())
            {
                var dbObj = tr.GetObject(id,OpenMode.ForRead);
                var types = new List<Type>();
                types.Add(dbObj.GetType());
                while (true)
                {
                    var type = types[0].BaseType;
                    types.Insert(0,type);
                    if (type == typeof(RXObject))
                        break;
                }
                foreach (Type t in types)
                {
                    ed.WriteMessage($"\n\n - {t.Name} -");
                    foreach (var prop in t.GetProperties(flags))
                    {
                        ed.WriteMessage("\n{0,prop.Name);
                        try
                        {
                            ed.WriteMessage("{0}",null));
                        }
                        catch (System.Exception e)
                        {
                            ed.WriteMessage(e.Message);
                        }
                    }
                }
                tr.Commit();
            }
        }