- 积分
- 24557
- 明经币
- 个
- 注册时间
- 2004-3-17
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2010-12-8 13:52:38
|
显示全部楼层
本帖最后由 lzh741206 于 2010-12-8 20:24 编辑
- [CommandMethod("tt4")]
- 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[key];
- 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[key];
- }
- }
- return ObjectId.Null;
- }
- public ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
- {
- ObjectId id = btr.AppendEntity(ent);
- tr.AddNewlyCreatedDBObject(ent, true);
- return id;
- }
|
|