繁体   English   中英

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

[英]How to Get AutoCad Geometry Property using .Net Api c#

嗨,如何使用 .NET API 获取 AutoCAD 绘图 object 属性(几何)。如果,我需要在我的数据库中存储属性值之后获取属性。

  1. 我不知道如何在 SQL 数据库中存储 Autocad object 信息。

  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# object。 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,-40}: ", prop.Name);
                        try
                        {
                            ed.WriteMessage("{0}", prop.GetValue(dbObj, null));
                        }
                        catch (System.Exception e)
                        {
                            ed.WriteMessage(e.Message);
                        }
                    }
                }
                tr.Commit();
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM