Get lisp input data from existing drawing

Get lisp input data from existing drawing

Anonymous
Not applicable
1,127 Views
7 Replies
Message 1 of 8

Get lisp input data from existing drawing

Anonymous
Not applicable

Ho do I extract input information for LISP from an AUTOCAD drawing. For example, I have an already drawn rectangle, when I call my LISP command, it is supposed to get the coordinates of the rectangle's vertex and do something with them.

0 Likes
1,128 Views
7 Replies
Replies (7)
Message 2 of 8

hmsilva
Mentor
Mentor

Hello Singularities, and welcome to Autodesk Community!

 

If your rectangle is a LwPolyline, perhaps something like this

 

(defun c:demo (/ ent hnd i pt_lst ss)
  (if (setq ss (ssget '((0 . "LWPOLYLINE"))))
    (repeat (setq i (sslength ss))
      (setq hnd    (ssname ss (setq i (1- i)))
            ent    (entget hnd)
            pt_lst (vl-remove-if-not
                     '(lambda (x)
                        (= (car x) 10)
                      )
                     ent
                   )
      )
      (foreach x pt_lst
        (princ (cdr x))
      )
    )
  )
  (princ)
)

 

Hope that helps

Henrique

EESignature

Message 3 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Ho do I extract input information for LISP from an AUTOCAD drawing. For example, I have an already drawn rectangle, when I call my LISP command, it is supposed to get the coordinates of the rectangle's vertex and do something with them.


If you really mean "vertex" in the singular, and by that you mean its starting vertex, and the plural "them" refers to its coordinates, there are several ways to get that.  Given 'ent' as the entity name of the Polyline [assuming that's what it is, since that's what the Rectangle command draws]:

 

(cdr (assoc 10 (entget ent)))

which will give you only X and Y coordinates.  The Z coordinate, if you need it, is the Polyline's Elevation, which you can get with:

(cdr (assoc 38 (entget ent)))

or

(vlax-get (vlax-ename->vla-object ent) 'Elevation)

 

Other ways to get the first vertex:

 

(vlax-curve-getStartPoint ent)

or

(vlax-curve-getPointAtParam ent 0)

which will give you XYZ coordinates.

 

But if you mean vertices in the plural, hmsilva's suggestion is a good way for XY coordinates only, but there are ways to get XYZ coordinates:

 

(vlax-get (vlax-ename->vla-object ent) 'Coordinates)

which will give you an undifferentiated list of XYZ coordinates, the first three being those of the first vertex, the next three those of the second vertex, etc.  That will also work on "heavy" 2D and 3D Polylines, which hmsilva's approach won't.

 

And you can step through using

(vlax-curve-getPointAtParam ent ...)

with the ... replaced by 0 , 1, etc. integer parameter values [write back if you want more about how to do it that way].

 

Kent Cooper, AIA
Message 4 of 8

hmsilva
Mentor
Mentor

Hi Singularities,

 

in addition to Kent1Cooper's great explanation,


the method I prefer to use is step through the vlaobject parameters, and get the point at each parameter, because it allows us to get the vertices from all polylines types (3D, Heavy or Light), and because I'm using the 'cons' function to create my point list, I start to get the vertex coordinates from the end to the start, to get the point list with the correct vertices order.
i.e.

 

(setq ss (ssget "_+.:E:S" '((0 . "*POLYLINE"))))
(setq pt_lst (repeat (setq i (1+ (fix (vlax-curve-getendparam (ssname ss 0)))))
            (setq pt_lst (cons (vlax-curve-getpointatparam (ssname ss 0) (setq i (1- i))) pt_lst))
          )
)

 

I hope this helps

Henrique

EESignature

0 Likes
Message 5 of 8

owenwengerd
Advisor
Advisor

@hmsilva wrote:

the method I prefer to use is step through the vlaobject parameters, and get the point at each parameter...


Note that your method relies on an assumption about the parameterization of the object that is never guaranteed. See QuirkyPolyline: exposing foolish programmers.

--
Owen Wengerd
ManuSoft
0 Likes
Message 6 of 8

dbroad
Mentor
Mentor

In addition to what Kent, Henrique, and Owen have said:

1) What you want to do with the points may depend on whether you need to perform coordinate transformation via TRANS.

   For example, do you need to report or work in the current UCS, another entity's coordinate system, a display coordinate system, or a block's coordinate system?

2) (vlax-get <AcDb2dPolyline object> 'coordinates) will return a flat x,y,z coordinate list but (vlax-get <AcDbPolyline>  'coordinates) will return a flat x,y coordinate list.

 

Happy New Year Everyone!

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 7 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

2) (vlax-get <AcDb2dPolyline object> 'coordinates) will return a flat x,y,z coordinate list but (vlax-get <AcDbPolyline>  'coordinates) will return a flat x,y coordinate list.

....


Yes, my item about that was incorrect in suggesting that the 'Coordinates return would be in XYZ for any kind of Polyline.

 

There's been a lot about your "for example" rectangle, but to answer the more generic question, the main methods are (entget) and extracting the information from the entity data list that it returns, and conversion to a VLA object with (vlax-ename->vla-object) and extracting the information from the VLA properties that are then accessible with (vla-get...) and/or (vlax-get...) functions.  You can study about (entget) and (vlax-...) functions in Help, and while for some reason the AutoLISP Reference doesn't talk about (vla-...) [without the x] functions, you'll find many examples of all these approaches on this Forum.

Kent Cooper, AIA
0 Likes
Message 8 of 8

hmsilva
Mentor
Mentor

@owenwengerd wrote:
Note that your method relies on an assumption about the parameterization of the object that is never guaranteed. See QuirkyPolyline: exposing foolish programmers.

Hi Owen,

 

@I have read your article @manusoft, and follow up the conversation between you and Fenton @adndevblog.
@I also remember an old thread here @Anonymous.Autodesk between you, JU, DB, TT, DK, LE...


And it's a good point,
Never take an assumption about the parameterization of the object that is never guaranteed!

 

Happy New Year to all

Henrique

EESignature

0 Likes