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

C# Question about classes

9 REPLIES 9
Reply
Message 1 of 10
Anonymous
264 Views, 9 Replies

C# Question about classes

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
9 REPLIES 9
Message 2 of 10
Anonymous
in reply to: Anonymous

OK, i've found a solution, thanks.

--
Roland Feletic
Message 3 of 10
Anonymous
in reply to: Anonymous

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
Message 4 of 10
Anonymous
in reply to: Anonymous

"Tony Tanzillo" wrote ...

> 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...

--

Would you please explain why it is necessary for the MSTFenster command to
run in order to instantiate the class?

--
Regards,

Fred Chateau
Message 5 of 10
Anonymous
in reply to: Anonymous

>> Would you please explain why it is necessary
>> for the MSTFenster command to
>> run in order to instantiate the class?

I already did.

Please see the first paragraph of the post.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Message 6 of 10
Anonymous
in reply to: Anonymous

Thank you, Tony. I think that your post helps like every post of you 😉
I tried it before your reply a little bit different and had another problem
with MSTFenster.
So just one question before i change my code, does it mean that it is not
possible to start autocad or a new document setting the values in Massstab
without starting the command MSTFenster form e.g acaddoc.lsp?

--
Roland Feletic

"Tony Tanzillo" schrieb im Newsbeitrag
news:5129269@discussion.autodesk.com...
>> Would you please explain why it is necessary
>> for the MSTFenster command to
>> run in order to instantiate the class?

I already did.

Please see the first paragraph of the post.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Message 7 of 10
Anonymous
in reply to: Anonymous

Autodesk's design has some flaws. Namely, you don't have
control of the creation of classes with non-static command
methods.

I generally don't use it, and prefer to roll my own means
of achiving the same thing, with more control.

See the code here:

http://www.caddzone.com/MyDocumentClass.cs

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5130929@discussion.autodesk.com...
Thank you, Tony. I think that your post helps like every post of you 😉
I tried it before your reply a little bit different and had another problem
with MSTFenster.
So just one question before i change my code, does it mean that it is not
possible to start autocad or a new document setting the values in Massstab
without starting the command MSTFenster form e.g acaddoc.lsp?

--
Roland Feletic

"Tony Tanzillo" schrieb im Newsbeitrag
news:5129269@discussion.autodesk.com...
>> Would you please explain why it is necessary
>> for the MSTFenster command to
>> run in order to instantiate the class?

I already did.

Please see the first paragraph of the post.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Message 8 of 10
Anonymous
in reply to: Anonymous

"Tony Tanzillo" wrote ...

> I already did.

> Please see the first paragraph of the post.

My mind must have been somewhere else while I was reading your post. We are
nearing deadline to roll out a new AutoCAD 2007 OEM application and I'm
spending so much time in Visual Studio, I think I'm loosing touch with
reality. 🙂

Your explanations and advice are always appreciated...

--
Regards,

Fred Chateau
Message 9 of 10
Anonymous
in reply to: Anonymous

>> See the code here:

>> http://www.caddzone.com/MyDocumentClass.cs

If you downloaded this already, it was just updated
to include missing using clauses, and a few methods
that were inadvertently left out.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

"Tony Tanzillo" wrote in message news:5131270@discussion.autodesk.com...
Autodesk's design has some flaws. Namely, you don't have
control of the creation of classes with non-static command
methods.

I generally don't use it, and prefer to roll my own means
of achiving the same thing, with more control.

See the code here:

http://www.caddzone.com/MyDocumentClass.cs

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com

wrote in message news:5130929@discussion.autodesk.com...
Thank you, Tony. I think that your post helps like every post of you 😉
I tried it before your reply a little bit different and had another problem
with MSTFenster.
So just one question before i change my code, does it mean that it is not
possible to start autocad or a new document setting the values in Massstab
without starting the command MSTFenster form e.g acaddoc.lsp?

--
Roland Feletic

"Tony Tanzillo" schrieb im Newsbeitrag
news:5129269@discussion.autodesk.com...
>> Would you please explain why it is necessary
>> for the MSTFenster command to
>> run in order to instantiate the class?

I already did.

Please see the first paragraph of the post.

--
http://www.caddzone.com

AcadXTabs: MDI Document Tabs for AutoCAD 2004/2005/2006/2007
http://www.acadxtabs.com
Message 10 of 10
Anonymous
in reply to: Anonymous

Thank you very much. Your code is a great help. Now i try to change my code
and hope that i can do it.

--
Roland Feletic

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