.NET

Reply
Valued Mentor
AubelecBE
Posts: 373
Registered: ‎01-27-2010
Message 1 of 6 (194 Views)

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

194 Views, 5 Replies
02-12-2013 02:31 PM

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.

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 2 of 6 (174 Views)

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

02-12-2013 10:17 PM 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
Valued Mentor
AubelecBE
Posts: 373
Registered: ‎01-27-2010
Message 3 of 6 (149 Views)

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

02-14-2013 04:18 AM 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..

 

Active Contributor
DouceDeux
Posts: 46
Registered: ‎09-04-2012
Message 4 of 6 (140 Views)

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

02-14-2013 05:54 AM 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;

 

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 5 of 6 (122 Views)

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

02-14-2013 10:08 AM 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
Valued Mentor
AubelecBE
Posts: 373
Registered: ‎01-27-2010
Message 6 of 6 (112 Views)

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

02-15-2013 04:01 AM 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.

 

 

 

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community