PDA

View Full Version : How to Run a Script File Every Time I Open a DWG.



USMCBody
2016-12-08, 05:00 PM
So I have a script file that I wrote to do some basic things... lock view ports and such. Got that working.

I also wrote a lisp file to call the scrip file (thinking once it was loaded it would run it and it does not do that) The lisp works but I have to manually call it...

How do I make it so as every time I open a file it would run this lisp that calls the script to 'fix' the file?


To summarize....
I'm probably confusing everyone.... All I want to do is every time I open a file for it to automatically fix some things. Hopefully by running the script or lisp that calls the script that I have already got working....


Take Care

BlackBox
2016-12-08, 09:32 PM
The answer to this is in knowing more about the Startup Sequence (https://www.theswamp.org/index.php?topic=39112.msg443213#msg443213), and Startup Switches (https://knowledge.autodesk.com/support/autocad/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/AutoCAD-Core/files/GUID-8E54B6EC-5B52-4F62-B7FC-0D4E1EDF093A-htm.html).

The LISP works properly when manually invoked because you're calling it after everything is done loading, etc... The LISP is NOT working at drawing open automagically, because the Script engine has not yet been initialized when the LISP is called.

Your simplest options are to employ the Script using the /b Startup Switch, or to execute the same tasks as in your Script using LISP only (no more Script needed).


Cheers

USMCBody
2016-12-15, 07:40 PM
I've done the /b startup switch in my autocad icon.. didn't know I could use it in autocad....

So, here is how I think it will work (I'll have to test at a later date)
--Put my lisp (that calls my script) in the setup suite.

--In my lisp, use the /b switch as I've done in my autocad icon and it will que the script to run after the drawing is fully loaded. Something like this. I'll have to work out the syntax but my quick guess is... (Command ".script" /b "C:/Users/xxx/Fix File.scr")

--Since I put it in the startup suite it will run the lisp every time I open a drawing correct.

Am I going the right direction?

BlackBox
2016-12-15, 08:42 PM
You're welcome to use Startup Suite if you like; there are some (many?) who use it, and have little issue... That has not been my experience, and so I do not use it.

I would have to know what is in your Script's contents to know if this would be best course of action... *IF* it were me (and it's not), I'd just port the Script code to LISP and have AcadDoc.lsp load/run that, as you don't need to be dependent on the Script engine, nor even two different code files. Just one code file (LISP) to manage, and it can be executed earlier in the Startup Sequence.


Cheers

tedg
2016-12-16, 12:32 PM
You're welcome to use Startup Suite if you like; there are some (many?) who use it, and have little issue... That has not been my experience, and so I do not use it.

I would have to know what is in your Script's contents to know if this would be best course of action... *IF* it were me (and it's not), I'd just port the Script code to LISP and have AcadDoc.lsp load/run that, as you don't need to be dependent on the Script engine, nor even two different code files. Just one code file (LISP) to manage, and it can be executed earlier in the Startup Sequence.


Cheers

Yes ^^^ this!
That's what I would do too...
Have the script run (or convert it to lisp) within an Acaddoc.lsp (which is wayyy better then startup suite) and it will happen every time you open a drawing in Acad.

USMCBody
2016-12-16, 03:43 PM
So technically the start up suite doesn't run every time AutoCAD opens a new dwg correct? and the Acaddoc.lsp is run every time AutoCAD opens a dwg correct? The whole run every time autocad opens a dwg is important to me for this run....

I do want to keep it separate script's as I still want the option to run them separately if required.... if nothing else for nastalga since that is how I started it.... and that is the code I understand the best.... I'll try and remember to post the final product once I get it running...

Again you all are the best....

Tom Beauford
2016-12-16, 03:58 PM
So technically the start up suite doesn't run every time AutoCAD opens a new dwg correct? and the Acaddoc.lsp is run every time AutoCAD opens a dwg correct? The whole run every time autocad opens a dwg is important to me for this run....

I do want to keep it separate script's as I still want the option to run them separately if required.... if nothing else for nastalga since that is how I started it.... and that is the code I understand the best.... I'll try and remember to post the final product once I get it running...

Again you all are the best....

Both are supposed to, but Acaddoc.lsp always does. Startup Suite isn't as reliable. The control you have with Acaddoc.lsp between versions and installs is reason enough. Ever try to share a Startup Suite between versions or installs?

USMCBody
2016-12-19, 05:39 PM
So the start up suits reliability in question is that you need to set it up for every new version and possibly some updates? So as long as the version doesn't change then the startup suite should in theory run every time you open a dwg??? If that is correct, I don't mind as I'm used to adding to the start up suite every new install of autocad anyway...

Tom Beauford
2016-12-20, 12:33 AM
So the start up suits reliability in question is that you need to set it up for every new version and possibly some updates? So as long as the version doesn't change then the startup suite should in theory run every time you open a dwg??? If that is correct, I don't mind as I'm used to adding to the start up suite every new install of autocad anyway...

It's been years since many of us have tried Startup Suite so if using it works for you us it. AutoCAD will run the first acaddoc.lsp file it finds in a trusted path every time a drawing is opened. It's the method most of us use. I still use an acad.lsp file as well. Before making a decision you may want to look at Google search: https://www.google.com/search?newwindow=1&safe=active&espv=2&q=autocad+startup+suite+issues

USMCBody
2016-12-20, 10:00 PM
pondering..... if I go that path of not using the start up suite, I'll probably add a read me file next to the script, saying something like don't forget to modifty the blah blah blah file with this script.... just as by time I do it again I'll forget all of this.... B-(

Anyway thanks a ton. I'll try and compete it this holiday season when it's 'slow' if that happens this go around. I'll post my results here...

USMCBody
2016-12-29, 03:47 PM
So here is what I came up with... Found the local sub-program on the internet, and the person I got it from didn't know who wrote it originally, but I must say it is brilliant. I know enough to grasp at what it is doing, but I definitely couldn't have done it on my own. I did add some stuff to it as I wanted to add some visual cues and such which is what I've done in the main program....

Just add it to the start up suite and it works..

Just a few things, it doesn't work when AutoCAD starts up, but if you open a file after AutoCAD is open it will run. I don't fully understand it, but guessing it has to do with the loading order. I will say when it automatically runs it doesn't seem to display the text I placed in, but it does lock the viewports and seems to switch between the tabs quicker... You can also call it when ever you want and it will run as intended.... Anyway I just finished it and it seems to work for me for now....


One disclaimer. I have AutoCAD set to open multiple dwg's. This may not work if you had it open a new AutoCAD per dwg open.

Take Care,

Tom Beauford
2016-12-29, 04:26 PM
Your code will switch to every Layout to check and lock viewports every time a drawing is opened. That would drive me nuts on any drawing with multiple Layouts. Lot better code out there from a quick Google search like:
(repeat (setq n (sslength (setq ss (ssget "_X" '((0 . "VIEWPORT"))))))
(if (> (cdr (assoc 69 (entget (setq vp (ssname ss (setq n (1- n))))))) 1); not the Paper Space Viewport of its Layout
(vla-put-DisplayLocked (vlax-ename->vla-object vp) -1); lock it
); if
); repeat from Kent Cooper
Locking all viewports http://forums.autodesk.com/t5/autocad-2010-2011-2012/locking-all-viewports/m-p/5503926#M117319

USMCBody
2016-12-29, 04:37 PM
Ya I figured it would tweak some, but I did it as there are a lot of designers now that use Revit and AutoCAD.... On a side note it would be easy to make the local program copy it into the guts of the main program. That way you wouldn't get any of the 'fluff' and just specifically lock all the viewports (I was wanting to add some visual ques...) Anyway if you don't like the fluff Just make sure to keep the callout at the very end of my current main program and modify slightly so as the program loads it will run. Since your 'upgrading' the local program you would have to modify the (C:lockallviewports) to add the yes so (C:lockallviewports "y"). Basically mimic how I called the local program in the main program.... Sorry, if I'm confusing....

Tom Beauford
2016-12-29, 05:44 PM
Ya I figured it would tweak some, but I did it as there are a lot of designers now that use Revit and AutoCAD.... On a side note it would be easy to make the local program copy it into the guts of the main program. That way you wouldn't get any of the 'fluff' and just specifically lock all the viewports (I was wanting to add some visual ques...) Anyway if you don't like the fluff Just make sure to keep the callout at the very end of my current main program and modify slightly so as the program loads it will run. Since your 'upgrading' the local program you would have to modify the (C:lockallviewports) to add the yes so (C:lockallviewports "y"). Basically mimic how I called the local program in the main program.... Sorry, if I'm confusing....

The code I posted above will do everything yours does instantly without needing to switch to each Layout to modify each viewport. Load it however you want. To test it just paste the code to the command line in any drawing and hit enter. It will lock every viewport in every Layout instantly. It would be less distracting for your users.

From posts on the link you can see many code examples and how different people load them.