I am looking for a code.
Without loading and unloading,without start external program (debug)
C# code searches active file ,for example c:\home.dwg.
I wrote in this: netload and so on.
It saves many hours.
I read articles about it but I couldn't find a solution.
I read all help about programming in c# for vs.
Time which I have to spend waiting for starting cad every time
takes many minutes. I wrote in vba for autocad, there is a big difference
in waiting for a result.
Thank You in advance
Solved! Go to Solution.
can you ask again with a little bit more clearness?
I can't interpret your question to something that I can understand, I don't see what you are searching for (as you found something that saves you hours)?
If (just as one guess) you just want to have your dotNET-app automatically loaded, so you don't want to load it by hand) you might have a look to >>>this devcast<<<.
- alfred -
I will try to explain.
I create a new code (windows form or class) in visual studio c#.
Solution Explorer/ right mouse on namespace / properties
Debug / start external program / here we choose file to autocad.
So when I press F5 new autocad file opens, it takes about 30seconds.
I want to write a program withou setting external program in a debug – is it possible.
Instead of starting external program I would like myself open
autocad file e:\strop.dwg
When I press F5 C# code catches this active cad file (strop.dwg) ,
I wrote myself netload or put command in c#
I choose my dll file, I write name of command method.
When the c# finishes, I Stop debugging and the house.dwg is still
I read about it on this forum , I wrote this program, please correct this
I use visual studio 2010, autocad 2009.
I read this article, it doesn't work. I missed something
This is my code visual 2010 c#
Please correct and enclose visual studio files as a solution.
using Aplikacja = Autodesk.AutoCAD.ApplicationServices.Application;
public partial class HitAcad : Form
public AcadApplication cadApp;
public AcadDocument cadDoc;
private void Form1_Load(object sender, EventArgs e)
public bool Connect2Acad()
// Autodesk.AutoCAD.Interop.AcadApplication _AcadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServi
cadApp =new Autodesk.AutoCAD.Interop.AcadApplication();
cadApp.Visible = true;
catch (System.Exception AcadEx)
string sMsg = AcadEx.Message + " from: " + AcadEx.Source;
In help from autocad I found this but I can’t run it I suppose it is windows form.
Please enclose a vs studio file as a solution.
public static void ConnectToAcad()
AcadApplication acAppComObj = null;
// "AutoCAD.Application.17" uses 2007 or 2008,
const string strProgId = "AutoCAD.Application.17";
// Get a running instance of AutoCAD
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId
catch // An error occurs if no instance is running
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.Get
// If an instance of AutoCAD is not created then message and exit
// System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
// Display the application and return the name and version
acAppComObj.Visible = true;
// System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name + " version " + acAppComObj.Version);
// Get the active document
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " + (char)34 + "g:/nauka c# cad/CsMgd1/bin/debug/CsMgd1.dll" + (char)34 + ") ");
ok, three options:
A) as long as you want to define own application-commands (as you use it in CommandMethod(....) ) you have to use the project-type = DLL and you have to use _NETLOAD (or the plugin-methods starting with AutoCAD 2012 or registry-settings to get the DLL automatically loaded without using command _NETLOAD shown >>>here<<<). To start debugging such a project you have two options:
A1) as you mentioned it ... define an external application = acad.exe to be started
A2) attach your project (to be found under menu "debug" of Visual Studio) to a running instance of acad.exe ==> that sounds close to what you are looking for. BUT: as AutoCAD can't unload managed code you have the problem that when you first start this attach to process and NETLOAD your DLL you don't get the code unloaded. So stopping debugging, rewrite the code and again attaching to acad.exe will not load the new modificated version of your DLL. So also with this method you have to restart the acad.exe
B) your code works based on COM and works as ActiveX-Server ==> then you need again at least a starter-code that get's the connection to your external ActiveX-server. In this case I'm not sure, but I think that you are not allowed to rebuild your project as long as the DLL is currently connected via ActiveX to the acad.exe process.
C) your code is an external EXE the connects to AutoCAD and AutoCAD plays the ActiveX-server. Exept of that you cannot define then commands as under A) you will have the option to start and stop your EXE to connect to AutoCAD and to disconnect from AutoCAD without stopping the AutoCAD-process itself. But as the EXE runs outside of the process-memory of AutoCAD that's the worst performance way to go.
Hope that answers your questions, - alfred -
Thank You for answer
I read almost all help/ tutorial c# for autocad.
I am beginner with visual studio so I don't know
is it posibble to do this.
I will write sample code for point "A" after friday.
I will write again my aim maybe, I will try more clearly
step by step.
I wrote a program in c# visual studio for autocad.
I added reference to AcDbMgd.dll AcMgd.dll acax17enu.tlb.
Debug / start external program / here we choose file to autocad.
If it is necessery I add external program "cad.exe"
In visual studio c# I press F5 code
1) catches active autocad file if it is possible
( I wrote on hdd a file, d:\strop.dwg, I opened it before
pressing F5 )
2) opens new autocad file
In command line I write netload, choose dll file, write name of command
method, it opens the file d:\strop.dwg,
On the model space creats some drawing.
I stopping debugging
3) is it possible, cad file strop.dwg will be still open
after finishing of debugging?
Afterthat I add some lines in my c# code,I press F5
(now I would like to save time on opening second time new autocad file
is it possible ? or new autocad file has to be open)
new dll will rewrite old one. In command line of autocad I write
netload,I load dll, write command method.
Nowadays every time when I want to check my code new autocad file
is opening, I stop debugging autocad is closing.
If I want debugging often, opening and closing autocad takes about 70 seconds
for every time.
I would like to omit this opening and closing.
I wonder is it possible, to save time.
all answered with a short statement: use an 32bit os and so also a 32bit AutoCAD and you have the option "write while debugging" available in Visual Studio. As long as you work on 64bit you can just debug but not modify the code while debugging. Thats a limitation of Visual Studio.
If you have already setup your workstation then you may use something like VmWare or any other virtual-pc alternativ so you can run a 32bit environment.
All the other options will have to close and restart AutoCAD to get a modified DLL (as long as it has the same name) reloaded.
- alfred -
A Please explain me how this works "write while debugging". Maybe send an example. I searched on internet and I couldn’t find this therm.
B So in visual studio I create code, I compile it do dll, this dll I open in autocad.
During writing code in c# visual studio for autocad , doesn’t have connection between them. How to debug code ? I wrote an example.
How people cope with checking a code( debugging) when they write huge codes in c# / vb in visual studio.
I enclose vs files.
Thank You in Advance
public class Class1
public static void CreateMText()
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
// Open the Block table for read
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelS
OpenMode.ForWrite) as BlockTableRecord;
// Create a multiline text object
MText acMText = new MText();
acMText.Location = new Point3d(2, 2, 0);
acMText.Width = 4;
acMText.Contents = "This is a text string for the MText object.";
// Save the changes and dispose of the transaction
acDoc.SendStringToExecute("._zoom _all ", true, false, false);
Not being able to edit code on the fly while AutoCAD is open is mildly annoying, but what is, is. I take a logical, procedural approach to development so its not really a problem. Flailing away at code as its running instead of test/diagnose/implement/test isn't a very rigourous development environment, making it difficult to produce well organized code (at least, in my experience - *extremely* experienced coders can get away with this).
>> Not being able to edit code on the fly while AutoCAD is open is mildly annoying, but what is, is
As mentioned, that is only valid for 64bit. If you run a 32bit os you can edit-while-debug - and of course - it's a big advantage and makes development much faster.
And if you don't have a 32bit os then install a virtual-pc environment on your system that runs a 32bit os and AutoCAD.
>> Please explain me how this works "write while debugging". Maybe send an example
I can't send a sample for that because you can do that with every code as long as the debugged session runs in 32bit mode.
If the problem is that you can't set the acad.exe to be started for debugging then you use the Express-release of Visual Studio. Therefor you'll find two options to get that running:
a) check the Wizards within that ObjectARX-kit. I don't use them but I'm quite sure that using them you will have the correct settings within your project-user-file. Great source for that and around AutoCAD and dotNET is >>>here<<<.
>> So in visual studio I create code, I compile it do dll, this dll I open in autocad.
You set the options within your visual-studio project to start the acad.exe when you press "play" for start debugging your project. So Visual Studio starts AutoCAD for you (with some hooks) and when you NETLOAD your DLL every breakpoint or every exception (when enabled to stop on exception in your project) will stop at the code in the Visual Studio environment.
For 32bit session you can now modify the code, for a 64bit session a breakpoint or exception also stops the code (before you can continue), so you have the chance for getting information about variables, stack, .... but you can't edit.
HTH, - alfred -