Author Topic: Generically Combine 2 or more routines consecutively  (Read 2459 times)

0 Members and 1 Guest are viewing this topic.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Generically Combine 2 or more routines consecutively
« on: May 25, 2016, 05:06:38 PM »
Just curious if it is possible to combine 2 or more routines consecutively in 1 lisp?

I stumbled upon this and that is what brought that questions up.
Code: [Select]
(defun c:sub1()
(princ "Lisp Routine 1")
)
(defun c:sub2()
(princ "Lisp Routine 2")
)
(defun c:MainCommand()
(c:sub1)
(c:sub2)
)

I know you can kinda of do it with a script...

So If I have a couple of lisp routines that I typically push one after another would this setup work for that?
Civil3D 2020

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Generically Combine 2 or more routines consecutively
« Reply #1 on: May 25, 2016, 05:14:33 PM »
Just curious if it is possible to combine 2 or more routines consecutively in 1 lisp?

I stumbled upon this and that is what brought that questions up.
Code: [Select]
(defun c:sub1()
(princ "Lisp Routine 1")
)
(defun c:sub2()
(princ "Lisp Routine 2")
)
(defun c:MainCommand()
(c:sub1)
(c:sub2)
)

I know you can kinda of do it with a script...

So If I have a couple of lisp routines that I typically push one after another would this setup work for that?

Did you try it ??
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Generically Combine 2 or more routines consecutively
« Reply #2 on: May 25, 2016, 05:19:25 PM »
be nice now....  :idiot2: i forgot how to put the princ load lisp routine and execute it.
Civil3D 2020

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Generically Combine 2 or more routines consecutively
« Reply #3 on: May 25, 2016, 07:12:33 PM »
< ... > i forgot how to put the princ load lisp routine and execute it.

That's not what you asked. Is that what you want or is it just a comment on the state of things ?
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: Generically Combine 2 or more routines consecutively
« Reply #4 on: May 26, 2016, 03:17:38 AM »
haha...i did this in .net with a line / curve table generator I made... it's kind of goofy really...but it worked. First it fires the line portion, then I place a table, then it fires the curve portion, and places that table....was totally lazy...to combine it all was going to be a major PITA though, so I tell peeps too bad lol

Just curious if it is possible to combine 2 or more routines consecutively in 1 lisp?

I stumbled upon this and that is what brought that questions up.
Code: [Select]
(defun c:sub1()
(princ "Lisp Routine 1")
)
(defun c:sub2()
(princ "Lisp Routine 2")
)
(defun c:MainCommand()
(c:sub1)
(c:sub2)
)

I know you can kinda of do it with a script...

So If I have a couple of lisp routines that I typically push one after another would this setup work for that?

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Generically Combine 2 or more routines consecutively
« Reply #5 on: May 26, 2016, 04:58:51 AM »

Perhaps something like :

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;;-------------------------------------------------------------------
  3. ;; MainCommand.lsp
  4. ;; codehimbelonga me 2016.05.26
  5. ;;
  6.  
  7.   (load "K:\\SUPPORT\\Library101.vlx" "Unable to LOAD 'Library101.vlx' ")
  8. )
  9.  
  10. (if (not (vl-symbol-value 'c:sub1))
  11.   (load "K:\\SUPPORT\\Sub1.lsp" "Unable to LOAD 'Sub1.lsp' ")
  12. )
  13.  
  14. (if (not (vl-symbol-value 'c:sub2))
  15.   (load "K:\\SUPPORT\\Sub2.lsp" "Unable to LOAD 'Sub2.lsp' ")
  16. )
  17.               (vl-symbol-value 'c:sub1)
  18.               (vl-symbol-value 'c:sub2)
  19.          )
  20.     )
  21.   (alert "\nOoooops, \nThe fan has been hit")
  22. )
  23. ;;;-------------------------------------------------------------------
  24.  
  25. (defun c:MainCommand (/ *error*)
  26.   (defun *error* (msg)
  27.     (if (null (wcmatch (strcase msg t) "*break,*exit*"))
  28.       ;;"*break,*cancel*,*exit*"))
  29.       (progn (princ (strcat "\nxError: " msg)) (vl-bt))
  30.     )
  31.     (princ)
  32.   )
  33.  
  34.   (greets "Hello")
  35.   (c:sub1)
  36.   (c:sub2)
  37.   (princ)
  38. )
  39. ;;;-------------------------------------------------------------------
  40. ;;;-------------------------------------------------------------------
  41.  

Code - Auto/Visual Lisp: [Select]
  1. ;;;-------------------------------------------------------------------
  2. ;;Library101.vlx
  3. (defun lib1 () (princ "\nLisp Library 101"))
  4. (defun greets (msg)
  5.   (princ (strcat "\n" msg " " (getvar "loginname")))
  6.   (princ)
  7. )
  8.  

Code - Auto/Visual Lisp: [Select]
  1. ;;;-------------------------------------------------------------------
  2. ;;Sub1.lsp
  3. (defun c:sub1 () (princ "\nLisp Routine 1"))
  4.  
  5.  
Code - Auto/Visual Lisp: [Select]
  1. ;;;-------------------------------------------------------------------
  2. ;;Sub2.lsp
  3. (defun c:sub2 () (princ "\nLisp Routine 2"))
  4.  
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Generically Combine 2 or more routines consecutively
« Reply #6 on: May 26, 2016, 07:09:30 AM »
< ... > i forgot how to put the princ load lisp routine and execute it.

That's not what you asked. Is that what you want or is it just a comment on the state of things ?

Sorry for not getting back! The answer is; "It's what I want to do." :) And thanks for what you posted.
Civil3D 2020

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Generically Combine 2 or more routines consecutively
« Reply #7 on: May 26, 2016, 07:59:34 AM »
Ok I am trying this thing out. The Two lisp's that I want to do is

Sub1 - Show Polyline Direction

Sub2 - Change Polyline Direction

(I have all the lisp's in the same support folder.)

I run the command and get the following. (Not Sure what I did wrong.)

Code: [Select]
Command: mc
xError: no function definition: GREETS
Backtrace:
[0.51] (VL-BT)
[1.47] (*ERROR* "no function definition: GREETS")
[2.42] (_call-err-hook #<SUBR @0000000040eb5160 *ERROR*> "no function definition: GREETS")
[3.36] (sys-error "no function definition: GREETS")
:ERROR-BREAK.31 nil
[4.28] (#<SUBR @000000003ca48b88 null-fun-hk> "Hello")
[5.24] (GREETS "Hello")
[6.19] (C:MC)
[7.15] (#<SUBR @0000000040eb52f0 -rts_top->)
[8.12] (#<SUBR @000000003ca48700 veval-str-body> "(C:MC)" T #<FILE internal>)
:CALLBACK-ENTRY.6 (:CALLBACK-ENTRY)
:ARQ-SUBR-CALLBACK.3 (nil 0)
Command:



Code: [Select]

;;-------------------------------------------------------------------

;; MainCommand.lsp

;; codehimbelonga me 2016.05.26

;;

 

(if (not (vl-symbol-value 'lib1))

  (load "Library101.vlx" "Unable to LOAD 'Library101.vlx' ")

)

 

(if (not (vl-symbol-value 'c:sub1))

  (load "Sub1.lsp" "Unable to LOAD 'Sub1.lsp' ")

)



(if (not (vl-symbol-value 'c:sub2))

  (load "Sub2.lsp" "Unable to LOAD 'Sub2.lsp' ")

)

(if (not (and (vl-symbol-value 'lib1)

              (vl-symbol-value 'c:sub1)

              (vl-symbol-value 'c:sub2)

         )

    )

  (alert "\nOoooops, \nThe fan has been hit")

)

;;;-------------------------------------------------------------------

 

(defun c:MC (/ *error*)

  (defun *error* (msg)

    (if (null (wcmatch (strcase msg t) "*break,*exit*"))

      ;;"*break,*cancel*,*exit*"))

      (progn (princ (strcat "\nxError: " msg)) (vl-bt))

    )

    (princ)

  )

 

  (greets "Hello")

  (c:sub1)

  (c:sub2)

  (princ)

)

(princ)

;;;-------------------------------------------------------------------

;;;-------------------------------------------------------------------

 


Code: [Select]
;;;-------------------------------------------------------------------

;;Sub1.lsp

;; Original Code by Luis Esquival http://www.theswamp.org/index.php?topic=9441.msg169894#msg169894
;; Displays the direction of polylines with temporary arrows
;;
;; Modified by RonJonP
;; http://www.theswamp.org/index.php?topic=35706.msg409414#msg409414
(vl-load-com)
(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)))
   )
    )
    ;; is a circle
    (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 ;; vertex number
    (+ i 1)
    ;; object type
    etype
    ;; midpoint
    midpt
    ;; start vertex
    fromvertex
    ;; ending vertex
    nextvertex
    ;; curved or straight length
;;;;;;        (if (= eType "ARC")
;;;;;; (strcat
;;;;;;    :rcmPrefixArcText
;;;;;;    (rtos bulge (rcmd-getUnits-mode) :rcmPrec))
;;;;;; ;; is straight
;;;;;; (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)
  ;; set arrow length according to screen height
  ;; to draw the same arrows at any level of 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 ;; draw green arrow
     ;; when you are changing direction
     (grdraw pf1 pf2 3)
     (grdraw pf2 pf3 3)
     (grdraw pf2 pf4 3)
      )
      (progn ;; draw arrow
     (grdraw pf1 pf2 4)
     (grdraw pf2 pf3 4)
     (grdraw pf2 pf4 4)
      )
    )
  )
  (setq flag_dir nil)
)

;;; Command for test...
(defun c:Sub1 (/ 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)
)


Code: [Select]
;;;-------------------------------------------------------------------

;;Sub2.lsp

(defun c:Sub2 (/ elst vlst newlst new-vlst code42 code210
             obj nam pair clo)
 (command "_.undo" "_be")
 (while (null (setq en1 (entsel "\nPick an object to reverse: "))))
 (setq nam  (car en1)
       elst (entget nam)
       obj  (cdr (assoc 0 elst))
       clo  (= 1 (logand 1 (cdr (assoc 70 (entget nam)))))
 )
 (cond
   ((= obj "LWPOLYLINE")
    (setq new-vlst (list (assoc 10 elst))) ; start point
    (while (setq pair (car elst))
      (cond
        ((= (car pair) 10) ; vertex
         ;; collect vertex list
         (while (member (caar elst) '(10 40 41 42))
           (setq vlst (cons (car elst) vlst)
                 elst (cdr elst))
         ) ; end while
        )
        ((= (car pair) 210) ; extru direction??
         (setq code210 pair
               elst    (cdr elst))
        )
        ((setq newlst (cons pair newlst)
               elst   (cdr elst))
        )
      ) ; end cond stmt
    ) ; end while
    ;;  newlst= entlist less vertex list
    ;;  vlst= vertex list
    (while vlst ;  reverse vertex list
      (if (= (car (setq pair (car vlst))) 42) ; bulge
        ;; reverse the bulge
        (setq code42 (cons 42 (* (cdr pair) -1))
              vlst   (cdr vlst))
      )
      (if (= (car (setq pair (car vlst))) 41)
        ;; reverse the width position
        (setq vlst     (cdr vlst)
              new-vlst (cons (cons 40 (cdr pair)) new-vlst)
              new-vlst (cons (cons 41 (cdr (car vlst))) new-vlst)
              vlst     (cdr vlst))
      )
      (if code42 ; add bulge back to list
        (setq new-vlst (cons code42 new-vlst)
              code42   nil)
      )
      (if (= (car (setq pair (car vlst))) 10)
        (setq new-vlst (cons pair new-vlst)
              vlst     (cdr vlst))
      )
    ) ; end while
    (if clo ; closed pline
      (setq new-vlst (cdr new-vlst)) ; remove the start pt
      (setq new-vlst (reverse(cdr (reverse new-vlst))))
    )
    ;;  new-vlst contains the reverse vertex list
    (setq newlst (append new-vlst newlst))
    (if code210
      (setq newlst (append (list code210) newlst))
    )
    (entmod (reverse newlst))
    (entupd nam) ; Regenerates the polyline entity
   )
   ((= obj "POLYLINE")
    (prompt "\nNot yet working... Old Style Polyline."))
   ((prompt "\nObject selected is not a polyline"))
 ) ; end cond stmt
 (princ)
) ; end defun
Civil3D 2020

ChrisCarlson

  • Guest
Re: Generically Combine 2 or more routines consecutively
« Reply #8 on: May 26, 2016, 08:08:05 AM »
You can have as many routines in a single .LSP container that you want, provided each routine starts and ends and the non-executable routines are loaded before command calls. Mine goes something like this


Code - Auto/Visual Lisp: [Select]
  1. ;;---------------------=={ Error Trapping }==----------------------;;
  2. (defun *error* (msg)
  3.   (and msg
  4.        (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*"))
  5.        (princ (strcat "\nError: " msg))
  6.   )
  7. )      
  8. ;;------------------------------------------------------------;;
  9. (setvars blah 1)
  10.  
  11. (defun cc:Routine1 (/)
  12. (vlax-dosomethinginternally)
  13. )
  14.  
  15. (defun c:Code1 (/)
  16. (vlax-dosoemthingcool)
  17. )
  18.  
  19. (defun c:Code3 (/)
  20. (vlax-dothis)
  21. )
  22.  
  23. (defun c:Code3 (/)
  24. (cc:Routine1 "2")
  25. )

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Generically Combine 2 or more routines consecutively
« Reply #9 on: May 26, 2016, 08:14:07 AM »
Interesting... So let me ask. How would you setup the one I just posted. I guess I am having a hard time seeing how to execute the sub1 and then sub2 commands. I get that MC routine starts it. But the Sub commands usually start off by me typing in the command to initialize it.

Again I am prob. not understanding or feeling quit nuts.... :idiot2:
Civil3D 2020

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Generically Combine 2 or more routines consecutively
« Reply #10 on: May 26, 2016, 10:17:37 AM »
The error you're getting is because the library isn't being loaded.
I added that as a demonstration.
just remove the load call for Library101.vlx and the greets function call.

of change the .VLX extension to LSP and change the pathing in the load statements to suit your situation.
The code I posted was just a demo to show capability

It's good to see that the error trap helped solve the problem. You SHOULD have seen a message indicating that the file didn't load.

;;-----

(c:sub1) in the MC function is equivalent to sub1 at the command line.
That's what this is all about .... chaining the commands in one function.
« Last Edit: May 26, 2016, 10:21:13 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MSTG007

  • Gator
  • Posts: 2601
  • I can't remeber what I already asked! I need help!
Re: Generically Combine 2 or more routines consecutively
« Reply #11 on: May 26, 2016, 10:22:49 AM »
WOO HOOO :uglystupid2: Do you ever just don't get it; and then  :idea: (the light bulb turns on in my head).

Thanks for putting up with me on this! I appreciate it a lot!
Civil3D 2020

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2140
  • class keyThumper<T>:ILazy<T>
Re: Generically Combine 2 or more routines consecutively
« Reply #12 on: May 26, 2016, 10:27:45 AM »

You're welcome.

It's an important concept ... great to hear you've grasped it.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.