我正在爲AutoCAD中嵌入的應用程序開發3D零件預覽器。AutoCAD中的DirectX查看器使AutoCAD停止正常工作
3D是通過SlimDX(2.0版本)使用DirectX實現的。
我使用的.NET Framework 3.5
下面的代碼初始化了DirectX設備
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);
下面的代碼是從控制OnPaint方法調用,以呈現預覽
_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();
所有這些代碼工作正常,但是之後第一次渲染預覽時,AutoCAD將停止正常工作。 AutoCAD模型空間內的對象無法選擇。如果REGEN被調用,屏幕清晰並且沒有重繪。如果繪製了新對象,則會顯示但不能像現有對象那樣選擇。
我猜我的應用程序在某種程度上接管了DirectX引擎的控制,並停止使用它。是否有某種我錯過的發佈/處置調用可能允許我的應用程序和AutoCAD同時使用DirectX?