.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Question : How i can keep variable after quit the main function of the dll

5 REPLIES 5
Reply
Message 1 of 6
AubelecBE
481 Views, 5 Replies

Question : How i can keep variable after quit the main function of the dll

Hi .

 

I want to load my DLL, launch the function with a command. After i quit and lauch the same command. (The DLL is already in memory).

 

(using sessionflags.)

 

It is possible to keep the variable i have used in the the first launch for use in the second launch ?

 

The main function is for adding block in a collection (the user select block). The programm quit, so the user can open a another dwg and run the main function for adding another  block. (i memorize name, handle, name of file etc..)

 

 

Another question : it is possible to create a form in autocad but if the only active document is closed, the form is not closed and visible. ?

 

 

All there question is for create a best solution for manage my collection of block.

 

 

thank.

5 REPLIES 5
Message 2 of 6
_gile
in reply to: AubelecBE

Hi,

 

If your Command is defined within a class (as it should be), any field of this class will be accessible for the Command method (or any other method of this class) each time the command is launched.

If you want the field(s) to be shared between different dranwings, you have to set them static (Shared in VB).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6
AubelecBE
in reply to: _gile

ok thanks.

 

But how i can know if it is the first laucnh of the command or not ?

 

I dont use  static variable so..

 

Message 4 of 6
DouceDeux
in reply to: AubelecBE

If you want data to persist between command calls within the same session of AutoCAD, you're gonna have to use static variables.

 

When describing your scenario, please be clear and differenciate program quitting (closing AutoCAD) from closing a document. You say "After i quit"; do you mean to say "After the command has ended"? There is no way to make data persist if you close the acad.exe other than external files and the clipboard.

You also say "The programm quit, so the user can open a another dwg and run the main function".

Do you mean to say "The user closes the current DWG and opens up another one where he uses the command again"

 

About your form question, you can register the event for when documents are closed within the document manager,  usually in the myPlugin file inside the initialize function.

 

Application.DocumentManager.DocumentDestroyed += new DocumentDestroyedEventHandler(DocumentManager_DocumentDestroyed);

void DocumentManager_DocumentDestroyed(object sender, DocumentDestroyedEventArgs e)
{
  myFormClass newForm = new myFormClass();
Application.ShowModalWindow(newForm); }

The above event is for when the DWG has been completely closed, you don't have access to the document or its database anymore. The e argument only holds the name of the file destroyed. If you need to have access to the document and its database, you would need to use:

Application.DocumentManager.DocumentToBeDestroyed += new DocumentCollectionEventHandler(DocumentManager_DocumentToBeDestroyed);

void DocumentManager_DocumentToBeDestroyed(object sender, DocumentCollectionEventArgs e)
{
  myFormClass newForm = new myFormClass();
  Application.ShowModalWindow(newForm);
}

 Here the sender object is the document manager, so you can cast it like this if you need to:
DocumentCollection newDC = sender as DocumentCollection;

The argument e is the document about to be destroyed. You can cast it the same way:

Document newDocument = e as Document;

 

Message 5 of 6
_gile
in reply to: AubelecBE

Hi,

 

You can try this little sample (may be self explanatory)

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

[assembly: CommandClass(typeof(PerDocumentOrSessionVariablesSample.CommandMethods))]

namespace PerDocumentOrSessionVariablesSample
{
    public class CommandMethods
    {
        private string perDocument = "perDocumentDefault";
        private static string perSession = "perSessionDefault";

        [CommandMethod("Test", CommandFlags.Session)]
        public void Test()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;

            PromptStringOptions pso = new PromptStringOptions("\nEnter the per session value: ");
            pso.DefaultValue = perSession;
            pso.UseDefaultValue = true;
            PromptResult pr = ed.GetString(pso);
            if (pr.Status != PromptStatus.OK) return;
            perSession = pr.StringResult;

            pso.Message = "\nEnter the per document value: ";
            pso.DefaultValue = perDocument;
            pr = ed.GetString(pso);
            if (pr.Status != PromptStatus.OK) return;
            perDocument = pr.StringResult;

            ed.WriteMessage("\nPer session value: {0}, per document value: {1}", perSession, perDocument);
        }
    }
}

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 6
AubelecBE
in reply to: _gile

Thanks for the reply.

 

For me my app is the DLL i have loaded. Autocad is always running. I dont close Autocad.

 

I have to test all this for learn the best use of static variable.

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost