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?
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.
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
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();
}
}
}
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.
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.
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.
Can't find what you're looking for? Ask the community or share your knowledge.