Jump to content

Please Help me with AutoLISP Programming


Mellie

Recommended Posts

Good morning,

 

I am trying to write a program in which will take the text "Road" (which is put in its own layer called Road) and move it to the right by 3791.4481.

 

I am also trying to take the text "Field" (which is put in its own layer called Field) and move it to the left by 3791.4481.

 

Below is the code:

 

 

(defun C: Melanie () ; define function

;Selects Road text and moves it to the Field text coordinates
 (setq Road (ssget "X" '(8."Road"))
(setq pt 1 '(0 0 0))
(setq pt 2 '(3791.4481 0 0))
(command "_.Move" Road "" pt1 pt2)

;Selects Field text and moves it to the Road text coordinates
(setq Field (ssget "X"'(8."Field"))
 (setq pt 3 '(0 0 0))
 (setq pt 4 '(-3791.4481 0 0))
 (command "_.Move" Field "" pt3 pt4)
 (princ)
 
(princ "Done")
  
 ))     ; End of condition statement that determines if cancel button has been pressed

)       ;end defun

 

 

Please help as when ever I try to load this program it doesn't work.

 

Kind regards,

 

Mellie

Edited by SLW210
Code Tags
Link to comment
Share on other sites

You should write :

 

(setq Road (ssget "X" '((8 . "Road"))))
(setq pt1 '(0 0 0))
(setq pt2 '(3791.4481 0 0))

and :

(setq Field (ssget "X" '((8 . "Field"))))
(setq pt3 '(0 0 0))
(setq pt4 '(-3791.4481 0 0))

Edited by SLW210
Code Tags
Link to comment
Share on other sites

Okay so I modified my code to the following:

 

(defun C: Melanie () ; define function
;Selects Road text and moves it to the Field text coordinates
 
(setq Road (ssget "X" '((8."Road"))))
(setq pt1 '(0 0 0))
(setq pt2 '(3791.4481 0 0))
(command "_.Move" Road "" pt1 pt2))
;Selects Field text and moves it to the Road text coordinates
(setq Field (ssget "X"'((8."Field"))))
 (setq pt3 '(0 0 0))
 (setq pt4 '(-3791.4481 0 0))
 (command "_.Move" Field "" pt3 pt4))
 (princ)
 
(princ "Done")
  
 ))     ; End of condition statement that determines if cancel button has been pressed

)       ;end defun

But I get the following error:

 

Command: (LOAD "C:/Users/patenaude/Desktop/Melanie.LSP") bad DEFUN syntax: (C: MELANIE nil (SETQ ROAD (SSGET "X" (QUOTE (8.0 "Road")))) (SETQ PT1 (QUOTE (0 0 0))) (SETQ PT2 (QUOTE (3791.45 0 0))) (COMMAND "_.Move" ROAD "" PT1 PT2))

 

What does this mean?

 

Thanks

 

Mellie

Edited by SLW210
Code Tags
Link to comment
Share on other sites

How would you make this code select the object automatically.

 

I have a text called Read in its own layer which is also called Read. How would I select that layer and move it to the right by 3100"?

 

I know if you use ssget "X" you could select everything in that layer and move it to the right by 3100 but I'm confused on how to write the program.

 

Please Help.

Link to comment
Share on other sites

I added the spaces that you mentioned and when I load my program autocad finally recognizes it but when I enter the Melanie Command and press enter, nothing happens.

Link to comment
Share on other sites

(defun c:movelayer ( / lay p1 p2 ss )
 (initget 1)
 (setq lay (getstring t "\nSpecify layer name : "))
 (initget 1)
 (setq p1 (getpoint "\nPick or specify start point : "))
 (initget 1)
 (setq p2 (getpoint p1 "\nPick or specify end point : "))
 (if (tblsearch "LAYER" lay)
   (if (/= 4 (cdr (assoc 70 (tblsearch "LAYER" lay))))
     (progn
       (setq ss (ssget "_X" (list (cons 8 lay))))
       (command "_.move" ss "" p1 p2)
     )
     (alert (strcat "Layer : \"" lay "\" is locked - unable to perform operation..."))
   )
   (alert (strcat "Layer : \"" lay "\" don't exist - unable to perform operation..."))
 )
 (princ)
)

 

HTH, M.R.

Link to comment
Share on other sites

I didn't want to re-write your code, so you could still understand it, but I made a couple of corrections, and it should run just fine. I would personally add some more checks in there to make sure it's doing everything correctly, but this should do for now.

 

I took out pt3 since you already defined 0,0,0. I also fixed your parenthesis. Lastly I put in a check to make sure there was something on that layer so it didn't error out if there wasn't.

 

(defun C:Melanie ( / road pt1 pt2 field pt4) ; define function
 ;Selects Road text and moves it to the Field text coordinates
 
 (setq Road (ssget "X" '((8 . "Road"))))
 (setq pt1 '(0 0 0))
 (setq pt2 '(3791.4481 0 0))
 (if road
   (command "_.Move" Road "" pt1 pt2));make sure there is something on that layer
 ;Selects Field text and moves it to the Road text coordinates
 (setq Field (ssget "X"'((8 . "Field"))))
 (setq pt4 '(-3791.4481 0 0))
 (if field
   (command "_.Move" Field "" pt1 pt4));make sure there is something on that layer
 
 
 (princ "Done")
 
 (princ)
 
 ) ;end defun

Link to comment
Share on other sites

Perfect thank you!!

 

The "if" statement is a great idea but what happens if there's nothing in the layer? Could there be a message box that appears that had an "OK" button? How you I go about that?

 

Kind regards

Link to comment
Share on other sites

That's great thank you but would it be possible to comment your code as I am just starting to understand Lisp and am still a beginner.

 

That would be really appreciated!

 

Kind regards

Link to comment
Share on other sites

Consider the following code:

([color=BLUE]defun[/color] c:movetextonlayer ( [color=BLUE]/[/color] sel )
   [color=GREEN];; Define function & declare local variables[/color]
   [color=GREEN];; See http://bit.ly/15Qw104 to understand why.[/color]

   ([color=BLUE]if[/color] [color=GREEN];; if the following expression returns a non-nil value[/color]
       ([color=BLUE]setq[/color] sel [color=GREEN];; Assign the return of the following expression to the symbol 'sel'[/color]
           ([color=BLUE]ssget[/color] [color=GREEN];; Retrieve a selection set[/color]
               [color=MAROON]"_X"[/color] [color=GREEN];; Iterate over the entire drawing database[/color]
               [color=GREEN];; Retrieve entities whose DXF data matches the following filter:[/color]
              '( 
                   (0 . [color=MAROON]"TEXT"[/color]) [color=GREEN];; Entity type of TEXT[/color]
                   (8 . [color=MAROON]"Read"[/color]) [color=GREEN];; Entity layer is 'Read'[/color]
               )
           ) [color=GREEN];; end ssget[/color]
       ) [color=GREEN];; end setq[/color]
       [color=GREEN];; THEN:[/color]
       ([color=BLUE]command[/color] [color=GREEN];; Evaluate the following expressions at the command-line[/color]
           [color=MAROON]"_.move"[/color] [color=GREEN];; Invoke the MOVE command (see http://bit.ly/18cyqA3 for command prefixes)[/color]
           sel [color=GREEN];; Selection Set to be moved[/color]
           [color=MAROON]""[/color] [color=GREEN];; 'Enter' to submit selection[/color]
           [color=MAROON]"_non"[/color] [color=GREEN];; Ignore any active Object Snaps for the next input[/color]
           '(0.0 0.0) [color=GREEN];; Use a base point of the origin[/color]
           [color=MAROON]"_non"[/color] [color=GREEN];; Ignore any active Object Snaps for the next input[/color]
           '(3100.0 0.0) [color=GREEN];; Move text 3100 in the positive x-direction[/color]
       ) [color=GREEN];; end command[/color]
       [color=GREEN];; ELSE:[/color]
       ([color=BLUE]princ[/color] [color=MAROON]"\nNo Text objects found on layer \"Read\"."[/color]) [color=GREEN];; Inform user that no objects were found[/color]
   ) [color=GREEN];; end if[/color]
   ([color=BLUE]princ[/color]) [color=GREEN];; Suppress the return of the last evaluated expression[/color]
) [color=GREEN];; end defun[/color]

Link to comment
Share on other sites

I am trying to write a condition statement for the above code that Commandobill posted.

 

I have this program that when ever you write the command Framing, it brings up a menu with a whole bunch of drop down menus. I am trying to get that piece of code to only occur when certain drawings are selected from a drop down menu in section "equipbotttomval". Is this possible? If so, how would I incorporate this into the following code:

 

(defun C:Melanie ( / road pt1 pt2 field pt4) ; define function
 ;Selects Road text and moves it to the Field text coordinates
 
 (setq Road (ssget "X" '((8 . "Road"))))
 (setq pt1 '(0 0 0))
 (setq pt2 '(3791.4481 0 0))
 (if road
   (command "_.Move" Road "" pt1 pt2));make sure there is something on that layer
 ;Selects Field text and moves it to the Road text coordinates
 (setq Field (ssget "X"'((8 . "Field"))))
 (setq pt4 '(-3791.4481 0 0))
 (if field
   (command "_.Move" Field "" pt1 pt4));make sure there is something on that layer
 
 
 (princ "Done")
 
 (princ)
 
 ) ;end defun

 

This is what I have so far regarding the condition statement:

 


 (cond
 ((or(= equipbottomval "04-1008035-STD")(= equipbottomval "04-1008077-STD")(= equipbottomval "04-1008156-STD"))))

 

 

 

 

Kind regards

Edited by Mellie
adding a question
Link to comment
Share on other sites

Add this part of code at end of LEE code and before

(princ)[/code/
[code]  (if (< 0 (setq sl (sslength sel)))
   (setq sl (rtos (sslength sel) 2 0))
   (setq sl "No Objects"))
 (alert (strcat "\nNumber of objects moved on layer \"Read\": " sl "  Object(s)"))

This will show the number of moved objects?

Link to comment
Share on other sites

I already have a program that when you type a command it brings up a window with 11 drop down menus. I would like the condition statement to say that when one these specific drawings are selected (04-1008035-STD or 04-1008077-STD or 04-1008156-STD) from the equipbottom drop down, that it prompts the following code:

 



(defun C:road () ; define function



; Calling the road function in the condition statement


 (cond
 ((or(= equipbottomval "04-1008035-STD")(= 
equipbottomval "04-1008077-STD")(= equipbottomval 
"04-1008156-STD"))))
  


;Selects Road text and moves it to the Field text coordinates
 

(setq Road (ssget "X" '((8 . "Road"))))
(setq pt1 '(0 0 
0))
(setq pt2 '(3100 0 0))
(command "_.move" Road "" pt1 
pt2)


;Selects Field text and moves it to the Road text coordinates


(setq Field (ssget "X" '((8 . "Field"))))
 (setq pt3 '(0 0 
0))
 (setq pt4 '(-3100 0 0))
 (command "_.move" Field "" pt3 
pt4)
 
 (Princ "Done")
 
 (princ)



 )  ;End Defun

 

This code needs to run automatically without having to select anything but the OK button on the window.

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...