I'm working on a 3D part previewer for an application that is embedded in AutoCAD.
The 3D is implemented using DirectX via SlimDX (2.0 version).
I'm using .NET Framework 3.5
The following code initializes the DirectX Device
foreach (var item in ObjectTable.Objects)
item.Dispose();
_presentParameters = new PresentParameters()
{
BackBufferFormat = Format.Unknown,
SwapEffect = SwapEffect.Discard,
BackBufferWidth = 1500,
BackBufferHeight = 1500,
EnableAutoDepthStencil = true,
AutoDepthStencilFormat = Format.D16,
PresentationInterval = PresentInterval.Immediate
};
_device = new Device(new Direct3D(), 0, DeviceType.Hardware, preview3DTarget.Handle, CreateFlags.HardwareVertexProcessing, _presentParameters);
_device.SetRenderState(RenderState.ZEnable, true);
_device.SetRenderState(RenderState.Lighting, false);
_device.SetRenderState(RenderState.CullMode, Cull.None);
The following code is called from the control OnPaint method to Render the Preview
_device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
_device.BeginScene();
float ratio = (float)preview3DTarget.ClientSize.Width / preview3DTarget.ClientSize.Height;
_device.SetTransform(TransformState.Projection, Matrix.OrthoLH(200, 200, 0.0f, 200.0f));
_device.SetTransform(TransformState.World, Matrix.Translation(-center.X, -center.Y, -center.Z));
_device.SetTransform(TransformState.View, Matrix.RotationYawPitchRoll(Math.radians(0),
Math.radians(0), 0.0f) *
Matrix.Translation(0f, 0f, 100));
for (int i = 0; i < this.vertices.Count; i += 4)
{
_device.DrawPrimitives(PrimitiveType.TriangleStrip, i, 2);
}
for (int i = 0; i < this.points.Count; i += 5)
{
_device.DrawPrimitives(PrimitiveType.LineStrip, i + this.vertices.Count, 4);
}
_device.EndScene();
_device.Present();
All of this code works correctly, however after the first time the preview is rendered, AutoCAD stops functioning properly. Objects within the AutoCAD model space cannot be selected. If a REGEN is called, the screen is clear and nothing is redrawn. If a new object is drawn, it appears but, like the existing objects, cannot be selected.
I'm guessing my application is in someway taking over control of the DirectX engine and stopping AutoCAD from using it. Is there some sort of release/dispose call that I'm missing that might allow my application and AutoCAD to use DirectX at the same time?