.NET

Reply
*perry
Message 1 of 7 (117 Views)

mimicing arx wizards "docdata" class

117 Views, 6 Replies
04-11-2006 09:14 AM
Im just transitioning from C++/Arx to the C#/net environment.
One problem that struck me right away was handling "global" variables
on a per document basis. (Sorry if this more generic C# than Acad).
Im not sure how to go about this. My first attempt to handle this was
to simply create a class, called it "DocData" (pretty original eh?),
and stuff into it the variables I wanted. Then I would watch for the
document became current event and instantiate my class.
This approach just created more questions than answers. I want to be
able to access these docdata variables from any point in my program
just like I can do with an Arx program. To do so I made the variables
static. Thats is probably not good. I would think that as soon as I instantiate
an object of my docdata class that all the static variables in any existing
objects of this class would change. So it seems the variables cannot be static.
That means to access them I need to create another instance of the class whenever
I want to get a variable from it. Seems like I could end up with a lot of objects
of this class floating around just cuz I wanted to extract info from one.
Another thing: since C# doesnt have destructors how do I really know when one
of these docdata objects truly "goes away"? What I mean is, if I am using the
document became current event and instantiating one of these objects on each of
these calls what happens when I swap active documents? do the docdata objects
I created get trash collected or are they still around? If I have two documents
open and swap between them several times am I just creating a bunch of duplicate
docdata objects?
Is the document became current event the best one to use for this purpose?
I want an event that fires each time a document is loaded, whether its a new
empty drawing or an existing one.

I have not found anything even close to addressing this problem in any of the
.net examples or here in the NG. How do others manage this?
*Tony Tanzillo
Message 2 of 7 (117 Views)

Re: mimicing arx wizards "docdata" class

04-11-2006 10:39 AM in reply to: *perry
Have a look here:

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

--
http://www.caddzone.com

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

"perry" wrote in message news:5139008@discussion.autodesk.com...
Im just transitioning from C++/Arx to the C#/net environment.
One problem that struck me right away was handling "global" variables
on a per document basis. (Sorry if this more generic C# than Acad).
Im not sure how to go about this. My first attempt to handle this was
to simply create a class, called it "DocData" (pretty original eh?),
and stuff into it the variables I wanted. Then I would watch for the
document became current event and instantiate my class.
This approach just created more questions than answers. I want to be
able to access these docdata variables from any point in my program
just like I can do with an Arx program. To do so I made the variables
static. Thats is probably not good. I would think that as soon as I instantiate
an object of my docdata class that all the static variables in any existing
objects of this class would change. So it seems the variables cannot be static.
That means to access them I need to create another instance of the class whenever
I want to get a variable from it. Seems like I could end up with a lot of objects
of this class floating around just cuz I wanted to extract info from one.
Another thing: since C# doesnt have destructors how do I really know when one
of these docdata objects truly "goes away"? What I mean is, if I am using the
document became current event and instantiating one of these objects on each of
these calls what happens when I swap active documents? do the docdata objects
I created get trash collected or are they still around? If I have two documents
open and swap between them several times am I just creating a bunch of duplicate
docdata objects?
Is the document became current event the best one to use for this purpose?
I want an event that fires each time a document is loaded, whether its a new
empty drawing or an existing one.

I have not found anything even close to addressing this problem in any of the
.net examples or here in the NG. How do others manage this?
*perry
Message 3 of 7 (117 Views)

Re: mimicing arx wizards "docdata" class

04-11-2006 02:12 PM in reply to: *perry
Tony Tanzillo wrote:
> Have a look here:
>
> http://www.caddzone.com/DocumentDataObject.cs
>
Thanks a bunch Tony.
This is far superior to my meager approach.
It does however kinda highlight my lack of experience.
I successfully integrated the two classes; "documentdataobject"
which I initialized within my main class's initialization (IEextensionapp),
and the example "instance" class which was a comment on the file. The "MyDocData"
class.
Im not sure how to incorporate my own variables.
If I place non static variables in this class then the only way I know
to access them from somewhere else is to create an object of that class
but this is not an option do to the access level of the MyDocData class.
Perry
*perry
Message 4 of 7 (117 Views)

Is this it?

04-11-2006 03:19 PM in reply to: *perry
perry wrote:
>
> Thanks a bunch Tony.
> This is far superior to my meager approach.
> It does however kinda highlight my lack of experience.
> I successfully integrated the two classes; "documentdataobject"
> which I initialized within my main class's initialization (IEextensionapp),
> and the example "instance" class which was a comment on the file. The "MyDocData"
> class.
> Im not sure how to incorporate my own variables.
> If I place non static variables in this class then the only way I know
> to access them from somewhere else is to create an object of that class
> but this is not an option do to the access level of the MyDocData class.
> Perry

After a little more experimentation I "think" I got it.
I placed a bunch of non-static variables up at the top of the file
(MyDocData.cs) and initialized them in the class's Attach() method.
At that point I was able to access these vars anywhere else in my program
via a line like this:
MyDocData.ActiveDocData.myVariable
as opposed to:
MyDocData.DocData.myVariable
or simply:
DocData.myVariable

is this correct?
Whats the purpose of the DocData method in this class?

thanks, Perry
*Tony Tanzillo
Message 5 of 7 (117 Views)

Re: Is this it?

04-11-2006 05:56 PM in reply to: *perry
Right - You add non-static members to your derived
class, and by doing so you have different copies of
them for each document.

The DocData() members are just analogs to the ARX
counterpart (AcApDataManager), and the one that
takes no arguments is another way of referencing the
ActiveDocData property.

One caveat about this class that I didn't mention,
is that because it uses static members to manage
the per-document instances, you can't derive and
use more than one class from it.

To do that, you would need to make it more like
the ARX counterpart (or use generics in .NET 2.0).

--
http://www.caddzone.com

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

"perry" wrote in message news:5139693@discussion.autodesk.com...
perry wrote:
>
> Thanks a bunch Tony.
> This is far superior to my meager approach.
> It does however kinda highlight my lack of experience.
> I successfully integrated the two classes; "documentdataobject"
> which I initialized within my main class's initialization (IEextensionapp),
> and the example "instance" class which was a comment on the file. The "MyDocData"
> class.
> Im not sure how to incorporate my own variables.
> If I place non static variables in this class then the only way I know
> to access them from somewhere else is to create an object of that class
> but this is not an option do to the access level of the MyDocData class.
> Perry

After a little more experimentation I "think" I got it.
I placed a bunch of non-static variables up at the top of the file
(MyDocData.cs) and initialized them in the class's Attach() method.
At that point I was able to access these vars anywhere else in my program
via a line like this:
MyDocData.ActiveDocData.myVariable
as opposed to:
MyDocData.DocData.myVariable
or simply:
DocData.myVariable

is this correct?
Whats the purpose of the DocData method in this class?

thanks, Perry
*perry
Message 6 of 7 (117 Views)

Re: Is this it?

04-12-2006 10:51 AM in reply to: *perry
Tony Tanzillo wrote:
> Right - You add non-static members to your derived
> class, and by doing so you have different copies of
> them for each document.
>
> The DocData() members are just analogs to the ARX
> counterpart (AcApDataManager), and the one that
> takes no arguments is another way of referencing the
> ActiveDocData property.
>
> One caveat about this class that I didn't mention,
> is that because it uses static members to manage
> the per-document instances, you can't derive and
> use more than one class from it.
>
> To do that, you would need to make it more like
> the ARX counterpart (or use generics in .NET 2.0).
>
So it was correct to initialize members in the "attach" method
and null them in the "detach" method?
BTW, why not use .NET 2.0 since its readily available now?

Still seems odd that this functionality, which seems crucial,
was not implemented in any of the .net exampls/labs on the SDK.
Do not the majority of .net programmers have to deal with this issue?
Does VB.net address this?
I'm not sure if theres anything like this in the Arx wizard
(for .net) since Im still using c# express and the wizard will
not install in that version.
*Tony Tanzillo
Message 7 of 7 (117 Views)

Re: Is this it?

04-12-2006 01:33 PM in reply to: *perry
>> So it was correct to initialize members in the "attach"
>> method and null them in the "detach" method?

Well, it's not incorrect. The Attach() and Detach() virtuals
are just formal means of getting notified when the instance
is associated with a document, and before it becomes disassociated. The constructor is usually the
place where you do initialization, and you can do that
in this case too as long as it isn't dependent on the
document. Read the comments.

There's really no etched-in-stone rules for using a class
like that. I wrote it because I needed a simple, reusable
solution for what the counterpart ArxWizard-generated
code does in C++.

So rather than taking it literally, consider it as merely
one example of how you might go about rolling your
own solution.

>> Do not the majority of .net programmers have to deal
>> with this issue?

Yea, but if they can't implement their own solution
for something as simple as this, they're going be
hitting lots of walls. The ObjectARX wizard was not
intended to be a crutch that compensates for a lack
of general programming skills or API knowlege.

There's nothing mystical about the class I pointed
you to, it's just a home-grown solution to a more
common problem.

--
http://www.caddzone.com

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

"perry" wrote in message news:5140799@discussion.autodesk.com...
Tony Tanzillo wrote:
> Right - You add non-static members to your derived
> class, and by doing so you have different copies of
> them for each document.
>
> The DocData() members are just analogs to the ARX
> counterpart (AcApDataManager), and the one that
> takes no arguments is another way of referencing the
> ActiveDocData property.
>
> One caveat about this class that I didn't mention,
> is that because it uses static members to manage
> the per-document instances, you can't derive and
> use more than one class from it.
>
> To do that, you would need to make it more like
> the ARX counterpart (or use generics in .NET 2.0).
>
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.