linghe
路人甲
路人甲
  • 注册日期2004-06-04
  • 发帖数20
  • QQ
  • 铜币102枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2263回复:5

[求助]AO 开发 DWG转换成SHP

楼主#
更多 发布于:2006-08-14 16:33
<P>各位大虾:</P>

<P>小弟正在用AO二次开发,搞一个DWG数据转换为SHP数据,苦于没有思路</P>
<P>如果有那位搞过这方面的数据格式转换</P>
<P>请指点一下,说一下大致思路!</P>
<P>多谢!!!!!!!!!<BR></P>
喜欢0 评分0
gis
gis
管理员
管理员
  • 注册日期2003-07-16
  • 发帖数15947
  • QQ554730525
  • 铜币25339枚
  • 威望15364点
  • 贡献值0点
  • 银元0个
  • GIS帝国居民
  • 帝国沙发管家
  • GIS帝国明星
  • GIS帝国铁杆
1楼#
发布于:2006-08-15 17:08
<P>下面这个文章思路不错,希望对你有用,代码是c#写的</P>
<P>1. Load the CAD-FeatureDataset: <BR>IWorkspaceFactory pWorkspaceFact = new CadWorkspaceFactory() as IWorkspaceFactory; <BR>IWorkspace pWorkspace = pWorkspaceFact.OpenFromFile(System.IO.Path.GetDirectoryName(openFileDialog1.FileName), 0); <BR>IFeatureWorkspace myFWS = pWorkspace as IFeatureWorkspace; <BR>IFeatureDataset myFDS = myFWS.OpenFeatureDataset(System.IO.Path.GetFileName(openFileDialog1.FileName)); <BR><BR>We need this one later: <BR><BR>FeatureLayer theLayer=null; <BR><BR><BR>2. Get a reference to the output-featuredatasetname and output-featuredataset (This depends on your "environment", all what you need is an existing FeatureDataset of your output-workspace. My FeatureDataset is called "myClient.ReferenceDataset") : <BR><BR>IFeatureDatasetName outName = null; <BR>IFeatureDataset outFDS = null; <BR>if (myClient.ReferenceDataset!=null) <BR>{ <BR>outName = myClient.ReferenceDataset.FullName as IFeatureDatasetName; <BR>outFDS = myClient.ReferenceDataset; <BR>} <BR><BR>3. QI the CAD-FeatureDataset as an IFeatureClassContainer, later we have to iterate through the FeatureClasses in the CAD-File: <BR><BR>IFeatureClassContainer myFCContainer = myFDS as IFeatureClassContainer; <BR><BR>4. We need a: <BR>IFeatureDataConverter myFDC = new FeatureDataConverterClass(); <BR><BR>5. Loop through the FeatureClasses in the CAD-File: <BR><BR>IDatasetName inDataName = myFDS.FullName as IDatasetName; <BR>IFeatureDatasetName inFDataName = inDataName as IFeatureDatasetName; <BR>IEnumDatasetName inEnumDS_FC = inFDataName.FeatureClassNames as IEnumDatasetName; <BR>inEnumDS_FC.Reset(); <BR>IDatasetName inDSname = inEnumDS_FC.Next(); <BR>while (inDSname!=null) <BR>{ <BR>(...) <BR>} <BR><BR>6. where (...) is: <BR><BR>6.1 decide if the CAD-FeatureClass is a Annotation or something else (Polygon,Point,...etc.): <BR><BR>IFeatureClassName myFCname = inDSname as IFeatureClassName; <BR>if (inDSname.Name=="Annotation") <BR>theLayer = new CadAnnotationLayerClass() as IFeatureLayer; <BR>else <BR>theLayer = new CadFeatureLayerClass() as IFeatureLayer; <BR>theLayer.Name=inDSname.Name; <BR><BR><BR>6.2 Get the FeatureClass, ShapeField and GeomeryDef from the iterated CAD-FeatureClass: <BR>IFeatureClass myFCL = myFCContainer.get_ClassByName(inDSname.Name); <BR>IField ShapeField = myFCL.Fields.get_Field(myFCL.FindField(myFCL.ShapeFieldName)); <BR>IGeometryDef myGeom = ShapeField.GeometryDef; <BR><BR><BR>6.3 What happens here is to adapt the Domain of the Output-Featuredataset to the Domain of the CAD-FeatureClass. If you don't do this, you probably can't insert the features into the target-featuredataset, because the extents are out of bounds: <BR><BR>First get the Domain of the CAD-FDS: <BR><BR>IGeoDataset myInGeo = myFDS as IGeoDataset; <BR>ISpatialReference myInRef = myInGeo.SpatialReference; <BR>double inXMin=0; double inXMax=0; double inYMin=0; double inYMax=0; <BR>myInRef.GetDomain(out inXMin,out inXMax, out inYMin, out inYMax); <BR><BR>Now get the actual domain of the target-featuredataset: <BR><BR>IGeoDataset myOutGeo = outFDS as IGeoDataset; <BR>ISpatialReference myOutRef = myOutGeo.SpatialReference; <BR>double outXMin=0; double outXMax=0; double outYMin=0; double outYMax=0; <BR>myOutRef.GetDomain(out outXMin,out outXMax, out outYMin, out outYMax); <BR><BR>Compare and update the Domain of the target-featuredataset: <BR><BR>double newXMin=0; double newXMax=0; double newYMin=0; double newYMax=0; <BR>if (inXMin...[see code below!] <BR><BR><BR>6.4 Convert the CAD-FeatureClass, catch invalid objects and/or have fun! : <BR><BR>IEnumInvalidObject myEnumInv = myFDC.ConvertFeatureClass(myFCname,myFilter,outName, <BR>myFCname, <BR>myGeom,myFCL.Fields,"",1000,0); <BR>myEnumInv.Reset(); <BR>IInvalidObjectInfo myInv = myEnumInv.Next(); <BR>while (myInv!=null) <BR>{ <BR>MessageBox.Show("Invalid Object #"+myInv.InvalidObjectID.ToString()+": "+myInv.ErrorDescription); <BR>myInv = myEnumInv.Next(); <BR>} <BR><BR><BR>I'm sure this is not the best solution (Interfaces, Interfaces, Interfaces...nagnagnag) but this one works for me. <BR>Below you can see the part of my Code. Hope this helps! <BR>Be Aware: The code is NOT "null reference exception"-clear :-) <BR><BR>If something is unclear [ :-} ] don't hesitate to contact me. <BR><BR>Greetings <BR>Volker    IWorkspaceFactory pWorkspaceFact = new CadWorkspaceFactory() as IWorkspaceFactory; IWorkspace pWorkspace = pWorkspaceFact.OpenFromFile(System.IO.Path.GetDirectoryName(openFileDialog1.FileName), 0); IFeatureWorkspace myFWS = pWorkspace as IFeatureWorkspace; IFeatureDataset myFDS = myFWS.OpenFeatureDataset(System.IO.Path.GetFileName(openFileDialog1.FileName)); IFeatureDatasetName outName = null; IFeatureDataset outFDS = null; if (myClient.ReferenceDataset!=null) { outName = myClient.ReferenceDataset.FullName as IFeatureDatasetName; outFDS = myClient.ReferenceDataset; } else { if (myClient.myData.m_FeatureDataset!=null) { outName = myClient.myData.m_FeatureDataset.FullName as IFeatureDatasetName; outFDS = myClient.myData.m_FeatureDataset; } } if (outName==null) { return; //givin' up } IFeatureLayer theLayer=null; IFeatureClassContainer myFCContainer = myFDS as IFeatureClassContainer; IFeatureDataConverter myFDC = new FeatureDataConverterClass(); IDatasetName inDataName = myFDS.FullName as IDatasetName; IFeatureDatasetName inFDataName = inDataName as IFeatureDatasetName; IEnumDatasetName inEnumDS_FC = inFDataName.FeatureClassNames as IEnumDatasetName; inEnumDS_FC.Reset(); IDatasetName inDSname = inEnumDS_FC.Next(); while (inDSname!=null) { IFeatureClassName myFCname = inDSname as IFeatureClassName; if (inDSname.Name=="Annotation") theLayer = new CadAnnotationLayerClass() as IFeatureLayer; else theLayer = new CadFeatureLayerClass() as IFeatureLayer; theLayer.Name=inDSname.Name; IFeatureClass myFCL = myFCContainer.get_ClassByName(inDSname.Name); IField ShapeField = myFCL.Fields.get_Field(myFCL.FindField(myFCL.ShapeFieldName)); IGeometryDef myGeom = ShapeField.GeometryDef; IQueryFilter myFilter = new QueryFilterClass(); myFilter.SubFields="*"; IGeoDataset myInGeo = myFDS as IGeoDataset; ISpatialReference myInRef = myInGeo.SpatialReference; double inXMin=0; double inXMax=0; double inYMin=0; double inYMax=0; myInRef.GetDomain(out inXMin,out inXMax, out inYMin, out inYMax); IGeoDataset myOutGeo = outFDS as IGeoDataset; ISpatialReference myOutRef = myOutGeo.SpatialReference; double outXMin=0; double outXMax=0; double outYMin=0; double outYMax=0; myOutRef.GetDomain(out outXMin,out outXMax, out outYMin, out outYMax); double newXMin=0; double newXMax=0; double newYMin=0; double newYMax=0; if (inXMin<outXMin) newXMin=inXMin; else newXMin=outXMin; if (inXMax>outXMax) newXMax=inXMax; else newXMax=outXMax; if (inYMin<outYMin) newYMin=inYMin; else newYMin=outYMin; if (inYMax>outYMax) newYMax=inYMax; else newYMax=outYMax; myOutRef.SetDomain(newXMin,newXMax,newYMin,newYMax); IEnumInvalidObject myEnumInv = myFDC.ConvertFeatureClass(myFCname,myFilter,outName, myFCname, myGeom,myFCL.Fields,"",1000,0); myEnumInv.Reset(); IInvalidObjectInfo myInv = myEnumInv.Next(); while (myInv!=null) { MessageBox.Show("Invalid Object #"+myInv.InvalidObjectID.ToString()+": "+myInv.ErrorDescription); myInv = myEnumInv.Next(); } } </P>
举报 回复(0) 喜欢(0)     评分
whmwxhanshan123
路人甲
路人甲
  • 注册日期2006-06-17
  • 发帖数3108
  • QQ
  • 铜币6445枚
  • 威望0点
  • 贡献值0点
  • 银元0个
2楼#
发布于:2006-08-15 18:55
<img src="images/post/smile/dvbbs/em02.gif" /><img src="images/post/smile/dvbbs/em01.gif" />
举报 回复(0) 喜欢(0)     评分
linghe
路人甲
路人甲
  • 注册日期2004-06-04
  • 发帖数20
  • QQ
  • 铜币102枚
  • 威望0点
  • 贡献值0点
  • 银元0个
3楼#
发布于:2006-08-15 22:29
<P>多谢版主的指点!!</P>
<P>我对第二部分不是太懂,能否解释一下,This depends on your "environment", all what you need is an existing FeatureDataset of your output-workspace. My FeatureDataset is called "myClient.ReferenceDataset") </P>
<P>多谢!!!</P>
举报 回复(0) 喜欢(0)     评分
zhoujs
路人甲
路人甲
  • 注册日期2004-07-09
  • 发帖数38
  • QQ24938384
  • 铜币276枚
  • 威望0点
  • 贡献值0点
  • 银元0个
4楼#
发布于:2006-10-11 15:44
<P>谢谢,偶也很需要</P>
Sam.Zhou/GIS系统架构师
举报 回复(0) 喜欢(0)     评分
zhousky
论坛版主
论坛版主
  • 注册日期2003-08-01
  • 发帖数281
  • QQ
  • 铜币1027枚
  • 威望3点
  • 贡献值0点
  • 银元0个
5楼#
发布于:2006-11-30 23:43
我们有在AE下现在的代码,CAD转SHP,SHP转CAD,可以QQ联系:171567188
不要看我噢
举报 回复(0) 喜欢(0)     评分
游客

返回顶部