.NET

Reply
Member
emre.ozturk
Posts: 4
Registered: ‎07-10-2013
Message 1 of 10 (3,887 Views)

C# and autocad

3887 Views, 9 Replies
07-10-2013 11:25 PM

Hello,

 

I want to ask a questions about csharp and autocad interaction. Could I manage the autocad using by csharp codes, I mean, I should create a program with C#, and when I press a button, I should draw a line in autocad. is it possible?

 

I found a page about this subject 

http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627

 

at the above webpage, is proper documentation?

Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 2 of 10 (3,859 Views)

Re: C# and autocad

07-11-2013 07:23 AM in reply to: emre.ozturk
you can go through the AutoCAD .net developers guide where you will get details as well as example with code. Below is the link:

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS73099cc142f48755a52158612bd434e551-7fd...

Regards
Zakir
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 3 of 10 (3,857 Views)

Re: C# and autocad

07-11-2013 07:29 AM in reply to: emre.ozturk

Below is the code to create a line. Here you will get line with this command. If you want to draw the line from a button , just call that sub from button event.

 

Capture.JPG

Member
emre.ozturk
Posts: 4
Registered: ‎07-10-2013
Message 4 of 10 (3,828 Views)

Re: C# and autocad

07-12-2013 12:33 AM in reply to: emre.ozturk

 mzakiralam,

 

Thank you for your response, 

But when I try code that you advice, It didnt run, it gave a failure as below

 

Could not load file or assembly 'accoremgd, 

 

 

Could you send a simple and basic project about this subject,

 

Full test code 

 

Spoiler

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.EditorInput;

namespace WindowsFormsApplication1
{


public partial class Form1 : Form
{

 


public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

 

[CommandMethod("AddLine")]
public static void AddLine()
{

Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
//open block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

// opren the block table record model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;



// create line that starts at 5,5 ends at 12,3
Line acLine = new Line(new Point3d(5, 5, 0), new Point3d(12, 3, 0));

// add the new object to the block table record and the transaction

acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);

//save the new object to the database
acTrans.Commit();
}


}

private void button1_Click(object sender, EventArgs e)
{
AddLine();
}


}
}

 

*Expert Elite*
_gile
Posts: 2,103
Registered: ‎04-29-2006
Message 5 of 10 (3,824 Views)

Re: C# and autocad

07-12-2013 12:53 AM in reply to: emre.ozturk

Hi,

 

You have to add the references to the AutoCAD .NET API libraries to your project (see here).

These DLLs are mosty: AcDbMgd.dll and AcMgd.dll plus, if you're targeting A2013 or 2014, AcCoreMgd.dll.

Gilles Chanteau
Member
emre.ozturk
Posts: 4
Registered: ‎07-10-2013
Message 6 of 10 (3,821 Views)

Re: C# and autocad

07-12-2013 02:08 AM in reply to: _gile

Hi,

 

Yes I added all required .DLL 

*Expert Elite*
_gile
Posts: 2,103
Registered: ‎04-29-2006
Message 7 of 10 (3,818 Views)

Re: C# and autocad

07-12-2013 03:01 AM in reply to: emre.ozturk

The code posted by mzakiralam defines an AutoCAD command. It have to be compiled as class library (dll) and loaded into AutoCAD with the NETLOAD command.

 

The AutoCAD .NET API is designed to run in-process only, if you want to make an out-of-process stand-alone application (exe), you'll have to get or create an instance of AutoCAD and use the AutoCAD ActiveX Automation (COM) libraries.

 

You should have a look here.

Gilles Chanteau
Mentor
mzakiralam
Posts: 231
Registered: ‎11-09-2012
Message 8 of 10 (3,810 Views)

Re: C# and autocad

07-12-2013 04:05 AM in reply to: emre.ozturk

I canl send you complete example project in VB if you still required. In the mean time you should have look on basics of AutoCAD.NET API (use below link please for that). How to create a project and how to load that assembly into AutoCAD. Please remember that as _gile mentioned in his previous message of this thread that you have to add proper reference to build DLL.

 

http://exchange.autodesk.com/autocad/enu/online-help/browse#WS73099cc142f48755f2fc9df120970276f7-535...

*Expert Elite*
_gile
Posts: 2,103
Registered: ‎04-29-2006
Message 9 of 10 (3,802 Views)

Re: C# and autocad

07-12-2013 04:53 AM in reply to: emre.ozturk

I attached a little VS2010 solution which contains 3 projects to show you different ways to make a stand-alone application to interact with AutoCAD. All uses the AutoCAD COM API.

 

- InteropSample uses references to Autodesk.AutoCAD.Interop and Autodesk.AutoCAD.Interop.Common which are version and plateform dependant, so you may have to change them.


- DynamicSample uses the dynamic type to avoid having AutoCAD.Interop references. It requires the .NET Framework 4.


- LateBindingSample uses late binding and reflectionto avoid having AutoCAD.Interop references. It contains some extension methods to make the code more readable.

Gilles Chanteau
Member
emre.ozturk
Posts: 4
Registered: ‎07-10-2013
Message 10 of 10 (3,796 Views)

Re: C# and autocad

07-12-2013 05:14 AM in reply to: _gile

Thanks to everbody to your help, 

I applied wrong method to create line via exe file, 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.