Remove page setups with exceptions

Remove page setups with exceptions

miroko
Enthusiast Enthusiast
1,677 Views
14 Replies
Message 1 of 15

Remove page setups with exceptions

miroko
Enthusiast
Enthusiast

Hello

 

On page

 

 

http://jtbworld.com/autocad-pagesetup-lsp

 

is nice lisp for removing all page setups except some which are on list.

 

First it was returning error that is too much brackets, that is solved.

But now it returns error that is 'too few arguments'.

 

 

here is lisp:

 

 

'(
"PageSetupName1"
"Selected Window to A0 to 'DWG To PDF'"

)
(defun C:deleteAllPageSetupsExcept (doc names)
  (vlax-for pc (vla-get-plotconfigurations doc)
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)

 

 

Could anyone help fixing it?

 

 

miroko

0 Likes
Accepted solutions (1)
1,678 Views
14 Replies
Replies (14)
Message 2 of 15

Shneuph
Collaborator
Collaborator

If you're only running it in the document you have open and not trying to feed it multiple drawing files then try just this:

(untested)

(Defun C:deleteAllPageSetupsExcept (names)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)

 

EDIT:

Maybe I should explain a little.

 

A) Typically, you would define a function that you plan on passing arguments to without the C:.   C: is to define a command.  A function is called with lisp.  So, this lisp should be defined as a function (no "C:")

(Defun deleteallpagestupsexcept (names) ...

 

Then, in a command that the user would actually use you could do something like:

 

(Defun C:DeleteAllPageSetupsExcept ( / list)
  (while UserEntersNames
    (setq list (addname to list)
    );while
;then pass the list to your function (deleteallpatestupsexcept list) );defun

(obviously pseudocode)

 

 

B) The "Too few Arguments" error was because the command/function thing was asking for 2 arguments (defun C:deleteAllPageSetupsExcept (doc names)  "doc" and "names"  If you were only passing it a list of names then there were too few arguments because you were missing doc. See:

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-AutoLISP/file...

 

Hope that explains a little better.

 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 3 of 15

miroko
Enthusiast
Enthusiast

Thank you for answer and explanations.

 

Actually orignal function from web page which I gave in first post was not including C: but was not working so I thought to add it.

 

Anyway, it is not working still for me, still getting same error about arguments or malformed input or function don't exist etc..

 

I tried many different versions and cannot make it work.

 

 

miroko

0 Likes
Message 4 of 15

Shneuph
Collaborator
Collaborator

This code works for me.

(Defun deleteAllPageSetupsExcept (names)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)

Give it a try and if it does not work please post the exact error message you get.  Use it like this:

 

(deleteAllPageSetupsExcept '("PageSetupNameToKeep"))

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 5 of 15

miroko
Enthusiast
Enthusiast

Hi

 

Most probably I am doing something wrong since it works for you.

 

I tried like that (with defined list in lisp):

 

(Defun RemoveAllPageSetupsExcept '(
"Selected Window to A0 to 'DWG To PDF'"
"A0 to 'DWG To PDF'"
)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)

 

 

When I drag-drop it into AutoCAD I get error : syntax error.

When I load this lisp by button with command:

^C^C(load RemoveAllPageSetupsExcept.lsp);RemoveAllPageSetupsExcept

 

then I get

error: bad argument type: stringp nil

 

 

miroko

0 Likes
Message 6 of 15

dbroad
Mentor
Mentor

@miroko  It's probably best that you not modify a master's lisp files unless you understand something of AutoLISP.  In this case, you can't just change the name by adding a c: prefix and turn it into a command.  Commands cannot have arguments.

 

Also, if you had posted the whole code, you would have included the comments of how to use it.

 

If you want to add a command that embeds a lisp function you like, do it this way:

(defun embeded_function .....) or (load "embeded_function")

 

(defun c:mynewcommand ()

  (embeded_function   [embedded arguments as necessary])

  (princ)

)

 

Architect, Registered NC, VA, SC, & GA.
Message 7 of 15

miroko
Enthusiast
Enthusiast

@dbroad

yes, you are correct.

in the first post I included webpage link to original code with instructions

 

I dont know much about Autolisp so was just trying to get some solution and failed

0 Likes
Message 8 of 15

dbroad
Mentor
Mentor

That's OK.  Just copy/paste the function call that works inside the command function you build, as I demonstrated.  Be sure to include either the function definition in the same file or a load statement that loads it from another file.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 9 of 15

miroko
Enthusiast
Enthusiast

@dbroad

 

I went back to original code from given web page.

 

original is:

 

;;; (deleteAllPageSetupsExcept <AcadDocument> <PageSetupName>)
;;; (deleteAllPageSetupsExcept (vla-get-activedocument (vlax-get-acad-object))
'("PageSetupName1" "PageSetupName2"))
(defun deleteAllPageSetupsExcept (doc names)
  (vlax-for pc (vla-get-plotconfigurations doc)
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)

 

 

After combining it with command and removing the 'doc' (as per suggestion from @Shneuph since I want it to work on current drawing only) then I get this:

 

(defun c:dapse ()
 '("PageSetupName1" "PageSetupName2")
(Defun deleteAllPageSetupsExcept (names)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (vla-delete pc)
    )
  )
)
  (princ)
)

 

There is no error but function does not remove the pagesetups. Is there something small missing here?

0 Likes
Message 10 of 15

Shneuph
Collaborator
Collaborator

Try this.   I typically define functions and the commands separately (in separate files even).  Then call the function from within the command passing the argument (names list) to it. This should work when you use command dapse.

 

(Defun c:dapse (); Define your command
  (deleteallpagesetupsexcept '("Name1" "Name 2"));call function w/ arguement (names list)
  (princ)
  );end your command definition

(Defun deleteAllPageSetupsExcept (names)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (progn
	(vla-delete pc)
	(terpri)
	(princ (vla-get-name pc))
	(princ " - Deleted")
	);progn
      );if
    )
  )

 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 11 of 15

dbroad
Mentor
Mentor

As @Shneuph did for you and as I specifically explained,  the defun for the sub function is placed outside the main command function.  Only the function call goes inside your new command defun.  You should be able to use @Shneuph's without modification.  I was hoping to lead you to fend for yourself rather than just to give you the solution.

Architect, Registered NC, VA, SC, & GA.
0 Likes
Message 12 of 15

miroko
Enthusiast
Enthusiast

@dbroad@Shneuph

 

The code which is provided in the post from @Shneuph above is returning error when I start command

 

error: Automation Error. Object was erased

 

 

I tried to find what that error means in internet but did not find solution.

 

 

when I load lisp it just says:

(LOAD "...access path/RemoveAllPageSetupsExcept.lsp") DELETEALLPAGESETUPSEXCEPT

 

 

0 Likes
Message 13 of 15

Shneuph
Collaborator
Collaborator
Accepted solution

Last minute code change fail.  I need to princ the name of the pagesetup before deleting it probably, huh?  Please try again with the below (red lines were reversed)

 

 

(Defun c:dapse (); Define your command
  (deleteallpagesetupsexcept '("Name1" "Name 2"));call function w/ arguement (names list)
  (princ)
  );end your command definition

(Defun deleteAllPageSetupsExcept (names)
  (vlax-for pc (vla-get-plotconfigurations (vla-get-activedocument (vlax-get-acad-object)))
    (if (not (member (vla-get-name pc) names))
      (progn
	(terpri)
	(princ (vla-get-name pc))
	(vla-delete pc)
	(princ " - Deleted")
	);progn
      );if
    )
  )
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 14 of 15

miroko
Enthusiast
Enthusiast

@Shneuph

thank you for the lisp, it is working fine now.

 

@dbroad

thank you also for help

0 Likes
Message 15 of 15

dbroad
Mentor
Mentor

You're welcome.  Keep learning lisp.  If you keep practicing, someday you might be the master.

Architect, Registered NC, VA, SC, & GA.
0 Likes