VLISP应用两例源码解析(视口锁定,求多边形形心与面积)
Vlisp入手快,更加形象。在某些属性获取与修改更新方面,比autolisp更为直观。特举例说明。
第一
视口锁定,对于autolisp,根据dxf资料,entities段,VIEWPORT,其视口显示锁定的组码为90,当包括16384 (0x4000) = 启用视口缩放锁定。如果要获取是否设置了该位,需要将90组码值与0x4000进行按位与运算。为1时,设置了锁定。相当繁琐且代码可读性差。
对于vlisp,使用dump操作,取得视口对象的属性方法表。- (defun C:dump(/ ent obj)
- (while (setq ent(entsel "\nSelect entity to get object data:"))
- (setq obj (vlax-ename->vla-object(car ent)))
- (vlax-dump-object obj T)
- (vlax-release-object obj)
- )
- (princ)
- )
得到列表如下:- IAcadPViewport2: IAcadPViewport 接口
- ;特性值:
- ; Application (RO) = #<VLA-OBJECT IAcadApplication 00eeb450>
- ; ArcSmoothness = 1000
- ; Center = (420.5 633.25 0.0)
- ; Clipped (RO) = 0
- ; CustomScale = 0.00666667
- ; Direction = (0.0 0.0 1.0)
- ; DisplayLocked = -1
- ; Document (RO) = #<VLA-OBJECT IAcadDocument 0340c954>
- ; EntityTransparency = "ByLayer"
- ; GridOn = 0
- ; Handle (RO) = "17193"
- ; HasExtensionDictionary (RO) = -1
- ; HasSheetView (RO) = 0
- ; Height = 1086.5
- ; Hyperlinks (RO) = #<VLA-OBJECT IAcadHyperlinks 5097e804>
- ; LabelBlockId = 0
- ; Layer = "0"
- ; LayerPropertyOverrides (RO) = 0
- ; LensLength = 50.0
- ; Linetype = "ByLayer"
- ; LinetypeScale = 1.0
- ; Lineweight = -1
- ; Material = "ByLayer"
- ; ModelView = nil
- ; ObjectID (RO) = 2127793880
- ; ObjectName (RO) = "AcDbViewport"
- ; OwnerID (RO) = 2127654608
- ; PlotStyleName = "ByLayer"
- ; ShadePlot = 0
- ; SheetView = nil
- ; SnapBasePoint = (0.0 0.0)
- ; SnapOn = 0
- ; SnapRotationAngle = 0.0
- ; StandardScale = 1
- ; StandardScale2 = 4
- ; Target = (0.0 0.0 0.0)
- ; TrueColor = #<VLA-OBJECT IAcadAcCmColor 2c7ec540>
- ; TwistAngle = 0.0
- ; UCSIconAtOrigin = -1
- ; UCSIconOn = -1
- ; UCSPerViewport = -1
- ; ViewportOn = -1
- ; Visible = -1
- ; VisualStyle = 1
- ; Width = 821.0
- ;支持的方法:
- ; ArrayPolar (3)
- ; ArrayRectangular (6)
- ; Copy ()
- ; Delete ()
- ; Display (1)
- ; GetBoundingBox (2)
- ; GetExtensionDictionary ()
- ; GetGridSpacing (2)
- ; GetSnapSpacing (2)
- ; GetXData (3)
- ; Highlight (1)
- ; IntersectWith (2)
- ; Mirror (2)
- ; Mirror3D (3)
- ; Move (2)
- ; Rotate (2)
- ; Rotate3D (3)
- ; ScaleEntity (2)
- ; SetGridSpacing (2)
- ; SetSnapSpacing (2)
- ; SetXData (2)
- ; SyncModelView ()
- ; TransformBy (1)
- ; Update ()
- T
复制代码 很容易判断出属性为 DisplayLocked
使用- vlax-get-property与vlax-put-property便可完成设置
复制代码 代码如下:- (defun C:VCF( / enobj vallist n lockflag)
- ;Designed by 林霄云 2014年4月18日
- ;视口显示锁定与解锁循环
- (if (and (setq en (entsel "选择视口"))
- (= (get-dxf 0 (setq en (car en))) "VIEWPORT")
- )
- (progn
- (setq enobj (vlax-ename->vla-object en))
- (setq lockflag (vlax-get-property enobj "DisplayLocked"))
- (princ (strcat "所选视口显示锁定状态为" (vl-princ-to-string lockflag)))
- (if (= lockflag :vlax-true)
- (progn (vlax-put-property enobj "DisplayLocked" :vlax-false) (princ "\n所选视口显示锁定状态修改为:否——视口解锁" ))
- (progn (vlax-put-property enobj "DisplayLocked" :vlax-true) (princ "\n所选视口显示锁定状态修改为:是——视口锁定" ))
- );if
- );progn
- );if
- (princ)
- );defun
第二
获取多边形形心与面积
形心与面积是面域的属性。可以将多边形(封闭可转换成面域的)转换后,读取相应属性即可。
代码如下:- (defun C:xingxin()
- (setq en (car(entsel "\n选取闭合曲线:")))
- ;(setvar "DELOBJ" 1)
- (if ( /= (get-dxf 0 en) "REGION")
- (progn
- (command "region" en "")
- (setq en (entlast))
- ));if
- (setq
- ENT (vlax-ename->vla-object en)
- CEN (vlax-get ENT "centroid")
- AREA (vlax-get ENT "AREA"))
- (princ (strcat "\narea: " (rtos area 2 3)))
- (princ (strcat "\ncentroid: " (vl-princ-to-string cen )))
- (command "point" CEN )
- (princ)
- )
值得注意的是,该获取的坐标CEN,是UCS坐标系下的,故该程序支持UCS。
附带的get-dxf函数:- ;;get-DXF 实体dxf数据 (get-DXF code ename)
- ;;get-dxf code ename, Designed by 林霄云。精简自e派。进行判断的,ename必须ename。
- (defun get-DXF (code ename )
- (cond ((= (type ename) 'ENAME)
- (if (= code -3)
- (cdr (assoc code (entget ename '("*"))))
- (cdr (assoc code (entget ename)))
- ))
- );COND
- )
|