因为已经写过加载dwg了,dxf应该不陌生,dxf是个开源格式,所以加载比较简单这里直接附上as的代码,但是真正使用场景还是比较少,dwg文件比较多
1 package widgetscadastre.SZ_ImportCAD 2 { 3 /** 4 * ... 5 * @author haibalai 6 */ 7 import com.esri.ags.Graphic; 8 import com.esri.ags.geometry.Extent; 9 import com.esri.ags.geometry.MapPoint; 10 import com.esri.ags.geometry.Polygon; 11 import com.esri.ags.geometry.Polyline; 12 import com.szpl.extension.base.impl.AccessPoint; 13 import flash.events.Event; 14 import flash.net.FileFilter; 15 import flash.net.FileReference; 16 import flash.utils.Dictionary; 17 18 import mx.collections.ArrayCollection; 19 import mx.controls.Alert; 20 import mx.utils.ObjectUtil; 21 import mx.utils.StringUtil; 22 23 public class DxfTool 24 { 25 26 public function DxfTool() 27 { 28 29 } 30 31 32 33 34 [Bindable] 35 private var layerList:ArrayCollection = null; 36 37 private var coorArray:Array = null; 38 private var initExtent:Extent = null; 39 40 private var _callFun:Function; 41 public function dxfLoad(bytedata:String,callFun:Function = null) 42 { 43 _callFun = callFun; 44 dxfHandle(bytedata); 45 46 } 47 48 49 50 private function dxfHandle(coorStr:String):void 51 { 52 53 layerList = new ArrayCollection; 54 try 55 { 56 57 coorArray = StringUtil.trim(coorStr).split(" "); 58 59 var i:int=0; //当前读取的行数 60 var entitiesRowNum:int = 0; //实体所在行数 61 initExtent = new Extent; 62 63 while(i<coorArray.length) 64 { 65 if(StringUtil.trim(coorArray[i]).toUpperCase()=="$LIMMIN") //读取地图范围 66 { 67 i = i + 2; 68 initExtent.xmin = Number(StringUtil.trim(coorArray[i])); //最小x 69 i = i + 2; 70 initExtent.ymin = Number(StringUtil.trim(coorArray[i])); //最小y 71 } 72 else if(StringUtil.trim(coorArray[i]).toUpperCase()=="$LIMMAX") 73 { 74 i = i + 2; 75 initExtent.xmax = Number(StringUtil.trim(coorArray[i])); //最大x 76 i = i + 2; 77 initExtent.ymax = Number(StringUtil.trim(coorArray[i])); //最大y 78 } 79 else if(StringUtil.trim(coorArray[i]).toUpperCase()=="ENTITIES") 80 { 81 entitiesRowNum=i; //记录实体所在行位置 82 i++; 83 break; 84 } 85 i++; 86 } 87 88 var dictionary:Dictionary = new Dictionary; 89 //加载图层 90 while(i<coorArray.length) 91 { 92 if(StringUtil.trim(coorArray[i])=="8") //读取到图层 93 { 94 i++; 95 if (dictionary[StringUtil.trim(coorArray[i])]==null) //判断图层是否存在 96 { 97 var obj:Object = {name:StringUtil.trim(coorArray[i]), RowNum:i}; //图层名及图层所在行数 98 dictionary[StringUtil.trim(coorArray[i])]=obj; 99 this.layerList.addItem(obj); 100 } 101 102 103 i++; 104 } 105 if(StringUtil.trim(coorArray[i])=="") //遇到空行则终止循环 106 { 107 i++; 108 break; 109 } 110 i++; 111 } 112 } 113 catch(e:Error) 114 { 115 Alert.show("读取有误:"+e.message.toString()); 116 } 117 loadLayer() 118 119 } 120 121 private function loadLayer():void 122 { 123 var coord:String = ""; 124 var graphicArr:Array = new Array(); 125 if(layerList!=null) 126 { 127 for (var j:int = 0; j < layerList.length; j++ ) 128 { 129 130 131 var layerObj:Object = layerList[j]; 132 var i:Number = layerObj.RowNum; //当前图层所在行数 133 while(i < coorArray.length) 134 { 135 if(StringUtil.trim(coorArray[i]) == layerObj.name) //找到对应图层 136 { 137 i++; 138 while(i<coorArray.length) 139 { 140 if(StringUtil.trim(coorArray[i])=="100") 141 { 142 i++; 143 break; 144 } 145 i++; 146 } 147 148 switch(StringUtil.trim(coorArray[i])) 149 { 150 case "AcDbPolyline": 151 { 152 i = i + 2; 153 var iNodeCount:Number = Number(StringUtil.trim(coorArray[i])); //顶点个数 154 i = i + 2; 155 var geomeryType:String = StringUtil.trim(coorArray[i]); //图形类型 156 157 i = i + 2; 158 var pointArray:Array = new Array; 159 160 while(pointArray.length < iNodeCount) 161 { 162 var mapPoint:MapPoint = new MapPoint; 163 i = i + 2; 164 165 mapPoint.x = Number(StringUtil.trim(coorArray[i])); //x坐标 166 i = i + 2; 167 168 mapPoint.y = Number(StringUtil.trim(coorArray[i])); //y坐标 169 170 pointArray.push(mapPoint); 171 172 173 } 174 if(geomeryType == "0") // 0代表折线 175 { 176 //var paths:Array = new Array; 177 //paths.push(pointArray); 178 // 179 //var polyline:Polyline = new Polyline; 180 //polyline.paths = paths; 181 // 182 //var gra:Graphic = new Graphic; 183 //gra.geometry = polyline; 184 185 } 186 else if(geomeryType == "1") // 1代表折线闭合为多边形 187 { 188 var rings:Array = new Array; 189 rings.push(pointArray); 190 191 var polygon:Polygon = new Polygon; 192 polygon.rings = rings; 193 194 var graphic:Graphic = new Graphic; 195 graphic.geometry = polygon; 196 graphicArr.push(graphic); 197 198 199 } 200 break; 201 } 202 case "AcDbLine": 203 break; 204 case "AcDbPoint": 205 break; 206 case "AcDbSpline": 207 208 break; 209 } 210 i++; 211 } 212 else 213 { 214 i++; 215 } 216 } 217 218 } 219 220 } 221 if (graphicArr.length > 0) 222 { 223 for each(var graphic:Graphic in graphicArr) 224 { 225 coord += AccessPoint.exchange.polygon_to_CoordText(graphic.geometry as Polygon) + "*"; 226 } 227 coord = coord.substring(0,coord.length - 1) 228 229 } 230 231 _callFun.call(null, coord); 232 } 233 234 } 235 236 }