.NET

Reply
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 1 of 10 (60 Views)

C# Question about classes

60 Views, 9 Replies
03-30-2006 12:43 AM
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
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 2 of 10 (60 Views)

Re: C# Question about classes

03-30-2006 06:10 AM in reply to: RolandF
OK, i've found a solution, thanks.

--
Roland Feletic
*Tony Tanzillo
Message 3 of 10 (60 Views)

Re: C# Question about classes

03-30-2006 06:15 AM in reply to: RolandF
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
*Fred Chateau
Message 4 of 10 (60 Views)

Re: C# Question about classes

03-31-2006 02:43 PM in reply to: RolandF
"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
*Tony Tanzillo
Message 5 of 10 (60 Views)

Re: C# Question about classes

03-31-2006 11:00 PM in reply to: RolandF
>> 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
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 6 of 10 (60 Views)

Re: C# Question about classes

04-04-2006 12:06 AM in reply to: RolandF
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
*Tony Tanzillo
Message 7 of 10 (60 Views)

Re: C# Question about classes

04-04-2006 07:37 AM in reply to: RolandF
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
*Fred Chateau
Message 8 of 10 (60 Views)

Re: C# Question about classes

04-04-2006 08:28 AM in reply to: RolandF
"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. :smileyhappy:

Your explanations and advice are always appreciated...

--
Regards,

Fred Chateau
*Tony Tanzillo
Message 9 of 10 (60 Views)

Re: C# Question about classes

04-04-2006 11:36 AM in reply to: RolandF
>> 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
Distinguished Contributor
RolandF
Posts: 172
Registered: ‎11-19-2003
Message 10 of 10 (60 Views)

Re: C# Question about classes

04-05-2006 09:24 AM in reply to: RolandF
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

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