For the following examples, assume I have three points from which I wish to construct an LWPolyline:
(setq p1 '(1.2 3.4)
p2 '(3.2 5.1)
p3 '(6.2 8.4)
)
Since the vertices of an LWPolyline are 2D, (with the Z-coordinate constant, enumerated by the LWPolyline elevation), these points must be 2D.
For the construction of the LWPolyline, there are three routes you can take:
1) Command Call:The slowest method, but perhaps most intuitive for a beginner, using existing AutoCAD command knowledge:
(command "_.pline" "_non" p1 "_non" p2 "_non" p3)
Note that the "_non" prefix is used to avoid interference with any active Object Snaps.
2) EntmakeThe fastest method, but maybe a little obscure if you aren't familiar with the various DXF group codes:
(entmakex
(list
(cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 3)
(cons 70 0)
(cons 10 p1)
(cons 10 p2)
(cons 10 p3)
)
)
A full reference for the DXF group codes utilised by the above code can be found
here.
3) Visual LISP AddLightWeightPolyline methodLet us first grab modelspace, just for this example:
(setq mspace
(vla-get-modelspace
(vla-get-activedocument (vlax-get-acad-object))
)
)
Now, there are three ways you can invoke the
AddLightWeightPolyline method:
The undocumented
vlax-invoke function:
(vlax-invoke mspace 'addlightweightpolyline (append p1 p2 p3))
This avoids the need to use Variants and Safearrays, but, you must ensure that all the coordinate values are Reals (Doubles), otherwise this method will fail. This subtlety can sometimes be overlooked and is a difficult bug to spot.
The
vlax-invoke-method function, or the
vla-addlightweightpolyline function:
These perform in much the same way, accepting the same parameter data types. The subtle difference is that
vla-addlightweightpolyline is an expression that is resolved at compilation, whereas
vlax-invoke-method function is an expression that is resolved at runtime. This difference would imply that, if you have mispelled a method called by
vlax-invoke-method, the error will arise at runtime, whereas a mispelling using the
vla-* function will be caught at compilation. Furthermore, the
vla-* functions are derived from the ActiveX type library, (which, for the AutoCAD object model, is registered automatically); when working with functions derived from other object models (such as Excel), you would have to import the relevant type library to obtain a set of functions analogous to the vla-* functions (but with a different (specified) prefix).
vlax-invoke-method(vlax-invoke-method mspace 'addlightweightpolyline
(vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray vlax-vbdouble '(0 . 5)) (append p1 p2 p3)
)
)
)
vla-addlightweightpolyline(vla-addlightweightpolyline mspace
(vlax-make-variant
(vlax-safearray-fill
(vlax-make-safearray vlax-vbdouble '(0 . 5)) (append p1 p2 p3)
)
)
)
Note that none of the above demonstrated methods allow for changes in UCS, or indeed express whether the original point set is defined in UCS or WCS.
All of the above methods could be altered to accept a list of points, but I shall leave this as an exercise for you to think about since I am merely providing an outline.
Finally, none of the posted code has been tested and all of the information has been plucked out of my head, having been gathered over the years mostly from these forums; so there may well be some mistakes - don't hesitate to correct me.
I hope this helps!
Lee