明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4613|回复: 12

[基础] 实现填充和图块例子

  [复制链接]
发表于 2010-12-7 21:32 | 显示全部楼层 |阅读模式
本帖最后由 chpmould 于 2010-12-9 20:04 编辑

请教老师: 如何实现将图形进行填充之后再建成块

谢谢狐哥的指导,问题已解决...
 楼主| 发表于 2010-12-7 21:34 | 显示全部楼层
以下是简单绘制实体
  1.         public void test()
  2.         {        
  3.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  4.             Editor ed = doc.Editor;
  5.             Database db = doc.Database;
  6.             using (doc.LockDocument())
  7.             using (Transaction tr = db.TransactionManager.StartTransaction())
  8.             {               
  9.                 BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  10.                 BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  11.                 Circle c1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 8);
  12.                 Line l1 = new Line(new Point3d(10, 0, 0), new Point3d(-10, 0, 0));
  13.                 btr.AppendEntity(c1);
  14.                 btr.AppendEntity(l1);
  15.                 tr.AddNewlyCreatedDBObject(c1, true);
  16.                 tr.AddNewlyCreatedDBObject(l1, true);
  17.                 tr.Commit();      
  18.             }
  19.         }
发表于 2010-12-8 10:11 | 显示全部楼层
http://www.mjtd.com/helpcenter/netguide/
创建和编辑AutoCad图元
 楼主| 发表于 2010-12-8 12:31 | 显示全部楼层
我现在遇到的问题是,如果我做了填充就做块不成功,如果做了块就做填充不成功,我最终的要求是填充和块一起做,请老师指导一下。。。
发表于 2010-12-8 13:52 | 显示全部楼层
本帖最后由 lzh741206 于 2010-12-8 20:24 编辑

  1.         [CommandMethod("tt4")]
  2.         public void test24()
  3.         {

  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Editor ed = doc.Editor;
  6.             Database db = doc.Database;

  7.             string blkname = "Test";

  8.             using (Transaction tr = db.TransactionManager.StartTransaction())
  9.             {

  10.                 BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  11.                 ObjectId blkdefid = GetRecorId(tr, bt, blkname);

  12.                 if (blkdefid == ObjectId.Null)
  13.                 {

  14.                     BlockTableRecord blkdef = new BlockTableRecord { Name = blkname };
  15.                     bt.UpgradeOpen();
  16.                     blkdefid = bt.Add(blkdef);
  17.                     tr.AddNewlyCreatedDBObject(blkdef, true);
  18.                     bt.DowngradeOpen();

  19.                     List<Entity> loopents =
  20.                         new List<Entity>
  21.                         {
  22.                             new Arc(Point3d.Origin, 10, 0, Math.PI),
  23.                             new Line(new Point3d(-10, 0, 0), new Point3d(10, 0, 0))
  24.                         };

  25.                     ObjectIdCollection loopids =
  26.                         new ObjectIdCollection(
  27.                             loopents.Select(ent => AddEntity(tr, blkdef, ent)).ToArray());

  28.                     Hatch hatch = new Hatch();
  29.                     hatch.SetDatabaseDefaults();
  30.                     hatch.SetHatchPattern(HatchPatternType.PreDefined, "angle");
  31.                     hatch.Associative = false;
  32.                     hatch.AppendLoop(HatchLoopTypes.Outermost, loopids);
  33.                     hatch.EvaluateHatch(true);

  34.                     loopents.ForEach(ent => ent.Erase());

  35.                     List<Entity> ents =
  36.                         new List<Entity>
  37.                         {
  38.                             new Circle(Point3d.Origin, Vector3d.ZAxis, 10),
  39.                             new Line(new Point3d(-15, 0, 0), new Point3d(15, 0, 0)),
  40.                             hatch
  41.                         };

  42.                     int i = 1;
  43.                     ents.ForEach(ent => { ent.ColorIndex = i++; AddEntity(tr, blkdef, ent); });

  44.                 }

  45.                 BlockReference bref = new BlockReference(Point3d.Origin, blkdefid);
  46.                 BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
  47.                 AddEntity(tr, btr, bref);

  48.                 tr.Commit();

  49.             }
  50.         }

  51.         /// <summary>
  52.         /// 在符号表中获取对应键值的记录Id
  53.         /// </summary>
  54.         /// <param name="table">符号表</param>
  55.         /// <param name="key">记录键值</param>
  56.         /// <returns>对应键值的记录Id</returns>
  57.         public static ObjectId GetRecorId<T>(Transaction tr, T table, string key) where T : SymbolTable
  58.         {
  59.             if (table.Has(key))
  60.             {
  61.                 if (Application.Version.Major < 18)
  62.                 {
  63.                     ObjectId idres = table[key];
  64.                     if (!idres.IsErased)
  65.                         return idres;
  66.                     foreach (ObjectId id in table)
  67.                     {
  68.                         if (!id.IsErased)
  69.                         {
  70.                             SymbolTableRecord str = tr.GetObject(id, OpenMode.ForRead) as SymbolTableRecord;
  71.                             if (str.Name == key)
  72.                                 return str.ObjectId;
  73.                         }
  74.                     }
  75.                 }
  76.                 else
  77.                 {
  78.                     return table[key];
  79.                 }
  80.             }
  81.             return ObjectId.Null;
  82.         }

  83.         public ObjectId AddEntity(Transaction tr, BlockTableRecord btr, Entity ent)
  84.         {
  85.             ObjectId id = btr.AppendEntity(ent);
  86.             tr.AddNewlyCreatedDBObject(ent, true);
  87.             return id;
  88.         }
 楼主| 发表于 2010-12-8 20:14 | 显示全部楼层
本帖最后由 chpmould 于 2010-12-8 20:18 编辑

非常感谢狐哥的指导,这个问题已困扰我一个多星期了。。。
我测试编译的时候程序提示: 非泛型方法“Autodesk.AutoCAD.DatabaseServices.ObjectId.Getobject(Autodesk.AutoCAD.DatabaseServices.OpenMode,bool,bool)”不能与类型实参一起使用

提示是这一条语句“SymbolTableRecord str = id.GetObject<SymbolTableRecord>(tr);”不能通过编译,
发表于 2010-12-8 20:25 | 显示全部楼层
我的代码有这个扩展函数了,一时就没改了
5楼代码已更改
 楼主| 发表于 2010-12-8 20:44 | 显示全部楼层
本帖最后由 chpmould 于 2010-12-8 20:44 编辑

谢谢,现在编译成功。
另为请教一下在这段程序中如何更改图片中填充样例的角度和比例?

本帖子中包含更多资源

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

x
 楼主| 发表于 2010-12-9 12:28 | 显示全部楼层
另为请教一下在这段程序中如何更改填充样例的角度和比例?
发表于 2010-12-9 12:53 | 显示全部楼层
                    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);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-3 21:54 , Processed in 0.710423 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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