找回密码
 立即注册
查看: 1721|回复: 0

[分享] C#学习实例-5

[复制链接]
发表于 2013-5-7 20:54:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 ScmTools 于 2013-5-7 22:34 编辑

填充和图块应用
[sell=5]
public void HBlock()
{
            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();
            }
}
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;
}
[/sell]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2026-12-1 21:44 , Processed in 0.167232 second(s), 28 queries , Gzip On.

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表