/* 根据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(); } } } }
|