Per document global vars using .net API

My goal is to create a .net object I can re-use in each drawing. The creation process of my objects is not trivial (tin surfaces), so its not an option to reload every time needed.

I originally wrote the program for autocad, now am adapting for bricscad. I kind of expect the API's to behave the same, and I would guess the intent of Bricsys is to do that, so likey have come accross an accidental difference.

In autocad, you can make per drawing ,net objects by simply declaring a class as non-static. This is explained in: http://through-the-interface.typepad.com/through_the_interface/2006/10/perdocument_dat_1.html

so my class looks like:

[assembly: CommandClass(typeof(CTCivilXtra.LispCommands))]
namespace CTCivilXtra {
public class LispCommands {
  public TinSurfaceBase Surf1;

//this next line added because bcad does not seem to recognize lispfunctions unless I do it????why?????
  private Database db = AcAp.DocumentManager.MdiActiveDocument.Database;

  [LispFunction("LispLoadSurfFromFile")]
  public ResultBuffer LispLoadSurfFromFile(ResultBuffer args) { code clipped....}

}

What I am finding is the Surf1 does not stay in memory in bcad. Every time I run LispLoadSurfFromFile, it re-initializes the LispCommands class, thus setting Surf1 back to null. Added to this issue, is that I noticed the lisp functions defined, are not available unless I get a reference to the current document database as shown above. So I am seeing two odd behaviors compared to how autocad handles things. I don't care if i have to add the db reference code, but am worried bcad does not support per-documentvars like this, or even global vars at all if the class gets re-initialized each time you run it. Any advice here?

Comments

  • so I tried a couple things, one thing seems to work.

    Make the global variables as static (not the class though), like:

     static public TinSurfaceBase Surf1;

    I guess I see now that static vars within a non-static class, are "shared" within a prog. I knew this book-wise, but have never needed it before.

    I am still wondering why i need that db reference. It took me 4 hours of narrowing down why one of my progs with lispfuctions worked in bcad, and one did not.  It turns out the one that worked had some code that collected acad layers when it started so I figured out that was the key.

    I am now having trouble with transient entity display, another thread....

  • You can use Document.UserData for storing per document information
This discussion has been closed.