[转]在AutoCAD中根据MapInfo导出DXF文件块属性填充图斑

摘要:
现状地类有林地,159255127有林地农村居民点,255204204农村居民点望天田,255255110望天田机耕道,255180178机耕道旱地,255255183旱地果园,255223127果园河流水面,171255255河流水面荒草地,210255180荒草地坑塘水面,171255255坑塘水面裸岩石砾地,192192192裸岩石砾地祼岩地,255255255祼岩地农村宅基地,255204

现状地类
有林地,159255127有林地
农村居民点,255204204农村居民点
望天田,255255110望天田
机耕道,255180178机耕道
旱地,255255183旱地
果园,255223127果园
河流水面,171255255河流水面
荒草地,210255180荒草地
坑塘水面,171255255坑塘水面
裸岩石砾地,192192192裸岩石砾地
祼岩地,255255255祼岩地
农村宅基地,255204204农村宅基地
特殊用地,255132132特殊用地
沟渠,171255255沟渠
工矿用地,255180178工矿用地

第一行:"现状地类" 为Mapinfo中的字段名
以下行为具体配置,如第一行有林地,159255127有林地

第一个"有林地"为字段里的值,后跟的9位数据为RGB颜色,后面的"有林地"是图层名,图层不存在会自动创建
配置文件名为: d:\xdxah.ini 在程序中此路径和名称固定

/* 根据MAPINFO导出块属性和配置文件填充图斑
* 此程序需一配置文件
* 作者:王晓东
* QQ:10516321 Email:xiaook@gmail.com
* http://goat.cublog.cn
*/

usingSystem.Collections.Generic;
usingSystem.IO;
usingSystem.Text;
usingAutodesk.AutoCAD.ApplicationServices;
usingAutodesk.AutoCAD.DatabaseServices;
usingAutodesk.AutoCAD.EditorInput;
usingAutodesk.AutoCAD.Runtime;
/*Copyright © 王晓东 2010
AutoCAD version:AutoCAD 2006
Description:
To use HatchByBlockAttribute.dll:
1. Start AutoCAD and open a new drawing.
2. Type netload and select HatchByBlockAttribute.dll.
3. Execute the xah command.*/

namespaceHatchByBlockAttribute
{
/// <summary>

/// Summary for Class1.

/// </summary>

publicclassClass1
{
Document acDoc=Application.DocumentManager.MdiActiveDocument;
Editor acDocEd=Application.DocumentManager.MdiActiveDocument.Editor;
Database acCurDb=Application.DocumentManager.MdiActiveDocument.Database;
[CommandMethod("xah")]
publicvoidxah()
{
acDocEd.WriteMessage(@"\n 根据MAPINFO导出块属性和配置文件填充图斑 \n此程序需一配置文件 \n作者:王晓东 \nQQ:10516321 Email:xiaook@gmail.com");
//读取配置文件

StreamReaderconfigfs=newStreamReader(@"d:\xdxah.ini",Encoding.Default);
Dictionary<string,string>confDict=newDictionary<string,string>();
stringstr=configfs.ReadLine();//提取首行字段名

stringkeyField=str;
str=configfs.ReadLine();
while(str!=null)
{
string[]strarr=str.Split(',');
confDict.Add(strarr[0],strarr[1]);
str=configfs.ReadLine();
}
//选择所有块对象

ObjectIdCollection acObjIdColl=newObjectIdCollection();
PromptSelectionResult acPtRes=acDocEd.SelectAll();
if(acPtRes.Status==PromptStatus.OK)
{
SelectionSet ss=acPtRes.Value;
acObjIdColl=newObjectIdCollection(ss.GetObjectIds());
}
//事务

using(Transaction acTrans=acCurDb.TransactionManager.StartTransaction())
{
ObjectIdCollection objIdCol=newObjectIdCollection();
BlockTable acBlkTbl=acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead)asBlockTable;
BlockTableRecord acBlkTblRec=acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForWrite)asBlockTableRecord;
LayerTable acLyrTbl;
acLyrTbl=acTrans.GetObject(acCurDb.LayerTableId,OpenMode.ForRead)asLayerTable;
//仅提取所有块参照

ObjectIdCollection objColTmp=newObjectIdCollection();
for(inti=0;i<acObjIdColl.Count;i++)
{
DBObject dbObject=acTrans.GetObject(acObjIdColl[i],OpenMode.ForRead);
if(dbObjectisBlockReference)
{
objColTmp.Add(acObjIdColl[i]);
}
}
acObjIdColl=objColTmp;
for(intj=0;j<acObjIdColl.Count;j++)//遍历对象

{
BlockReference bkRef=(BlockReference)acTrans.GetObject(acObjIdColl[j],OpenMode.ForRead);
AttributeCollectionattCol=bkRef.AttributeCollection;
stringsHatchColor="";
Hatch acHatch=newHatch();
acHatch.SetDatabaseDefaults();
for(intk=0;k<attCol.Count;k++)//遍历属性

{
AttributeReference attRef=(AttributeReference)acTrans.GetObject(attCol[k],OpenMode.ForRead,false,true);
if(attRef.Tag==keyField)
{
for(inti=0;i<confDict.Count;i++)//编历配置文件项

{
if(confDict.ContainsKey(attRef.TextString))//确定字典内是否包含此地类主键

{
sHatchColor=confDict[attRef.TextString];
}
}
}
}
//包含此地类主键,填充此块

DBObjectCollection bkDBCol=newDBObjectCollection();
bkRef.Explode(bkDBCol);
//填充前将此块分解并提取Polyline对象

for(intkk=0;kk<bkDBCol.Count;kk++)
{
if(bkDBCol[kk]isPolyline)
{
Polyline pl=(Polyline)bkDBCol[kk];
if(pl.Closed)//只填充闭合多线段

{
ObjectIdCollection bkIDCol=newObjectIdCollection();//要填充对象的Collection

acBlkTblRec.AppendEntity((Entity)bkDBCol[kk]);
acTrans.AddNewlyCreatedDBObject(bkDBCol[kk],true);
bkIDCol.Add(bkDBCol[kk].ObjectId);
acHatch.AppendLoop((int)HatchLoopTypes.Outermost,bkIDCol);
}
}
}
//填充

if(sHatchColor!="")
{
byteR=byte.Parse(sHatchColor.Substring(0,3));
byteG=byte.Parse(sHatchColor.Substring(3,3));
byteB=byte.Parse(sHatchColor.Substring(6,3));
stringlayer=sHatchColor.Substring(9);
if(acLyrTbl.Has(layer)==false)
{
LayerTableRecord acLyrTblRec=newLayerTableRecord();
// Assign the layer the ACI color 1 and a name

acLyrTblRec.Color=Autodesk.AutoCAD.Colors.Color.FromRgb(R,G,B);
acLyrTblRec.Name=layer;
// Upgrade the Layer table for write

acLyrTbl.UpgradeOpen();
// Append the new layer to the Layer table and the transaction

acLyrTbl.Add(acLyrTblRec);
acTrans.AddNewlyCreatedDBObject(acLyrTblRec,true);
}
acHatch.Layer=layer;
acBlkTblRec.AppendEntity(acHatch);
acTrans.AddNewlyCreatedDBObject(acHatch,true);
acHatch.SetHatchPattern(HatchPatternType.PreDefined,"SOLID");
//acHatch.ColorIndex = int.Parse(sHatchColor);

acHatch.Color=Autodesk.AutoCAD.Colors.Color.FromRgb(R,G,B);
acHatch.Associative=false;
acHatch.EvaluateHatch(true);
}
}
////提示清理边界

//PromptKeywordOptions pKeyOpts = new PromptKeywordOptions("");

//pKeyOpts.Message = "\n清理对象仅留图案填充[No/Yes]";

//pKeyOpts.Keywords.Add("Y");

//pKeyOpts.Keywords.Add("N");

//pKeyOpts.Keywords.Default = "N";

//pKeyOpts.AllowNone = true;

//PromptResult pKeyRes = acDoc.Editor.GetKeywords(pKeyOpts);

//if (pKeyRes.StringResult == "Y")

//{

// //选择所有块对象

// TypedValue[] tv = new TypedValue[1];

// //tv.SetValue(new TypedValue((int)DxfCode.Operator, "<not"), 0);

// tv.SetValue(new TypedValue((int)DxfCode.Start, "HATCH"), 0);

// //tv.SetValue(new TypedValue((int)DxfCode.Operator, ">not"), 2);

// SelectionFilter sf = new SelectionFilter(tv);

// ObjectIdCollection acHatchColl = new ObjectIdCollection();

// PromptSelectionResult acPtRes1 = acDocEd.SelectAll(sf);

// ObjectIdCollection acObjForDelColl = new ObjectIdCollection();

// PromptSelectionResult acPtRes2 = acDocEd.SelectAll();

// if ((acPtRes1.Status == PromptStatus.OK) && (acPtRes2.Status == PromptStatus.OK))

// {

// SelectionSet ss = acPtRes1.Value;

// acHatchColl = new ObjectIdCollection(ss.GetObjectIds());

// foreach (ObjectId a in acHatchColl)

// {

// acObjForDelColl.Remove(a);

// }

// for (int i = 0; i < acObjForDelColl.Count; i++)

// {

// DBObject dbobj = acObjForDelColl[i].GetObject(OpenMode.ForRead);

// dbobj.Erase();

// }

// }

//}

acTrans.Commit();
}
}
}
}

免责声明:文章转载自《[转]在AutoCAD中根据MapInfo导出DXF文件块属性填充图斑》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C++学习之开发环境搭建篇(一)字典树的使用(匹配子串)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

C#开发GIS应用简明教程(三)

  第三章数据处理        在介绍数据处理之前,我们先在MapInfo中生成自己的地图,在本章中将全部使用自己生成的地图.这是因为MapX本身提供的地图和实际应用的地图在数据结构上有较大的差异.不适合用来介绍MapX的数据处理.        首先安装好MapInfo,最好能使用7.0以上的版本.以下操作在MapInfo Professional...

AutoCAD利用VBA宏绘制直线

利用VBA宏代码绘制直线,可以设置线性,设置直线颜色,对直线进行操作。 绘制直线主要用到addline函数,该函数接受两个参数,第一个参数指点直线起点startpoint,第二个参数指定直线终点endpoint,新的直线实例被添加到绘图空间。 定义绘制直线的函数creatline,代码如下。 Sub creatline() Dim startpoint(0...

CAD数据导入Arcgis10.1的依赖关系

这段时间在做基于Arcgis10.1API处理AutoCAD图纸数据并将处理后的数据坐标转换为xml文件,以便于在开发的项目中使用。通过这段时间的开发总结以下问题希望能对童鞋有所帮助: 1、遇到CAD格式的数据有非法的数据时(比如无法正常识别的点、线、面)就不能通过arcgis层面去处理,要在cad本身去做处理通过AutoCAD的相关命令对模型进行处理。...

C# AutoCAD ActiveX 二次开发 CAD图层合并

因使用AutoCAD版本为2004,2006版本以后才有.net开发包,因此采用ActiveX方式进行开发。 功能为:将几个CAD图层合并到其中一个上,并设置坐标,线和标记的颜色和字体。 CAD对象的层级为 AcadApplication -   AcadDocument -     AcadModelSpace       AcadEntity...

如何把一个对象的颜色设置成 ByLayer

AutoCAD  .net API中可以用如下代码来设置entity的颜色为bylayer,请注意 BYLAYER的colorIndex为256。很简单,光贴代码。 [CommandMethod("SetColorByLayer")] public void SetColorByLayer() { ObjectId objId = GetSelectE...

用C#.Net实现AutoCAD块属性提取

 ____这是我以前写过的一篇文章,也是SmartSoft中用到的,作为SmartSoft专题的一部分转贴到这里,与大家共享。此类实现AutoCAD块属性提取功能,在VS.Net2003(2005)+AutoCAD2004(2007)下调试通过。   using System;using AutoCAD=Autodesk.AutoCAD.Interop...