Lui 发表于 2022-7-5 18:45:15

如何使用Att插入块

大家好,
 
最近我开始用auto lisp编写代码,但由于我的知识还处于非常基础的水平,我一直无法完成我认为很容易的任务。
 
我一直在尝试做的是一个自动lisp,插入一个包含两个链接到多段线的属性的块,这些属性将显示链接到的对象的面积和层名称;因此,如果将此对象更改为其他图层或用户修改多段线,它将自动更新。
 
目前,我已经能够写出以下几行,但我达到了一个点,那就是我无法理解如何覆盖att参数来重新定义与之相关的ID对象名。
 
 
;DESCRIPTION
;
;This lisp places a block with ATTs of "AREA" and "LAYER NAME" that are filled automatically and are related to an specific polyline or hatch defined by the user.
;
;NOTES: The block and its ATTs must be defined in the drawing prior to execute this routine.
;

;Definition of the lisp and its local variables

(defun c:prueba ()
       
;Requests the user to select a polyline or hatch and stores the selection into a variable.
(setq OBJ_REF (car (entsel "\n Selecciona una polilinea o un hatch: ")))

;Shows in the command line the entity name of the selected object.
(print OBJ_REF)(terpri)

;Shows in the command line the layer name of the selected object.
(princ (strcat "\n El layer del objeto es: "(cdr (assoc 8 (entget OBJ_REF)))))

;Requests the user to select the point where the block will be inserted and stores it into a variable.
(setq INS_PNT (getpoint "\n Indica el punto de insercion del bloque:"))

;Inserts the block in the position previously indicated by the user.
(command "._insert" "MA_Tag_Areas" INS_PNT "" "" "")

;Selects the last entity inserted in the drawing and store its name into a variable.
(setq BLK_NAM (entlast))
(princ "\n El nombre del bloque es: ")(terpri)
(princ BLK_NAM)
(princ "\n ")

Obtains the DXF group doted pairs and stores it into a variable.
(setq BLK_LIST (entget BLK_NAM))
(princ "\n El contenido del bloque es: ")(terpri)
(princ BLK_LIST)
(princ "\n ")

;Avoids to finish the routine with the text "nill" shown at the command line
(princ)
)
 
 
任何帮助都将不胜感激。

BIGAL 发表于 2022-7-5 19:10:51

在此处搜索更多Pline区域有许多标记Pline和在Pline改变形状时自动更新区域等的示例。他们使用字段来提供信息。很确定一个真正好的是由GP_。也看看http://www.lee-mac.com

SLW210 发表于 2022-7-5 19:21:38

请阅读代码发布指南并编辑您的帖子,将代码包含在代码标签中。

Lui 发表于 2022-7-5 19:31:11

比加尔,
 
感谢您的快速回复。
 
我已经在很多网站上搜索过了,比如李·麦克、AfraLISP、CADnotes等等。问题是,即使我找到了如何编辑属性,我也找不到如何在块内编辑这些属性,以便它们与对象保持链接,或者至少不使用AutoLisp。我已经看到了一些可以使用VisualLisp的解决方案,但实际上我不知道如何在VisualLisp中编码,我不想只是复制/粘贴代码,因为我正在尝试学习,我想完全理解代码。
 
我会继续在这个论坛内搜索,正如你建议的“普林地区”。
 
当心,

BIGAL 发表于 2022-7-5 19:52:51

这是一个很好的全科医生_
 

;;   Write the area of the selected polylines in the position of   ;;
;;   maximum inscribed circle.                                     ;;
;;                                                               ;;
;;   19.01.2013 - Gian Paolo Cattaneo                              ;;

(defun c:arpoly (/ sel poly POLY_vl Dx Dy Lp List_vert_poly list_p_int P_center dist step1 step2)
   (prompt "\nSelect Polyline: ")
   (setq sel (ssget '((0 . "LWPOLYLINE"))))
   (if sel
       (progn
         (repeat (setq :n (sslength sel))
               (setq poly (ssname sel (setq :n (1- :n))))
               (setq jjj 0)
               (setq step1 30) ;--> grid_1 density
               (setq step2 10) ;--> grid_2 density
               (setq POLY_vl (vlax-ename->vla-object POLY))
               (setq list_vert_poly (LM:LWPoly->List POLY))
               (grid_1)   
               (Point_int)
               ;(grid+) ;increase accuracy
               (Point_center)
               (if (= jjj 1) (setq p_prov P_center))
            ;(repeat 2 ;increase accuracy
                   (grid_2)
                   (Point_center)
            
               (entmake
                   (list
                     (cons 0 "TEXT")
                     (cons 8 (getvar "clayer"))
                     (cons 7 (getvar "textstyle"))
                     (cons 10 P_center)
                     (cons 11 P_center)
                     (cons 40 (getvar "textsize"))
                     (cons 72 1)
                     (cons 73 2)
                     (cons 1 (rtos (vlax-curve-getArea poly) 2 2))
                   )
               )
         )
         (princ)
       )
       (alert "No selected lwpolyline")
   )
)      

;; LWPolyline to Point List-Lee Mac
;; Returns a list of points describing the supplied LWPolyline
(defun LM:LWPoly->List ( ent / der di1 di2 inc lst par rad )
   (setq par 0)
   (repeat (cdr (assoc 90 (entget ent)))
       (if (setq der (vlax-curve-getsecondderiv ent par))
         (if (equal der '(0.0 0.0 0.0) 1e-
               (setq lst (cons (vlax-curve-getpointatparam ent par) lst))
               (if
                   (setq rad (distance '(0.0 0.0) (vlax-curve-getfirstderiv ent par))
                         di1 (vlax-curve-getdistatparam ent par)
                         di2 (vlax-curve-getdistatparam ent (1+ par))
                   )
                   (progn
                     (setq inc (/ (- di2 di1) (1+ (fix (* 10 (/ (- di2 di1) rad (+ pi pi)))))))
                     (while (< di1 di2)
                           (setq lst (cons (vlax-curve-getpointatdist ent di1) lst)
                                 di1 (+ di1 inc)
                           )
                     )
                   )
               )
         )
       )
       (setq par (1+ par))
   )
   (setq x lst)
   lst
)

; Restituisce una griglia di punti all'interno del getboundingbox della poly selezionata
; Returns a grid of points within the BoundingBox of the selected poly
(defun grid_1 (/ P1_ P2_ n P> )
   (vla-getboundingbox POLY_vl 'p1 'p2)
   (setq P1_ (vlax-safearray->list p1))
   (setq P2_ (vlax-safearray->list p2))
   (setq P1_ (list (car P1_) (cadr P1_)))
   (setq P2_ (list (car P2_) (cadr P2_)))
   (setq Dx (/ (- (car P2_) (car P1_)) step1))
   (setq Dy (/ (- (cadr P2_) (cadr P1_)) step1))
   (setq n 0)
   (setq P> P1_)
   (setq Lp (list P1_))
   (repeat (* (1+ step1) step1)
       (setq P> (list (+ (car P>) Dx) (cadr P>)))
       (setq Lp (cons P> Lp))
       (setq n (1+ n))
       (if (= n step1)
         (progn
               (setq n 0)
               (setq P1_ (list (car P1_) (+ (cadr P1_) Dy)))
               (setq P> P1_)
               (setq Lp (cons P> Lp))
         )
       )
   )
   (setq Lp (cdr Lp))
   (setq a Lp)
)

; Restituisce una griglia di punti intorno al punto centrale (provvisorio)
; Returns a grid of points around the center point (provisional)
(defun grid_2 (/ P1_P> n)
   (setq list_p_int nil)
   (setq P1_ (list (- (car P_center) (* Dx 2)) (- (cadr P_center) (* Dy 2))))
   (setq Dx (/ (* 4 Dx) step2))
   (setq Dy (/ (* 4 Dy) step2))
   (setq n 0)
   (setq P> P1_)
   (setq list_p_int (list P1_))
   (repeat (* (1+ step2) step2)
       (setq P> (list (+ (car P>) Dx) (cadr P>)))
       (setq list_p_int (cons P> list_p_int))
       (setq n (1+ n))
       (if (= n step2)
         (progn
               (setq n 0)
               (setq P1_ (list (car P1_) (+ (cadr P1_) Dy)))
               (setq P> P1_)
               (setq list_p_int (cons P> list_p_int))
         )
       )
   )
   (setq dlist_p_int)
)
   
; restituisce la lista dei punti interni ad un poligono
; dati:- lista coordinate dei punti -> Lp
;      - lista coordinate vertici poligono -> list_vert_poly
; Returns the list of inside points
(defun Point_int (/ P_distant n Pr cont attr p# Pa Pa_ Pb )
   (setq P_distant (list (car (getvar "extmax")) (* 2 (cadr (getvar "extmax")))))   
   (setq list_p_int nil)
   (foreach Pr Lp
       (setq cont -1)
       (setq attr 0)
       (setq p# nil)
       (setq Pa (nth (setq cont (1+ cont)) list_vert_poly))
       (setq Pa_ Pa)
       (repeat (length list_vert_poly)
         (setq Pb (nth (setq cont (1+ cont)) list_vert_poly))
         (if (= cont (length list_vert_poly)) (setq Pb Pa_))
         (setq P# (inters Pa Pb Pr P_distant))
         (if (/= P# nil) (setq attr (1+ attr)))
         (setq Pa Pb)
       )
       (if (> (rem attr 2) 0) (setq list_p_int (cons Pr list_p_int)))      
   )
   (setq b list_p_int)
)

; Infittisce la griglia inserendo altri punti
; nel centro delle diagonali tra i punti interni
; Increases the grid density
(defun grid+ (/ G+)
   (setq G+
       (mapcar '(lambda ( x ) (list (+ (car x) (/ Dx 2)) (+ (cadr x) (/ Dy 2)))) list_p_int)
   )
   (setq list_p_int (append G+ list_p_int))
   (setq c list_p_int)
)

; Da una lista di punti restituisce quello più lontano da un oggetto
; dati:- lista dei punti -> list_p_int
;      - oggetto -> POLY_vl
; Returns the farthest point from the polyline
(defun Point_center (/ Pa n Pvic)
   (setq Dist 0.0000001)
   (setq P_center nil)
   (foreach Pa list_p_int
(setq Pvic (vlax-curve-getClosestPointTo POLY_vl Pa))
       (if (> (distance Pa Pvic) Dist)
         (progn
               (setq P_center Pa)
               (setq Dist (distance Pa Pvic))
         )
       )
   )
   (setq jjj (1+ jjj))
   (setq pc P_center)
)

(vl-load-com)
(prompt "\n ") (prompt "\n ") (prompt "\n ")
(princ "\nType \"ARPOLY\" to invoke")
(princ)
页: [1]
查看完整版本: 如何使用Att插入块