明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1351|回复: 2

[源码] Copy a leader using Visual LISP

[复制链接]
发表于 2014-4-4 21:19:20 | 显示全部楼层 |阅读模式
本帖最后由 Lisper 于 2015-7-30 09:17 编辑

Copy a leader using Visual LISP
By Wayne Brill

Recently I had a case where the question was how to create a new leader based on an existing leader using AutoLISP. Here is an example that I came up with. When you run this example you select a leader and the MText annotation for the leader. After you run the function you will need to move the Leader manually to see that there are are two leaders at the same location.

Another way to do this would be to use the Copy method and set the annotation property. This example is just getting the points from the existing leader, the MText, and then creating a new Leader at the same location.   


(defun C:copyLeaderTest (/ vla_ldr vla_mtx coords numberOfPoints
           arrayForPoints points attachPointFromExistingMText
           textForMText widthForMText insertioinPointForMText
           acadObj doc modelSpace newMText
               drawingDirectionForMText newLeader verticalTextPosition)

  (vl-load-com)
  (setvar "osmode" 0)
  (setvar "orthomode" 0)

  ; Get Leader and MText
  (while (setq ss1 (ssget (list (cons -4 "<OR")
    (cons 0 "LEADER") (cons 0 "MTEXT")(cons -4 "OR>"))))

   ; Ensure selection set has two entities
  (if (/= (sslength ss1) 2)
    (alert "Select 1 MText and 1 leader.")
    ; Have a Leader and MText get the ActiveX Object
    (progn
      (if (= (cdr (assoc 0 (entget (ssname ss1 0)))) "LEADER")
    (setq vla_ldr (vlax-ename->vla-object (ssname ss1 0))
          vla_mtx (vlax-ename->vla-object (ssname ss1 1))
    )
    (setq vla_ldr (vlax-ename->vla-object (ssname ss1 1))
          vla_mtx (vlax-ename->vla-object (ssname ss1 0))
    )
      )

         
      ; Get the coordinates from the existing leader
      ; and make an array of doubles
      (setq coords (vlax-get vla_ldr 'Coordinates))
      (setq numberOfPoints (1- (length coords)))
      (setq arrayForPoints (cons 0 numberOfPoints))
      (setq points (vlax-make-safearray vlax-vbDouble
                             arrayForPoints))
      (vlax-safearray-fill points coords)
      
    ; Store the current attachmentpoint of the existing MText  
    (setq attachPointFromExistingMText (vla-get-attachmentpoint vla_mtx))

    ; change the attachmentPoint for existing leader to Top Left
    ; This is will make the insertion point correct for the new MText
    (vla-put-AttachmentPoint vla_mtx acAttachmentPointTopLeft)
   
    (setq textForMText (vla-get-TextString vla_mtx))
    (setq widthForMText (vla-get-width vla_mtx))
    (setq insertioinPointForMText (vla-get-insertionpoint vla_mtx))

    ; Get modelspace and add the new MText
    (setq acadObj (vlax-get-acad-object))
    (setq doc (vla-get-ActiveDocument acadObj))
    (setq modelSpace (vla-get-ModelSpace doc))  
    (setq newMText (vla-AddMText modelSpace
          insertioinPointForMText widthForMText textForMText))

    (vla-put-AttachmentPoint newMText attachPointFromExistingMText)

    ; make the attachmentPoint what it was
    (vla-put-AttachmentPoint vla_mtx attachPointFromExistingMText)

    (setq drawingDirectionForMText (vla-get-drawingdirection vla_mtx))
    (vla-put-drawingdirection newMText drawingDirectionForMText)
      
    ;; Create the leader object in model space
    (setq newLeader (vla-AddLeader modelSpace points newMText acLineWithArrow))

    (setq verticalTextPosition(vla-get-VerticalTextPosition vla_ldr))
    (vla-put-VerticalTextPosition newLeader verticalTextPosition)
      
    (vla-put-ArrowheadBlock
        newLeader
        (vla-get-ArrowheadBlock vla_ldr)
    )

    (vla-put-layer newLeader (vla-get-layer vla_ldr))
    (vla-put-layer newMText (vla-get-layer vla_mtx))

   ; (vla-Erase vla_ldr)
   ; (vla-Erase vla_mtx)

     )
  )
)

  (princ)
)


评分

参与人数 1明经币 +1 收起 理由
自贡黄明儒 + 1 这个不错

查看全部评分

发表于 2014-4-4 21:40:34 | 显示全部楼层
参考用,很好的资料
发表于 2014-4-4 22:51:48 | 显示全部楼层
哇~~~~~~~~真心看不懂啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 06:56 , Processed in 0.163452 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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