Because your command handler function is
non-static, AutoCAD creates an instance of
your class for each open document, when
you issue the command the first time in the
document.
That means, that you do not have to create
any more instances of the class yourself in
the DocumentAdded handler, and probably
should not do that.
You can store the instances of your command
class that AutoCAD creates for you, in a static
Hashtable, which allows you to reference them
by the Document they're assocated with:
// In your Massstab class:
internal static Hashtable docMap = new Hashtable();
Then, in your class's constructor, you add the
instance to the hashtable, keyed to the active
document:
public Massstab()
{
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
docMap[doc] = this; // add this instance to static map
}
Now, you can reference the instance of your class that
is (loosly) associated with a given document, by just
getting it out of the hashtable, using a Document instance
as the key, which you pass to the default indexer:
// (from anywhere in your assembly):
Document doc = AcadApp.DocumentManager.MdiActiveDocument;
Massstab m_thisdoc = Massstab.docMap[doc];
Now, m_thisdoc is the instance of your Massstab class
that's associated with the active document, and you can
do whatever you need with it.
Beware that the above assumes that your MSTFenster
command has been issued at least once in the document
that is returned by MdiActiveDocument. If the command
was never used, the instance of the class may not exist
for the document yet, and the result of the above will
be null. Also note that this approach relies on the fact
that the Managed API is creating instances of your class,
rather than you doing it manually via new Massstab().
If you create instances yourself, none of this will work.
In the above, 'AcadApp' is:
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
Does that help?
--
http://www.caddzone.com
AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
wrote in message news:5126450@discussion.autodesk.com...
I have a class which i start at the beginning of every drawing which sets
some values. This values i want to use from other classes.
[assembly: CommandClass(typeof(RSNNAcadApp.Massstab.Massstab))]
namespace RSNNAcadApp.Massstab
{
class Massstab
{
private double m_UFAK;
private Unit m_UNIT;
private double m_SFAK;
private bool m_LayoutMM;
private Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
public Massstab()
{
this.Read(); //Here i set some values
}
[CommandMethod("MSTFenster")]
public void MSTWindow()
{
//Here is some code to change the values
}
private void Write()
{
//This one reads the values from Xrecords
}
public double UFAK
{
get
{
return m_UFAK;
}
}
public Unit UNIT
{
get
{
return m_UNIT;
}
}
public double SFAK
{
get
{
return m_SFAK;
}
}
public bool LayoutMM
{
get
{
return m_LayoutMM;
}
}
}
}
~~~~~~~~~~~~~~~~
With this code i load the class above
namespace RSNNAcadApp
{
public class App : IExtensionApplication
{
public void DocumentAdded(object sender, DocumentCollectionEventArgs
e)
{
new Massstab.Massstab();
}
public void Initialize()
{
new Massstab.Massstab();
Application.DocumentManager.DocumentCreated += new
DocumentCollectionEventHandler(DocumentAdded);
}
public void
Terminate()
{
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now my question, i have another class which needs the values from the first
class (Massstab) which has different values for every drawing and which i
load as you can see at the beginning of every drawing. How can i do that?
Maybe it is a silly question, but i have no idea how to do that, maybe i did
something wrong, i would be glad for any help. And i also hope that you
understand what i want to do 😉
--
Roland Feletic