找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1012|回复: 3

[每日一码] AUTOCAD下各种API创建LINE的写法

[复制链接]

已领礼包: 20个

财富等级: 恭喜发财

发表于 2017-2-10 11:36:05 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 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();
}

评分

参与人数 1D豆 +5 收起 理由
WhoCanSay + 5 很给力!经验;技术要点;资料分享奖!

查看全部评分

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2017-2-10 13:42:54 | 显示全部楼层
是不是还有VBScript法?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 20个

财富等级: 恭喜发财

 楼主| 发表于 2017-2-10 14:36:32 | 显示全部楼层

ACAD 没有 Vbscript API, 即使用这个写,最后也是要用上面的API其中之一。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-12 10:33 , Processed in 0.181378 second(s), 41 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表