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

Configuration Section and Autocad

6 REPLIES 6
Reply
Message 1 of 7
ahmed.felix
595 Views, 6 Replies

Configuration Section and Autocad

I'm having trouble with making configuration sections in the app.config file to work with Autocad. If I change the project output type to a windows application and have a button call some code, the section handler works just fine and returns the expected object, but apparently calling that same code from within an Autocad command in a class library dll doesn't work, it returns nothing. Any ideas on why this is happening? Does Autocad blocks the use of the System.Configuration assembly somehow? I found this other post from several years ago: http://discussion.autodesk.com/forums/thread.jspa?messageID=5128820䉴 in which the guy apparently has the same problem and wasn't solved. Hopefully I'll have better luck having passed more than 3 years from that. Thank you.
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: ahmed.felix


If you can post some relevant code, perhaps someone

can help.  Most here will not guess, if they've not
come

across the same issue.


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I'm
having trouble with making configuration sections in the app.config file to
work with Autocad. If I change the project output type to a windows
application and have a button call some code, the section handler works just
fine and returns the expected object, but apparently calling that same code
from within an Autocad command in a class library dll doesn't work, it returns
nothing. Any ideas on why this is happening? Does Autocad blocks the use of
the System.Configuration assembly somehow? I found this other post from
several years ago:
http://discussion.autodesk.com/forums/thread.jspa?messageID=5128820? in which
the guy apparently has the same problem and wasn't solved. Hopefully I'll have
better luck having passed more than 3 years from that. Thank
you.
Message 3 of 7
Anonymous
in reply to: ahmed.felix


app.config in a .NET project will become
[appName].exe.config after compiling. Acad .NET API DLL by itself is not a EXE
application, it is part of AutoCAD application (acad.exe). That is why Acad
comes with acad.exe.config file since Acad2005 (in AutoCAD installation folder).
You should place configurations for your .NET API code into
acad.exe.config.

 

For example, in our office, almost all .NET API
apps for Acad need to access our enterprise database, so we let AutoCAD know
which database to connect to when a .NET API app is loaded by:

 

1. Add <appSettings> key in the
axcad.exe.config file:

 

<configuration>
    
    <startup>
     
    <!--We always use the latest version of the framework
installed on the computer. If you
       
    are having problems then explicitly specify .NET 2.0 by
uncommenting the following line.
     
        <supportedRuntime
version="v2.0.50727"/>
       
-->
        
</startup>
        
<appSettings>


size=2>
             
<add key="DatabaseServer"
value="ServerName"/>
         
    <add key="Database" value="DatabaseName"/>

       
        ...

       
</appSettings>

    ...

    ...

</configuration>

 

2. In the .NET API class:

 

public class MyStartupAddin :
Autodesk.AutoCAD.Runtime.IExtensionApplication

{

    private static string
mConnectionString;

    ...

    public void
Initialize()
    {

        string server
=
System.Configuration.ConfigurationManager.AppSettings["DatabaseServer"];
   
    string database =
System.Configuration.ConfigurationManager.AppSettings["Database"];

       

        //Compose the
ConnectionString

       
mConnectionString=.......

    }

}

 

Of course you can place as many as your app's
settings here. However, with all said, if you have a lots of .NET API apps in
Acad, you may not want to use acad.exe.config to store app settings. That would
make acad.exe.config very messy, not to mention possible update issue because of
it being located in every CAD computer.

 

I'd recommend only store key settings that rarely
changes in acad.exe.config. You may need to create your own configuration
storage mechnism that suit your need, considering the management/deployment
requirement. In my case, I only store the database identification
information. All other .NET API apps' settings are stored in database. So, as
long as Acad starts and reads the acad.exe.config for database location, it will
be able to get all other needed settings from our database, and all those
settings can be easily managed in a central location (database), without issue
of being out-dated.

 

For some configurations you may not have other
options than let them stay in acad.exe.config, such as configuration needed to
WCF/WWF, should your .NET code uses these technology.

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I'm
having trouble with making configuration sections in the app.config file to
work with Autocad. If I change the project output type to a windows
application and have a button call some code, the section handler works just
fine and returns the expected object, but apparently calling that same code
from within an Autocad command in a class library dll doesn't work, it returns
nothing. Any ideas on why this is happening? Does Autocad blocks the use of
the System.Configuration assembly somehow? I found this other post from
several years ago:
http://discussion.autodesk.com/forums/thread.jspa?messageID=5128820? in which
the guy apparently has the same problem and wasn't solved. Hopefully I'll have
better luck having passed more than 3 years from that. Thank
you.
Message 4 of 7
ahmed.felix
in reply to: ahmed.felix

My configuration file looks like the one in the attachment (I can't post xml here, it doesn't show). This gets deserialized by the function XmlSerializerSectionHandler (http://www.codinghorror.com/blog/archives/000161.html), which returns the object defined by this class:
----------------------------------------------------------

Public Class MyStuff

Dim dblFoo As Double

Public Property Foo() As Integer
Get
Return dblFoo
End Get
Set(ByVal value As Integer)
dblFoo = value
End Set
End Property

Dim intValues As Collection(Of Integer)
Public Property Values() As Collection(Of Integer)
Get
Return intValues
End Get
Set(ByVal value As Collection(Of Integer))
intValues = value
End Set
End Property

End Class

---------------------------------------------------------------------
Now, I just use this code:
---------------------------------------------------------------------

Dim myObject As MyObject
myObject = ConfigurationManager.GetSection("MyObject")


----------------------------------------
Now, this code works just fine when run from a windows application, but running it from a dll loaded into Autocad returns nothing. As far as I'm understanding from Norman Yuan post, there's no way to make this work from inside Autocad other than pretty much rewrite part of the System.Configuration to work from a custom xml file? I thought it might be because of being a dll and acad.exe being the application as you just said, but it seemed a little bit odd since app.config can be used inside a data access layer which is a dll used by another program, can't it? The main application can reference a dll, and that dll has its own configuration file in which to store database information and such.
Message 5 of 7
ahmed.felix
in reply to: ahmed.felix

Actually, thinking about it, if I use the deserialization approach there's no real need to use the acad.exe.config file, using any xml file and passing the node as parameter will return the object, only thing to do is parse the nodes. Pointing the file path to the application name + .config even allows for the use of the app.config file of the dll during development time. Making it work as transparently as a normal configuration file should be easy with just a few functions.
Message 6 of 7
Anonymous
in reply to: ahmed.felix


I've not used GetSection(), because I use the
IDE-generated

Properties.Settings class (derived from
ApplicationSettingsBase),

and that works in both a standalone .EXE
and a DLL loaded into

AutoCAD (where the config data for each DLLs's Settings
class

ends up as a section in a single .config file).

 

My suspicion is that because you're not specifying
where the

.config file is, it uses a path deduced from the
size=2>host process.

 

If you have reflector, have a look at
ApplicationSettigsBase

to see how it deduces the location of the .config file,
and

the section name.


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
My
configuration file looks like the one in the attachment (I can't post xml
here, it doesn't show). This gets deserialized by the function
XmlSerializerSectionHandler
(http://www.codinghorror.com/blog/archives/000161.html), which returns the
object defined by this class:
---------------------------------------------------------- Public Class
MyStuff Dim dblFoo As Double Public Property Foo() As Integer Get Return
dblFoo End Get Set(ByVal value As Integer) dblFoo = value End Set End Property
Dim intValues As Collection(Of Integer) Public Property Values() As
Collection(Of Integer) Get Return intValues End Get Set(ByVal value As
Collection(Of Integer)) intValues = value End Set End Property End Class
--------------------------------------------------------------------- Now, I
just use this code:
--------------------------------------------------------------------- Dim
myObject As MyObject myObject = ConfigurationManager.GetSection("MyObject")
---------------------------------------- Now, this code works just fine when
run from a windows application, but running it from a dll loaded into Autocad
returns nothing. As far as I'm understanding from Norman Yuan post, there's no
way to make this work from inside Autocad other than pretty much rewrite part
of the System.Configuration to work from a custom xml file? I thought it might
be because of being a dll and acad.exe being the application as you just said,
but it seemed a little bit odd since app.config can be used inside a data
access layer which is a dll used by another program, can't it? The main
application can reference a dll, and that dll has its own configuration file
in which to store database information and such.
Message 7 of 7
Anonymous
in reply to: ahmed.felix


I don't think acad.exe.config is used.

 

It depends on whether the settings you're saving
have

user-scope or application/machine-scope.

 

If the settings are user-scope (different settings
for

each user), the .config file is buried in
subdirectory

under the user's root folder


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Actually,
thinking about it, if I use the deserialization approach there's no real need
to use the acad.exe.config file, using any xml file and passing the node as
parameter will return the object, only thing to do is parse the nodes.
Pointing the file path to the application name + .config even allows for the
use of the app.config file of the dll during development time. Making it work
as transparently as a normal configuration file should be easy with just a few
functions.

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