- 积分
- 5155
- 明经币
- 个
- 注册时间
- 2003-1-15
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
要用ARX实现关联的图案填充,必须使用“永久反应器”这一技术,而C#只有“事件”(相当于ARX中的“临时反应器”),尽管填充对象(Hatch类)有一个Associative属性,但简单地设置为true,并不能解决问题。我一直觉得要用C#实现关联的图案填充必须要借助ARX的帮助,后来发现,用纯C#还是可以实现关联填充的……- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace AssociativeHatch
- {
- public class Commands
- {
- [CommandMethod("nt")]
- public void AssociativeHatch()
- {
- Database db = HostApplicationServices.WorkingDatabase;
- // 创建填充边界对象.
- Line lineEnt1 = new Line(new Point3d(0, 0, 0), new Point3d(1000, 0, 0));
- Line lineEnt2 = new Line(new Point3d(1000, 0, 0), new Point3d(800, 600, 0));
- Line lineEnt3 = new Line(new Point3d(800, 600, 0), new Point3d(0, 0, 0));
- Circle circleEnt = new Circle(new Point3d(500, 200, 0), Vector3d.ZAxis, 100);
- ObjectId loopId1 = AppendEntity(lineEnt1);
- ObjectId loopId2 = AppendEntity(lineEnt2);
- ObjectId loopId3 = AppendEntity(lineEnt3);
- ObjectId loopId4 = AppendEntity(circleEnt);
- // 将填充边界对象ID添加到两个ObjectId集合中.
- ObjectIdCollection loops1 = new ObjectIdCollection();
- loops1.Add(loopId1);
- loops1.Add(loopId2);
- loops1.Add(loopId3);
- ObjectIdCollection loops2 = new ObjectIdCollection();
- loops2.Add(loopId4);
- // 将两个ObjectId集合添加到一个ObjectId集合数组中.
- ObjectIdCollection[] loops = new ObjectIdCollection[2];
- loops.SetValue(loops1, 0);
- loops.SetValue(loops2, 1);
- // 填充图案对象.
- Hatch hatchEnt;
- ObjectId hatEntId = AddHatch(out hatchEnt, 0, "ANGLE", Math.PI / 3, 10);
- // 关联填充图案与边界.
- using (Transaction tr = db.TransactionManager.StartTransaction())
- {
- hatchEnt = (Hatch)tr.GetObject(hatEntId, OpenMode.ForWrite);
- hatchEnt.Associative = true;
- for (int i = 0; i < loops.Length; i++)
- {
- hatchEnt.AppendLoop(HatchLoopTypes.Default, loops[i]);
- }
- tr.Commit();
- }
- }
- // 由图案填充类型、填充图案名称、
- // 填充角度和填充比例创建图案填充的函数.
- // partType:0为预定义图案;1为用户定义图案;2为自定义图案.
- private ObjectId AddHatch(out Hatch hatchEnt, HatchPatternType patType,
- String patName, Double patternAngle, Double patternScale)
- {
- Hatch ent = new Hatch();
- ent.HatchObjectType = HatchObjectType.HatchObject;
- Database db = HostApplicationServices.WorkingDatabase;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject
- (db.BlockTableId, OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject
- (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- ObjectId entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- ent.SetDatabaseDefaults();
- ent.PatternAngle = patternAngle;
- ent.PatternScale = patternScale;
- ent.SetHatchPattern(patType, patName);
- hatchEnt = ent;
- trans.Commit();
- return entId;
- }
- }
- // 添加图元到数据库.
- private ObjectId AppendEntity(Entity ent)
- {
- Database db = HostApplicationServices.WorkingDatabase;
- ObjectId entId;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
- OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject
- (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- entId = btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- trans.Commit();
- }
- return entId;
- }
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
参与人数 1 | 威望 +1 |
明经币 +5 |
金钱 +20 |
贡献 +5 |
激情 +5 |
收起
理由
|
雪山飞狐_lzh
| + 1 |
+ 5 |
+ 20 |
+ 5 |
+ 5 |
【精华】好文章 |
查看全部评分
|