Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Distinguished Contributor
120 Posts
0 Kudos
Registered: ‎03-12-2004
Post 1 of 18
Accepted Solution

Polyline Vertex Counter

2106 Views, 17 Replies
07-29-2011 10:50 PM

I am looking to create a utility that will count the vertices of a polyline and display the number in the form of a multileader attached to the last vertex, Maybe with the ability to add a code to the multileader by way of an attribute.

 

The reason for this is that at every vertex there will be a block depicting a tree, the code will identify the tree type, a schedule will then pick up the tree type and numbers & add them together to give me a tree schedule.

 

Can anyone help with this, i don't have much experience with lisp but i am keen to learn.

Creative Intentions
AutoCAD Certified Professional
Win 7 Pro 64bit, Dell Precision M6500

Hi Geg .

 

You may have old version of Autocad so that's why the routine did not work for you .

 

Anyway here is another way of coding and with the same result .

 

(defun c:Test (/ e l p1 p2)
; Tharwat 30. 07. 2011
(vl-load-com)
(if
(and
(setq e (car (entsel "\n Select a Polyline :")))
(member (cdr (assoc 0 (entget e)))
'("LWPOLYLINE" "POLYLINE")
)
)
(progn
(if (eq (vla-get-objectname (vlax-ename->vla-object e))
"AcDb3dPolyline"
)
(setq l
(/ (length (vlax-get (vlax-ename->vla-object e) 'Coordinates))
3
)
)
(setq l
(/ (length (vlax-get (vlax-ename->vla-object e) 'Coordinates))
2
)
)
)
(setq p1 (vlax-curve-getendpoint e))
(setq p2 (polar p1 0.785398 1.0))
(command "_.mleader" "_non" p1 "_non" p2 (itoa l))
)
(princ "\n Missed the entity or it's not a Polyline !! ")
)
(princ)
)

 Tharwat

Distinguished Mentor
654 Posts
63 Kudos
Registered: ‎07-02-2010
Post 2 of 18

Re: Polyline Vertex Counter

07-30-2011 12:02 AM in reply to: GegH1

Can you please upload an image of your desired result ?

 

What is the scale of your drawing ?

 

Thanks.

 

Tharwat

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Distinguished Contributor
120 Posts
0 Kudos
Registered: ‎03-12-2004
Post 3 of 18

Re: Polyline Vertex Counter

07-30-2011 03:38 AM in reply to: _Tharwat

Tharwat,

Thankyou for your response.

 

Attached is an image of what i am looking to achieve.

The scale i work at is 1:1 in millimetres. Generally the output is 1:100 in paperspace

Creative Intentions
AutoCAD Certified Professional
Win 7 Pro 64bit, Dell Precision M6500
Distinguished Mentor
654 Posts
63 Kudos
Registered: ‎07-02-2010
Post 4 of 18

Re: Polyline Vertex Counter

07-30-2011 03:55 AM in reply to: GegH1

Hope this what you mean .

 

(defun c:Test (/ spc e l p1 p2 led)
; Tharwat 30. 07. 2011
(vl-load-com) (setq spc (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)) ) ) (if (and (setq e (car (entsel "\n Select a Polyline :"))) (member (cdr (assoc 0 (entget e))) '("LWPOLYLINE" "POLYLINE") ) ) (progn (setq l (/ (length (vlax-get (vlax-ename->vla-object e) 'Coordinates)) 2 ) ) (setq p1 (vlax-curve-getendpoint e)) (setq p2 (polar p1 0.785398 1.0)) (setq led (vlax-invoke spc 'Addmleader (append p1 p2) 0)) (vla-put-textstring led (itoa l)) ) (princ "\n Missed the entity or it's not a Polyline !! ") ) (princ) )

 Tharwat

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Distinguished Contributor
120 Posts
0 Kudos
Registered: ‎03-12-2004
Post 5 of 18

Re: Polyline Vertex Counter

07-30-2011 04:49 AM in reply to: _Tharwat

That was quick. Thankyou.

 

I seem to get the following error though?

 

Command: test
; error: no function definition: VLAX-GET-ACAD-OBJECT

Creative Intentions
AutoCAD Certified Professional
Win 7 Pro 64bit, Dell Precision M6500
*Expert Elite*
6,040 Posts
670 Kudos
Registered: ‎09-13-2004
Post 6 of 18

Re: Polyline Vertex Counter

07-30-2011 04:58 AM in reply to: _Tharwat

TharwaT313 wrote:

.....

(if

  (and

    (setq e (car (entsel "\n Select a Polyline :")))

    (member (cdr (assoc 0 (entget e))) '("LWPOLYLINE" "POLYLINE"))

  )

  (progn

    (setq l

      (/

        (length (vlax-get (vlax-ename->vla-object e) 'Coordinates))

        2

      )

    )

....


For "heavy" Polylines [2D or 3D], the 'Coordinates VLA Property lists all three XYZ coordinates for each vertex.  So if you might have that kind of Polyline, I think you would need to either:

A.  use an (if) function to make that divisor 3 if it's a "POLYLINE" and 2 if it's a "LWPOLYLINE"; or

B.  use some other method of finding the number of vertices instead, such as this, which works for any Polyline type:

 

(if

  (and

    (setq e (car (entsel "\n Select a Polyline :")))

    (member (cdr (assoc 0 (entget e))) '("LWPOLYLINE" "POLYLINE"))

  )

  (progn

    (setq l (1+ (fix (vlax-curve-getEndParam e))))

....

 

On the other hand, if you would never encounter "heavy" Polylines, test for only the "LWPOLYLINE" entity type, and just divide the 'Coordinates VLA Property by 2, or take another approach such as this:

 

    (setq l (cdr (assoc 90 (entget e))))

Kent Cooper
Distinguished Mentor
654 Posts
63 Kudos
Registered: ‎07-02-2010
Post 7 of 18

Re: Polyline Vertex Counter

07-30-2011 10:03 AM in reply to: GegH1

Hi Geg .

 

You may have old version of Autocad so that's why the routine did not work for you .

 

Anyway here is another way of coding and with the same result .

 

(defun c:Test (/ e l p1 p2)
 ; Tharwat 30. 07. 2011
  (vl-load-com)
  (if
    (and
      (setq e (car (entsel "\n Select a Polyline :")))
      (member (cdr (assoc 0 (entget e)))
              '("LWPOLYLINE" "POLYLINE")
      )
    )
     (progn
       (if (eq (vla-get-objectname (vlax-ename->vla-object e))
               "AcDb3dPolyline"
           )
         (setq l
                (/ (length (vlax-get (vlax-ename->vla-object e) 'Coordinates))
                   3
                )
         )
         (setq l
                (/ (length (vlax-get (vlax-ename->vla-object e) 'Coordinates))
                   2
                )
         )
       )
       (setq p1 (vlax-curve-getendpoint e))
       (setq p2 (polar p1 0.785398 1.0))
       (command "_.mleader" "_non" p1 "_non" p2 (itoa l))
     )
     (princ "\n Missed the entity or it's not a Polyline !! ")
  )
  (princ)
)

 Tharwat

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Distinguished Mentor
654 Posts
63 Kudos
Registered: ‎07-02-2010
Post 8 of 18

Re: Polyline Vertex Counter

07-30-2011 10:10 AM in reply to: Kent1Cooper

Thanks Kent .

 

I do understand what you mean and I would consider that your idea is another way of counting vertices and both are

working perfectely .

 

Tharwat

____________________________________________________
Get learn and learn and practice , to become experienced guy .
Valued Contributor
71 Posts
3 Kudos
Registered: ‎03-21-2009
Post 9 of 18

Re: Polyline Vertex Counter

07-30-2011 01:57 PM in reply to: Kent1Cooper

what part of the code determines the size of the mleader... it is being shown very tiny and I can't figure out what part of the code addresses the size of the mleader and text.

TIA

Steve

*Expert Elite*
1,250 Posts
33 Kudos
Registered: ‎01-09-2007
Post 10 of 18

Re: Polyline Vertex Counter

07-30-2011 02:41 PM in reply to: stevesfr

Hey Stevesfr, none of the code.  Your current settings of your multileader style determines that.  Adjust your settings to your current style and try again

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!