ArcGis 開啟CAD檔案的幾種方法
阿新 • • 發佈:2019-01-23
方法一、
iworkspacefactory pworkspacefactory;
ifeatureworkspace pfeatureworkspace;
ifeaturelayer pfeaturelayer;
ifeaturedataset pfeaturedataset;
//開啟cad資料集
pworkspacefactory = new cadworkspacefactoryclass();
pfeatureworkspace = (ifeatureworkspace)pworkspacefactory.openfromfile(directorypath, 0);
//開啟一個要素集
pfeaturedataset = pfeatureworkspace.openfeaturedataset(filename);
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = new cadannotationlayerclass();
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = new featurelayerclass();
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
}
}
方法二、
//--定義工作空間,並用cadworkspacefactoryclass()例項化它
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
//--開啟相應的工作空間,並賦值給要素空間,openfromfile()
//--中的引數為cad資料夾的路徑
ifeatureworkspace pfeatureworkspace = pworkspacefactory.openfromfile(directorypath, 0) as ifeatureworkspace;
/*--開啟線要素類,如果要開啟點型別的要素,需要把下邊的程式碼該成:
*-- ifeatureclass pfeatureclass = pfeatureworkspace.openfeatureclass (filename + ":point");
*-- 由此可見filename為cad圖的名字,後邊加上要開啟的要素類的型別,中間用冒號
*-- 隔開,大家可以想想多邊形和標註是怎麼開啟的。 */
//point polyline
ifeatureclass pfeatureclass = pfeatureworkspace.openfeatureclass(filename + ":point");
ifeaturelayer pfeaturelayer = new featurelayerclass();
pfeaturelayer.featureclass = pfeatureclass;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
方法三、
/*--下邊的兩行程式碼是定義一個cad工作空間,然後開啟它,但這次不是賦值給
--ifeatureworkspace物件,而是賦值給iworkspace定義的物件*/
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
iworkspace pworkspace = pworkspacefactory.openfromfile(directorypath, 0);
//--定義一個cad畫圖空間,並把上邊開啟的工作空間賦給它
icaddrawingworkspace pcaddrawingworkspace = pworkspace as icaddrawingworkspace;
//--定義一個cad的畫圖資料集,並且開啟上邊指定的工作空間中一幅cad圖
//--然後賦值給cad資料集
icaddrawingdataset pcaddrawingdataset = pcaddrawingworkspace.opencaddrawingdataset(filename);
//--通過icadlayer類,把上邊得到的cad資料局賦值給icadlayer類物件的
//--caddrawingdataset屬性
icadlayer pcadlayer = new cadlayerclass();
pcadlayer.caddrawingdataset = pcaddrawingdataset;
//--利用mapcontrol載入cad層
this.axmc_main.map.addlayer(pcadlayer);
this.axmc_main.activeview.**();
方法四、
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
ifeatureworkspace pfeatureworkspace = pworkspacefactory.openfromfile(directorypath, 0) as ifeatureworkspace;
ifeaturedataset pfeaturedataset = pfeatureworkspace.openfeaturedataset(filename);
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
ifeaturelayer pfeaturelayer = new featurelayerclass();
pfeaturelayer.featureclass = pfeatureclass;
//icadlayer pcadlayer = new cadlayerclass();
//pcadlayer = pfeaturelayer as icadlayer;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
}
方法五、
//add passed file to mapcontrol
icaddrawingdataset pcaddrawingdataset = getcaddrawingdataset(directorypath, filename);
if (pcaddrawingdataset == null) return;
icadlayer pcadlayer = new cadlayerclass();
pcadlayer.caddrawingdataset = pcaddrawingdataset;
pcadlayer.name = filename;
this.axmc_main.addlayer(pcadlayer, 0);
/// <summary>
/// 獲取記錄集
/// </summary>
/// <param name="directorypath"></param>
/// <param name="filename"></param>
/// <returns></returns>
private icaddrawingdataset getcaddrawingdataset(string directorypath, string filename)
{
//create a workspacename object
iworkspacename pworkspacename = new workspacenameclass();
pworkspacename.workspacefactoryprogid = "esridatasourcesfile.cadworkspacefactory";
pworkspacename.pathname = directorypath;
//create a caddrawingname object
idatasetname pdatasetname = new caddrawingnameclass();
pdatasetname.name = filename;
pdatasetname.workspacename = pworkspacename;
//open the cad drawing
iname pname = (iname)pdatasetname;
return (icaddrawingdataset)pname.open();
}
需要注意
如果你的arcgis地理資訊系統 版本是v9.2的話,可能是因為你的檔名中含有中文字元的原因.據我所知,在arcgis地理資訊系統 9.2中無法顯示檔名含有中文字元的cad檔案.你可以在arcmap中直接開啟試試,如果也無法顯
此文來自: 馬開東部落格 轉載請註明出處 網址: http://www.makaidong.com
示的話,可能是檔名的問題(注:路徑中也不能含有中文字元).
gis地理資訊系統 for web應用開發之道
外掛式gis地理資訊系統 應用框架的設計與實現
public void esridatasourcesfile_icaddrawingdataset_example(string nameoffile, string nameofcaddrawing)
{
//set the workspace factory
iworkspacefactory workspacefactory = new esri.arcgis地理資訊系統 .datasourcesfile.cadworkspacefactoryclass();
//open the workspace
iworkspace workspace = workspacefactory.openfromfile(nameoffile, 0);
//set the caddrawingworkspace
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace caddrawingworkspace = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace)workspace;
//open the caddrawingdataset
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset caddrawingdataset = caddrawingworkspace.opencaddrawngdataset(nameofcaddrawing);
//create the cadlayer
esri.arcgis地理資訊系統 .carto.icadlayer cadlayer = new esri.arcgis地理資訊系統 .carto.cadlayerclass();
//set the cadlayer to the caddrawingdataset
cadlayer.caddrawingdataset = caddrawingdataset;
//list if the caddrawingdataset exists
console.writeline("does drawing exist: {0}" , cadlayer.caddrawingdataset.exists );
//list the caddrawingdataset filepath
console.writeline("cad drawing path: {0}" , cadlayer.caddrawingdataset.filepath );
//list if the caddrawingdataset is 2d
console.writeline("is drawing 2d?: {0}" , cadlayer.caddrawingdataset.is2d);
//list if the caddrawingdataset is 3d
console.writeline("is drawing 3d?: {0}" , cadlayer.caddrawingdataset.is3d );
//list if the caddrawingdataset is an autocad drawing file
console.writeline("is drawing from autocad?: {0}" , cadlayer.caddrawingdataset.isautocad );
//list if the caddrawingdataset is an microsation drawing file
console.writeline("is drawing from microstation?: {0}" , cadlayer.caddrawingdataset.isdgn); }
中間過程實驗程式碼:
實驗1
public static string addcadlayer(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
//// 增加圖層
//esri.arcgis地理資訊系統 .carto.featurelayer pfeaturelayer = null;
//ifeaturelayer pfeaturelayer;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
//pworkspacefactory = new cadworkspacefactoryclass();
//pworkspacefactory = new pworkspacefactory();
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace pfeatureworkspace = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset pfeaturedataset = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset)pfeatureworkspace.opencaddrawingdataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
esri.arcgis地理資訊系統 .carto.icadlayer pfeatureclasscontainer = new esri.arcgis地理資訊系統 .carto.cadlayerclass();
pfeatureclasscontainer.caddrawingdataset = pfeaturedataset;
//ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
//for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
//{
// ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
// if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
// {
// //如果是註記,則添加註記層
// pfeaturelayer = (esri.arcgis地理資訊系統 .carto.featurelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
// }
// else//如果是點、線、面,則新增要素層
// {
// pfeaturelayer = (esri.arcgis地理資訊系統 .carto.featurelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
// pfeaturelayer.name = pfeatureclass.aliasname;
// }
//}
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
//if (pfeaturelayer != null)
//{
map.addlayer(pfeatureclasscontainer);
// //map.movelayer(pfeaturelayer, map.layercount - 1);
//}
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}
實驗2
public static string addcadlayer2(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
// 增加圖層
esri.arcgis地理資訊系統 .carto.ifeaturelayer pfeaturelayer = null;
//ifeaturelayer pfeaturelayer;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
//pworkspacefactory = new cadworkspacefactoryclass();
//pworkspacefactory = new pworkspacefactory();
ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
ifeaturedataset pfeaturedataset = (ifeaturedataset)pfeatureworkspace.openfeaturedataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 3; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
//pfeaturelayer = new cadannotationlayerclass();
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
esri.arcgis地理資訊系統 .carto.igeofeaturelayer iglayer = (esri.arcgis地理資訊系統 .carto.igeofeaturelayer)pfeaturelayer;
esri.arcgis地理資訊系統 .carto.isimplerenderer isr = (esri.arcgis地理資訊系統 .carto.isimplerenderer)mapcontext.createobject("esricarto.simplerenderer");
esri.arcgis地理資訊系統 .display.irgbcolor irgbc = (esri.arcgis地理資訊系統 .display.irgbcolor)mapcontext.createobject("esridisplay.rgbcolor");
irgbc.red = 255;
irgbc.green = 0;
irgbc.blue = 0;
esri.arcgis地理資訊系統 .geometry.esrigeometrytype featype = pfeaturelayer.featureclass.shapetype;
if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypoint)
{
esri.arcgis地理資訊系統 .display.isimplemarkersymbol ifs = (esri.arcgis地理資訊系統 .display.isimplemarkersymbol)mapcontext.createobject("esridisplay.simplemarkersymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypolyline)
{
esri.arcgis地理資訊系統 .display.isimplelinesymbol ifs = (esri.arcgis地理資訊系統 .display.isimplelinesymbol)mapcontext.createobject("esridisplay.simplelinesymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypolygon)
{
esri.arcgis地理資訊系統 .display.isimplefillsymbol ifs = (esri.arcgis地理資訊系統 .display.isimplefillsymbol)mapcontext.createobject("esridisplay.simplefillsymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else
{
esri.arcgis地理資訊系統 .display.isimplelinesymbol ifs = (esri.arcgis地理資訊系統 .display.isimplelinesymbol)mapcontext.createobject("esridisplay.simplelinesymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
//throw new exception("圖層型別不確定!");
}
iglayer.renderer = (esri.arcgis地理資訊系統 .carto.ifeaturerenderer)isr;
//imapserver ms = (imapserver)in_mapcontext.serverobject;
//imapserverobjects2 mso = (imapserverobjects2)ms;
//string mapname = ms.defaultmapname;
//imap map = mso.get_map(mapname);
//map.addlayer(pfeaturelayer);
//mso.refreshserverobjects();
}
}
//// 從上下文物件中得到伺服器物件
//imapserver ms = (imapserver)mapcontext.serverobject;
//imapserverinfo mapinfo = ms.getserverinfo(ms.defaultmapname);
//imapdescription newmapdesc = mapinfo.defaultmapdescription;
//// 釋放伺服器上下文物件
//mapcontext.releasecontext();
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
if (pfeaturelayer != null)
{
map.addlayer(pfeaturelayer);
//移動層到底層
//map.movelayer(pfeaturelayer, map.layercount - 1);
}
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}
最終除錯正確程式碼:
public static string addcadlayer(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
// 增加圖層
esri.arcgis地理資訊系統 .carto.ifeaturelayer pfeaturelayer = null;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
ifeaturedataset pfeaturedataset = (ifeaturedataset)pfeatureworkspace.openfeaturedataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
map.addlayer(pfeaturelayer);
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
}
}
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}
iworkspacefactory pworkspacefactory;
ifeatureworkspace pfeatureworkspace;
ifeaturelayer pfeaturelayer;
ifeaturedataset pfeaturedataset;
//開啟cad資料集
pworkspacefactory = new cadworkspacefactoryclass();
pfeatureworkspace = (ifeatureworkspace)pworkspacefactory.openfromfile(directorypath, 0);
//開啟一個要素集
pfeaturedataset = pfeatureworkspace.openfeaturedataset(filename);
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = new cadannotationlayerclass();
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = new featurelayerclass();
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
}
}
方法二、
//--定義工作空間,並用cadworkspacefactoryclass()例項化它
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
//--開啟相應的工作空間,並賦值給要素空間,openfromfile()
//--中的引數為cad資料夾的路徑
ifeatureworkspace pfeatureworkspace = pworkspacefactory.openfromfile(directorypath, 0) as ifeatureworkspace;
/*--開啟線要素類,如果要開啟點型別的要素,需要把下邊的程式碼該成:
*-- ifeatureclass pfeatureclass = pfeatureworkspace.openfeatureclass (filename + ":point");
*-- 由此可見filename為cad圖的名字,後邊加上要開啟的要素類的型別,中間用冒號
*-- 隔開,大家可以想想多邊形和標註是怎麼開啟的。 */
//point polyline
ifeatureclass pfeatureclass = pfeatureworkspace.openfeatureclass(filename + ":point");
ifeaturelayer pfeaturelayer = new featurelayerclass();
pfeaturelayer.featureclass = pfeatureclass;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
方法三、
/*--下邊的兩行程式碼是定義一個cad工作空間,然後開啟它,但這次不是賦值給
--ifeatureworkspace物件,而是賦值給iworkspace定義的物件*/
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
iworkspace pworkspace = pworkspacefactory.openfromfile(directorypath, 0);
//--定義一個cad畫圖空間,並把上邊開啟的工作空間賦給它
icaddrawingworkspace pcaddrawingworkspace = pworkspace as icaddrawingworkspace;
//--定義一個cad的畫圖資料集,並且開啟上邊指定的工作空間中一幅cad圖
//--然後賦值給cad資料集
icaddrawingdataset pcaddrawingdataset = pcaddrawingworkspace.opencaddrawingdataset(filename);
//--通過icadlayer類,把上邊得到的cad資料局賦值給icadlayer類物件的
//--caddrawingdataset屬性
icadlayer pcadlayer = new cadlayerclass();
pcadlayer.caddrawingdataset = pcaddrawingdataset;
//--利用mapcontrol載入cad層
this.axmc_main.map.addlayer(pcadlayer);
this.axmc_main.activeview.**();
方法四、
iworkspacefactory pworkspacefactory = new cadworkspacefactoryclass();
ifeatureworkspace pfeatureworkspace = pworkspacefactory.openfromfile(directorypath, 0) as ifeatureworkspace;
ifeaturedataset pfeaturedataset = pfeatureworkspace.openfeaturedataset(filename);
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
ifeaturelayer pfeaturelayer = new featurelayerclass();
pfeaturelayer.featureclass = pfeatureclass;
//icadlayer pcadlayer = new cadlayerclass();
//pcadlayer = pfeaturelayer as icadlayer;
this.axmc_main.map.addlayer(pfeaturelayer);
this.axmc_main.activeview.**();
}
方法五、
//add passed file to mapcontrol
icaddrawingdataset pcaddrawingdataset = getcaddrawingdataset(directorypath, filename);
if (pcaddrawingdataset == null) return;
icadlayer pcadlayer = new cadlayerclass();
pcadlayer.caddrawingdataset = pcaddrawingdataset;
pcadlayer.name = filename;
this.axmc_main.addlayer(pcadlayer, 0);
/// <summary>
/// 獲取記錄集
/// </summary>
/// <param name="directorypath"></param>
/// <param name="filename"></param>
/// <returns></returns>
private icaddrawingdataset getcaddrawingdataset(string directorypath, string filename)
{
//create a workspacename object
iworkspacename pworkspacename = new workspacenameclass();
pworkspacename.workspacefactoryprogid = "esridatasourcesfile.cadworkspacefactory";
pworkspacename.pathname = directorypath;
//create a caddrawingname object
idatasetname pdatasetname = new caddrawingnameclass();
pdatasetname.name = filename;
pdatasetname.workspacename = pworkspacename;
//open the cad drawing
iname pname = (iname)pdatasetname;
return (icaddrawingdataset)pname.open();
}
需要注意
如果你的arcgis地理資訊系統 版本是v9.2的話,可能是因為你的檔名中含有中文字元的原因.據我所知,在arcgis地理資訊系統 9.2中無法顯示檔名含有中文字元的cad檔案.你可以在arcmap中直接開啟試試,如果也無法顯
此文來自: 馬開東部落格 轉載請註明出處 網址: http://www.makaidong.com
示的話,可能是檔名的問題(注:路徑中也不能含有中文字元).
gis地理資訊系統 for web應用開發之道
外掛式gis地理資訊系統 應用框架的設計與實現
public void esridatasourcesfile_icaddrawingdataset_example(string nameoffile, string nameofcaddrawing)
{
//set the workspace factory
iworkspacefactory workspacefactory = new esri.arcgis地理資訊系統 .datasourcesfile.cadworkspacefactoryclass();
//open the workspace
iworkspace workspace = workspacefactory.openfromfile(nameoffile, 0);
//set the caddrawingworkspace
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace caddrawingworkspace = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace)workspace;
//open the caddrawingdataset
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset caddrawingdataset = caddrawingworkspace.opencaddrawngdataset(nameofcaddrawing);
//create the cadlayer
esri.arcgis地理資訊系統 .carto.icadlayer cadlayer = new esri.arcgis地理資訊系統 .carto.cadlayerclass();
//set the cadlayer to the caddrawingdataset
cadlayer.caddrawingdataset = caddrawingdataset;
//list if the caddrawingdataset exists
console.writeline("does drawing exist: {0}" , cadlayer.caddrawingdataset.exists );
//list the caddrawingdataset filepath
console.writeline("cad drawing path: {0}" , cadlayer.caddrawingdataset.filepath );
//list if the caddrawingdataset is 2d
console.writeline("is drawing 2d?: {0}" , cadlayer.caddrawingdataset.is2d);
//list if the caddrawingdataset is 3d
console.writeline("is drawing 3d?: {0}" , cadlayer.caddrawingdataset.is3d );
//list if the caddrawingdataset is an autocad drawing file
console.writeline("is drawing from autocad?: {0}" , cadlayer.caddrawingdataset.isautocad );
//list if the caddrawingdataset is an microsation drawing file
console.writeline("is drawing from microstation?: {0}" , cadlayer.caddrawingdataset.isdgn); }
中間過程實驗程式碼:
實驗1
public static string addcadlayer(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
//// 增加圖層
//esri.arcgis地理資訊系統 .carto.featurelayer pfeaturelayer = null;
//ifeaturelayer pfeaturelayer;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
//pworkspacefactory = new cadworkspacefactoryclass();
//pworkspacefactory = new pworkspacefactory();
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace pfeatureworkspace = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset pfeaturedataset = (esri.arcgis地理資訊系統 .datasourcesfile.icaddrawingdataset)pfeatureworkspace.opencaddrawingdataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
esri.arcgis地理資訊系統 .carto.icadlayer pfeatureclasscontainer = new esri.arcgis地理資訊系統 .carto.cadlayerclass();
pfeatureclasscontainer.caddrawingdataset = pfeaturedataset;
//ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
//for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
//{
// ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
// if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
// {
// //如果是註記,則添加註記層
// pfeaturelayer = (esri.arcgis地理資訊系統 .carto.featurelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
// }
// else//如果是點、線、面,則新增要素層
// {
// pfeaturelayer = (esri.arcgis地理資訊系統 .carto.featurelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
// pfeaturelayer.name = pfeatureclass.aliasname;
// }
//}
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
//if (pfeaturelayer != null)
//{
map.addlayer(pfeatureclasscontainer);
// //map.movelayer(pfeaturelayer, map.layercount - 1);
//}
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}
實驗2
public static string addcadlayer2(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
// 增加圖層
esri.arcgis地理資訊系統 .carto.ifeaturelayer pfeaturelayer = null;
//ifeaturelayer pfeaturelayer;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
//pworkspacefactory = new cadworkspacefactoryclass();
//pworkspacefactory = new pworkspacefactory();
ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
ifeaturedataset pfeaturedataset = (ifeaturedataset)pfeatureworkspace.openfeaturedataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 3; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
//pfeaturelayer = new cadannotationlayerclass();
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
esri.arcgis地理資訊系統 .carto.igeofeaturelayer iglayer = (esri.arcgis地理資訊系統 .carto.igeofeaturelayer)pfeaturelayer;
esri.arcgis地理資訊系統 .carto.isimplerenderer isr = (esri.arcgis地理資訊系統 .carto.isimplerenderer)mapcontext.createobject("esricarto.simplerenderer");
esri.arcgis地理資訊系統 .display.irgbcolor irgbc = (esri.arcgis地理資訊系統 .display.irgbcolor)mapcontext.createobject("esridisplay.rgbcolor");
irgbc.red = 255;
irgbc.green = 0;
irgbc.blue = 0;
esri.arcgis地理資訊系統 .geometry.esrigeometrytype featype = pfeaturelayer.featureclass.shapetype;
if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypoint)
{
esri.arcgis地理資訊系統 .display.isimplemarkersymbol ifs = (esri.arcgis地理資訊系統 .display.isimplemarkersymbol)mapcontext.createobject("esridisplay.simplemarkersymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypolyline)
{
esri.arcgis地理資訊系統 .display.isimplelinesymbol ifs = (esri.arcgis地理資訊系統 .display.isimplelinesymbol)mapcontext.createobject("esridisplay.simplelinesymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else if (featype == esri.arcgis地理資訊系統 .geometry.esrigeometrytype.esrigeometrypolygon)
{
esri.arcgis地理資訊系統 .display.isimplefillsymbol ifs = (esri.arcgis地理資訊系統 .display.isimplefillsymbol)mapcontext.createobject("esridisplay.simplefillsymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
}
else
{
esri.arcgis地理資訊系統 .display.isimplelinesymbol ifs = (esri.arcgis地理資訊系統 .display.isimplelinesymbol)mapcontext.createobject("esridisplay.simplelinesymbol");
ifs.color = (esri.arcgis地理資訊系統 .display.icolor)irgbc;
isr.symbol = (esri.arcgis地理資訊系統 .display.isymbol)ifs;
//throw new exception("圖層型別不確定!");
}
iglayer.renderer = (esri.arcgis地理資訊系統 .carto.ifeaturerenderer)isr;
//imapserver ms = (imapserver)in_mapcontext.serverobject;
//imapserverobjects2 mso = (imapserverobjects2)ms;
//string mapname = ms.defaultmapname;
//imap map = mso.get_map(mapname);
//map.addlayer(pfeaturelayer);
//mso.refreshserverobjects();
}
}
//// 從上下文物件中得到伺服器物件
//imapserver ms = (imapserver)mapcontext.serverobject;
//imapserverinfo mapinfo = ms.getserverinfo(ms.defaultmapname);
//imapdescription newmapdesc = mapinfo.defaultmapdescription;
//// 釋放伺服器上下文物件
//mapcontext.releasecontext();
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
if (pfeaturelayer != null)
{
map.addlayer(pfeaturelayer);
//移動層到底層
//map.movelayer(pfeaturelayer, map.layercount - 1);
}
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}
最終除錯正確程式碼:
public static string addcadlayer(map map1, toc toc)
{
// 得到伺服器上下文物件
mapfunctionality agsmapfunctionlity = map1.getfunctionality(1) as mapfunctionality;
mapresourcelocal localmapresource = (mapresourcelocal)agsmapfunctionlity.resource;
esri.arcgis地理資訊系統 .server.iservercontext mapcontext = localmapresource.servercontextinfo.servercontext;
// 增加圖層
esri.arcgis地理資訊系統 .carto.ifeaturelayer pfeaturelayer = null;
//開啟cad資料集
iworkspacefactory factory = (iworkspacefactory)mapcontext.createobject("esridatasourcesfile.cadworkspacefactory");
ifeatureworkspace pfeatureworkspace = (ifeatureworkspace)factory.openfromfile(@"c:\inetpub\wwwroot\northamerica", 0);
//開啟一個要素集
ifeaturedataset pfeaturedataset = (ifeaturedataset)pfeatureworkspace.openfeaturedataset("new_shapefile_exportcad.dwg");
//ifeaturclasscontainer可以管理ifeaturedataset中的每個要素類
ifeatureclasscontainer pfeatureclasscontainer = (ifeatureclasscontainer)pfeaturedataset;
//對cad檔案中的要素進行遍歷處理
for (int i = 0; i < pfeatureclasscontainer.classcount - 1; i++)
{
ifeatureclass pfeatureclass = pfeatureclasscontainer.get_class(i);
if (pfeatureclass.featuretype == esrifeaturetype.esriftannotation)
{
//如果是註記,則添加註記層
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadannotationlayerclass");
}
else//如果是點、線、面,則新增要素層
{
pfeaturelayer = (esri.arcgis地理資訊系統 .carto.ifeaturelayer)mapcontext.createobject("esricarto.cadfeaturelayer");
pfeaturelayer.name = pfeatureclass.aliasname;
pfeaturelayer.featureclass = pfeatureclass;
esri.arcgis地理資訊系統 .carto.imapserverobjects mapserverobjects = localmapresource.mapserver as esri.arcgis地理資訊系統 .carto.imapserverobjects;
esri.arcgis地理資訊系統 .carto.imap map = mapserverobjects.get_map(localmapresource.dataframe) as esri.arcgis地理資訊系統 .carto.imap;
map.addlayer(pfeaturelayer);
//重新整理伺服器物件,響應新增圖層
localmapresource.refreshserverobjects();
}
}
//toc控制元件重新整理
toc.refresh();
map1.callbackresults.copyfrom(toc.callbackresults);
map1.refresh();
return map1.callbackresults.tostring();
}