ydp_00769 发表于 2010-10-21 09:34:00

[求助]自定义块的填充问题

自已定义了一个块,将其画出并返回ObjectId。
根据返回的块ID,进行填充操作,代码如下:
public static void SetHatchPattern(Database acCurDb,ObjectId objEnt)
{

// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,
OpenMode.ForRead) as BlockTable;

// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl,
OpenMode.ForWrite) as BlockTableRecord;

// Adds the arc and line to an object id collection
ObjectIdCollection acObjIdColl = new ObjectIdCollection();
acObjIdColl.Add(objEnt);

// Create the hatch object and append it to the block table record
Hatch acHatch = new Hatch();
acBlkTblRec.AppendEntity(acHatch);
acTrans.AddNewlyCreatedDBObject(acHatch, true);

acHatch.SetDatabaseDefaults();
acHatch.ColorIndex = 1;

//acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
acHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");//实心填充,直接用设置的颜色
acHatch.Associative = true;
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);

// Evaluate the hatch
acHatch.EvaluateHatch(true);

// Increase the pattern scale by 2 and re-evaluate the hatch
acHatch.PatternScale = acHatch.PatternScale + 2;
acHatch.SetHatchPattern(acHatch.PatternType, acHatch.PatternName);
acHatch.EvaluateHatch(true);

// Save the new object to the database
acTrans.Commit();
}
}
当执行
acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl);
时,总是报错,不知是为什么啊?请高手指教!!!急

sieben 发表于 2010-10-21 10:13:00

<p>图块?填充?</p>
<p>为什么不用一个封闭曲线测试一下你的代码?</p>

ydp_00769 发表于 2010-10-21 10:39:00

<p>这个图块是由封闭曲线组成的啊,</p>
<p>之所以用图块,是因为所绘图形由一批这种自定义的图块组成。</p>
<p>就是想在这些图块插入后,给它们的封闭区间填充,然后就出错</p>

雪山飞狐_lzh 发表于 2010-10-21 18:10:00

Hatch的边界只能是曲线集合或多义线

chpmould 发表于 2010-10-31 18:05:00

现在还看不懂,但一定要顶

ydp_00769 发表于 2010-11-24 14:42:00

问题解决了,搞了好久,ObjectIdCollection集合中加入line,只要封闭是可以填充的。polyline加入到集合中也不能填,问题虽解决,但还是没搞懂这一点:

//========图块填充============
BlockTableRecordEnumerator enume = record.GetEnumerator();//获取块中图元对象计数器
ObjectIdCollection ids = new ObjectIdCollection();
while (enume.MoveNext())
{
DBObject dbobj = trans.GetObject(enume.Current, OpenMode.ForRead);
if (dbobj.GetType() == typeof(Line))
{
ids.Add(dbobj.ObjectId);
}
}

List<ObjectIdCollection> IDS = new List<ObjectIdCollection>();
IDS.Add(ids);
//填充图块
CDraw.HatchObject(db, trans, record, IDS, "SOLID", eColor.Red);
//============================

//填充指定实体
public static void HatchObject(Database db,Transaction trans,BlockTableRecord btr,List<ObjectIdCollection> IDS,string strHatchPattern,eColor color)
{
Hatch acHatch = new Hatch();
acHatch.SetDatabaseDefaults();
acHatch.ColorIndex = (int)color;
acHatch.SetHatchPattern(HatchPatternType.PreDefined, strHatchPattern);//实心填充,直接用设置的颜色
//acHatch.Associative = true;
foreach (ObjectIdCollection ids in IDS)
{
acHatch.InsertLoopAt(0, HatchLoopTypes.Default, ids);
}

BlockTable bt1 = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr1 = trans.GetObject(bt1, OpenMode.ForWrite) as BlockTableRecord;

btr.AppendEntity(acHatch);
trans.AddNewlyCreatedDBObject(acHatch, true);
}
页: [1]
查看完整版本: [求助]自定义块的填充问题