Author Topic: Lisp Help: finding last point and inserting block at it  (Read 1238 times)

0 Members and 1 Guest are viewing this topic.

AVCAD

  • Newt
  • Posts: 29
Lisp Help: finding last point and inserting block at it
« on: July 06, 2023, 05:45:12 PM »
Hey all...

wrote this list...ugly as it as is it works....but I need to the last polyline point to be a variable so i can insert a block at it...

so far this will find the second point i select but not the last if i have more then 2 points.
Code - Auto/Visual Lisp: [Select]
  1. (defun varget ()
  2.   (setq lis '("orthomode"))
  3.   (setq var (mapcar 'getvar lis))
  4.   (setq var1 '(1))
  5.   (setq no 0)
  6.   (repeat (length lis)
  7.     (setvar (nth no lis) (nth no var1))
  8.     (setq no (1+ no))
  9.   )
  10.   (princ)
  11. )
  12.  
  13. (defun varset ()
  14.   ;;reset system variables above to 0
  15.   (setq no 0)
  16.   (repeat (length lis)
  17.     (setvar (nth no lis) (nth no var))
  18.     (setq no (1+ no))
  19.   )
  20.   (princ)
  21. )
  22.  
  23.  
  24. (defun C:wire2 (/      pt1    pt2    pt3    blklay blkname       lay1
  25.                 lay2   lay3   lay4   lay5   lay6   lay7   lay8
  26.                 prevlayer     P      LN     CON
  27.                )
  28.   (progn
  29.     (varget)
  30.  
  31.     (setq blkname "C:/xxxx/xxxx/xxxx.dwg")
  32.  
  33.     (setq lay1 "1_AV_LINE-VIDEO")
  34.     (setq lay2 "1_AV_LINE-AUDIO")
  35.     (setq lay3 "1_AV_LINE-COMM")
  36.     (setq lay4 "1_AV_LINE-COAX")
  37.     (setq lay5 "1_AV_LINE-CONTROL")
  38.     (setq lay6 "1_AV_LINE-ETHERNET")
  39.     (setq lay7 "1_AV_LINE-POWER")
  40.     (setq lay8 "1_AV_LINE-FIBER")
  41.  
  42.     (setq prevlayer (getvar "clayer"))
  43.     (setq P (getstring
  44.               "Audio(A)/Video(V)/Control(C)/Ethernet(E)/Power(P):"
  45.             )
  46.     )
  47.  
  48.     (command "SNAP" "2.0")
  49.  
  50.     (setq pt1 (getpoint "\nSelect Starting Point:"))
  51.     (IF (= P "V")
  52.       (command "-LAYER" "M" lay1 "C" "84" "" "" "_.pline" pt1 PAUSE)
  53.     )
  54.     (setq pt2 (getvar "lastpoint"))
  55.     (setq LN (entlast))
  56.     (IF (= P "A")
  57.       (command "-LAYER" "M" lay2 "C" "150" "" "" "_.pline" pt1 PAUSE)
  58.     )
  59.     (setq pt2 (getvar "lastpoint"))
  60.     (setq LN (entlast))
  61.     (IF (= P "CO")
  62.       (command "-LAYER" "M" lay3 "C" "206" "" "" "_.pline" pt1 PAUSE)
  63.     )
  64.     (setq pt2 (getvar "lastpoint"))
  65.     (setq LN (entlast))
  66.     (IF (= P "R")
  67.       (command "-LAYER" "M" lay4 "C" "44" "" "" "_.pline" pt1 PAUSE)
  68.     )
  69.     (setq pt2 (getvar "lastpoint"))
  70.     (setq LN (entlast))
  71.     (IF (= P "C")
  72.       (command "-LAYER" "M" lay5 "C" "14" "" "" "_.pline" pt1 PAUSE)
  73.     )
  74.     (setq pt2 (getvar "lastpoint"))
  75.     (setq LN (entlast))
  76.     (IF (= P "E")
  77.       (command "-LAYER" "M" lay6 "C" "144" "" "" "_.pline" pt1 PAUSE)
  78.     )
  79.     (setq pt2 (getvar "lastpoint"))
  80.     (setq LN (entlast))
  81.     (IF (= P "P")
  82.       (command "-LAYER" "M" lay7 "C" "200" "" "" "_.pline" pt1 PAUSE)
  83.     )
  84.     (setq pt2 (getvar "lastpoint"))
  85.     (setq LN (entlast))
  86.     (IF (= P "F")
  87.       (command "-LAYER" "M" lay8 "C" "30" "" "" "_.pline" pt1 PAUSE)
  88.     )
  89.     (setq pt2 (getvar "lastpoint"))
  90.     (setq LN (entlast))
  91.     (IF (= P "V")
  92.       (setq blklay "1_AV_LINE-VIDEO")
  93.     )
  94.     (IF (= P "A")
  95.       (setq blklay "1_AV_LINE-AUDIO")
  96.     )
  97.     (IF (= P "CO")
  98.       (setq blklay "1_AV_LINE-COMM")
  99.     )
  100.     (IF (= P "R")
  101.       (setq blklay "1_AV_LINE-COAX")
  102.     )
  103.     (IF (= P "C")
  104.       (setq blklay "1_AV_LINE-CONTROL")
  105.     )
  106.     (IF (= P "E")
  107.       (setq blklay "1_AV_LINE-ETHERNET")
  108.     )
  109.     (IF (= P "P")
  110.       (setq blklay "1_AV_LINE-POWER")
  111.     )
  112.     (IF (= P "F")
  113.       (setq blklay "1_AV_LINE-FIBER")
  114.     )
  115.     (while (> (getvar "CMDACTIVE") 0) (command "\\"))
  116.     ;;
  117.     ;;inserts 1st block at start of line
  118.     ;;
  119.     (command "attreq" "0")
  120.     (command "break" LN pt1 "@12.0<0")
  121.     (command "-insert" blkname pt1 "" "" "")
  122.     (setq CON (entlast))
  123.     (command "move" CON "" pt1 "@12.0<0")
  124.     (command "EXPLODE" CON nil)
  125.     (command "_.chprop" (ENTLAST) "" "LA" blklay "")
  126.     (command "attreq" "1")
  127.     ;;
  128.     ;;inserts 2nd block at end of line
  129.     ;;
  130.     (command "break" LN pt2 "@12.0<180")
  131.     (command "attreq" "0")
  132.     (command "-insert" blkname pt2 "" "" "")
  133.     (setq CON (entlast))
  134.                                         ;(command "move" CON "" pt2 "@12.0<180")
  135.     (command "EXPLODE" CON nil)
  136.     (command "_.chprop" (ENTLAST) "" "LA" blklay "")
  137.     (command "attreq" "1")
  138.  
  139.     (varset)
  140.     (setvar "clayer" prevlayer)
  141.     (princ)
  142.   )
  143. )


This is basically drawings a line and putting a block at the start point and one at the end point. I just cant get that end point.

Any help would be greatly appreciated!

Thanks in advance!

edit-kdub: add AutoLisp code tag to code pane
« Last Edit: July 06, 2023, 06:34:44 PM by kdub_nz »

mhupp

  • Bull Frog
  • Posts: 250
Re: Lisp Help: finding last point and inserting block at it
« Reply #1 on: July 06, 2023, 07:20:30 PM »
Try this. it will have you draw the polyline first, then go back and pull points of each vertex and place the block on them. also using Cond instead of if
https://www.afralisp.net/autolisp/tutorials/cond-vs-if.php

Code - Auto/Visual Lisp: [Select]
  1. (defun C:WIRE2 (/ blkname prevlayer rep)
  2.   (progn
  3.     (varget)
  4.     ;save listed variables values as a list to restore later.
  5.     ;(setq lst (list "clayer" "osnap" "osmode")
  6.     ;      cur (mapcar 'getvar lst)
  7.     ;)
  8.     (setq blkname "C:/xxxx/xxxx/xxxx.dwg")
  9.     (setq prevlayer (getvar "clayer"))
  10.     (initget "Video Audio Coax Comm Control Ethernet Power")
  11.     (setq rep ;will only accept words above
  12.       (cond     ;also defaults to Video so you can just hit enter if that is your choice
  13.         ((getkword "\n[V]ideo/(A)udio/(Co)Comm/(Cx)Coax/(C)ontrol/(E)thernet/(P)ower: ")) ("Video")
  14.       )
  15.     )
  16.     (prompt "\nDraw Polyline Blocks will be added after")
  17.     (cond
  18.       ((= rep "Video")
  19.         (command "-LAYER" "M" "1_AV_LINE-VIDEO" "C" "84" "" "")
  20.       )
  21.       ((= rep "Audio")
  22.         (command "-LAYER" "M" "1_AV_LINE-AUDIO" "C" "150" "" "")
  23.       )
  24.       ((= rep "Comm")
  25.         (command "-LAYER" "M" "1_AV_LINE-COMM" "C" "206" "" "")
  26.       )
  27.       ((= rep "Coax")
  28.         (command "-LAYER" "M" "1_AV_LINE-COAX" "C" "44" "" "")
  29.       )
  30.       ((= rep "Control")
  31.         (command "-LAYER" "M" "1_AV_LINE-CONTROL" "C" "14" "" "")
  32.       )
  33.       ((= rep "Ethernet")
  34.         (command "-LAYER" "M" "1_AV_LINE-ETHERNET" "C" "144" "" "")
  35.       )
  36.       ((= rep "Power")
  37.         (command "-LAYER" "M" "1_AV_LINE-POWER" "C" "200" "" "")
  38.       )
  39.       ((= rep "Fiber")
  40.         (command "-LAYER" "M" "1_AV_LINE-FIBER" "C" "30" "" "")
  41.       )
  42.     )
  43.     (command "_.pline")
  44.     (if (setq coords (vl-remove-if 'not (mapcar (function (lambda (p) (if (= 10 (car p)) (cdr p)))) (entget (entlast)))))
  45.       (foreach pt Coords
  46.         (command "_.Insert" (strcat "*" blkname) pt "" "" "") ;* means it will explode block when inserted
  47.       )
  48.     )
  49.     ; I don't know what you where doing with break and move took that out.
  50.     (varset)
  51.     ;(mapcar 'setvar lst cur) ;restores listed variables from start of lisp.
  52.     (princ)
  53.   )  ;Progn
  54. )    ;defun

-edit fixed typo
« Last Edit: July 07, 2023, 09:08:55 PM by mhupp »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2141
  • class keyThumper<T>:ILazy<T>
Re: Lisp Help: finding last point and inserting block at it
« Reply #2 on: July 06, 2023, 07:24:09 PM »
Perhaps try something like this.

first:
revise your assignment for var 'p' so the value is upperCase.

next:
create a separate function to 'make' the correct layer.
( seperation of concerns will make the core code easier to read and debug. )

next:
create a serarate function to draw the pline.
( same reason )

this will give you ONE location th assign a value to pt1 and pt2. from their it's a piece of cake :)

regards.

PS: I won't provide code 'cause you will learn more from modifying your own code.
and you have some stuff in there where I'm ot really sure of your intent.

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.

AVCAD

  • Newt
  • Posts: 29
Re: Lisp Help: finding last point and inserting block at it
« Reply #3 on: July 07, 2023, 11:38:53 AM »
Thanks for the input.

I tried the code above and it just says no function =REP.


I tried to rework some of my code and it looks like i got it work.

here is what i did, may not be pretty but works

Code: [Select]
(defun C:cwire (/ pt1 pt2 pt3 blklay blkname lay1 lay2 lay3 lay4 lay5 lay6 lay7 lay8 prevlayer prevsnapmode prevortho P LN CON firstpoint lastpoint)
(progn

;sets block being inserted path
(setq blkname "C:/xxxx/xxxx/xxxx.dwg")

;sets layer names to be recalled
(setq lay1 "1_AV_LINE-VIDEO")
(setq lay2 "1_AV_LINE-AUDIO")
(setq lay3 "1_AV_LINE-COMM")
(setq lay4 "1_AV_LINE-COAX")
(setq lay5 "1_AV_LINE-CONTROL")
(setq lay6 "1_AV_LINE-ETHERNET")
(setq lay7 "1_AV_LINE-POWER")
(setq lay8 "1_AV_LINE-FIBER")

;Sets system current settings to a variable to be recalled
(setq prevlayer (getvar "clayer"))
(setq prevsnapmode (getvar "osmode"))
(setq prevortho (getvar "orthomode"))

;sets system settings to SF requirements settings
(setvar "osmode" 0)
(setvar "orthomode" 1)
(command "SNAP" "2.0")

;gets what line type you want to use, will auto put on correct layer
(setq P (getstring "Audio(A)/Video(V)/Control(C)/Ethernet(E)/Power(P):"))

;runs command to make layer and draw line
;starting point
(setq pt1 (getpoint "\nSelect Starting Point:"))

;sets layer drawings line
(IF (or (= P "V")(= P "v"))(command "-LAYER" "M" lay1 "C" "84" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "A")(= P "a"))(command "-LAYER" "M" lay2 "C" "150" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "CO")(= P "co"))(command "-LAYER" "M" lay3 "C" "206" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "R")(= P "r"))(command "-LAYER" "M" lay4 "C" "44" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "C")(= P "c"))(command "-LAYER" "M" lay5 "C" "14" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "E")(= P "e"))(command "-LAYER" "M" lay6 "C" "144" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "P")(= P "p"))(command "-LAYER" "M" lay7 "C" "200" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "F")(= P "f"))(command "-LAYER" "M" lay8 "C" "30" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))

;set layer for block to be inserted on
(IF (or (= P "V")(= P "v"))(setq blklay "1_AV_LINE-VIDEO"))
(IF (or (= P "A")(= P "a"))(setq blklay "1_AV_LINE-AUDIO"))
(IF (or (= P "CO")(= P "co"))(setq blklay "1_AV_LINE-COMM"))
(IF (or (= P "R")(= P "r"))(setq blklay "1_AV_LINE-COAX"))
(IF (or (= P "C")(= P "c"))(setq blklay "1_AV_LINE-CONTROL"))
(IF (or (= P "E")(= P "e"))(setq blklay "1_AV_LINE-ETHERNET"))
(IF (or (= P "P")(= P "p"))(setq blklay "1_AV_LINE-POWER"))
(IF (or (= P "F")(= P "f"))(setq blklay "1_AV_LINE-FIBER"))
(while (> (getvar "CMDACTIVE") 0) (command "\\"))
[b][i](setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)))))
(setq firstpoint (nth 0 vertex_lst))
(setq lastpoint  (nth (- (length vertex_lst) 1) vertex_lst))[/i][/b]

;;inserts 1st block at start of line
(command "attreq" "0")
(command "break" LN firstpoint "@12.0<0")
(command "_.Insert" (strcat "*" blkname) firstpoint "" "")
(setq CON (entlast))
(command "move" CON "" firstpoint "@12.0<0")
(command "_.chprop" CON "" "LA" blklay "")
(command "attreq" "1")

;;inserts 2nd block at end of line
(command "attreq" "0")
(command "break" LN lastpoint "@12.0<180")
(command "_.Insert" (strcat "*" blkname) lastpoint "" "")
(setq CON (entlast))
(command "_.chprop" CON "" "LA" blklay "")
(command "attreq" "1")

;resets users current layer snapmode and ortho to the settings that were current prior to running command
(setvar 'clayer prevlayer)
(setvar 'osmode prevsnapmode)
(setvar 'orthomode prevortho)

(princ)
);Progn
 );defun


JohnK

  • Administrator
  • Seagull
  • Posts: 10651
Re: Lisp Help: finding last point and inserting block at it
« Reply #4 on: July 07, 2023, 12:37:53 PM »
You are getting that errror because mhupp had a few typos in his code (simple fix).

Code - Auto/Visual Lisp: [Select]
  1. ...
  2.    ((= rep "Video")
  3.     (command "-LAYER" "M" "1_AV_LINE-VIDEO" "C" "84" "" ""))
  4.    ((= rep "Audio")
  5.     (command "-LAYER" "M" "1_AV_LINE-AUDIO" "C" "150" "" ""))
  6.    ((= rep "Comm")
  7.     (command "-LAYER" "M" "1_AV_LINE-COMM" "C" "206" "" ""))
  8. ...

However, kdub_nz suggested you try to seperate out your functions (good advice). See the following for a simple example:
https://www.theswamp.org/index.php?topic=58149.msg613580#msg613580
If you design your programs like this (break them up into small chunks of operations) the "main program"--which just assembles those chunks into a logical pattern--is so much easier.

That-being-said, you have some syntax issues with your code; for example, with the "SETS LAYER DRAWING LINE" section.
The satement "(SETQ LN (ENTLAST))" will be issued many times because those statements are not related to the IF statement(s)).

I would really attempt to create separate functions to help you lay-out the program easier!
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10651
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

AVCAD

  • Newt
  • Posts: 29
Re: Lisp Help: finding last point and inserting block at it
« Reply #6 on: July 11, 2023, 10:06:16 AM »
OK so I tried to rework some of this in to function programs to call up instead of just running everything. This code works great for what I want it to do. The only portion i cant get to work is trimming or breaking the line that is drawn at the 3rd and 4th blocks being inserted.

What this does is:
Clock button (or run command), select what type of line (ie audio, video etc..), draw polyline (no nothing is in the z direction), trims line at the start point and end points, inserts connector block at start and end of line, inserts cable label block at 36 units from start and end point of line drawn, (this is where it doesnt work)..trim or break line at this block (block is 24 units total, 12 from mid point left and right)


Code: [Select]
(defun C:cwire (/ pt1 pt2 pt3 blklay blkname blkname2 lay1 lay2 lay3 lay4 lay5 lay6 lay7 lay8 prevlayer prevsnapmode prevortho P LN CON1 CON2 firstpoint lastpoint)
(setstuff)
(getstuff)
(dostuff)
(resetstuff)
(princ)
);defun

(progn

(defun setstuff ()
;sets block being inserted path
(setq blkname "C:/xxx/xxx/CONNECTOR.dwg")
(setq blkname2 "C:/xxx/xxx/WIRE LABEL.dwg")

;sets layer names to be recalled
(setq lay1 "1_AV_LINE-VIDEO")
(setq lay2 "1_AV_LINE-AUDIO")
;;(setq lay3 "1_AV_LINE-COMM")
;;(setq lay4 "1_AV_LINE-COAX")
(setq lay5 "1_AV_LINE-CONTROL")
(setq lay6 "1_AV_LINE-ETHERNET")
(setq lay7 "1_AV_LINE-POWER")
(setq lay8 "1_AV_LINE-FIBER")

;Sets system current settings to a variable to be recalled
(setq prevlayer (getvar "clayer"))
(setq prevsnapmode (getvar "osmode"))
(setq prevortho (getvar "orthomode"))

;sets system settings to SF requirements settings
(setvar "osmode" 0)
(setvar "orthomode" 1)
(command "SNAP" "2.0")
(princ)
)

(defun getstuff ()
;gets what line type you want to use, will auto put on correct layer
(setq P (getstring "Audio(A)/Video(V)/Control(C)/Ethernet(E)/Power(P):"))

;runs command to make layer and draw line
;starting point
(setq pt1 (getpoint "\nSelect Starting Point:"))

;sets layer drawings line
(IF (or (= P "V")(= P "v"))(command "-LAYER" "M" lay1 "C" "84" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "A")(= P "a"))(command "-LAYER" "M" lay2 "C" "150" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
;;(IF (or (= P "CO")(= P "co"))(command "-LAYER" "M" lay3 "C" "206" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
;;(IF (or (= P "R")(= P "r"))(command "-LAYER" "M" lay4 "C" "44" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "C")(= P "c"))(command "-LAYER" "M" lay5 "C" "14" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "E")(= P "e"))(command "-LAYER" "M" lay6 "C" "144" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "P")(= P "p"))(command "-LAYER" "M" lay7 "C" "200" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
(IF (or (= P "F")(= P "f"))(command "-LAYER" "M" lay8 "C" "30" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))

;set layer for block to be inserted on
(IF (or (= P "V")(= P "v"))(setq blklay "1_AV_LINE-VIDEO"))
(IF (or (= P "A")(= P "a"))(setq blklay "1_AV_LINE-AUDIO"))
;;(IF (or (= P "CO")(= P "co"))(setq blklay "1_AV_LINE-COMM"))
;;(IF (or (= P "R")(= P "r"))(setq blklay "1_AV_LINE-COAX"))
(IF (or (= P "C")(= P "c"))(setq blklay "1_AV_LINE-CONTROL"))
(IF (or (= P "E")(= P "e"))(setq blklay "1_AV_LINE-ETHERNET"))
(IF (or (= P "P")(= P "p"))(setq blklay "1_AV_LINE-POWER"))
(IF (or (= P "F")(= P "f"))(setq blklay "1_AV_LINE-FIBER"))
(while (> (getvar "CMDACTIVE") 0) (command "\\"))
(setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)))))
(setq firstpoint (nth 0 vertex_lst))
(setq lastpoint  (nth (- (length vertex_lst) 1) vertex_lst))
(princ)
)


(defun dostuff ()
;;inserts 1st block at start of line: CONNECTOR
(command "attreq" "0")
(command "break" LN firstpoint "@12.0<0")
(command "_.Insert" (strcat "*" blkname) firstpoint "" "")
(setq CON1 (entlast))
(command "move" CON1 "" firstpoint "@12.0<0")
(command "_.chprop" CON1 "" "LA" blklay "")
(command "attreq" "1")

;;inserts 2nd block at end of line: CONNECTOR
(command "attreq" "0")
(command "break" LN lastpoint "@12.0<180")
(command "_.Insert" (strcat "*" blkname) lastpoint "" "")
(setq CON2 (entlast))
(command "_.chprop" CON2 "" "LA" blklay "")
(command "attreq" "1")

;;inserts 3rd block at start of line: WIRE LABEL
(command "attreq" "0")
(command "_.Insert" (strcat "*" blkname2) firstpoint "" "")
(setq LBL1 (entlast))
(command "move" LBL1 "" firstpoint "@36.0<0")
(command "_.chprop" LBL1 "" "LA" blklay "")
(command "attreq" "1")

;;inserts 4th block at end of line: WIRE LABEL
(command "attreq" "0")
(command "_.Insert" (strcat "*" blkname2) lastpoint "" "")
(setq LBL2 (entlast))
(command "move" LBL2 "" firstpoint "@36.0<180")
(command "_.chprop" LBL2 "" "LA" blklay "")
(command "attreq" "1")
(princ)
)

(defun resetstuff ()
;resets users current layer snapmode and ortho to the settings that were current prior to running command
(setvar 'clayer prevlayer)
(setvar 'osmode prevsnapmode)
(setvar 'orthomode prevortho)
(princ)
)

(princ)
);Progn

Thanks for any help in advance! You guys/gals here are the best.

JohnK

  • Administrator
  • Seagull
  • Posts: 10651
Re: Lisp Help: finding last point and inserting block at it
« Reply #7 on: July 11, 2023, 11:09:25 AM »
haha ...nice function names. Very descriptive.

I would like to give you a bit of direction of how you structure your functions/code. The following are just a few sections from your `getstuff` function. This is how I would begin to split this up into little chunks. Play with these functions and understand what they do and allow you to NOT do (for example, try to BREAK the `getlinetypekeyword` and the `getpt` functions).

Please NOTE: This is not the most efficient way to code this operation, but it will give you a very good introduction and a formula for how to use the 'chunks' you make.

Code - Auto/Visual Lisp: [Select]
  1. (defun getpt (/ pt)
  2.   ;; getpt
  3.   ;; This function will prompt the user for a point until a valid point is picked.
  4.   ;;
  5.   ;; EX: (setq pt (getpt))
  6.   ;; RETURNS: point
  7.   (while
  8.     (not (setq pt (getpoint "\nSelect point: ")))
  9.     )
  10.   pt
  11.   )
  12.  
  13. (defun getlinetypekeyword ( )
  14.   ;; getlinetypekeyword
  15.   ;; This function will prompt user for a keyword to set the linetype.
  16.   ;;
  17.   ;; EX: (setq p (getlinetypekeyword))
  18.   ;; RETURNS: A keyword.
  19.   (initget 0 "Audio Video Control Ethernet Power")
  20.   (cond
  21.     ((getkword "\nAudio(A)/Video(V)/Control(C)/Ethernet(E)/Power(P) <Audio>: "))
  22.     ("Audio"))
  23.   )
  24.  
  25. (defun createlayer (name color)
  26.   ;; createlayer
  27.   ;; This function will create a layer based on name and color and set it current.
  28.   ;;
  29.   ;; EX: (createlayer "LAYER1" "1")
  30.   ;; RETURNS: nil
  31.   (command "_.layer" "m" name "c" color "" "")
  32.   (princ)
  33.  )
  34.  
  35.  
  36. (defun c:main-test ( / pt keywd)
  37.   ;; main-test
  38.   ;; This function will prompt the user for a point and...
  39.   ;;
  40.  
  41.   ;; ...
  42.  
  43.   ;; in the main function you start assembling the parts like this:
  44.   (setq pt (getpt)                                ; -Get a point from the enduser
  45.         keywd (getlinetypekeyword)                ; -Get a keyword from the enduser
  46.                                                   ;  which will be used to create a layer.
  47.         )
  48.  
  49.   (if (equal keywd "Video")
  50.     (createlayer "1_AV_LINE-VIDEO" "84"))         ; -Create a layer for `Video`.
  51.   (if (equal keywd "Audio")
  52.     (createlayer "1_AV_LINE-AUDIO" "150"))        ; -Create a layer for `Audio`.
  53.  
  54.   ;; ...
  55.   (princ)
  56.  )


EDIT: Removed unnecessary variable decl.
« Last Edit: July 12, 2023, 08:22:53 AM by JohnK »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

AVCAD

  • Newt
  • Posts: 29
Re: Lisp Help: finding last point and inserting block at it
« Reply #8 on: July 11, 2023, 12:25:34 PM »
Thanks John K

I will review this and see what I can do.

AVCAD

  • Newt
  • Posts: 29
Re: Lisp Help: finding last point and inserting block at it
« Reply #9 on: July 25, 2023, 04:55:09 PM »
Well, I got this to work...except i can only draw my lines from left to right...not a huge deal but someone will complain eventually....

I used LeeMac's Autobreak for this to work
http://lee-mac.com/autoblockbreak.html

I am loading his routine and then calling it up in my routine to trim my line to my blocks.

Yes Yes ugly code....but it works! I am sure someone can make it nicer and probably do this in way less code.

Thanks for the Help much appreciated.


Code - Auto/Visual Lisp: [Select]
  1. (defun C:cwire (/ pt1 pt2 pt3 blklay blkname blkname2 lay1 lay2 lay3 lay4 lay5 lay6 lay7 lay8 prevlayer prevsnapmode prevortho P LN CON1 CON2 firstpoint lastpoint)
  2. (setq echo (getvar 'cmdecho))
  3. (setvar 'cmdecho 0)
  4. (Needtoload)
  5. (setstuff)
  6. (getstuff)
  7. (dostuff)
  8. (resetstuff)
  9. (setvar 'cmdecho echo)
  10. );defun
  11.  
  12.  
  13. (defun needtoload ()
  14. (load "AutoBlockBreakV1-9")
  15. )
  16.  
  17. ;sets block being inserted path
  18. (defun setstuff ()
  19. (setq blkname "C:/AutoCAD - Custom/Lisps/CONNECTOR.dwg")
  20. (setq blkname2 "C:/AutoCAD - Custom/Lisps/WIRE LABEL.dwg")
  21.  
  22. ;sets layer names to be recalled
  23. (setq lay1 "1_AV_LINE-VIDEO")
  24. (setq lay2 "1_AV_LINE-AUDIO")
  25. ;;(setq lay3 "1_AV_LINE-COMM")---future
  26. ;;(setq lay4 "1_AV_LINE-COAX")---future
  27. (setq lay5 "1_AV_LINE-CONTROL")
  28. (setq lay6 "1_AV_LINE-ETHERNET")
  29. (setq lay7 "1_AV_LINE-POWER")
  30. (setq lay8 "1_AV_LINE-FIBER")
  31.  
  32. ;Sets system current settings to a variable to be recalled
  33. (setq prevlayer (getvar "clayer"))
  34. (setq prevsnapmode (getvar "osmode"))
  35. (setq prevortho (getvar "orthomode"))
  36.  
  37. ;sets system settings to SF requirements settings
  38. (setvar "osmode" 0)
  39. (setvar "orthomode" 1)
  40. (command "SNAP" "2.0")
  41. )
  42.  
  43. (defun getstuff ()
  44. ;gets what line type you want to use, will auto put on correct layer
  45. (setq P (getstring "Audio(A)/Video(V)/Control(C)/Ethernet(E)/Power(P):"))
  46.  
  47. ;runs command to make layer and draw line
  48. ;starting point
  49. (setq pt1 (getpoint "\nSelect Starting Point:"))
  50.  
  51. ;sets layer drawings line
  52. (IF (or (= P "V")(= P "v"))(command "-LAYER" "M" lay1 "C" "84" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  53. (IF (or (= P "A")(= P "a"))(command "-LAYER" "M" lay2 "C" "150" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  54. ;;(IF (or (= P "CO")(= P "co"))(command "-LAYER" "M" lay3 "C" "206" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))---future
  55. ;;(IF (or (= P "R")(= P "r"))(command "-LAYER" "M" lay4 "C" "44" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))----future
  56. (IF (or (= P "C")(= P "c"))(command "-LAYER" "M" lay5 "C" "14" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  57. (IF (or (= P "E")(= P "e"))(command "-LAYER" "M" lay6 "C" "144" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  58. (IF (or (= P "P")(= P "p"))(command "-LAYER" "M" lay7 "C" "200" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  59. (IF (or (= P "F")(= P "f"))(command "-LAYER" "M" lay8 "C" "30" "" "" "_.pline" pt1 PAUSE))(setq LN (entlast))
  60.  
  61. ;set layer for block to be inserted on
  62. (IF (or (= P "V")(= P "v"))(setq blklay "1_AV_LINE-VIDEO"))
  63. (IF (or (= P "A")(= P "a"))(setq blklay "1_AV_LINE-AUDIO"))
  64. ;;(IF (or (= P "CO")(= P "co"))(setq blklay "1_AV_LINE-COMM"))----future
  65. ;;(IF (or (= P "R")(= P "r"))(setq blklay "1_AV_LINE-COAX"))----future
  66. (IF (or (= P "C")(= P "c"))(setq blklay "1_AV_LINE-CONTROL"))
  67. (IF (or (= P "E")(= P "e"))(setq blklay "1_AV_LINE-ETHERNET"))
  68. (IF (or (= P "P")(= P "p"))(setq blklay "1_AV_LINE-POWER"))
  69. (IF (or (= P "F")(= P "f"))(setq blklay "1_AV_LINE-FIBER"))
  70. (while (> (getvar "CMDACTIVE") 0) (command "\\"))
  71. (setq vertex_lst (vl-remove nil (mapcar '(lambda (x) (if (= (car x) 10) (cdr x))) (entget (entlast)))))
  72. (setq firstpoint (nth 0 vertex_lst))
  73. (setq lastpoint  (nth (- (length vertex_lst) 1) vertex_lst))
  74. )
  75.  
  76. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77. ;;inserts 1st block at start of line: CONNECTOR;;;
  78. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  79. (defun dostuff (/ pt3)
  80. (command "attreq" "0")
  81. (command "break" LN firstpoint "@12.0<0")
  82. (command "_.Insert" (strcat "*" blkname) firstpoint "" "")
  83. (setq CON1 (entlast))
  84. (command "move" CON1 "" firstpoint "@12.0<0")
  85. (command "_.chprop" CON1 "" "LA" blklay "")
  86. (command "attreq" "1")
  87.  
  88. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  89. ;;inserts 2nd block at end of line: CONNECTOR;;;;;
  90. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  91. (command "attreq" "0")
  92. (command "break" LN lastpoint "@12.0<180")
  93. (command "_.Insert" (strcat "*" blkname) lastpoint "" "")
  94. (setq CON2 (entlast))
  95. (command "_.chprop" CON2 "" "LA" blklay "")
  96. (command "attreq" "1")
  97.  
  98. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  99. ;;inserts 3rd block at start of line: WIRE LABEL;;
  100. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  101. (command "attreq" "0")
  102. (command "_.Insert" (strcat "*" blkname2) firstpoint "" "")
  103. (setq LBL1 (entlast))
  104. (command "move" LBL1 "" firstpoint "@36.0<0")
  105. (command "_.chprop" LBL1 "" "LA" blklay "")
  106. (command "attreq" "1")
  107. (LM:AutoBlockBreak LBL1 t)
  108.  
  109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  110. ;;inserts 4th block at end of line: WIRE LABEL;;;;
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112. (command "attreq" "0")
  113. (command "_.Insert" (strcat "*" blkname2) lastpoint "" "")
  114. (setq LBL2 (entlast))
  115. (command "move" LBL2 "" firstpoint "@36.0<180")
  116. (command "_.chprop" LBL2 "" "LA" blklay "")
  117. (command "attreq" "1")
  118. (LM:AutoBlockBreak LBL2 t)
  119. )
  120.  
  121. (defun resetstuff ()
  122. ;resets users current layer snapmode and ortho to the settings that were current prior to running command
  123. (setvar 'clayer prevlayer)
  124. (setvar 'osmode prevsnapmode)
  125. (setvar 'orthomode prevortho)
  126. )
  127. )
  128.  
  129.