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");
}
}
}
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?
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. :beer: Haha
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
Re: c# DLL to be referenced in Excel doesn't work
Quote:
Originally Posted by
cyware784241
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).
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.
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.