acad.lsp is read twice for both opening application and opening first document

acad.lsp is read twice for both opening application and opening first document

rita.aguiar
Advocate Advocate
1,019 Views
3 Replies
Message 1 of 4

acad.lsp is read twice for both opening application and opening first document

rita.aguiar
Advocate
Advocate

acad.lsp is read twice for both opening application and opening first document and it shouldn't!!! I should not read acad.slp again when I am working at the document level, when working at the document level it should only read the acaddoc.lsp. Why is this happening?

 

I have created an AutoCAD plugin to register when the user opens the AutoCAD application. This plugin is loaded automatically when the application is opened by reading the acad.lsp which tells the application to load the plugin and execute a command:

 

acad.lsp

(command "trustedpaths" "C:/Users/rita.aguiar/Documents") ;this is the path of the dll
(command "_netload" "C:/Users/rita.aguiar/Documents/AppEvent.dll") ;this loads the .dll
(command "OpenEvent") ;this executes the first command in the plugin

 

When this "OpenEvent" command is executed, it writes on a txt document that the application was opened:

 

plugin

        [CommandMethod("OpenEvent")]
        public void OpenEvent()
        {
// writes on a txt that the AutoCAD application was opened
StreamWriter file = new StreamWriter("C://Users//" + Environment.UserName + "//AppData//Roaming//Autodesk//AutoCAD 2019//R23.0//enu//support//" + Environment.UserName + ".txt", append: true); file.WriteLine(Environment.UserName + "," + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "," + AppName + ",iniciar"); file.Close(); }

Afterwards, when the user decides to open a document or create a new one, this "OpenEvent" command is triggered again!!!! ;( and the plugin writes the same line of text again in the txt document.

I only want to register that the application was opened once. When a new document is created or opened it should not read the acad.lsp which works at the level of the application. Instead, it should only read the acaddoc.lsp that I created to load a second command named "AddAppEvent" which will set event handlers to register commands.

 

acaddoc.lsp

(command "trustedpaths" "C:/Users/rita.aguiar/Documents") ;this is the path of the dll
(command "_netload" "C:/Users/rita.aguiar/Documents/AppEvent.dll") ;this loads the .dll
(command "AddAppEvent") ;this executes the second command of the plugin which will set event handlers to register the commands ended by the user

 when the command AddAppEvent is executed:

 

        [CommandMethod("AddAppEvent")]
        public void AddAppEvent()
        {
            Document CurrentDoc = CAD.Core.Application.DocumentManager.MdiActiveDocument;

            CurrentDoc.CommandEnded += new CommandEventHandler(m_doc_CommandEnded);

            CurrentDoc.CommandEnded += new CommandEventHandler(m_doc_SaveCommandEnded);
        }

This only happens for the first ever document opened when the application is launched!!!! Why is this happening?

 Could it be that the .dll is the same for both acad and acaddoc? And that I should instead separate and have 2 dlls? I don't think so!! Because when opening/creating the second, third, ... documents it will not read the acad.lsp again as it does not write again that the application was opened.

 

Any help would be appretiated!

0 Likes
1,020 Views
3 Replies
Replies (3)
Message 2 of 4

rita.aguiar
Advocate
Advocate

Here says:

 

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2017/ENU/AutoCAD-Customization...

 

The recommended functionality of acad.lsp and acaddoc.lsp can be overridden with the ACADLSPASDOC system variable. If the ACADLSPASDOC system variable is set to 0 (the default setting), the acad.lsp file is loaded just once: upon application startup. If set to 1, the acad.lsp file is reloaded when a new drawing is created or an existing drawing file is opened.

 

However, I just checked and ACADLSPASDOC  is set to 0.

 

It still keeps on writting twice on the file that the application was opened. when it is being closed it is also writing it twice on the txt file.

0 Likes
Message 3 of 4

dbhunia
Advisor
Advisor

Hi,

 

I do not know why you are facing problem.......

 


@rita.aguiar wrote:

Here says:

...............................

However, I just checked and ACADLSPASDOC  is set to 0.

 

It still keeps on writting twice on the file that the application was opened. when it is being closed it is also writing it twice on the txt file.


 

OK  let's try......

 

First

 

Try again ACADLSPASDOC  is set to 0.

 

By doing so whenever you try to open another drawing in the current AutoCAD session it will give a message, given below...

 

1.PNG

 

If you get this Message box first click on the check box of "Always perform my current choice" & then "Load acad.lsp into the first drawing only...."

 

Now close AutoCAD & reopen it, hopefully your problem will resolve......

 

 

Second

 

If you still facing the same problem then.......

 

Force your C# code to check conditions & then do whatever you want....... like this.......

 

 

using System.Diagnostics;
................
  [CommandMethod("OpenEvent")]
public void OpenEvent()
{ Process Current_Process = Process.GetCurrentProcess(); if (Current_Process.MainWindowTitle == "Autodesk AutoCAD 2019") { //............................ Do Something } else { //............................ Do Nothing } }

 

Hopefully your problem will resolve......

 

 

Third

 

If you still facing the same problem then.......

 

Then try to "Reset Settings to Default" of your AutoCAD,......... but be careful by doing so it will reset the all "custom settings" of your AutoCAD.....

 

And try again......


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 4 of 4

rita.aguiar
Advocate
Advocate

Thank you so much for your help @dbhunia

 

I tried your first suggestion and unfortunately after setting ACADLSPASDOC to 0 and opening a new drawing I do not get a Message Dialog.

 

I also tried your second suggestion and I was astounished to find out that the MainWindowTitle of the Process is

AutoCAD 2019 - [Drawing1.dwg] when the AutoCAD Start window is opened, even before ever opening a first drawing!
After opening a first drawing, if it is a new doc the MainWindowTitle  is AutoCAD 2019 - [Drawing1.dwg]. If it's an existing drawing it will still be Autodesk AutoCAD 2019 - [Drawing1.dwg] regardless of the name of the drawing being opened (even more astounished!) So I could not use a condition that checks the MainWindowTitle.

 

Just tried third, but it keeps on writting twice too.

 

0 Likes