Author Topic: Help update this LISP from Luis Please...  (Read 5272 times)

0 Members and 1 Guest are viewing this topic.

trogg

  • Bull Frog
  • Posts: 246
Help update this LISP from Luis Please...
« on: November 12, 2010, 10:05:44 PM »
I found this LISPhttp://www.theswamp.org/index.php?topic=9441.msg169894#msg169894 routine that was graciously given by Luis that draws arrows showing the direction of an object (Polyline). But when I run it, the error shows this " ; error: no function definition: VLAX-ENAME->VLA-OBJECT". I have no idea how to use Visual Lisp to fix this. I am in an AutoLISP class right now and the more code that I read on here makes me realize that I should learn Visual Lisp as well.
Anyway, I would really appreciate help with fixing this code.

Code: [Select]
(defun GetArcSegment  (cen r fromvertex p2 / a1 a2 d)
  (if (and fromvertex p2)
    (progn
      (setq a1 (angle cen fromvertex)
    a2 (angle cen p2))
      (if (or (< a1 a2) (equal a1 a2 0.001))
(setq d (* r (- a2 a1)))
(setq d (* r (- (+ 6.2831853 a2) a1)))))
    ;; es un circulo
    (setq d (* r 6.2831853))))

(defun GetBulgeData
     (bulge fromvertex p2 / dir theta beta radio dat)
  (setq dir
      (cond ((minusp bulge) -1.0)
    (t 1.0))
theta
      (* 4.0
(atan
   (abs bulge))))
  (if (> theta pi)
    (setq
      theta (- (* 2.0 pi) theta)
      dir   (* -1.0 dir)))
  (setq theta (/ theta 2.0)
radio
      (abs
(/
  (distance fromvertex p2)
  (* 2.0
     (abs (sin theta)))))
beta
      (+
(angle fromvertex p2)
(*
  (- (/ pi 2.0) theta)
  dir))
pc    (polar fromvertex beta radio))
  (getarcsegment pc radio p2 fromvertex))

(defun GetLWPolyData  (vla_poly /    name      endParam
       param closed    fromVertex
       p2 midp    bulge     vlist)
  (setq closed (vla-get-closed vla_poly))
  (setq endParam (vlax-curve-GetEndParam vla_poly))
  (setq param endParam)
  (setq i 0)
  (while (> param 0)
    (setq param (1- param))
    (setq fromVertex (vlax-curve-GetPointAtParam vla_poly i))
    (setq bulge (vla-getBulge vla_poly (fix i)))
    (setq nextVertex
   (vlax-curve-GetPointAtParam
     vla_poly
     (+ i 1)))
    (setq dis (distance fromVertex nextVertex))
    (setq midPt
   (vlax-curve-getPointAtParam vla_poly (+ i 0.5)))
    (if (not (zerop bulge))
      (progn
(setq bulge (GetBulgeData
      bulge
      fromVertex
      nextVertex))
(setq etype "ARC"))
      (progn bulge (setq etype "LINE")))
;;;;;;    (if (not :rcmPrefixArcText)
;;;;;;      (setq :rcmPrefixArcText "L="))
    (setq vlist
   (cons
     (list
       ;; numero vertice
       (+ i 1)
       ;; tipo de objeto
       eType
       ;; punto medio
       midPt
       ;; inicia vertice
       fromVertex
       ;; termina vertice
       nextVertex
       ;; longitud de curva o recta
;;;;;;        (if (= eType "ARC")
;;;;;; (strcat
;;;;;;    :rcmPrefixArcText
;;;;;;    (rtos bulge (rcmd-getUnits-mode) :rcmPrec))
;;;;;; ;; es una recta
;;;;;; (rtos dis (rcmd-getUnits-mode) :rcmPrec))
       )
     vlist))
    (setq i (1+ i)))
  (reverse vlist))

(defun dib_flechdir
       (lst_dat / unidad angf dirf pfm pf1 pf2 pf3 pf4 pftemp)
  ;; establecer longitud de flecha de acuerdo a la altura de pantalla
  ;; para dibujar las flechas iguales a cualquier nivel de zoom
  (setq unidad (/ (getvar "VIEWSIZE") 15))
  (foreach dat lst_dat
    (setq angf (cadr dat)
  dirf (caddr dat)
  pfm  (polar (car dat) (+ angf (/ pi 2)) (* unidad 0.3))
  pf1  (polar pfm (- angf pi) (/ unidad 2.0))
  pf2  (polar pfm angf (/ unidad 2.0)))
    (if (= dirf 1)
      (setq pf3 (polar pf2 (- angf (/ (* pi 5.0) 6.0)) (/ unidad 4.0))
    pf4 (polar pf2 (+ angf (/ (* pi 5.0) 6.0)) (/ unidad 4.0)))
      (setq pftemp pf1
    pf1    pf2
    pf2    pftemp
    pf3    (polar pf2 (+ angf (/ pi 6.0)) (/ unidad 4.0))
    pf4    (polar pf2 (- angf (/ pi 6.0)) (/ unidad 4.0))))
    (if flag_dir
      (progn
;; dibujar flecha color verde
;; cuando se le cambie de direccion
(grdraw pf1 pf2 3)
(grdraw pf2 pf3 3)
(grdraw pf2 pf4 3))
      (progn
;; dibujar flecha
(grdraw pf1 pf2 4)
(grdraw pf2 pf3 4)
(grdraw pf2 pf4 4))))
  (setq flag_dir nil))

;;; Command for test...
(defun C:TST  (/ pol obj pol_data)
  (setq pol (car (entsel "\nSelect polyline: "))
obj (vlax-ename->vla-object pol)
pol_data (GetLWPolyData obj))
  (dib_flechdir
    (setq
      lst_dat
       (vl-remove
nil
(mapcar
   (function
     (lambda (i)
       (if (nth 2 i)
(list
   (nth 2 i)
   (angle (nth 3 i) (nth 4 i))
   1))))
   pol_data))))
  (princ))
(princ)
I must also say that since I have fallen into the swamp, you are the best LISP source that I have found. Some of you are just insane with what you come up with. I really enjoy seeing what you come up with. and you inspire me to try coding more and more. In my LISP class, most everybody else is just in there because they have to be in the class. And to be honest, it aint the best class but at least they offer a LISP class. The prevailing thought about AutoLISP( in regards to the class) is that AutoCAD already does the things that LISP can do so there's no need to program yourself anymore. But especially coming here, that concept is smashed. And now I am reading other books and code for "fun." And you can find me hangin' out at the swamp.
Thankyou swampers,
~Greg

MP

  • Seagull
  • Posts: 17400
Re: Help update this LISP from Luis Please...
« Reply #1 on: November 12, 2010, 10:06:36 PM »
(vl-load-com)

that is, execute it before running that proggy (put in your startup)
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

MP

  • Seagull
  • Posts: 17400
Re: Help update this LISP from Luis Please...
« Reply #2 on: November 12, 2010, 10:10:50 PM »
You could also put it right in the code, i.e.

Code: [Select]
(defun GetArcSegment  (cen r fromvertex p2 / a1 a2 d)

    (vl-load-com)

    ...

)

But it's better to put in your startup (IMO).
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Help update this LISP from Luis Please...
« Reply #3 on: November 12, 2010, 10:17:48 PM »
Quote
"The prevailing thought about AutoLISP( in regards to the class) is that AutoCAD already does the things that LISP can do so there's no need to program yourself anymore."

 :? Wowsa   ... that's like saying a program is perfect and can never be improved upon  :-D

Anyways ... welcome the the swamp Greg  :-) we enjoy writing code better than the norm. Hopefully we can be an invaluable resource. 8-)


Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

trogg

  • Bull Frog
  • Posts: 246
Re: Help update this LISP from Luis Please...
« Reply #4 on: November 12, 2010, 10:27:12 PM »
Thanks MP,
That was ridiculously quick and easy. I was thinking that I might get a reply tomorrow. So thank you very much.
Question about this code or if you know of another. After placing the (vl-load-com) at the beginning as you suggested, it works great for PolyLines that have been created and have been left alone. However, I have Polylines that after creating them, I double-click them and then choose the "Fit" option (or Fit Curve). This routine doesn't work on these types of PolyLines. is that something that could be fixed?

MP

  • Seagull
  • Posts: 17400
Re: Help update this LISP from Luis Please...
« Reply #5 on: November 12, 2010, 10:27:55 PM »
The prevailing thought about AutoLISP( in regards to the class) is that AutoCAD already does the things that LISP can do so there's no need to program yourself anymore.

I humbly submit that your instructor is a fool, he should not be "teaching" and you can him I said that (although that may not be a good strategy for you as he may "punish" you in his marking etc).
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Help update this LISP from Luis Please...
« Reply #6 on: November 12, 2010, 10:31:02 PM »
The prevailing thought about AutoLISP( in regards to the class) is that AutoCAD already does the things that LISP can do so there's no need to program yourself anymore.

I humbly submit that your instructor is a fool, he should not be "teaching" and you can him I said that (although that may not be a good strategy for you as he may "punish" you in his marking etc).

X2

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

trogg

  • Bull Frog
  • Posts: 246
Re: Help update this LISP from Luis Please...
« Reply #7 on: November 12, 2010, 10:34:31 PM »
After sitting through many painfully boring classes and then jumping online and seeing what people can actually do and how much time it can save drafting and how much fun you crazy guys have at doing it, I just figured that this guy just does it for a job and doesn't really care. It's sad that there isn't much inspiration from this class. I figure that with all the changes that come out with each release of AutoCAD, the program HAVEN'T been maxed out yet!!!
I think of the swamp as my real learning area (class).

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Help update this LISP from Luis Please...
« Reply #8 on: November 12, 2010, 10:45:06 PM »
Here's a quick fix for 2dpolylines  8-):

Code: [Select]
(defun getarcsegment (cen r fromvertex p2 / a1 a2 d)
  (if (and fromvertex p2)
    (progn (setq a1 (angle cen fromvertex)
a2 (angle cen p2)
   )
   (if (or (< a1 a2) (equal a1 a2 0.001))
     (setq d (* r (- a2 a1)))
     (setq d (* r (- (+ 6.2831853 a2) a1)))
   )
    )
    ;; es un circulo
    (setq d (* r 6.2831853))
  )
)

(defun getbulgedata (bulge fromvertex p2 / dir theta beta radio dat)
  (setq dir   (cond ((minusp bulge) -1.0)
    (t 1.0)
      )
theta (* 4.0 (atan (abs bulge)))
  )
  (if (> theta pi)
    (setq theta (- (* 2.0 pi) theta)
  dir (* -1.0 dir)
    )
  )
  (setq theta (/ theta 2.0)
radio (abs (/ (distance fromvertex p2) (* 2.0 (abs (sin theta)))))
beta  (+ (angle fromvertex p2) (* (- (/ pi 2.0) theta) dir))
pc    (polar fromvertex beta radio)
  )
  (getarcsegment pc radio p2 fromvertex)
)

(defun getlwpolydata
       (vla_poly / name endparam param closed fromvertex p2 midp bulge vlist)
  (setq closed (vla-get-closed vla_poly))
  (setq endparam (vlax-curve-getendparam vla_poly))
  (setq param endparam)
  (setq i 0)
  (while (> param 0)
    (setq param (1- param))
    (setq fromvertex (vlax-curve-getpointatparam vla_poly i))
    (if (vlax-property-available-p vla_poly 'bulge)
      (setq bulge (vla-getbulge vla_poly (fix i)))
    )
    (setq nextvertex (vlax-curve-getpointatparam vla_poly (+ i 1)))
    (setq dis (distance fromvertex nextvertex))
    (setq midpt (vlax-curve-getpointatparam vla_poly (+ i 0.5)))
    (if (and bulge (not (zerop bulge)))
      (progn (setq bulge (getbulgedata bulge fromvertex nextvertex))
     (setq etype "ARC")
      )
      (progn bulge (setq etype "LINE"))
    )
;;;;;;    (if (not :rcmPrefixArcText)
;;;;;;      (setq :rcmPrefixArcText "L="))
    (setq vlist (cons (list ;; numero vertice
    (+ i 1)
    ;; tipo de objeto
    etype
    ;; punto medio
    midpt
    ;; inicia vertice
    fromvertex
    ;; termina vertice
    nextvertex
    ;; longitud de curva o recta
;;;;;;        (if (= eType "ARC")
;;;;;; (strcat
;;;;;;    :rcmPrefixArcText
;;;;;;    (rtos bulge (rcmd-getUnits-mode) :rcmPrec))
;;;;;; ;; es una recta
;;;;;; (rtos dis (rcmd-getUnits-mode) :rcmPrec))
      )
      vlist
)
    )
    (setq i (1+ i))
  )
  (reverse vlist)
)

(defun dib_flechdir (lst_dat / unidad angf dirf pfm pf1 pf2 pf3 pf4 pftemp)
  ;; establecer longitud de flecha de acuerdo a la altura de pantalla
  ;; para dibujar las flechas iguales a cualquier nivel de zoom
  (setq unidad (/ (getvar "VIEWSIZE") 15))
  (foreach dat lst_dat
    (setq angf (cadr dat)
  dirf (caddr dat)
  pfm  (polar (car dat) (+ angf (/ pi 2)) (* unidad 0.3))
  pf1  (polar pfm (- angf pi) (/ unidad 2.0))
  pf2  (polar pfm angf (/ unidad 2.0))
    )
    (if (= dirf 1)
      (setq pf3 (polar pf2 (- angf (/ (* pi 5.0) 6.0)) (/ unidad 4.0))
    pf4 (polar pf2 (+ angf (/ (* pi 5.0) 6.0)) (/ unidad 4.0))
      )
      (setq pftemp pf1
    pf1    pf2
    pf2    pftemp
    pf3    (polar pf2 (+ angf (/ pi 6.0)) (/ unidad 4.0))
    pf4    (polar pf2 (- angf (/ pi 6.0)) (/ unidad 4.0))
      )
    )
    (if flag_dir
      (progn ;; dibujar flecha color verde
     ;; cuando se le cambie de direccion
     (grdraw pf1 pf2 3)
     (grdraw pf2 pf3 3)
     (grdraw pf2 pf4 3)
      )
      (progn ;; dibujar flecha
     (grdraw pf1 pf2 4)
     (grdraw pf2 pf3 4)
     (grdraw pf2 pf4 4)
      )
    )
  )
  (setq flag_dir nil)
)

;;; Command for test...
(defun c:tst (/ pol obj pol_data)
  (setq pol (car (entsel "\nSelect polyline: "))
obj (vlax-ename->vla-object pol)
pol_data (getlwpolydata obj)
  )
  (dib_flechdir
    (setq lst_dat
   (vl-remove
     nil
     (mapcar (function (lambda (i)
(if (nth 2 i)
   (list (nth 2 i) (angle (nth 3 i) (nth 4 i)) 1)
)
       )
     )
     pol_data
     )
   )
    )
  )
  (princ)
)

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

MP

  • Seagull
  • Posts: 17400
Re: Help update this LISP from Luis Please...
« Reply #9 on: November 12, 2010, 10:49:58 PM »
Thanks MP, That was ridiculously quick and easy. I was thinking that I might get a reply tomorrow. So thank you very much.

Much of LISP is, that's why it's such a valuable tool. You're very welcome.

Question about this code or if you know of another. After placing the (vl-load-com) at the beginning as you suggested, it works great for PolyLines that have been created and have been left alone. However, I have Polylines that after creating them, I double-click them and then choose the "Fit" option (or Fit Curve). This routine doesn't work on these types of PolyLines. is that something that could be fixed?

I didn't really look at the code (kinda tired right now) but it appears to have been written for light weight polylines, aka as lwpolylines (ergo the GetLWPolyData function buried in the code). So the "fix" would be to add code to deal with heavy weight polylines (hereafter referred to as normal polylines), and then branch accordingly depending upon what is supplied.

Normal polylines can be created from light polylines, as you noted, when they are splined or ccurve fit. They can also be created natively depending upon the status of the plinetype variable (hint: read up on it).

The very distilled distinction between lightweight and normal polylines is ... normal polylines consist of a head entity, child vertex entities, and a tail (seqend) entity, whereas a lightweight polyline hosts all the data (including vertex data) in one entity.

Light weight polyline dxf data data:

Code: [Select]
(
   (-1 . <Entity name: 7ffff708470>)
   (0 . "LWPOLYLINE")
   (330 . <Entity name: 7ffff706ec0>)
   (5 . "D7")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbPolyline")
   (90 . 5)
   (70 . 0)
   (43 . 0.0)
   (38 . 0.0)
   (39 . 0.0)
   (10 2327.99 1837.07)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (10 2348.73 1854.86)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (10 2383.84 1856.98)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (10 2402.13 1844.9)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (10 2406.87 1828.26)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (210 0.0 0.0 1.0)
)

Notice the 10, 40, 41, 42 and 91 dxf group codes.

Now the same geometry via a normal poyline, and the data stored with each vertex:

Code: [Select]
(
   (-1 . <Entity name: 7ffff7085c0>)
   (0 . "POLYLINE")
   (330 . <Entity name: 7ffff706ec0>)
   (5 . "EC")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDb2dPolyline")
   (66 . 1)
   (10 0.0 0.0 0.0)
   (70 . 0)
   (40 . 0.0)
   (41 . 0.0)
   (210 0.0 0.0 1.0)
   (71 . 0)
   (72 . 0)
   (73 . 0)
   (74 . 0)
   (75 . 0)
)

(
   (-1 . <Entity name: 7ffff7085e0>)
   (0 . "VERTEX")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "EE")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbVertex")
   (100 . "AcDb2dVertex")
   (10 2327.99 1837.07 0.0)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (70 . 0)
   (50 . 0.0)
)

(
   (-1 . <Entity name: 7ffff708600>)
   (0 . "VERTEX")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "F0")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbVertex")
   (100 . "AcDb2dVertex")
   (10 2348.73 1854.86 0.0)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (70 . 0)
   (50 . 0.0)
)

(
   (-1 . <Entity name: 7ffff708610>)
   (0 . "VERTEX")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "F1")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbVertex")
   (100 . "AcDb2dVertex")
   (10 2383.84 1856.98 0.0)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (70 . 0)
   (50 . 0.0)
)

(
   (-1 . <Entity name: 7ffff708620>)
   (0 . "VERTEX")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "F2")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbVertex")
   (100 . "AcDb2dVertex")
   (10 2402.13 1844.9 0.0)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (70 . 0)
   (50 . 0.0)
)

(
   (-1 . <Entity name: 7ffff708630>)
   (0 . "VERTEX")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "F3")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (100 . "AcDbVertex")
   (100 . "AcDb2dVertex")
   (10 2406.87 1828.26 0.0)
   (40 . 0.0)
   (41 . 0.0)
   (42 . 0.0)
   (91 . 0)
   (70 . 0)
   (50 . 0.0)
)

(
   (-1 . <Entity name: 7ffff7085d0>)
   (0 . "SEQEND")
   (330 . <Entity name: 7ffff7085c0>)
   (5 . "ED")
   (100 . "AcDbEntity")
   (67 . 0)
   (410 . "Model")
   (8 . "0")
   (-2 . <Entity name: 7ffff7085c0>)
)

That's all I have for now. Hope it. Others will chime in with additional info to augment this. Have fun, and welcome to the swamp.
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

trogg

  • Bull Frog
  • Posts: 246
Re: Help update this LISP from Luis Please...
« Reply #10 on: November 12, 2010, 10:56:20 PM »
RonJon,
That is beautiful
Thank you very much.
Now I am going to compare the two codes and try to figure out what you did.
But like I said, I don't know VLisp so I don't know what the VL things mean.
~Greg

trogg

  • Bull Frog
  • Posts: 246
Re: Help update this LISP from Luis Please...
« Reply #11 on: November 12, 2010, 11:02:20 PM »
MP
Thank you for the help. I will have to do as you said and  "read up on it."
We just this week hit the subject of dxf codes and filtering them. but this is way better than the class and the examples in class.
Thanks for the explanations.
~Greg

ronjonp

  • Needs a day job
  • Posts: 7025
Re: Help update this LISP from Luis Please...
« Reply #12 on: November 12, 2010, 11:04:48 PM »
RonJon,
That is beautiful
Thank you very much.
Now I am going to compare the two codes and try to figure out what you did.
But like I said, I don't know VLisp so I don't know what the VL things mean.
~Greg

First off use the VLIDE (vlide at command line). Under Debug menu check "Break on Error". Copy and paste the code into the editor then highlight all, run it using the load selection button (grey box with arrow pointing down)

This will help you troubleshoot anything (well lisp related) :-)

I'm off to bed ...have a good night.
« Last Edit: November 12, 2010, 11:16:19 PM by ronjonp »

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

trogg

  • Bull Frog
  • Posts: 246
Re: Help update this LISP from Luis Please...
« Reply #13 on: November 12, 2010, 11:23:00 PM »

Cool tips - I will try them
Question about VL--- the code that is for VLISP such as "vla_poly, vl-load-com, vlax-curve-getendparam..." is that produced in the Visual Lisp editor or is that stuff that you manually put in (type in)?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: Help update this LISP from Luis Please...
« Reply #14 on: November 13, 2010, 12:09:04 AM »
You type them in.

Look this over when you get time.
http://carnet-de-cablage.chez-alice.fr/Doc/lakose_The_Visual_LISP_Developers_Bible.pdf

It is a large file so don't get in a hurry. 8-)
I've reached the age where the happy hour is a nap. ()
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.