本文主要是介绍Arcgis 符号库中图形到Grid控件的加载,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
///调用方法private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
// 如果是符号样式列
if (e.Column.Equals(colSTYLEIMAGE))
{
string strStyleCode = e.DisplayText;
Rectangle r = e.Bounds;
if (GISOpr.getInstance().AllStyleList.Contains(strStyleCode))
{
ISymbol symbol = GISOpr.getInstance().AllStyleList[strStyleCode] as ISymbol;
if (symbol != null)
{
Image image = GISGlobal.SymbolToBitmp(symbol, r.Width, r.Height, 1);
e.Graphics.DrawImageUnscaled(image, r);
e.Handled = true;
}
}
}
}
/// <summary>
/// 全局初始化方法:
/// 初始化数据库连接
/// 初始化Nhibernate环境
/// 加载Nhibernate类库及映射文件
/// 同时获取初始化系统参数
/// 该方法在所有方法之前调用
/// </summary>
/// <param name="user"></param>
/// <returns></returns>
public bool GlobalInit(ZUser user)
{
bool bresult = false;
DBCore db = new DBCore(true);
// 设置当前用户
currUser = user;
try
{
paramList = db.GetAll(typeof(Tbsysparams), "Paramid");
foreach (Tbsysparams param in paramList)
{
if (param.Paramenname.ToUpper() == "SDESERVER")
m_sServer = param.Paramvalue;
if (param.Paramenname.ToUpper() == "SDEINSTANCE")
m_sInstance = param.Paramvalue;
if (param.Paramenname.ToUpper() == "SDEVERSION")
m_sVersion = param.Paramvalue;
if (param.Paramenname.ToUpper() == "SDEUSER")
m_sUser = param.Paramvalue;
if (param.Paramenname.ToUpper() == "SDEPASSWORD")
m_sPassword = param.Paramvalue;
if (param.Paramenname.ToUpper() == "GEOOBJFIELDNAME")
m_sGeoObjFieldName = param.Paramvalue;
if (param.Paramenname.ToUpper() == "SERVERSTYLEFILENAME")
m_sRelStyleFileDir = param.Paramvalue;
}
bresult = true;
}
finally
{
db.CloseSession();
db = null;
}
return bresult;
}
/// <summary>
/// 全局初始化ArcEngine组件 方法
/// 与CloseGISLicense方法对应,只需要调用一次
/// 该方法在OpenGISConn方法之前调用,以保证组件被正常初始化了
/// </summary>
/// <param name="err">返回初始化过程中的提示信息</param>
/// <returns>返回true表示初始成功</returns>
public bool InitGISLicense(out string err)
{
bool bresult = true;
esriLicenseStatus licenseStatus;
err = string.Empty;
this.m_nLicenseSem += 1;
// 初次初始化
if (this.m_nLicenseSem == 1 && this.m_pAoInitialize == null)
{
// first try copy protection EngineGeoDB
licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
if (licenseStatus == esriLicenseStatus.esriLicenseNotLicensed)
{
// next try Desktop Desktop ArcEngine
licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
if (licenseStatus == esriLicenseStatus.esriLicenseNotLicensed ||
licenseStatus == esriLicenseStatus.esriLicenseUnavailable)
licenseStatus = this.CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
}
if (licenseStatus == esriLicenseStatus.esriLicenseNotLicensed)
{
err = "You are not licensed to run this product";
bresult = false;
}
else if (licenseStatus == esriLicenseStatus.esriLicenseUnavailable)
{
err = "There are insufient licenses to run";
bresult = false;
}
else if (licenseStatus == esriLicenseStatus.esriLicenseFailure)
{
err = "Unexpected license failure please contact you administrator";
bresult = false;
}
else if (licenseStatus == esriLicenseStatus.esriLicenseAlreadyInitialized)
{
err = "You license has already been initialized please check you implementation";
bresult = false;
}
else if (licenseStatus == esriLicenseStatus.esriLicenseCheckedOut)
err = "Licenses checked out successfully";
}
return bresult;
}
/// <summary>
/// 初始化GIS环境,建立SDE数据连接,为地图加载做准备
/// </summary>
/// <returns></returns>
public bool OpenGISConn()
{
Hashtable propList = new Hashtable();
if (!bconnected)
{
try
{
propList.Add("SERVER", m_sServer);
propList.Add("INSTANCE", m_sInstance);
propList.Add("VERSION", m_sVersion);
propList.Add("USER", m_sUser);
propList.Add("PASSWORD", m_sPassword);
ws = this.GetWorkspace(propList, "esriDataSourcesGDB.SdeWorkspaceFactory");
bconnected = true;
}
finally
{
propList = null;
}
}
return bconnected;
}
/// <summary>
/// 加载系统所设置的ServerStyle符号
/// 并将符号全部保存到当前的HashTable中,Key为地物代码,Value为对应符号的引用
/// </summary>
/// <returns></returns>
public bool LoadServerStyle(string sAppPath)
{
// ServerStyle类
ServerStyleGalleryClass styleFile = null;
// 导入符号接口
IStyleGallery pStyleGallery = null;
// 设置符号文件位置
IStyleGalleryStorage pStyleStore = null;
// 符号枚举接口
IEnumStyleGalleryItem pEnuStyleItem = null;
// 单个符号对象
IStyleGalleryItem pStyleItem = null;
string sFullPath = sAppPath + "//" + m_sRelStyleFileDir;
string sFileName = string.Empty;
this.m_nStyleSem += 1;
if (this.m_nStyleSem==1 && !bStyleLoaded && Directory.Exists(sFullPath))
{
styleFile = new ServerStyleGalleryClass();
pStyleGallery = styleFile as IStyleGallery;
pStyleStore = styleFile as IStyleGalleryStorage;
string[] fileList = Directory.GetFiles(sFullPath, "*.ServerStyle");
for (int i = 0; i < fileList.Length; i++)
{
sFileName = fileList[i];
// 导入*.ServerStyle符号
pStyleGallery.ImportStyle(sFileName);
pStyleStore.TargetFile = sFileName;
// 枚举所有点符号
pEnuStyleItem = pStyleGallery.get_Items("Marker Symbols", sFileName, "");
pEnuStyleItem.Reset();
pStyleItem = pEnuStyleItem.Next();
while (pStyleItem != null)
{
// 名称,分类都设置为GeObjNum
if (!stylesList.Contains(pStyleItem.Name))
stylesList.Add(pStyleItem.Name, pStyleItem.Item);
pStyleItem = pEnuStyleItem.Next();
}
// 枚举所有线符号
pEnuStyleItem = pStyleGallery.get_Items("Line Symbols", sFileName, "");
pEnuStyleItem.Reset();
pStyleItem = pEnuStyleItem.Next();
while (pStyleItem != null)
{
// 名称,分类都设置为GeObjNum
if (!stylesList.Contains(pStyleItem.Name))
stylesList.Add(pStyleItem.Name, pStyleItem.Item);
pStyleItem = pEnuStyleItem.Next();
}
// 枚举所有面符号
pEnuStyleItem = pStyleGallery.get_Items("Fill Symbols", sFileName, "");
pEnuStyleItem.Reset();
pStyleItem = pEnuStyleItem.Next();
while (pStyleItem != null)
{
// 名称,分类都设置为GeObjNum
// 将符号的地物代码、及符号放入全局符号列表中
if (!stylesList.Contains(pStyleItem.Name))
stylesList.Add(pStyleItem.Name, pStyleItem.Item);
pStyleItem = pEnuStyleItem.Next();
}
bStyleLoaded = true;
}
}
return bStyleLoaded;
}
/// <summary>
/// 将ISymbol 转换为bitmap对象
/// </summary>
/// <param name="pSymbol">符号对象</param>
/// <param name="iwidth">显示的image宽</param>
/// <param name="iheight">显示的image高</param>
/// <param name="lGap">显示的image边距:1或2即可</param>
/// <returns></returns>
public static System.Drawing.Bitmap SymbolToBitmp(ESRI.ArcGIS.Display.ISymbol pSymbol, int iwidth, int iheight, int lGap)
{
///根据高宽创建图象
Bitmap bmp = new Bitmap(iwidth, iheight);
Graphics gImage = Graphics.FromImage(bmp);//转化成Graphics
gImage.Clear(Color.White); //清除底色,设置为白色
double dpi = gImage.DpiX; //
IEnvelope pEnvelope = new EnvelopeClass();//创建矩形范围
pEnvelope.PutCoords(0, 0, (double)bmp.Width, (double)bmp.Height);
//设置边界值
tagRECT deviceRect;
deviceRect.left = lGap;
deviceRect.right = bmp.Width - lGap;
deviceRect.top = lGap;
deviceRect.bottom = bmp.Height - lGap;
IDisplayTransformation pDisplayTransformation = new DisplayTransformationClass();
pDisplayTransformation.VisibleBounds = pEnvelope;
pDisplayTransformation.Bounds = pEnvelope;
pDisplayTransformation.set_DeviceFrame(ref deviceRect);
pDisplayTransformation.Resolution = dpi;
IGeometry pGeo = CreateSymShape(pSymbol, pEnvelope);
System.IntPtr hdc = new IntPtr();
hdc = gImage.GetHdc();
pSymbol.SetupDC((int)hdc, pDisplayTransformation);
pSymbol.Draw(pGeo);
pSymbol.ResetDC();
gImage.ReleaseHdc(hdc);
gImage.Dispose();
return bmp;
}
/// <summary>
/// 根据符号类型 获取该符号的显示空间描述
/// </summary>
/// <param name="pSymbol"></param>
/// <param name="pEnvelope"></param>
/// <returns></returns>
public static ESRI.ArcGIS.Geometry.IGeometry CreateSymShape(ISymbol pSymbol, IEnvelope pEnvelope)
{
ESRI.ArcGIS.Display.IMarkerSymbol IMarkerSym;
IMarkerSym = pSymbol as IMarkerSymbol;
if (IMarkerSym != null)
{
IArea pArea;
pArea = pEnvelope as IArea;
return pArea.Centroid as IGeometry;
}
else
{
ESRI.ArcGIS.Display.ILineSymbol IlineSym;
ESRI.ArcGIS.Display.ITextSymbol ITextSym;
IlineSym = pSymbol as ILineSymbol;
ITextSym = pSymbol as ITextSymbol;
if (IlineSym != null || ITextSym != null)
{
ESRI.ArcGIS.Geometry.IPolyline IpLine;
IpLine = new PolylineClass();
IpLine.FromPoint = pEnvelope.LowerLeft;
IpLine.ToPoint = pEnvelope.UpperRight;
return IpLine as IGeometry;
}
else
{
return pEnvelope as IGeometry;
}
}
}
这篇关于Arcgis 符号库中图形到Grid控件的加载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!