.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issues with AutoCAD 2025 Library files

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
luckyboy82292
753 Views, 11 Replies

Issues with AutoCAD 2025 Library files

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!

11 REPLIES 11
Message 2 of 12
_gile
in reply to: luckyboy82292

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

Message 3 of 12
luckyboy82292
in reply to: _gile

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.

Message 4 of 12
_gile
in reply to: luckyboy82292

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

Message 5 of 12
luckyboy82292
in reply to: _gile

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".

Message 6 of 12
_gile
in reply to: luckyboy82292

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

Message 7 of 12
luckyboy82292
in reply to: _gile

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

Message 8 of 12
luckyboy82292
in reply to: _gile

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

Message 9 of 12
_gile
in reply to: luckyboy82292

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

Message 10 of 12
luckyboy82292
in reply to: _gile

Thank you so much sir. You saved my day.

Message 11 of 12

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
Message 12 of 12
_gile
in reply to: nomanmurtaza95


@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

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report