注册 登录
明经CAD社区 返回首页

sieben的个人空间 http://www.mjtd.com/?43121 [收藏] [复制] [分享] [RSS]

日志

使用GraphicsManager预览实体

热度 1已有 684 次阅读2024-5-28 15:59 |个人分类:揾食艰难|系统分类:开发

注:此代码在ACAD 2019和ZWCAD 2022,2023可行;Kean大佬2007年那个代码已不能用于新版ACAD;感谢ZWCAD的周工!
public static Bitmap GetImage_Kernel_Entity(Color backgroundColor, Entity tEnt, Extents3d? viewExt, Size imageSize, double zoomFactor = 1.0)
{
#if _ACAD
try
{
if (imageSize.Width == 0 || imageSize.Height == 0) return null;
if (viewExt == null)
{
viewExt = tEnt.GetExtents_3d();// aEnt.GetExtend3d( tEnt);
if (viewExt == null || viewExt.Value.Length_X() == 0 || viewExt.Value.Length_Y() == 0) return null;
}
var doc = ac.doc;
var m_Manager = doc.GraphicsManager;
KernelDescriptor kDecs = new KernelDescriptor();
kDecs.addRequirement(KernelDescriptor.Drawing3D);
GraphicsKernel gk = Manager.AcquireGraphicsKernel(kDecs);
var m_Device = m_Manager.CreateAutoCADOffScreenDevice(gk);
var m_Model = m_Manager.CreateAutoCADModel(gk);

var m_View = new View();
try
{
m_Device.BackgroundColor = backgroundColor;
m_Device.OnSize(imageSize);
var center = viewExt.Value.Center_XY();
m_View.EraseAll();
m_View.SetView(center, center, Vector3d.YAxis, viewExt.Value.Length_X(), viewExt.Value.Length_Y());
m_View.ZoomExtents(viewExt.Value.MinPoint, viewExt.Value.MaxPoint);
m_View.Zoom(m_View.FieldWidth * zoomFactor / imageSize.Width);

m_Device.Add(m_View);
Entity tEnt2 = GetOppositeColorEntity(backgroundColor, tEnt);
m_View.Add(tEnt2, m_Model);

m_View.Invalidate();
m_View.Update();
Rectangle rect = new Rectangle(0, 0, imageSize.Width, imageSize.Height);
return m_View.GetSnapshot(rect);
}
catch (System.Exception ex2)
{
ae.WriteMessage(ex2);
return null;
}
finally
{
if (m_Device != null) m_Device.Erase(m_View);
if (m_View != null)
{
m_View.EraseAll();
m_View.Dispose();
m_View = null;
}
if (m_Manager != null)
{
if (m_Model != null)
{
m_Model.Dispose();
m_Model = null;
}
if (m_Device != null)
{
m_Device.Dispose();
m_Device = null;
}
}
}
}
catch (System.Exception ex1)
{
ae.WriteMessage(ex1);
return null;
}
#else
try
{
if (imageSize.Width == 0 || imageSize.Height == 0) return null;
if (viewExt == null)
{
viewExt = tEnt.GetExtents_3d();// aEnt.GetExtend3d( tEnt);
if (viewExt == null || viewExt.Value.Length_X() == 0 || viewExt.Value.Length_Y() == 0) return null;
}
var doc = ac.doc;
var m_Manager = doc.GraphicsManager;
var gk = new GraphicsKernel();
var m_Device = m_Manager.CreateZWCADOffScreenDevice(gk);
var m_Model = m_Manager.CreateZWCADModel(gk);
var m_View = new View();
try
{
m_Device.BackgroundColor = backgroundColor;
m_Device.OnSize(imageSize);
m_Device.Add(m_View);

if (!SetView_Extents3d(m_View, viewExt.Value, imageSize.Width, imageSize.Height, zoomFactor))
{
return null;
}

Display_Entity(backgroundColor, m_View, m_Model, tEnt);
return m_Device.GetSnapshot(m_View.Viewport);
}
catch (System.Exception ex2)
{
ae.WriteMessage(ex2);
return null;
}
finally
{
if (m_Device != null) m_Device.Erase(m_View);
if (m_View != null)
{
m_View.EraseAll();
m_View.Dispose();
m_View = null;
}
if (m_Manager != null)
{
if (m_Model != null)
{
m_Model.Dispose();
m_Model = null;
}
if (m_Device != null)
{
m_Device.Dispose();
m_Device = null;
}
}
}
}
catch (System.Exception ex1)
{
ae.WriteMessage(ex1);
return null;
}
#endif
}

路过

雷人

握手

鲜花

鸡蛋

发表评论 评论 (2 个评论)

回复 你有种再说一遍 2024-7-17 17:17
写得太肉酸了,多用using,少用dispose,using有try的
回复 sieben 2024-7-17 21:41
你有种再说一遍: 写得太肉酸了,多用using,少用dispose,using有try的
谢谢大佬指教!

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-2-10 07:37 , Processed in 0.085648 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部