2

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

JuniorSoft
  • 33
  • 5
  • The geometrical properties of the objects differ according to the type of object.For example, a circle is defined by its center (Point3d), its radius (double) and its normal (Vector3d), a line by its starting point (Point3d) and its end point (Point3d). You have to specify which types of objects you are targeting and for each type which properties. – gileCAD Apr 13 '21 at 05:29
  • I Need all my drawing objects' properties. For example: In My Autocad Drawing I have One circle, One Rectangle, and some House plan drawings. so Need to all properties based on type like circle type, rectangle, etc., – JuniorSoft Apr 13 '21 at 07:02

2 Answers2

2

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();
           }
       }
   }
}
gileCAD
  • 2,295
  • 1
  • 10
  • 10
0

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();
            }
        }
JuniorSoft
  • 33
  • 5