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

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

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
nirvanbd
2417 Views, 5 Replies

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

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.

5 REPLIES 5
Message 2 of 6
hgasty1001
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

 

 

Message 3 of 6
_gile
in reply to: hgasty1001

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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 6
nirvanbd
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.

Message 5 of 6
_gile
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
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 6 of 6
nirvanbd
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.

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