<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can this code be written in &amp;quot;. Net&amp;quot; language? in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11017381#M13367</link>
    <description>&lt;P&gt;You can P/Invoke the unmanaged acdbEntget and acdbGetAdsName functions.&lt;/P&gt;
&lt;P&gt;Note the acdbGetAdsName is major version and platform dependent.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;    // Replace the DLL name according to the AutoCAD targeted version
    // 2013-2014:   "acdb19.dll"
    // 2015-2016:   "acdb20.dll"
    // 2017:        "acdb21.dll"
    // 2018:        "acdb22.dll"
    // 2019-2020:   "acdb23.dll"
    // 2021-2022:   "acdb24.dll"
    // Replace the EntryPoint according to AutoCAD plateform
    // 32 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z"
    // 64 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z"
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("acdb24.dll", CallingConvention = CallingConvention.Cdecl,
        EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z")]
    static extern ErrorStatus acdbGetAdsName(out AdsName ename, ObjectId id);

    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
        EntryPoint = "acdbEntGet")]
    static extern IntPtr acdbEntGet(AdsName ename);

    public static ResultBuffer EntGet(ObjectId id)
    {
        var errorStatus = acdbGetAdsName(out AdsName ename, id);
        if (errorStatus != ErrorStatus.OK)
            throw new Autodesk.AutoCAD.Runtime.Exception(errorStatus);
        var result = acdbEntGet(ename);
        if (result != IntPtr.Zero) 
            return ResultBuffer.Create(result, true);
        return null;
    }

    [CommandMethod("TEST")]
    public static void Test()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        var entRes = ed.GetEntity("\nSelect an entity: ");
        if (entRes.Status == PromptStatus.OK)
        {
            var rb = EntGet(entRes.ObjectId);
            if (rb != null)
            {
                foreach (TypedValue typedValue in rb)
                {
                    ed.WriteMessage($"\n{typedValue.TypeCode,-3}\t{typedValue.Value}");
                }
                Application.DisplayTextScreen = true;
            }
        }
    }&lt;/LI-CODE&gt;</description>
    <pubDate>Sun, 20 Mar 2022 17:24:05 GMT</pubDate>
    <dc:creator>_gile</dc:creator>
    <dc:date>2022-03-20T17:24:05Z</dc:date>
    <item>
      <title>Can this code be written in ". Net" language?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11015985#M13364</link>
      <description>&lt;P&gt;Can you help write an example?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(defun c:test()&lt;/P&gt;&lt;P&gt;(setq p1 (entsel))&lt;/P&gt;&lt;P&gt;(setq p1 (car p1))&lt;/P&gt;&lt;P&gt;(setq p1_data (entget p1))&lt;/P&gt;&lt;P&gt;(setq p1_data (assoc 1 p1_data ))&lt;BR /&gt;)&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2022 15:06:21 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11015985#M13364</guid>
      <dc:creator>546817208</dc:creator>
      <dc:date>2022-03-19T15:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: Can this code be written in ". Net" language?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11016043#M13365</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The short answer is: "Not this way".&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;because the .NET API does not use DXF data (as returned by AutoLISP's entget function) to access entity properties;&lt;/LI&gt;
&lt;LI&gt;because .NET uses strong and static typing instead of dynamic typing, looking at your example, this means the p1 cannot be assigned to list and&amp;nbsp; to an ename (same for p1_data) and that we must first determine the entity type to get its property value, e.g. DBText.TextString, MText.Contents, ... for (cdr (assoc 1 ...)).&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;The closest .NET code to your LISP example (using dynamic typing and without any error checking) should be something like this (nobody should write such .NET code in real life):&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            var ppr = ed.GetEntity("\nSelect object: ");
            dynamic ent = ppr.ObjectId;
            ed.WriteMessage($"\n{ent.TextString}");
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In real life, the equivalent .NET code should be closer to this one:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;        [CommandMethod("TEST")]
        public static void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;
            var peo = new PromptEntityOptions("\nSelet a text: ");
            peo.SetRejectMessage("\nSelected object is not a text.");
            peo.AddAllowedClass(typeof(DBText), true);
            var per = ed.GetEntity(peo);
            if (per.Status != PromptStatus.OK) 
                return;
            using (var tr = db.TransactionManager.StartTransaction())
            {
                var ent = (DBText)tr.GetObject(per.ObjectId, OpenMode.ForRead);
                ed.WriteMessage($"\n{ent.TextString}");
                tr.Commit();
            }
        }&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Mar 2022 16:15:00 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11016043#M13365</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-03-19T16:15:00Z</dc:date>
    </item>
    <item>
      <title>Re: Can this code be written in ". Net" language?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11016962#M13366</link>
      <description>(setq p1_data (entget p1)) output:&lt;BR /&gt;((-1 . &amp;lt;图元名: 16f882e3e50&amp;gt;) (0 . "TCH_SPACE") (330 . &amp;lt;图元名: 16730aff9f0&amp;gt;) (5 . "1EDCAC5") (100 . "AcDbEntity") (67 . 0) (410 . "Model") (8 . "SPACE") (100 . "TDbEntity") (46 . 0.0) (47 . 100.0) (68 . 0) (100 . "TDbSpace") (70 . 5) (10 1.12856e+06 393884.0 0.0) (1 . "便民用房") (2 . "1033") (7 . "宋体") (40 . 3.5) (41 . 16.335) (42 . 16260.0) (43 . 120.0) (50 . 0.0) (90 . 0))&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;"assoc 1 p1_data" = per.ObjectId&lt;BR /&gt;"assoc 41 p1_data" = ???&lt;BR /&gt;&lt;BR /&gt;Can ". Net" get the return value of LISP function?&lt;BR /&gt;</description>
      <pubDate>Sun, 20 Mar 2022 10:42:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11016962#M13366</guid>
      <dc:creator>546817208</dc:creator>
      <dc:date>2022-03-20T10:42:32Z</dc:date>
    </item>
    <item>
      <title>Re: Can this code be written in ". Net" language?</title>
      <link>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11017381#M13367</link>
      <description>&lt;P&gt;You can P/Invoke the unmanaged acdbEntget and acdbGetAdsName functions.&lt;/P&gt;
&lt;P&gt;Note the acdbGetAdsName is major version and platform dependent.&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;    // Replace the DLL name according to the AutoCAD targeted version
    // 2013-2014:   "acdb19.dll"
    // 2015-2016:   "acdb20.dll"
    // 2017:        "acdb21.dll"
    // 2018:        "acdb22.dll"
    // 2019-2020:   "acdb23.dll"
    // 2021-2022:   "acdb24.dll"
    // Replace the EntryPoint according to AutoCAD plateform
    // 32 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AAY01JVAcDbObjectId@@@Z"
    // 64 bits:     "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z"
    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("acdb24.dll", CallingConvention = CallingConvention.Cdecl,
        EntryPoint = "?acdbGetAdsName@@YA?AW4ErrorStatus@Acad@@AEAY01_JVAcDbObjectId@@@Z")]
    static extern ErrorStatus acdbGetAdsName(out AdsName ename, ObjectId id);

    [System.Security.SuppressUnmanagedCodeSecurity]
    [DllImport("accore.dll", CallingConvention = CallingConvention.Cdecl,
        EntryPoint = "acdbEntGet")]
    static extern IntPtr acdbEntGet(AdsName ename);

    public static ResultBuffer EntGet(ObjectId id)
    {
        var errorStatus = acdbGetAdsName(out AdsName ename, id);
        if (errorStatus != ErrorStatus.OK)
            throw new Autodesk.AutoCAD.Runtime.Exception(errorStatus);
        var result = acdbEntGet(ename);
        if (result != IntPtr.Zero) 
            return ResultBuffer.Create(result, true);
        return null;
    }

    [CommandMethod("TEST")]
    public static void Test()
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        var entRes = ed.GetEntity("\nSelect an entity: ");
        if (entRes.Status == PromptStatus.OK)
        {
            var rb = EntGet(entRes.ObjectId);
            if (rb != null)
            {
                foreach (TypedValue typedValue in rb)
                {
                    ed.WriteMessage($"\n{typedValue.TypeCode,-3}\t{typedValue.Value}");
                }
                Application.DisplayTextScreen = true;
            }
        }
    }&lt;/LI-CODE&gt;</description>
      <pubDate>Sun, 20 Mar 2022 17:24:05 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/can-this-code-be-written-in-quot-net-quot-language/m-p/11017381#M13367</guid>
      <dc:creator>_gile</dc:creator>
      <dc:date>2022-03-20T17:24:05Z</dc:date>
    </item>
  </channel>
</rss>

