简体   繁体   中英

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

Hi, How to get AutoCAD drawing object property(Geometry) using .NET API. If, I get the property After I need to store property value in my database.

  1. I don't know How to store Autocad object Information in an SQL database.

  2. I need Autocad to .net API sample videos link.

  3. I need Autocad to database connection sample videos link.

My API Code:

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

    }

I Need to Get the property and store to My C# object. AutoCcad_Object_PropertyImage

You can use Reflection to get all the property of an entity. In the following example, the PrintDump method prints the result in the AutCAD text screen, but you can change this to suit your needs.

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

I write some code it gets all entities in modelspace. Like in Autocad drawing file contains a circle, rectangle, triangle, or some image. all drawing properties will get.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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