- 积分
- 1491
- 明经币
- 个
- 注册时间
- 2004-5-8
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 作者 于 2009-9-28 16:05:39 编辑
被飞狐批评了,改了重发一个- [assembly: CommandClass(typeof(WolfDrawOverrule.WolfHelper))]
- [assembly: ExtensionApplication(typeof(WolfDrawOverrule.WolfLineAnnoOverrule))]
- namespace WolfDrawOverrule
- {
- class WolfPoleOverrule : IExtensionApplication
- {
- #region IExtensionApplication Members
- //初始化例程,重定义生效
- void IExtensionApplication.Initialize()
- {
- WolfHelper.OverruleStart();
- Overrule.Overruling = true;
- }
- //
- void IExtensionApplication.Terminate()
- {
- WolfHelper.OverruleEnd();
- Overrule.Overruling = false;
- }
- #endregion
- }
- static class WolfHelper
- {
- //Xdata应用程序名称
- public readonly static string PoleRegAppName = "Wolf.Pole";
- //是否显示基线
- public static bool DrawPoleBaseLine = false;
- public static void OverruleStart()
- {
- Overrule.AddOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), TelegraphPoleOverrule.TheOverrule, false);
- Overrule.AddOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), TelegraphPoleTransformOverrule.TheOverrule, false);
- }
- public static void OverruleEnd()
- {
- Overrule.RemoveOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), TelegraphPoleOverrule.TheOverrule);
- Overrule.RemoveOverrule(RXObject.GetClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline)), TelegraphPoleTransformOverrule.TheOverrule);
- }
- //测试代码,使用不方便,可以自行替换。Esc生成对象
- [CommandMethod("POLE")]
- public static void AddPole()
- {
- Document doc = Application.DocumentManager.MdiActiveDocument;
- Editor ed = doc.Editor;
- PromptPointResult res = ed.GetPoint("\n请输入起点:");
- Point3dCollection point3dColl = new Point3dCollection();
- while (res.Status == PromptStatus.OK)
- {
- point3dColl.Add(res.Value);
- PromptPointOptions opts = new PromptPointOptions("\n请输入下一点:");
- opts.BasePoint = res.Value;
- opts.UseBasePoint = true;
- res = ed.GetPoint(opts);
- }
- if (point3dColl.Count < 2)
- {
- ed.WriteMessage("\n错误线段点位小于2个,不能画线.");
- return;
- }
- Database db = doc.Database;
- using (Transaction trans = AcadPropers.Tm.StartTransaction())
- {
- RegAppTable rat = (RegAppTable)trans.GetObject(AcadPropers.Database.RegAppTableId, OpenMode.ForRead, false);
- if (!rat.Has(PoleRegAppName))
- {
- rat.UpgradeOpen();
- RegAppTableRecord regapp = new RegAppTableRecord();
- regapp.Name = PoleRegAppName;
- rat.Add(regapp);
- trans.AddNewlyCreatedDBObject(regapp, true);
- }
- Autodesk.AutoCAD.DatabaseServices.Polyline polyline = new Autodesk.AutoCAD.DatabaseServices.Polyline();
- for (int i = 0; i < point3dColl.Count - 1; i++)
- {
- Point2d point2d = new Point2d(point3dColl[i].X, point3dColl[i].Y);
- polyline.AddVertexAt(i, point2d, 0, 0, 0);
- }
- //设置代码及图块名称
- SetTo(polyline, WolfHelper.PoleRegAppName, "171101", "gc013a");
- BlockTableRecord btr =
- (BlockTableRecord)trans.GetObject(
- db.CurrentSpaceId,
- OpenMode.ForWrite,
- false);
- btr.AppendEntity(polyline);
- trans.AddNewlyCreatedDBObject(polyline, true);
- trans.Commit();
- }
- }
- //设置对象XData属性
- public static void SetTo(Entity entity, string RegAppName, string code, string blockName)
- {
- ResultBuffer rb = new ResultBuffer();
- rb.Add(new TypedValue((int)DxfCode.ExtendedDataRegAppName, RegAppName));
- if (code != "" || blockName != "")
- {
- rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, code));
- rb.Add(new TypedValue((int)DxfCode.ExtendedDataAsciiString, blockName));
- }
- entity.XData = rb;
- }
- }
- static class PoleGeometryHelper
- {
- //获取重定义规则对象
- public static DBObjectCollection GetPoleGeometrys(Autodesk.AutoCAD.DatabaseServices.Polyline polyline)
- {
- DBObjectCollection dbObjColl = new DBObjectCollection();
- try
- {
- string layer = polyline.Layer;
- Point3dCollection point3dColl = polyline.GetPolylinePoint3ds();
- ResultBuffer rb = polyline.XData;
- //获取电力线方向箭头块
- string blockName = rb.AsArray()[2].Value as string;
- for (int i = 0; i < point3dColl.Count; i++)
- {
- Point3d startPoint3d = point3dColl[i];
- BlockReference block = GetBlock("gc170", startPoint3d, layer, 0, new Scale3d(1, 1, 1));
- dbObjColl.Add(block);
- if (i + 1 < point3dColl.Count)
- {
- Point3d endPoint3d = point3dColl[i + 1];
- Vector3d vector = (startPoint3d - endPoint3d).GetNormal();
- double douAngle = vector.AngleOnPlane(polyline.GetPlane());
- block = GetBlock(blockName, startPoint3d, layer, (douAngle - Math.PI), new Scale3d(1, 1, 1));
- dbObjColl.Add(block);
- block = GetBlock(blockName, endPoint3d, layer, douAngle, new Scale3d(1, 1, 1));
- dbObjColl.Add(block);
- }
- }
- return dbObjColl;
- }
- catch
- {
- return dbObjColl;
- }
- }
- //获取图块
- private static BlockReference GetBlock(string blcokName, Point3d insertPt, string layer, double angle, Scale3d scale3d)
- {
- Database db = AcadPropers.Database;
- BlockReference blockRef = null;
- using (Transaction trans = AcadPropers.Tm.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
- if (bt.Has(blcokName))
- {
- ObjectId blkId = bt[blcokName];
- blockRef = new BlockReference(insertPt, blkId);
- blockRef.ScaleFactors = new Scale3d(1, 1, 1);
- blockRef.Rotation = angle;
- blockRef.ScaleFactors = scale3d;
- blockRef.Layer = layer;
- }
- trans.Commit();
- }
- return blockRef;
- }
- }
- //显示重定义
- class TelegraphPoleOverrule : DrawableOverrule
- {
- public static TelegraphPoleOverrule TheOverrule = new TelegraphPoleOverrule();
- //设置重定义的过滤条件
- public TelegraphPoleOverrule()
- {
- SetXDataFilter(WolfHelper.PoleRegAppName);
- }
- //显示重载
- public override bool WorldDraw(Drawable drawable, WorldDraw wd)
- {
- Autodesk.AutoCAD.DatabaseServices.Polyline polyline = (Autodesk.AutoCAD.DatabaseServices.Polyline)drawable;
- DBObjectCollection dbObjColl = PoleGeometryHelper.GetPoleGeometrys(polyline);
- foreach (DBObject dbObject in dbObjColl)
- {
- wd.Geometry.Draw(dbObject);
- }
- //是否显示基线
- if (WolfHelper.DrawPoleBaseLine)
- base.WorldDraw(drawable, wd);
- return true;
- }
- }
- class TelegraphPoleTransformOverrule : TransformOverrule
- {
- public static TelegraphPoleTransformOverrule TheOverrule = new TelegraphPoleTransformOverrule();
- public TelegraphPoleTransformOverrule()
- {
- SetXDataFilter(WolfHelper.PoleRegAppName);
- }
- //重载Explode方法
- public override void Explode(Entity entity, DBObjectCollection entitySet)
- {
- Autodesk.AutoCAD.DatabaseServices.Polyline polyline = (Autodesk.AutoCAD.DatabaseServices.Polyline)entity;
- DBObjectCollection dbObjColl = PoleGeometryHelper.GetPoleGeometrys(polyline);
- foreach (DBObject dbObj in dbObjColl)
- {
- //此处必须使用add方法
- //entitySet = dbObjColl;无法生存对象
- entitySet.Add(dbObj);
- }
- }
- }
- }
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
参与人数 1 | 威望 +1 |
明经币 +2 |
金钱 +20 |
贡献 +5 |
激情 +5 |
收起
理由
|
雪山飞狐_lzh
| + 1 |
+ 2 |
+ 20 |
+ 5 |
+ 5 |
【精华】好程序 |
查看全部评分
|