Jump to content

Need help with writing my first LISP routine


jfbrubaker1969

Recommended Posts

I have been using AutoCAD for a long time and I have used a bunch of LISP routines over the years, but I've always been able to find one that met my needs at that particular time. Because of this I've never put the time into learning about how to write a LISP routine. Now I find myself in need of a custom LISP routine and I could use some help. I've been doing a bunch of research on the web and I believe I have come up with most of what I need, but I'm having some trouble with one part and need some assistance.

 

I do some custom cutting with a laser engraver and the laser cuts in the order that objects are created in AutoCAD. With this LISP, I hope to select and create new objects in the order that I want the laser to process them (in theory).

 

First I will define what I need the LISP routine to do:

 

step 1. select an object

step 2. create a copy of the object at the exact same location as original object

step 3. delete original (old) object

step 4. change color of new object (to red for ex.)

step 5. repeat as necessary (loop)

 

Now for the code I believe I have figured out so far:

 

(defun c:CopyObj ()

 

(vl-load-com)

 

;reference to the Utilities Object :

(setq util (vla-get-utility

(vla-get-activedocument

(vlax-get-acad-object))))

 

;select the object

(vla-getentity util 'OldObj 'ip "\nSelect Object: ")

 

;Define the base point

(setq BasePt (vlax-3d-point 0 0 0))

 

;copy the object

(setq NewObj (vla-copy OldObj))

 

;move the new object to the same location as the original

(vla-move newobj BasePt BasePt)

 

;delete old object

(vla-delete OldObj)

 

;change color of new object

 

;loop

 

(princ)

 

);defun

 

(princ)

Link to comment
Share on other sites

Using vanilla lisp:

 

(defun c:CopyObj (/ ent)
 (while
   (setq ent (entsel "\nSelect Object: "))
   (command "_.copy" ent "" "0,0" "0,0"
            "_.erase" ent ""
            "change" "l" "" "p" "c" "red" "") ;_ end of command
 ) ;_ end of while
 (princ)
)

Link to comment
Share on other sites

vl version

;change color of new object
(setq obj (vlax-ename->vla-object (entlast)))
(vla-put-color obj x)  ; x 1-254
or
(vla-put-color (vlax-ename->vla-object (entlast)) x)

 

Re order have you thought about changing Z value of objects to use that for the order method in conjunction with a lisp. This works for stuff like HIDE and sometimes the draw order.

Link to comment
Share on other sites

Interesting route you took with the GetEntity method of the utility object, normally the entsel function is used.

Now to elaborate from your starting point, heres a commented example:

 


[b][color=BLACK]([/color][/b]defun C:test [b][color=FUCHSIA]([/color][/b] / obj pt copied_obj col [b][color=FUCHSIA])[/color][/b] [color=#8b4513]; define a function and localise all the symbols we used[/color]
 [color=#8b4513]; step 1. select an object[/color]
 [b][color=FUCHSIA]([/color][/b]vla-GetEntity [color=#8b4513]; invoke the 'GetEntity' method[/color]
   [b][color=NAVY]([/color][/b]vla-get-Utility [b][color=MAROON]([/color][/b]vla-get-ActiveDocument [b][color=GREEN]([/color][/b]vlax-get-acad-object[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b] [color=#8b4513]; object from which we invoke the method [b][color=NAVY]([/color][/b]in this case its the 'Utility' object[b][color=NAVY])[/color][/b][/color]
   'obj [color=#8b4513]; supply a symbol that will store the potentially selected object[/color]
   'pt [color=#8b4513]; supply a symbol that will store the picked point if theres selected object[/color]
   [color=#2f4f4f]"\nSelect Object: "[/color] [color=#8b4513]; [Optional] Prompt for the user[/color]
 [b][color=FUCHSIA])[/color][/b][color=#8b4513]; vla-GetEntity[/color]
 
 [color=#8b4513]; step 2. create a copy of the object at the exact same location as original object[/color]
 [b][color=FUCHSIA]([/color][/b]setq copied_obj [color=#8b4513]; store the copied object into the 'copied_obj' symbol[/color]
   [b][color=NAVY]([/color][/b]vla-Copy [color=#8b4513]; invoke the 'Copy' method[/color]
     obj [color=#8b4513]; now we use the stored/selected object to be copied[/color]
   [b][color=NAVY])[/color][/b][color=#8b4513]; vla-Copy[/color]
 [b][color=FUCHSIA])[/color][/b][color=#8b4513]; setq[/color]
 
 [color=#8b4513]; step 3. delete original [b][color=FUCHSIA]([/color][/b]old[b][color=FUCHSIA])[/color][/b] object[/color]
 [b][color=FUCHSIA]([/color][/b]vla-Delete [color=#8b4513]; invoke the 'Delete' method[/color]
   obj [color=#8b4513]; the stored/selected object will be erased[/color]
 [b][color=FUCHSIA])[/color][/b]
 
 [color=#8b4513]; step 4. change color of new object [b][color=FUCHSIA]([/color][/b]to red for ex.[b][color=FUCHSIA])[/color][/b]<-this is where I need help[/color]
 [b][color=FUCHSIA]([/color][/b]setq col [b][color=NAVY]([/color][/b]acad_colordlg 256 T[b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b] [color=#8b4513]; First lets prompt the user to choose his color[/color]
 [b][color=FUCHSIA]([/color][/b]vla-put-Color copied_obj col[b][color=FUCHSIA])[/color][/b]
 
 [b][color=FUCHSIA]([/color][/b]princ[b][color=FUCHSIA])[/color][/b] [color=#8b4513]; Exit cleanly[/color]
[b][color=BLACK])[/color][/b][color=#8b4513]; defun C:test[/color]
[b][color=BLACK]([/color][/b]vl-load-com[b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]princ[b][color=BLACK])[/color][/b] [color=#8b4513]; load visual lisp extensions and supress any prompts[/color]

[color=#8b4513]; Description for the [b][color=BLACK]([/color][/b]GetEntity[b][color=BLACK])[/color][/b] method:[/color]
[color=#8b4513]; object.GetEntity Object, PickedPoint [, Prompt][/color]
[color=#8b4513]; Example:[/color]
[color=#8b4513]; [b][color=BLACK]([/color][/b]vla-getentity util 'obj 'ip [color=#2f4f4f]"\nSelect Object: "[/color][b][color=BLACK])[/color][/b][/color]

[color=#8b4513]; Description for the [b][color=BLACK]([/color][/b]acad_colordlg[b][color=BLACK])[/color][/b] function:[/color]
[color=#8b4513]; [b][color=BLACK]([/color][/b]acad_colordlg colornum [flag][b][color=BLACK])[/color][/b][/color]
[color=#8b4513]; colornum ; Type: Integer ; An integer in the range 0-256 [b][color=BLACK]([/color][/b]inclusive[b][color=BLACK])[/color][/b], specifying the AutoCAD color number to display as the initial default. ; A colornum value of 0 defaults to ByBlock, and a value of 256 defaults to ByLayer.[/color]
[color=#8b4513]; flag ; Type: T or nil ; If set to nil, disables the ByLayer and ByBlock buttons. Omitting the flag argument or setting it to a non-nil value enables the ByLayer and ByBlock buttons.[/color]

 

Note that when you miss the object while trying to select it, the code will error out. On the other side to not delve on complexity I purposely avoided adding any conditionals that would prevent the other possible errors that will occur(aswell your understanding of the example).

 

However here is error-free approach that uses GetEntity method, since you got me started:

 


[b][color=BLACK]([/color][/b]defun C:test [b][color=FUCHSIA]([/color][/b] / _GetEntity util o n c [b][color=FUCHSIA])[/color][/b]
 [color=#8b4513]; [b][color=FUCHSIA]([/color][/b]_GetEntity nil [color=#2f4f4f]"\nSelect Object <exit>: "[/color][b][color=FUCHSIA])[/color][/b][/color]
 [b][color=FUCHSIA]([/color][/b]defun _GetEntity [b][color=NAVY]([/color][/b] util msg / o p r [b][color=NAVY])[/color][/b] [color=#8b4513]; you might ask why not use [b][color=NAVY]([/color][/b]entsel msg[b][color=NAVY])[/color][/b] - I wrote this for practice, to 'utilise' the [b][color=NAVY]([/color][/b]GetEntity[b][color=NAVY])[/color][/b] method[/color]
   [b][color=NAVY]([/color][/b]or util [b][color=MAROON]([/color][/b]setq util [b][color=GREEN]([/color][/b]vla-get-Utility [b][color=BLUE]([/color][/b]vla-get-ActiveDocument [b][color=RED]([/color][/b]vlax-get-acad-object[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]cond
     [b][color=MAROON]([/color][/b] [b][color=GREEN]([/color][/b]not [b][color=BLUE]([/color][/b]and [b][color=RED]([/color][/b]eq 'VLA-OBJECT [b][color=PURPLE]([/color][/b]type util[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b] [b][color=RED]([/color][/b]vlax-method-applicable-p util 'GetEntity[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b] [b][color=GREEN]([/color][/b]prompt [color=#2f4f4f]"\nUnable to interfere with the utility object."[/color][b][color=GREEN])[/color][/b] [b][color=MAROON])[/color][/b]
     [b][color=MAROON]([/color][/b] [b][color=GREEN]([/color][/b]setvar 'errno 0[b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]while [b][color=BLUE]([/color][/b]/= 52 [b][color=RED]([/color][/b]getvar 'errno[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
         [b][color=BLUE]([/color][/b]or
           [b][color=RED]([/color][/b]and [b][color=PURPLE]([/color][/b]vl-catch-all-error-p [b][color=TEAL]([/color][/b]vl-catch-all-apply 'vla-GetEntity [b][color=OLIVE]([/color][/b]list util 'o 'p msg[b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]princ [color=#2f4f4f]"\nMissed, try again"[/color][b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
           [b][color=RED]([/color][/b]and o [b][color=PURPLE]([/color][/b]setq r [b][color=TEAL]([/color][/b]list o [b][color=OLIVE]([/color][/b]vlax-safearray->list p[b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b] [b][color=PURPLE]([/color][/b]setvar 'errno 52[b][color=PURPLE])[/color][/b][b][color=RED])[/color][/b]
         [b][color=BLUE])[/color][/b][color=#8b4513]; or[/color]
       [b][color=GREEN])[/color][/b][color=#8b4513]; while[/color]
     [b][color=MAROON])[/color][/b]
   [b][color=NAVY])[/color][/b][color=#8b4513]; cond[/color]
   r
 [b][color=FUCHSIA])[/color][/b][color=#8b4513]; defun _GetEntity[/color]
 
 [b][color=FUCHSIA]([/color][/b]if [b][color=NAVY]([/color][/b]setq util [b][color=MAROON]([/color][/b]vla-get-Utility [b][color=GREEN]([/color][/b]vla-get-ActiveDocument [b][color=BLUE]([/color][/b]vlax-get-acad-object[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
   [b][color=NAVY]([/color][/b]while 
     [b][color=MAROON]([/color][/b]and 
       [b][color=GREEN]([/color][/b]setq o [b][color=BLUE]([/color][/b]car [b][color=RED]([/color][/b]_GetEntity util [color=#2f4f4f]"\nSelect Object <exit>: "[/color][b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]vlax-write-enabled-p o[b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]setq n [b][color=BLUE]([/color][/b]vla-Copy o[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
       [b][color=GREEN]([/color][/b]setq c [b][color=BLUE]([/color][/b]acad_colordlg 256 T[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
     [b][color=MAROON])[/color][/b][color=#8b4513]; and[/color]
     [b][color=MAROON]([/color][/b]vla-put-Color n c[b][color=MAROON])[/color][/b] [b][color=MAROON]([/color][/b]vla-Delete o[b][color=MAROON])[/color][/b]
   [b][color=NAVY])[/color][/b][color=#8b4513]; while[/color]
   [b][color=NAVY]([/color][/b]princ [color=#2f4f4f]"\nSorry."[/color][b][color=NAVY])[/color][/b]
 [b][color=FUCHSIA])[/color][/b][color=#8b4513]; if[/color]
 [b][color=FUCHSIA]([/color][/b]princ[b][color=FUCHSIA])[/color][/b]
[b][color=BLACK])[/color][/b][color=#8b4513]; defun[/color]
[b][color=BLACK]([/color][/b]vl-load-com[b][color=BLACK])[/color][/b] [b][color=BLACK]([/color][/b]princ[b][color=BLACK])[/color][/b]

 

Other's suggestions might be shorter and effective, I've just found here something unusual/new for me.

Please don't leave with the impression that I'm somewhat scientific guy, after reading my post. :geek: :lol:

Link to comment
Share on other sites

Other's suggestions might be shorter and effective, I've just found here something unusual/new for me.

Please don't leave with the impression that I'm somewhat scientific guy, after reading my post. :geek: :lol:

 

Let me guess Grrr, you're a fan of dr. Who 'go and tell the others the doctor is back and tell them I took the long way around' ... but certainly its a novel / different (refreshing?) approach :D

 

nice coding bro!

 

gr. Rlx

Link to comment
Share on other sites

Let me guess Grrr, you're a fan of dr. Who 'go and tell the others the doctor is back and tell them I took the long way around' ... but certainly its a novel / different (refreshing?) approach :D

 

nice coding bro!

 

gr. Rlx

 

Thanks Rlx, for me its a pleasure to explore the alternative ways to write different parts from the code. :)

As long I understand whats happening ofcourse... because we're all aware of the mind-boggling guys group.

Link to comment
Share on other sites

Hi Phillip, thanks again. Your code was exactly whit I was looking to do and it works great! As I had mentioned in my original post, I do some cutting on a laser engraver and being able to organize the parts in the order I want them to cut has really helped cut down on the time it takes. I guess because Autocad processes object in the order they are created when plotting, that's the order that the vectors are sent to the laser.

 

My thanks to everyone else as well, I will be studying the additional code that was posted so that I can learn as much as I can!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...