Issues with AutoCAD 2025 Library files

Issues with AutoCAD 2025 Library files

luckyboy82292
Advocate Advocate
2.748 Vistas
12 Respuestas
Mensaje 1 de 13

Issues with AutoCAD 2025 Library files

luckyboy82292
Advocate
Advocate

Hi everyone!

 

I have some issues with AutoCAD 2025. When I reference to library files, it says 'Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found'. And I couldn't debug or create the application plugin. So any help in this issue will be greatly appreciated.

 

Thank you!

0 Me gusta
Soluciones aceptadas (1)
2.749 Vistas
12 Respuestas
Respuestas (12)
Mensaje 2 de 13

_gile
Consultant
Consultant

Hi,

I guess you're trying to use Marshal.GetActiveObject. If so, you should see this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Me gusta
Mensaje 3 de 13

luckyboy82292
Advocate
Advocate

Thank you for sharing the link. But I need a proper solution where I know the exact solution like how to resolve the issue. A step by step guide will be very appreciated.

0 Me gusta
Mensaje 4 de 13

_gile
Consultant
Consultant

You can create the following class to your project (referencing System.Runtime.InteropServices).

public class MarshalExtension
{
    [DllImport("ole32")]
    private static extern int CLSIDFromProgIDEx(
    [MarshalAs(UnmanagedType.LPWStr)] string lpszProgID,
    out Guid lpclsid);
    [DllImport("oleaut32")]
    private static extern int GetActiveObject(
      [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
      IntPtr pvReserved,
      [MarshalAs(UnmanagedType.IUnknown)] out object ppunk);

    public static object? GetActiveObject(string progId,
                                         bool throwOnError = false)
    {
        ArgumentNullException.ThrowIfNull(progId);

        var hr = CLSIDFromProgIDEx(progId, out var clsid);
        if (hr < 0)
        {
            if (throwOnError)
                Marshal.ThrowExceptionForHR(hr);

            return null;
        }
        hr = GetActiveObject(clsid, IntPtr.Zero, out var obj);
        if (hr < 0)
        {
            if (throwOnError)
                Marshal.ThrowExceptionForHR(hr);

            return null;
        }
        return obj;
    }
}

Then, instead of: Marshal.GetActiveObject(progId); simply call:

MarshalExtension.GetActiveObject(progId);

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Me gusta
Mensaje 5 de 13

luckyboy82292
Advocate
Advocate

This is the code. Error occurrs here "PromptEntityResult result = ed.GetEntity(prompt);" and at many places.

using System;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.Civil.ApplicationServices;
using Autodesk.Civil.DatabaseServices;

[assembly: CommandClass(typeof(ExportSurfacePoints.Plugin))]

namespace ExportSurfacePoints
{
    public class Plugin
    {
        [CommandMethod("ExportSurfacePoints")]
        public void ExportSurfacePoints()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            CivilDocument civilDoc = CivilApplication.ActiveDocument;

            // Prompt user to select a surface
            PromptEntityOptions prompt = new PromptEntityOptions("\nSelect a surface:");
            prompt.SetRejectMessage("\nSelected object is not a surface.");
            prompt.AddAllowedClass(typeof(TinSurface), true);

            PromptEntityResult result = ed.GetEntity(prompt);

            if (result.Status != PromptStatus.OK) return;

            using (Transaction tr = doc.TransactionManager.StartTransaction())
            {
                TinSurface surface = tr.GetObject(result.ObjectId, OpenMode.ForRead) as TinSurface;

                if (surface == null)
                {
                    ed.WriteMessage("\nError: Selected object is not a valid TIN Surface.");
                    return;
                }

                // Ask user to provide file name and location
                PromptSaveFileOptions saveFileOptions = new PromptSaveFileOptions("\nEnter file path to save surface points:");
                saveFileOptions.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
                PromptFileNameResult fileNameResult = ed.GetFileNameForSave(saveFileOptions);

                if (fileNameResult.Status != PromptStatus.OK) return;

                string filePath = fileNameResult.StringResult;

                // Export surface points to a file
                using (StreamWriter writer = new StreamWriter(filePath))
                {
                    writer.WriteLine("X,Y,Z");

                    foreach (TinSurfaceVertex vertex in surface.Vertices)
                    {
                        writer.WriteLine($"{vertex.Location.X},{vertex.Location.Y},{vertex.Location.Z}");
                    }
                }

                ed.WriteMessage($"\nSurface points exported to: {filePath}");

                tr.Commit();
            }
        }
    }
}

The errors are "Reference to type 'ICloneable' claims it is defined in 'System.Runtime', but it could not be found" and "Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found".

0 Me gusta
Mensaje 6 de 13

_gile
Consultant
Consultant

Is your plugin created from a .NET 8.0 template (class library targeting .NET or .NET Standard, not .NET Framework)?

If so, do you referenced AutoCAD 2025 libraries?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Me gusta
Mensaje 7 de 13

luckyboy82292
Advocate
Advocate

I targeted .NET Framework 4.8.1 and referenced the 2025 libraries.

0 Me gusta
Mensaje 8 de 13

luckyboy82292
Advocate
Advocate

When I use the same code with 2024 libs, it works. But it gives me these errors when use with 2025.

0 Me gusta
Mensaje 9 de 13

_gile
Consultant
Consultant
Solución aceptada

With AutoCAD 2025, you have to target .NET (Core) 8.0. See this topic.

If you start a project from scratch, you have to start from a class library template targetting .NET or .NET Standard as shown in this topic.

You can also start from some AutoCAD specific .NET Template as this one (attentively read the 'README' to correctly edit the template files).

Another way could be migrating a working project targetting .NET Framework to .NET 8 as desrcribed in this thread.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Me gusta
Mensaje 10 de 13

luckyboy82292
Advocate
Advocate

Thank you so much sir. You saved my day.

0 Me gusta
Mensaje 11 de 13

nomanmurtaza95
Observer
Observer

To address the issue of 'Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found' in AutoCAD 2025, the error typically occurs due to mismatches in the .NET framework libraries that are referenced in your project. Here are some steps you can take to resolve this:

1. Check .NET Framework Version:

Ensure that your AutoCAD 2025 plugin or application is targeting the correct .NET framework version. AutoCAD 2025 might require a newer version of .NET than what you're currently using.

  • Open the project properties.
  • Go to the Application tab and check the Target Framework setting. Make sure it's set to the correct version compatible with AutoCAD 2025 (typically .NET Framework 4.7 or above).

    For more detailed updates on AutoCAD 2025 and how AI-driven innovations are improving design capabilities, check out this article:
    AutoCAD 2025: AI-Driven Innovations for Designing
0 Me gusta
Mensaje 12 de 13

_gile
Consultant
Consultant

@nomanmurtaza95  a écrit :
Make sure it's set to the correct version compatible with AutoCAD 2025 (typically .NET Framework 4.7 or above).

From the developer's documentation:

If you are targeting AutoCAD 2025 or AutoCAD 2025-based programs, you should use:

  • Microsoft Visual Studio 2022 version 17.8.0
  • Microsoft .NET 8.0


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Me gusta
Mensaje 13 de 13

awhite40surveyor
Explorer
Explorer

SINCE 1989, it has always been a pain to deal with autocad libraries. i stick to writing my own libraries be for using from dist, angel, and function that i can do. at least i know it works. i only use them as a Gui interface for programing, anything else is a waste if my time. EVEN NOW THEY DO NOW GIVE YOU ALL THE LIBRARIES. AND I PAID GOOD MONEY ...LOL I HAVE ZERO FAITH IN THEM TO COME THUR. i used to do all my programming and just dxf it in.  if i have a 486 computer with window and autocad and this program i am writing would have already been written.  they make everything so complicated. civil 3d is even more complicated for simple task. why   a line, distance, azmith, table, text.    use their library and you are SCREWED. STRICT THE LEAST AMOUNT OF LIBRARIES AND WORK EVERTHING OUTSIDE AND USE THEM FOR A DUMB DISPLAY AND PRINT.   WHEN IT COMES TO PROGRAMING THE REST ID FINE....

if a company come up with a Gui only and saves as a dwg autodesk will be in trouble. what do i know 65 years old.

0 Me gusta