实现填充和图块例子
本帖最后由 chpmould 于 2010-12-9 20:04 编辑请教老师: 如何实现将图形进行填充之后再建成块
谢谢狐哥的指导,问题已解决... 以下是简单绘制实体 public void test()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
using (doc.LockDocument())
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt, OpenMode.ForWrite);
Circle c1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 8);
Line l1 = new Line(new Point3d(10, 0, 0), new Point3d(-10, 0, 0));
btr.AppendEntity(c1);
btr.AppendEntity(l1);
tr.AddNewlyCreatedDBObject(c1, true);
tr.AddNewlyCreatedDBObject(l1, true);
tr.Commit();
}
} http://www.mjtd.com/helpcenter/netguide/
创建和编辑AutoCad图元 我现在遇到的问题是,如果我做了填充就做块不成功,如果做了块就做填充不成功,我最终的要求是填充和块一起做,请老师指导一下。。。 本帖最后由 lzh741206 于 2010-12-8 20:24 编辑
public void test24()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
Database db = doc.Database;
string blkname = "Test";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId blkdefid = GetRecorId(tr, bt, blkname);
if (blkdefid == ObjectId.Null)
{
BlockTableRecord blkdef = new BlockTableRecord { Name = blkname };
bt.UpgradeOpen();
blkdefid = bt.Add(blkdef);
tr.AddNewlyCreatedDBObject(blkdef, true);
bt.DowngradeOpen();
List<Entity> loopents =
new List<Entity>
{
new Arc(Point3d.Origin, 10, 0, Math.PI),
new Line(new Point3d(-10, 0, 0), new Point3d(10, 0, 0))
};
ObjectIdCollection loopids =
new ObjectIdCollection(
loopents.Select(ent => AddEntity(tr, blkdef, ent)).ToArray());
Hatch hatch = new Hatch();
hatch.SetDatabaseDefaults();
hatch.SetHatchPattern(HatchPatternType.PreDefined, "angle");
hatch.Associative = false;
hatch.AppendLoop(HatchLoopTypes.Outermost, loopids);
hatch.EvaluateHatch(true);
loopents.ForEach(ent => ent.Erase());
List<Entity> ents =
new List<Entity>
{
new Circle(Point3d.Origin, Vector3d.ZAxis, 10),
new Line(new Point3d(-15, 0, 0), new Point3d(15, 0, 0)),
hatch
};
int i = 1;
ents.ForEach(ent => { ent.ColorIndex = i++; AddEntity(tr, blkdef, ent); });
}
BlockReference bref = new BlockReference(Point3d.Origin, blkdefid);
BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
AddEntity(tr, btr, bref);
tr.Commit();
}
}
/// <summary>
/// 在符号表中获取对应键值的记录Id
/// </summary>
/// <param name="table">符号表</param>
/// <param name="key">记录键值</param>
/// <returns>对应键值的记录Id</returns>
public static ObjectId GetRecorId<T>(Transaction tr, T table, string key) where T : SymbolTable
{
if (table.Has(key))
{
if (Application.Version.Major < 18)
{
ObjectId idres = table;
if (!idres.IsErased)
return idres;
foreach (ObjectId id in table)
{
if (!id.IsErased)
{
SymbolTableRecord str = tr.GetObject(id, OpenMode.ForRead) as SymbolTableRecord;
if (str.Name == key)
return str.ObjectId;
}
}
}
else
{
return table;
}
}
return ObjectId.Null;
}
public ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
{
ObjectId id = btr.AppendEntity(ent);
tr.AddNewlyCreatedDBObject(ent, true);
return id;
} 本帖最后由 chpmould 于 2010-12-8 20:18 编辑
非常感谢狐哥的指导,这个问题已困扰我一个多星期了。。。
我测试编译的时候程序提示: 非泛型方法“Autodesk.AutoCAD.DatabaseServices.ObjectId.Getobject(Autodesk.AutoCAD.DatabaseServices.OpenMode,bool,bool)”不能与类型实参一起使用
提示是这一条语句“SymbolTableRecord str = id.GetObject<SymbolTableRecord>(tr);”不能通过编译,
我的代码有这个扩展函数了,一时就没改了
5楼代码已更改 本帖最后由 chpmould 于 2010-12-8 20:44 编辑
谢谢,现在编译成功。
另为请教一下在这段程序中如何更改图片中填充样例的角度和比例?
另为请教一下在这段程序中如何更改填充样例的角度和比例?
Hatch hatch = new Hatch();
hatch.PatternAngle = Math.PI / 4;
hatch.PatternScale = 0.1;
hatch.Associative = false;
hatch.SetHatchPattern(HatchPatternType.PreDefined, "angle");
hatch.AppendLoop(HatchLoopTypes.Outermost, loopids);
hatch.SetDatabaseDefaults();
hatch.EvaluateHatch(true);
页:
[1]
2