I am working on an AutoCAD .NET Plug-in project. I am creating a user control to display the block without opening it in AutoCAD.
The user control using WindowsFormHost. And that windows form control uses an AutoCAD .NET API to display the drawing.
The problem is that the AutoCAD .NET dll is only available to call when AutoCAD is running. And every time I open the User control in XAML designer, it throws an error because the Win form control won't load the AutoCAD .DLL.
The code is quite long, so I take some snaps of it, so hopefully, it is enough.
- The WPF UserControl holds a WinForm Control.
<DockPanel>
<StackPanel DockPanel.Dock="Right" Orientation="Vertical">
<telerik:RadButton Name="ZoomOutBtn" Click="ZoomOutBtn_OnClick">Zoom Out</telerik:RadButton>
<telerik:RadButton Name="ZoomInBtn" Click="ZoomInBtn_OnClick">Zoom In</telerik:RadButton>
<telerik:RadButton Name="RefreshBtn" Click="RefreshBtn_OnClick">Refresh</telerik:RadButton>
</StackPanel>
<Grid DockPanel.Dock="Bottom">
<TextBlock Name="FilePathTb" Text="{Binding TargetFilePath}"></TextBlock>
</Grid>
<WindowsFormsHost MinWidth="500" MinHeight="400" >
<Gui:GsPreviewCtrl x:Name="GsPreviewCtrl"/>
</WindowsFormsHost>
</DockPanel>
- Win Form Control itself.
public class GsPreviewCtrl : Control
{
#region Fields
// current dwg
public Database mCurrentDwg;
// Gs specific
public Manager MpManager;
public Device mpDevice;
public Model mpModel;
public View mpView;
public static bool IsDesignMode => UserControls.Utils.IsDesignMode(null);
// A list of other properties and methods to render the object on the device.
.....
}
- A helper method to tell if the Winform control is in design mode or not.
public static class Utils
{
#region Static Fields
#endregion
#region Static Properties
public static bool DesignMode { get; } = string.Equals(
Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().MainModule?.FileName),
"devenv",
StringComparison.OrdinalIgnoreCase
);
#endregion
#region Static Methods
public static bool IsDesignMode(this Control control) { return DesignMode; }
#endregion
}
However, the helper method doesn't tell if the object is in design mode or not. This piece of code comes from the original post from AutoDesk Dev Blog.
https://adndevblog.typepad.com/autocad/2014/12/drawing-preview-in-wpf-palette.html