DXF2gdb

一、准备工作

1.1)netDxf解析CAD
1.2)fileGDBApi写入CAD数据

二、步骤

2.1) 下载esri.filegdb :链接:https://pan.baidu.com/s/1pJJ0RyZeMmRLLcJ-Tx0ikQ 密码:j2ei

2.2) Nuget 获得netDxf

2.3) 参考其中案例,需要先构造XML,用于生成shape 的table

<?xml version="1.0" encoding="UTF-8"?>
<esri:DataElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.1" xsi:type="esri:DEFeatureClass">
    <CatalogPath>\StateBoundaries</CatalogPath>
    <Name>StateBoundaries</Name>
    <ChildrenExpanded>false</ChildrenExpanded>
    <DatasetType>esriDTFeatureClass</DatasetType>
    <Versioned>false</Versioned>
    <CanVersion>false</CanVersion>
    <ConfigurationKeyword/>
    <HasOID>true</HasOID>
    <OIDFieldName>OBJECTID</OIDFieldName>
    <Fields xsi:type="esri:Fields">
        <FieldArray xsi:type="esri:ArrayOfField">
            <Field xsi:type="esri:Field">
                <Name>OBJECTID</Name>
                <Type>esriFieldTypeOID</Type>
                <IsNullable>false</IsNullable>
                <Length>4</Length>
                <Precision>0</Precision>
                <Scale>0</Scale>
                <Required>true</Required>
                <Editable>false</Editable>
                <AliasName>OBJECTID</AliasName>
                <ModelName>OBJECTID</ModelName>
            </Field>
            <Field xsi:type="esri:Field">
                <Name>Shape</Name>
                <Type>esriFieldTypeGeometry</Type>
                <IsNullable>true</IsNullable>
                <Length>0</Length>
                <Precision>0</Precision>
                <Scale>0</Scale>
                <Required>true</Required>
                <GeometryDef xsi:type="esri:GeometryDef">
                    <AvgNumPoints>0</AvgNumPoints>
                    <GeometryType>esriGeometryPolyline</GeometryType>
                    <HasM>false</HasM>
                    <HasZ>false</HasZ>
                    <SpatialReference xsi:type="esri:UnknownCoordinateSystem">
                        <XOrigin>-450359962737.0495</XOrigin>
                        <YOrigin>-450359962737.0495</YOrigin>
                        <XYScale>10000</XYScale>
                        <XYTolerance>0.001</XYTolerance>
                        <HighPrecision>true</HighPrecision>
                    </SpatialReference>
                    <GridSize0>0</GridSize0>
                    <GridSize1>0</GridSize1>
                    <GridSize2>0</GridSize2>
                </GeometryDef>
                <AliasName>Shape</AliasName>
                <ModelName>Shape</ModelName>
            </Field>
            <Field xsi:type="esri:Field">
                <Name>TYPE</Name>
                <Type>esriFieldTypeString</Type>
                <IsNullable>true</IsNullable>
                <Length>10</Length>
                <Precision>0</Precision>
                <Scale>0</Scale>
                <AliasName>TYPE</AliasName>
                <ModelName>TYPE</ModelName>
            </Field>
            <Field xsi:type="esri:Field">
                <Name>Shape_Length</Name>
                <Type>esriFieldTypeDouble</Type>
                <IsNullable>true</IsNullable>
                <Length>8</Length>
                <Precision>0</Precision>
                <Scale>0</Scale>
                <Required>true</Required>
                <Editable>false</Editable>
                <AliasName>Shape_Length</AliasName>
                <ModelName>Shape_Length</ModelName>
            </Field>
        </FieldArray>
    </Fields>
    <Indexes xsi:type="esri:Indexes">
        <IndexArray xsi:type="esri:ArrayOfIndex">
            <Index xsi:type="esri:Index">
                <Name>FDO_OBJECTID</Name>
                <IsUnique>true</IsUnique>
                <IsAscending>true</IsAscending>
                <Fields xsi:type="esri:Fields">
                    <FieldArray xsi:type="esri:ArrayOfField">
                        <Field xsi:type="esri:Field">
                            <Name>OBJECTID</Name>
                            <Type>esriFieldTypeOID</Type>
                            <IsNullable>false</IsNullable>
                            <Length>4</Length>
                            <Precision>0</Precision>
                            <Scale>0</Scale>
                            <Required>true</Required>
                            <Editable>false</Editable>
                            <AliasName>OBJECTID</AliasName>
                            <ModelName>OBJECTID</ModelName>
                        </Field>
                    </FieldArray>
                </Fields>
            </Index>
            <Index xsi:type="esri:Index">
                <Name>FDO_Shape</Name>
                <IsUnique>false</IsUnique>
                <IsAscending>true</IsAscending>
                <Fields xsi:type="esri:Fields">
                    <FieldArray xsi:type="esri:ArrayOfField">
                        <Field xsi:type="esri:Field">
                            <Name>Shape</Name>
                            <Type>esriFieldTypeGeometry</Type>
                            <IsNullable>true</IsNullable>
                            <Length>0</Length>
                            <Precision>0</Precision>
                            <Scale>0</Scale>
                            <Required>true</Required>
                            <GeometryDef xsi:type="esri:GeometryDef">
                                <AvgNumPoints>0</AvgNumPoints>
                                <GeometryType>esriGeometryPolyline</GeometryType>
                                <HasM>false</HasM>
                                <HasZ>false</HasZ>
                                <SpatialReference xsi:type="esri:UnknownCoordinateSystem">
                                    <XOrigin>-450359962737.0495</XOrigin>
                                    <YOrigin>-450359962737.0495</YOrigin>
                                    <XYScale>10000</XYScale>
                                    <XYTolerance>0.001</XYTolerance>
                                    <HighPrecision>true</HighPrecision>
                                 </SpatialReference>
                                <GridSize0>0</GridSize0>
                                <GridSize1>0</GridSize1>
                                <GridSize2>0</GridSize2>
                            </GeometryDef>
                            <AliasName>Shape</AliasName>
                            <ModelName>Shape</ModelName>
                        </Field>
                    </FieldArray>
                </Fields>
            </Index>
        </IndexArray>
    </Indexes>
    <CLSID>{52353152-891A-11D0-BEC6-00805F7C4268}</CLSID>
    <EXTCLSID/>
    <RelationshipClassNames xsi:type="esri:Names"/>
    <AliasName>Internal state boundaries of the United States</AliasName>
    <ModelName/>
    <HasGlobalID>false</HasGlobalID>
    <GlobalIDFieldName/>
    <RasterFieldName/>
    <ExtensionProperties xsi:type="esri:PropertySet">
        <PropertyArray xsi:type="esri:ArrayOfPropertySetProperty"/>
    </ExtensionProperties>
    <ControllerMemberships xsi:type="esri:ArrayOfControllerMembership"/>
    <FeatureType>esriFTSimple</FeatureType>
    <ShapeType>esriGeometryPolyline</ShapeType>
    <ShapeFieldName>Shape</ShapeFieldName>
    <HasM>false</HasM>
    <HasZ>false</HasZ>
    <HasSpatialIndex>true</HasSpatialIndex>
    <AreaFieldName/>
    <LengthFieldName>Shape_Length</LengthFieldName>
    <Extent xsi:nil="true"/>
    <SpatialReference xsi:type="esri:UnknownCoordinateSystem">
                        <XOrigin>-450359962737.0495</XOrigin>
                        <YOrigin>-450359962737.0495</YOrigin>
                        <XYScale>10000</XYScale>
                        <XYTolerance>0.001</XYTolerance>
                        <HighPrecision>true</HighPrecision>
     </SpatialReference>
</esri:DataElement>

因CAD坐标为独立坐标,故XML中的spatialReference 标签设置为未知坐标系统。

2.4) 利用netDxf + fileGdb 生成 gdb文件。

 void test()
        {
            DxfDocument doc = DxfDocument.Load("03.dxf"); //你的dxf

            Geodatabase.Delete("../GeodatabaseManagement/Gdb1.gdb"); //删除原先的

            Geodatabase geodatabase = Geodatabase.Create("../GeodatabaseManagement/Gdb1.gdb"); //构建新的

            string featureClassDef = "";
            using (StreamReader sr = new StreamReader("../../Samples/XMLsamples/FC_GCS_Line.xml"))  //读取XML 用于生成table
            {
                while (sr.Peek() >= 0)
                {
                    featureClassDef += sr.ReadLine() + "\n";
                }
                sr.Close();
            }
            //Create a table
            Table intrstat2Table = geodatabase.CreateTable(featureClassDef, "");


            foreach (var line in doc.LwPolylines)
            {
                List<LwPolylineVertex> vertex = line.Vertexes;

                // Create a new feature.
                Row intrstat2Row = intrstat2Table.CreateRowObject();

                int numPts2 = vertex.Count; //点数量

                int numParts2 = 1; //线的部分数量  用于多线,多部分 https://baike.baidu.com/item/SHP/7064162?fr=aladdin

                MultiPartShapeBuffer intrstat2Geometry = new MultiPartShapeBuffer();

                intrstat2Geometry.Setup(ShapeType.Polyline, numParts2, numPts2);

                // Set the point array to the array from the read geometry.
                Esri.FileGDB.Point[] points2 = intrstat2Geometry.Points;

                for (int i = 0; i < vertex.Count; i++)
                {
                    points2[i].x = vertex[i].Position.X;
                    points2[i].y = vertex[i].Position.Y;
                    if (i == (numPts2 - 1))
                    {
                        Console.WriteLine("{0}, {1}", points2[i].x, points2[i].y);
                    }
                }
                intrstat2Geometry.Points = points2;

                // Set the parts array to the array from the read geometry.

                int[] parts2 = intrstat2Geometry.Parts;
                for (int i = 0; i < numParts2; i++)
                {
                    parts2[i] = 0;
                }
                intrstat2Geometry.Parts = parts2;

                intrstat2Geometry.CalculateExtent();

                intrstat2Row.SetGeometry(intrstat2Geometry);
                //set type string 
                intrstat2Row.SetString(2, "LwPolyline");
                intrstat2Table.Insert(intrstat2Row);
                intrstat2Table.LoadOnlyMode(false);


            }
            intrstat2Table.Close();
        }

2.5) arcMap 中打开

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值