I have posted a few previous C#.Net posts: getting started, and a layer application. However, if I am going to provide more code samples I decided I need a better method for getting my source code from Visual Studio into a blog post other than manually modifying the HTML. I installed CopySourceAsHtml for this exact purpose and it works great! (very quick to setup). A couple of things to note though:
- I do not believe this add-in works with Visual Studio Express editions.
- The current version of CopySourceAsHtml does not appear to be available for VS2010 but it was very simple to modify the add-in file after installing.
- To use the add-in just select the source code then select Copy As HTML... from the Edit pulldown.
- I pasted my code into Word then just did another select all and pasted that into the blogger editor (for some reason going directly from VS to Blogger I lost the coloring).
So here is my first result of using this add-in, a simple program to add some geometry into AutoCAD (line).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace SLD_Demo
{
public class SLD_DrawTools
{
[CommandMethod("SLDdrawLine")]
public void SLDdrawLine()
{
//initiate drawing objects
/*In order to work with AutoCAD from .Net we do that through
the object hierarchy that AutoCAD is built from. The
Application is the root object. We then dig into that to get
to the current document. We will create a new Document object
called acDoc. From there we go one level further to access
the AutoCAD database for the current document.
*/
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//start db transaction
/*reading and creating entities within AutoCAD requires a
transaction with the AutoCAD document database (objects
defined above)*/
using (Transaction
acTrans = acCurDb.TransactionManager.StartTransaction())
{
//define line
/*when a new line object is created there are a few
constructor methods that can be used. In this example we
will define 2 new 3D point objects and then pass those
to the new line object as paramaters*/
Point3d startPoint = new Point3d(0,0,0);
Point3d endPoint = new Point3d(0,5,0);
Line sldLine = new Line(startPoint, endPoint);
//open block table and create new record
/*Note, at this point our new line is only defined within
our program, it is not in the AutoCAD drawing yet. In order
to add the defined line to the drawing we need to create
a new record in the AutoCAD document database.*/
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(
acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(
acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite)
as BlockTableRecord;
//add line to drawing
/*now that we have created a new block table record we
can add our new line to it.*/
acBlkTblRec.AppendEntity(sldLine);
acTrans.AddNewlyCreatedDBObject(sldLine, true);
//end the transaction with the AutoCAD database
/*This is a very important step. When we begin a
transaction with the AutoCAD database it has to be
terminated (commited). Otherwise future AutoCAD
commands will not function and the drawing will likely
crash*/
acTrans.Commit();
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
namespace SLD_Demo
{
public class SLD_DrawTools
{
[CommandMethod("SLDdrawLine")]
public void SLDdrawLine()
{
//initiate drawing objects
/*In order to work with AutoCAD from .Net we do that through
the object hierarchy that AutoCAD is built from. The
Application is the root object. We then dig into that to get
to the current document. We will create a new Document object
called acDoc. From there we go one level further to access
the AutoCAD database for the current document.
*/
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
//start db transaction
/*reading and creating entities within AutoCAD requires a
transaction with the AutoCAD document database (objects
defined above)*/
using (Transaction
acTrans = acCurDb.TransactionManager.StartTransaction())
{
//define line
/*when a new line object is created there are a few
constructor methods that can be used. In this example we
will define 2 new 3D point objects and then pass those
to the new line object as paramaters*/
Point3d startPoint = new Point3d(0,0,0);
Point3d endPoint = new Point3d(0,5,0);
Line sldLine = new Line(startPoint, endPoint);
//open block table and create new record
/*Note, at this point our new line is only defined within
our program, it is not in the AutoCAD drawing yet. In order
to add the defined line to the drawing we need to create
a new record in the AutoCAD document database.*/
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(
acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(
acBlkTbl[BlockTableRecord.ModelSpace],
OpenMode.ForWrite)
as BlockTableRecord;
//add line to drawing
/*now that we have created a new block table record we
can add our new line to it.*/
acBlkTblRec.AppendEntity(sldLine);
acTrans.AddNewlyCreatedDBObject(sldLine, true);
//end the transaction with the AutoCAD database
/*This is a very important step. When we begin a
transaction with the AutoCAD database it has to be
terminated (commited). Otherwise future AutoCAD
commands will not function and the drawing will likely
crash*/
acTrans.Commit();
}
}
}
}
UETech Engineering provides cheap civil CAD drafting and design works for our clients that range from civil, structural, electrical, mechanical, and utility sectors. Our professional team has 8 years of experience and in-depth knowledge of drafting services and is adept at delivering high quality solutions to the clients in Melbourne, Australia.
ReplyDelete