马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
本帖最后由 marting 于 2017-2-10 11:40 编辑
For many, AutoCAD is the tool that they use to create their designs and construction plans; whether you are doing 2D drafting or 3D modeling. However, of the biggest benefits of AutoCAD is that it is a platform that can be extended through a variety of APIs and customization techniques. If AutoCAD does not perform a task that you want it to, you can simply write the functionality yourself or find someone that can. The way AutoCAD is designed; really it is limited only by your imagination as to the problems that it can solve and the types of industries it can be used in.
As I finalize my presentations for AU2013, I decided to write some sample code that demonstrates the same concept but with each of the five different APIs that AutoCAD supports. The sample code shows how to draw a line in model space, of course there are always twists and differences to the way each programmer approaches a given problem.
AutoLISP - Command Function
; Draw a line from 5,0 to 10,5
(defun c:DL_AutoLISP ( / )
(command "._LINE" "5,0,0" "10,5,0" "")
(princ)
)
AutoLISP - COM API
; Draw line from 10,0 to 15,5
(defun c:DL_AutoLISPCOM ( / acDoc acModelSpace pt1 pt2)
; Load the AutoCAD COM Library
(vl-load-com)
; Get the current drawing
(setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
; Get model space for the current drawing
(setq acModelSpace (vla-get-modelspace acDoc))
; define a start and end point for the line
(setq pt1 (vlax-3D-point 10 0 0)
pt2 (vlax-3D-point 15 5 0))
; Add the new line to model space
(vla-addline acModelSpace pt1 pt2)
(princ)
)
AutoLISP - Entmake Function
; Draw line from 15,0 to 20,5
(defun c:DL_AutoLISPENTMAKE ( / )
(entmake '((0 . "LINE") ; Define the type of object to create, a line
(10 15.0 0.0 0.0) ; Define start point 15,0,0
(11 20.0 5.0 0.0) ; Define end point 20,5,0
)
)
(princ)
)
Visual Basic for Applications (VBA)
' Draw line from 20,0 to 25,5
Public Sub DrawLine()
Dim pt1(0 To 2) As Double, pt2(0 To 2) As Double
pt1(0) = 20: pt1(1) = 0: pt1(2) = 0
pt2(0) = 25: pt2(1) = 5: pt2(2) = 0
' Add the line to model space
ThisDrawing.ModelSpace.AddLine pt1, pt2
End Sub
JavaScript
// Function that executes the LINE command
function drawLine()
{
try {
Acad.Editor.executeCommandAsync(
'_.Line ' +
'0,0,0 ' +
'5,5,0 '
);
}
catch(e) {
write(e.message);
}
}
// Add a command to access the drawLine function
Acad.Editor.addCommand(
"DrawLine_JS",
"DL_JavaScript",
"DL_JavaScript",
Acad.CommandFlag.MODAL,
drawLine );
Managed .NET - VB.NET
<CommandMethod("DL_VBNET")> _
Public Sub DrawLine()
'' Get the current document and database
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Block table for read
Dim acBlkTbl As BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
'' Open the Block table record Model space for write
Dim acBlkTblRec As BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _ OpenMode.ForWrite)
'' Create a line that starts at 25,0 and ends at 30,5
Dim acLine As Line = New Line(New Point3d(25, 0, 0), _
New Point3d(30, 5, 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()
End Using
End Sub
ObjectARX/C++
static void Drawline();
extern "C" AcRx::AppRetCode acrxEntryPoint(AcRx::AppMsgCode msg, void* pkt)
{
switch(msg)
{
case AcRx::kInitAppMsg:
acrxDynamicLinker->unlockApplication(pkt);
acrxDynamicLinker->registerAppMDIAware(pkt);
// Commands to add
acedRegCmds->addCommand(_T("DrawLine_ARX"), _T("DL_ObjectARX"), _T("DL_ObjectARX"), ACRX_CMD_MODAL, Drawline);
break;
case AcRx::kUnloadAppMsg:
// Command Groups to remove
acedRegCmds->removeGroup(_T("DrawLine_ARX"));
break;
default:
break;
}
return AcRx::kRetOK;
}
static void Drawline()
{
// Get the current database
AcDbDatabase *pDb = acdbHostApplicationServices()->workingDatabase();
// Open the Block Table for read-only
AcDbBlockTable *pBlockTable;
pDb->getSymbolTable(pBlockTable, AcDb::kForRead);
// Get the Model Space block
AcDbBlockTableRecord *pBlockTableRecord;
pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
pBlockTable->close();
// Define the points that will be used to define the Line object
AcGePoint3d startPt(30.0, 0.0, 0.0);
AcGePoint3d endPt(35.0, 5.0, 0.0);
// Create the new Line object in memory
AcDbLine *pLine = new AcDbLine(startPt, endPt);
// Add the new Line object to Model space
pBlockTableRecord->appendAcDbEntity(pLine);
// Close the Model space block
pBlockTableRecord->close();
// Close the new line object
pLine->close();
}
|