.NET

Reply
Contributor
nirvanbd
Posts: 16
Registered: ‎01-25-2011
Message 1 of 6 (656 Views)
Accepted Solution

Best Way to load a wpf executable assembly (.exe) in Autocad 2012

656 Views, 5 Replies
12-01-2012 01:29 AM

I was trying to figure out the best way of loading an external WPF executable assembly into Autocad. The scenario is simple though to explain. 

 

The user enters a command in Autocad. The command invokes a WPF application, which loads a particular WPF Window. The command also need to pass a parameter to the WPF application when it requests the WPF Window to load. When the user is done with the Window, he closes it like any modal window that appear in Autocad. What are the possible ways to have such an interaction in AutoCAD using .Net and C# and WPF

 

nirvan.

Valued Mentor
gasty1001
Posts: 448
Registered: ‎04-11-2010
Message 2 of 6 (640 Views)

Re: Best Way to load a wpf executable assembly (.exe) in Autocad 2012

12-01-2012 07:55 AM in reply to: nirvanbd

Hi,

 

You can't load an exe in AutoCAD, you have to create a dll assembly with the references to the usual Acdbmgd.dll and Acmgd.dll libs (for 2013 you will need extra libs), As for WPF you can show a dialog in AutoCAD using something like Application.ShowModalDialog(wpfdialogInstance) from a command.

 

Gaston Nunez

 

 

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 3 of 6 (629 Views)

Re: Best Way to load a wpf executable assembly (.exe) in Autocad 2012

12-01-2012 11:43 AM in reply to: gasty1001

Hi,

 

I don't know much about WPF, but here's a way:

 

In your WPF project, change the Main definitions (App.g.cs and App.g.i.cs) so that they can accept arguments and pass these arguments (string array) to the WpfApplication.App constructor.

        [System.STAThreadAttribute()]
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
        public static void Main(params string[] args)
        {
            WpfApplication1.App app = new WpfApplication1.App(args);
            app.InitializeComponent();
            app.Run();
        }

 

Visual Studio intellisense would purpose you to create a stub method for this constructor, clic it or do it by yourself in the App.xaml.cs file.

Add an internal static property so that the project classes can get these arguments.

    public partial class App : Application
    {
        internal static string[] Args { get; private set; }

        public App(string[] args)
        {
            Args = args;
        }
    }

 

Now you can get the application arguments from your MainWindow and display the first argument in a textBox, for example:

        public MainWindow()
        {
            InitializeComponent();
            this.textBox1.Text = App.Args[0];
        }

 To call the WPF application from an AutoCAD command:

 

        [CommandMethod("Test", CommandFlags.Modal)]
        public void Test()
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = @"..\..\..\WpfApplication1\bin\Debug\WpfApplication1.exe";
            proc.StartInfo.Arguments = "foo";
            proc.Start();
        }

 I hope it helps despite my poor english.

Gilles Chanteau
Contributor
nirvanbd
Posts: 16
Registered: ‎01-25-2011
Message 4 of 6 (607 Views)

Re: Best Way to load a wpf executable assembly (.exe) in Autocad 2012

12-02-2012 12:27 AM in reply to: nirvanbd

Gilles Chanteau,

Thanks for taking time to post all the code. I tried your code and although it works it doesn't actually satisfy my requirements. The problem is that AutoCAD starts an altogether new process completely indenpendent of the AutoCAD process. Actually what I was looking for is that WPF loads a screen in Autocad process and that screen acts as a Modal Window in AutoCAD. So your solution, even though it works, doesn't really fit my scenario.

 

Gaston Nunez,

I am still trying your solution. The problem is converting wpf application into a dll. If I do that, I have to get rid of the Application Defiinition (App.xaml), which means that I will have to create the Application manually somewhere in the assembly. I really don't know what implications this will have on my original wpf application. I am still surfing over the net to get a conclusive grasp of the same.

 

regards,
Nirvan.

*Expert Elite*
_gile
Posts: 2,077
Registered: ‎04-29-2006
Message 5 of 6 (598 Views)

Re: Best Way to load a wpf executable assembly (.exe) in Autocad 2012

12-02-2012 01:56 AM in reply to: nirvanbd

Hi,

 

Assuming you have a class library where the command whcih will show the WPF window is defined.

 

Copy and paste the .xaml and xaml.cs files (except the App ones) from your .exe project folder into the autoCAD command one.

Open the AutoCAD command project in Visual Studio, right clic on the project > Add > Existing elements, add the xaml.cs files. do the same for the .xaml files.

Add a reference to system.Xaml.


 You should now be able to show the WPF in autoCAD from a command:

        [CommandMethod("Test", CommandFlags.Modal)]
        public void Test()
        {
            WpfApplication1.MainWindow wpf = new WpfApplication1.MainWindow();
            Application.ShowModalWindow(wpf);
        }

 

 

Gilles Chanteau
Contributor
nirvanbd
Posts: 16
Registered: ‎01-25-2011
Message 6 of 6 (591 Views)

Re: Best Way to load a wpf executable assembly (.exe) in Autocad 2012

12-02-2012 04:06 AM in reply to: nirvanbd

Gilles Chanteau

 

I think your solution of copying the xaml files and ViewModels into the Autocad plugin project seems to be the only solution. The only down side is that I will have to apply bug-fixes for wpf window/UserControl/ViewModels, at two places (one in WPF main project and other in AutoCAD plugin project). But still this is surely a workable solution. Thanks for all your inputs. I am also facing another issue relating to the loading of dll in AutoCAD, but I will put that up in another thread,

 

Nirvan.

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