See the top rated post in this thread. Click here

Results 1 to 7 of 7

Thread: c# DLL to be referenced in Excel doesn't work

  1. #1
    Member
    Join Date
    2019-08
    Posts
    4
    Login to Give a bone
    0

    Default c# DLL to be referenced in Excel doesn't work

    Hello,

    I'm trying to get the Excel communicate to Autocad using a DLL but it won't work.
    The DLL references fine in Excel, all the COM bits are ticked, the C# project has the references to the correct Autocad libraries (it compiles fine), I can call a simple test function in it but it fails when the Autocad object is called with the error :

    Run-time error '-2146233036 (80131534)': Automation error

    Help please !


    Code:
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using System.Linq;
    using Autodesk.AutoCAD.DatabaseServices;
    using Autodesk.AutoCAD.Runtime;
    using CadApp = Autodesk.AutoCAD.ApplicationServices.Application;
    using Autodesk.AutoCAD.ApplicationServices;
    
    [assembly: CommandClass(typeof(AutocadHandler.MyCommands))]
    
    namespace AutocadHandler
    {
        [ClassInterface(ClassInterfaceType.AutoDual)]
    
    
        public class MyCommands
        {
            public static void TestFunction()
            {                       
                string strFileName = "C:\\Users\\CORE I7\\Documents\\Drawing2XLS.dwg";
                var acDocMgr = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager;
                acDocMgr.Open(strFileName, false);
                acDocMgr.MdiActiveDocument.Editor.WriteMessage("Hello Excel");
            }
        }
    }
    Last edited by BlackBox; 2019-08-07 at 01:16 PM. Reason: Please use [CODE] Tags

  2. #2
    Member
    Join Date
    2009-04
    Posts
    8
    Login to Give a bone
    2

    Default Re: c# DLL to be referenced in Excel doesn't work

    Are you saying that you created the C# DLL project that uses AutoCAD .NET API assemblies; and you expose this DLL as COM; then in your Excel VBA project, you set reference to this C# DLL (as it exposed as COM component); the you run the Excel VBA code to automate AutoCAD and call functions/methods in the C# DLL. Is that right?

    If that is the case, you CANNOT use AutoCAD .NET API assemblies. These assemblies (accoremgd/acdbmgd/acmgd.dll) CAN ONLY BE USED inside AutoCAD process, while your app is automate AutoCAD from external app (Excel), an out-process. If you have to do AutoCAD automation from outside AutoCAD (i.e. from Excel, or whatever app...), you can only use AutoCAD COM APIs.

    Also, in most cases, automating AutoCAD from external app would hardly be good business solution: why your user need to run 2 apps (Excel and AutoCAD) instead of 1 (AutoCAD only) if AutoCAD is the app that must run to get work done?

  3. #3
    Programming Moderator BlackBox's Avatar
    Join Date
    2009-11
    Posts
    5,501
    Login to Give a bone
    0

    Default Re: c# DLL to be referenced in Excel doesn't work

    Sounds like you're using Excel to run AutoCAD externally (COM), but you're coding a plug-in that must be loaded into an instance of the AutoCAD application (then executed internally, once Initialize()-ed), no?


    [Edit] - Norman beat me; he's one of my heroes, so it's alright. Haha
    "How we think determines what we do, and what we do determines what we get."

    Sincpac C3D ~ Autodesk Exchange Apps

    Computer Specs:
    Dell Precision 3620, Core i7-7700K 4.2GHz, 64GB RAM, Samsung 970 Pro M.2, 8GB NVIDIA Quadro P4000

  4. #4
    Member
    Join Date
    2019-08
    Posts
    4
    Login to Give a bone
    0

    Default Re: c# DLL to be referenced in Excel doesn't work

    Norman, yes, you are right: the project needs to alter autocad projects from excel.

    Why from excel? Because the client uses excel files as data source in his Autocad projects.
    He wants to be able to switch the links to the excel file within a datalink from Excel (not necessarily the source file) as doing it manually would be a very slow and tedious process. In other words a given datalink that displays table(s) in the project needs to have it's file changed from scripts.

    I had tried so far:

    1. Excel VBA, using Autocad objects. There is no support for datalinks. BUT: I can retrieve the datalink dictionary object and alter it using LISP scripts. But there's a bug and the data from a certain note would append instead of change which corrupts the datalink.

    2. Instead of altering the datalink I tried to replace it with LISP (delete old/create new). But this is not possible unless the table from the autocad project has it's link detached. On reattaching the link the table which is now static also displays the old data + other issues. Not reliable.

    So all in all I want to change the Excel file path within an already created datalink.
    Is this possible at all?

    thank you

  5. #5
    Member
    Join Date
    2009-04
    Posts
    8
    Login to Give a bone
    0

    Default Re: c# DLL to be referenced in Excel doesn't work

    Quote Originally Posted by cyware784241 View Post
    Norman, yes, you are right: the project needs to alter autocad projects from excel.

    Why from excel? Because the client uses excel files as data source in his Autocad projects.
    He wants to be able to switch the links to the excel file within a datalink from Excel (not necessarily the source file) as doing it manually would be a very slow and tedious process. In other words a given datalink that displays table(s) in the project needs to have it's file changed from scripts.

    I had tried so far:

    1. Excel VBA, using Autocad objects. There is no support for datalinks. BUT: I can retrieve the datalink dictionary object and alter it using LISP scripts. But there's a bug and the data from a certain note would append instead of change which corrupts the datalink.

    2. Instead of altering the datalink I tried to replace it with LISP (delete old/create new). But this is not possible unless the table from the autocad project has it's link detached. On reattaching the link the table which is now static also displays the old data + other issues. Not reliable.

    So all in all I want to change the Excel file path within an already created datalink.
    Is this possible at all?

    thank you
    If you have already done some development with AutoCAD .NET API, then you might have already known that AutoCAD .NET API does provide support to deal with DataLink via Autodesk.AutoCAD.DatabaseServices.DataLink class (while AutoCAD COM API does not support handling DataLink, even you could hack it through NamedDictionary at your own risk of messing it up).

    From your description of the issue at hand, it seems the task is really simple: for some or all DataLinks ini a drawing, you want to verify/confirm/change the DataLink's source path. This could be done easily with .NET API, and there is no need to involve a running Excel app, if you only care about the file path. Even you also need to verify/change the data range/sheet in Excel file, it would also be quite easy with .NET technology to avoid running Excel by access content inside *.xlsx via, for example OpenXml.

    So, I see this is a perfect, easy and simple "get-started" AutoCAD .NET API development project, if you are interested in moving from VBA to .NET in AutoCAD programming, or if you have already started but have not gone that deeply (since you discuss this in .NET API forum).

  6. #6
    Member
    Join Date
    2019-08
    Posts
    4
    Login to Give a bone
    0

    Default Re: c# DLL to be referenced in Excel doesn't work

    OK, I will abandon this approach then, thank you for your explanations.

    Maybe it's doable from autoLisp.

  7. #7
    Administrator Ed Jobe's Avatar
    Join Date
    2000-11
    Location
    Turlock, CA
    Posts
    5,624
    Login to Give a bone
    0

    Default Re: c# DLL to be referenced in Excel doesn't work

    Autolisp can only use the COM api. So the only real solution is to use .NET from inside AutoCAD. It can get a path from xl, but you just can't automate AutoCAD from xl.
    C:> ED WORKING....

Similar Threads

  1. Autocad doesn't load DLL into memory until the execution
    By samideqlqpart350559 in forum Dot Net API
    Replies: 1
    Last Post: 2017-08-04, 01:41 PM
  2. Replies: 3
    Last Post: 2014-09-16, 11:34 AM
  3. Replies: 4
    Last Post: 2014-09-12, 05:31 PM
  4. DLL plugin worked at first but now doesn't
    By rollinkuhn in forum Dot Net API
    Replies: 1
    Last Post: 2011-01-27, 08:44 PM
  5. Problème chargement dll "myrevittools.dll"
    By kaartoum in forum Revit Architecture - API
    Replies: 1
    Last Post: 2008-10-01, 06:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •