毫无疑问,我很乐意帮助任何想更好地理解LISP的人:
- (defun c:AttLst (/ ss eLst bEnt aEnt aEntLst aVal blkLst) ; [b][color=Red]Define the function, localise the variables[/color][/b]
-
- (vl-load-com) ; [b][color=Red]Load the Visual LISP console [i](allows vl-... commands)[/i][/color][/b]
-
- (if ; [b][color=Red]If there exists a selection set such that:[/color][/b]
- (setq ss (ssget "X" ; [b][color=Red][i]"X" [/i]meaning search [i]entire database[/i] for entities with:[/color][/b]
- (list (cons 0 "INSERT") ; [b][color=Red]type: INSERT [i](Blocks, XRefs)[/i][/color][/b]
- (cons 66 1) ; [b][color=Red]Attributed[/color][/b]
- (if (getvar "CTAB") ; [b][color=Red]If there is a variable [i]"CTAB"[/i] (newer releases - determines Model Space/Paper Space[/color][/b]
- (cons 410 (getvar "CTAB")) ; [b][color=Red]Then filter by the CTAB variable[/color][/b]
- (cons 67 (- 1 (getvar "TILEMODE"))) ; [b][color=Red]Otherwise use TILEMODE variable to filter.[/color][/b]
- ) ; [b][color=Red]end if[/color][/b]
- ) ;[b][color=Red] end list [Filter List][/color][/b]
- ) ;[b][color=Red] end Selection set aquirement [ssget][/color][/b]
- ) ; [b][color=Red]end Variable Setting [selection set stored in variable "ss"][/color][/b]
- (progn ; [b][color=Red]Wrap the following code for use in the IF statement:[/color][/b]
- (setq eLst ; [b][color=Red]Store the following list of entity names to variable "eLst"[/color][/b]
- (vl-remove-if 'listp ; [b][color=Red]Remove from the list if the item is a List[/color][/b]
- (mapcar 'cadr ; [b][color=Red]Produce a list of entity names (and possible coord values) from[/color][/b]
- (ssnamex ss) ;[b][color=Red] Information provided by "ssnamex" about the Selection Set[/color][/b]
- ) ; [b][color=Red]end Mapcar[/color][/b]
- ) ;[b][color=Red] end vl-remove-if[/color][/b]
- ) ; [b][color=Red]end variable setting[/color][/b]
- (foreach e eLst ; [b][color=Red]For Each item (e) in the eLst (entity name list):[/color][/b]
- (setq bEnt (cdr (assoc 2 (entget e))) ; [b][color=Red]Retrieve the Block Name [store to "bEnt"][/color][/b]
- aEnt (entnext e) ; [b][color=Red]Retrieve the Attribute Entity Name [store to aEnt][/color][/b]
- ) ; [b][color=Red]end Variable setting[/color][/b]
- (while (= "ATTRIB" (cdr (assoc 0 (setq aEntLst (entget aEnt))))) ; [b][color=Red]While the Entity Type is "ATTRIB"[ute][/color][/b]
- (if (= (cdr (assoc 2 aEntLst)) "SIZE") ; [b][color=Red]If the ATTRIBute name is "SIZE"[/color][/b]
- (progn ; [b][color=Red]wrap the following for use with the IF[/color][/b]
- (setq aVal (cdr (assoc 1 aEntLst)) ; [b][color=Red]Store the ATTRIBute value [to aVal][/color][/b]
- blkLst (cons
- (cons bEnt aVal) ; [b][color=Red]Create an Associative list (dotted pair) of Block Name and Att. Value.[/color][/b]
- blkLst) ; [b][color=Red]Connect this to the main list[/color][/b]
- ) ; [b][color=Red]End Variable Setting[/color][/b]
- ) ; [b][color=Red]end Progn (code wrapper)[/color][/b]
- ) ;[b] [color=Red]end IF[/color][/b]
- (setq aEnt (entnext aEnt)) ; [b][color=Red]Move onto next Attribute in Block[/color][/b]
- ) ; [b][color=Red]End While[/color][/b]
- ) ; [b][color=Red]End Foreach[/color][/b]
- ) ; [color=Red][b]End Progn[/b][/color]
- (princ "\n<!> No Attributed Blocks Found <!>") ;[b][color=Red] If No Selection Set, then No Attributed Blocks Found in Drawing.[/color][/b]
- ) ; [b][color=Red]End IF[/color][/b]
- (alert (vl-princ-to-string blkLst)) ;[b][color=Red] Convert the Associative List to a String and Alert it in a Dialog Box to view result.[/color][/b]
- (princ) ; [b][color=Red]Exit Cleanly[/color][color=Red] - [suppress last function return][/color][/b]
- ) ; [b][color=Red]End Function[/color][/b]
以上内容应该可以帮助您更清楚地理解程序背后的过程,我已经对代码进行了格式化,使其更清晰、更简洁,并对每一行进行了注释和解释。
阅读愉快!
干杯
李
如果您有什么需要进一步解释的,请随时询问
希望这有帮助 |