Author Topic: Help with Macro: Set Current Layer + Start LISP  (Read 1010 times)

0 Members and 1 Guest are viewing this topic.

CEHill

  • Mosquito
  • Posts: 18
Help with Macro: Set Current Layer + Start LISP
« on: April 23, 2024, 09:05:25 AM »

GOAL:
Create a button macro that combines two actions.

Actions include:

1.) Set a layer (named Fastener) as current and

2.) Invokes an AutoLISP program that allows the user to select and insert a fastener. 

I have a basic understanding of button macro code. I am unsure of how to combine the two steps into one macro.

Thanks,
Clint

JohnK

  • Administrator
  • Seagull
  • Posts: 10674
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #1 on: April 23, 2024, 09:36:53 AM »
How would you do this "normally" (-i.e. macros, are made by just writing down the steps [commands] you'd type to accomplish a task)?

1. Type: "-la"
2. Type: "set"
3. Type: "Fastner"
4. Type: <ENTER>
5. Type: "-i"
...
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CEHill

  • Mosquito
  • Posts: 18
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #2 on: April 23, 2024, 09:52:44 AM »
Of course.

So, the portion of the code that invokes the LISP part of the code would be ^C^C(C:FAST)  ???


C.

JohnK

  • Administrator
  • Seagull
  • Posts: 10674
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #3 on: April 23, 2024, 10:04:02 AM »
^c^c means: cancel, cancel (or escape, escape).

(c:fast) means what?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CEHill

  • Mosquito
  • Posts: 18
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #4 on: April 23, 2024, 10:29:55 AM »
This code is an attempt to start the AutoLISP file, FAST.lsp, that was available years ago on the CADALYST website.

As supplied, the FAST.lsp code does not include code to create/set a specific layer.

To ensure that the output of the FAST.lsp is placed on the desired layer, I would like to create a button macro that

1.) Sets the FASTENER layer as current and

2.) Runs the FAST.lsp program.

The code successfully sets the FASTENER layer as current.

It does not invoke the AutoLISP program, FAST.lsp.

I need help with the AutoLISP portion below.

Code - Auto/Visual Lisp: [Select]
  1. ^C^C^C-Layer;S;FASTENER;;(C:FAST)

JohnK

  • Administrator
  • Seagull
  • Posts: 10674
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #5 on: April 23, 2024, 10:45:42 AM »
wait. I don't know anything about fast.lsp or how it works but adding in some "set a current layer" to a lisp is not that difficult and probably "easier" (more robust) to code because of all the situations you "should account for". For example: 1. What happens if the layer does not exist when you set it current? 2. What about setting the layer back to what it was before?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CEHill

  • Mosquito
  • Posts: 18
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #6 on: April 23, 2024, 10:51:44 AM »
I see and agree. This makes sense. My AutoLISP skills are much less than what I want them to be.

The program, FAST.lsp, is attached for your review as you can.

The desired layer to place the objects from this program is named FASTENER.   

JohnK

  • Administrator
  • Seagull
  • Posts: 10674
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #7 on: April 23, 2024, 11:16:42 AM »
Whoa!? Someone was swinging a sledgehammer when they wrote that; I would hate to have to maintain that code!

I'm going quick because I'm busy but here is a quick stab at adding in a layer for you. Comments added so you can make changes. Ask questions (but study these changes and understand what they do before just blindly asking questions).

Support function you'd need:

Place this in the lisp file.
Code - Auto/Visual Lisp: [Select]
  1. (defun make-or-modify-layer (LNAM$ LCLR# LTYP$ LWGT# #DESCR# / #prog)
  2.    ;;
  3.    ;; Ex:
  4.    ;; (make-or-modify-layer "Test-Layer" 1 "Continuous" 18 "TEST")
  5.    ;;  > create the layer
  6.    ;; (make-or-modify-layer "Test-Layer" 1 "Continuous" 18 "TEST")
  7.    ;;  > would override the current layer def.
  8.    ;;
  9.    ;; By: John K
  10.    ;;
  11.   (if (null (tblsearch "layer" LNAM$))
  12.     (set '#prog (lambda ( x ) (entmake x)))
  13.     (set '#prog (lambda (x / ent y)
  14.                   (setq ent (entget (tblobjname "LAYER" LNAM$)))
  15.                   (foreach y x
  16.                            (if (not (member (car y) '(0 100 2)))
  17.                              (setq ent (subst y (assoc (car y) ent) ent))))
  18.                   (entmod ent) )))
  19.   (#prog
  20.    (list
  21.      (cons 0   "LAYER")
  22.      (cons 100 "AcDbSymbolTableRecord")
  23.      (cons 100 "AcDbLayerTableRecord")
  24.      (cons 2    LNAM$)
  25.      (cons 6    LTYP$)
  26.      (cons 62   LCLR#)
  27.      (cons 70   0)
  28.      (cons 290  1)
  29.      (cons 370  LWGT#)
  30.      (list -3 (list "AcAecLayerStandard" '(1000 . "") (cons 1000 #DESCR#)))
  31.      )
  32.    )
  33.  (princ)
  34.  )

Make the edits:

In the function c:fast you will need to add a line to create the layer.
Code - Auto/Visual Lisp: [Select]
  1. (defun C:FAST (/ newtile size which_view display_list index w thread
  2.                  shank lg td scale fast_dim_file)
  3.   (setq olderr *error*
  4.         *error* fast_err)
  5.   (setq os (getvar "OSMODE"))
  6.   (setq plw (getvar "PLINEWID"))
  7.   (setvar "PLINEWID" 0)
  8.   (setvar "OSMODE" 111)
  9.   (setvar "ISOLINES" 24)
  10.   (FAST_DIALOG)
  11.  
  12.   (setq oldlayer (getvar "CLAYER"))                     ; -Save the endusers current layer
  13.   (make-or-modify-layer "Fastner" 1 "Continuous" 18 "Faster Layer")     ; -make sure the fastner layer is created.
  14.   (setvar "CLAYER" "Fastner")                           ; -Set the fastner layer current.
  15.  
  16.   (DRAW_FAST)
  17.   (setvar "OSMODE" os)
  18.   (setvar "PLINEWID" plw)
  19.  
  20.   (setvar "CLAYER" oldlayer)                            ; -Restore the old layer
  21.  
  22.   (setq *error* olderr); Restore old *error* handler
  23.   (princ)
  24. );end FAST
  25.  

Then in the error routine, you need to make sure you restore the users layer if the program crashes.
Code - Auto/Visual Lisp: [Select]
  1. (defun FAST_ERR (msg)
  2.   (if (or (/= msg "Function cancelled") ; If an error (such as ESC) occurs
  3.           (= msg "quit / exit abort"))
  4.   (princ)
  5.   (princ (strcat "\nError: " msg))) ; while this command is active...
  6.   (setvar "OSMODE" os)              ; Restore saved modes
  7.   (setvar "PLINEWID" plw)
  8.   (setvar "CLAYER" oldlayer)
  9.   (setq *error* olderr)            ; Restore old *error* handler
  10.   (princ)
  11. );end FAST_ERR
  12.  
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CEHill

  • Mosquito
  • Posts: 18
Re: Help with Macro: Set Current Layer + Start LISP
« Reply #8 on: April 23, 2024, 11:18:22 AM »
MANY thanks to you! I will commence my studies.