C# and autocad

C# and autocad

Anonymous
Not applicable
15,692 Views
9 Replies
Message 1 of 10

C# and autocad

Anonymous
Not applicable

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?

0 Likes
15,693 Views
9 Replies
Replies (9)
Message 2 of 10

Anonymous
Not applicable
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
Message 3 of 10

Anonymous
Not applicable

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

Message 4 of 10

Anonymous
Not applicable

 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();
}


}
}

 

0 Likes
Message 5 of 10

_gile
Consultant
Consultant

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 10

Anonymous
Not applicable

Hi,

 

Yes I added all required .DLL 

0 Likes
Message 7 of 10

_gile
Consultant
Consultant

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 8 of 10

Anonymous
Not applicable

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

0 Likes
Message 9 of 10

_gile
Consultant
Consultant

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 10 of 10

Anonymous
Not applicable

Thanks to everbody to your help, 

I applied wrong method to create line via exe file, 

0 Likes