明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 17418|回复: 18

[重定义] NetArx2010新特性-规则重定义

  [复制链接]
发表于 2009-5-13 21:30 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-5-14 14:46:31 编辑

.NetArx终于在AutoCAD2010版实现了自定义实体的子集--规则重定义,算是个好消息,
虽然很晚,而且功能没有ObjectArx的自定义实体强,但好歹没有强:)
下面的例子利用多行文字重定义为序号球,
更多的相关例子请看这里
  1. using Autodesk.AutoCAD.DatabaseServices;
  2. using Autodesk.AutoCAD.EditorInput;
  3. using Autodesk.AutoCAD.Geometry;
  4. using Autodesk.AutoCAD.GraphicsInterface;
  5. using Autodesk.AutoCAD.Runtime;
  6. using Autodesk.AutoCAD.ApplicationServices;
  7. [assembly: CommandClass(typeof(TlsCad.XhqHelper))]
  8. [assembly: ExtensionApplication(typeof(TlsCad.TlsApplication))]
  9. namespace TlsCad
  10. {
  11.     class TlsApplication : IExtensionApplication
  12.     {
  13.         void IExtensionApplication.Initialize()
  14.         {
  15.             XhqHelper.OverruleStart();
  16.             Overrule.Overruling = true;
  17.         }
  18.         void IExtensionApplication.Terminate()
  19.         {
  20.             XhqHelper.OverruleEnd();
  21.             Overrule.Overruling = false;
  22.         }
  23.     }
  24.     #region Helper
  25.     static class XhqHelper
  26.     {
  27.         public readonly static string RegAppName = "TlsCad.Xhq";
  28.         public readonly static double Radius = 8;
  29.         public readonly static double TextHeight = 8;
  30.         //获取起点
  31.         public static Point3d GetPoint(MText mtxt)
  32.         {
  33.             ResultBuffer rb = mtxt.GetXDataForApplication(RegAppName);
  34.             return (Point3d)rb.AsArray()[1].Value;
  35.         }
  36.         //设置起点,注意这里使用1011组码保存点,支持Copy、Move、Mirror等命令时实时更新XData
  37.         public static void SetPoint(MText mtxt, Point3d point)
  38.         {
  39.             ResultBuffer rb = new ResultBuffer(new TypedValue[] { new TypedValue(1001, RegAppName), new TypedValue(1011, point) });
  40.             mtxt.XData = rb;
  41.         }
  42.         [CommandMethod("xh")]
  43.         public static void XHQ()
  44.         {
  45.             PromptIntegerResult res1 = CadHelper.Editor.GetInteger(new PromptIntegerOptions("\n请输入序号:"));
  46.             PromptPointResult res2 = CadHelper.Editor.GetPoint(new PromptPointOptions("\n请输入起点:"));
  47.             XhqJig jig = new XhqJig(res2.Value, res1.Value.ToString());
  48.             PromptResult res = CadHelper.Editor.Drag(jig);
  49.             if (res.Status == PromptStatus.OK)
  50.             {
  51.                 Database db = Application.DocumentManager.MdiActiveDocument.Database;
  52.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  53.                 {
  54.                     BlockTableRecord btr =
  55.                         (BlockTableRecord)tr.GetObject(
  56.                             db.CurrentSpaceId,
  57.                             OpenMode.ForWrite,
  58.                             false);
  59.                     MText mtxt = jig.GetEntity();
  60.                     btr.AppendEntity(mtxt);
  61.                     tr.AddNewlyCreatedDBObject(mtxt, true);
  62.                     RegAppTable rat =
  63.                         (RegAppTable)tr.GetObject(
  64.                             db.RegAppTableId,
  65.                             OpenMode.ForRead,
  66.                             false);
  67.                     if (!rat.Has(RegAppName))
  68.                     {
  69.                         rat.UpgradeOpen();
  70.                         RegAppTableRecord regapp = new RegAppTableRecord();
  71.                         regapp.Name = RegAppName;
  72.                         rat.Add(regapp);
  73.                         tr.AddNewlyCreatedDBObject(regapp, true);
  74.                     }
  75.                     SetPoint(mtxt, res2.Value);
  76.                     tr.Commit();
  77.                 }
  78.             }
  79.         }
  80.         public static void OverruleStart()
  81.         {
  82.             Overrule.AddOverrule(RXObject.GetClass(typeof(MText)), XhqDrawOverrule.TheOverrule, false);
  83.             Overrule.AddOverrule(RXObject.GetClass(typeof(MText)), XhqGripOverrule.TheOverrule, false);
  84.             Overrule.AddOverrule(RXObject.GetClass(typeof(MText)), XhqOsnapOverrule.TheOverrule, false);
  85.             Overrule.AddOverrule(RXObject.GetClass(typeof(MText)), XhqTransformOverrule.TheOverrule, false);
  86.         }
  87.         public static void OverruleEnd()
  88.         {
  89.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(MText)), XhqDrawOverrule.TheOverrule);
  90.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(MText)), XhqGripOverrule.TheOverrule);
  91.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(MText)), XhqOsnapOverrule.TheOverrule);
  92.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(MText)), XhqTransformOverrule.TheOverrule);
  93.         }
  94.     }
  95.     #endregion
  96.     #region Jig
  97.     //序号球拖动类
  98.     class XhqJig : DrawJig
  99.     {
  100.         Point3d m_Location;
  101.         Line m_Line;
  102.         MText m_MText;
  103.         Circle m_Circle;
  104.         public XhqJig(Point3d FirstPoint, string No)
  105.         {
  106.             m_MText = new MText();
  107.             m_MText.Attachment = AttachmentPoint.MiddleCenter;
  108.             m_MText.Location = FirstPoint;
  109.             m_MText.TextHeight = XhqHelper.TextHeight;
  110.             m_MText.Contents = No;
  111.             m_Line = new Line(FirstPoint, FirstPoint);
  112.             m_Circle = new Circle();
  113.             m_Circle.Center = FirstPoint;
  114.             m_Circle.Radius = XhqHelper.Radius;
  115.         }
  116.         protected override SamplerStatus Sampler(JigPrompts prompts)
  117.         {
  118.             JigPromptPointOptions jigOpts = new JigPromptPointOptions();
  119.             jigOpts.UserInputControls =
  120.                 UserInputControls.Accept3dCoordinates |
  121.                 UserInputControls.NoZeroResponseAccepted |
  122.                 UserInputControls.NoNegativeResponseAccepted;
  123.             jigOpts.Message = "\n请输入终点:";
  124.             PromptPointResult res = prompts.AcquirePoint(jigOpts);
  125.             Point3d positionTemp = res.Value;
  126.             if (positionTemp != m_Location)
  127.             {
  128.                 m_Location = positionTemp;
  129.             }
  130.             else
  131.                 return SamplerStatus.NoChange;
  132.             if (res.Status == PromptStatus.Cancel)
  133.                 return SamplerStatus.Cancel;
  134.             else
  135.                 return SamplerStatus.OK;
  136.         }
  137.         protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
  138.         {
  139.             try
  140.             {
  141.                 Update();
  142.                 draw.Geometry.Draw(m_Circle);
  143.                 draw.Geometry.Draw(m_Line);
  144.                 draw.Geometry.Draw(m_MText);
  145.             }
  146.             catch (System.Exception)
  147.             {
  148.                 return false;
  149.             }
  150.             return true;
  151.         }
  152.         private void Update()
  153.         {
  154.             m_Circle.Center = m_Location;
  155.             Vector3d vec = m_Location - m_Line.StartPoint;
  156.             if (vec.Length <= m_Circle.Radius)
  157.             {
  158.                 m_Line.EndPoint = m_Line.StartPoint;
  159.             }
  160.             else
  161.             {
  162.                 vec = vec / vec.Length * m_Circle.Radius;
  163.                 m_Line.EndPoint = m_Location - vec;
  164.             }
  165.             m_MText.Location = m_Location;
  166.         }
  167.         public MText GetEntity()
  168.         {
  169.             Update();
  170.             return m_MText;
  171.         }
  172.     }
  173.     #endregion
  174.     #region Overrule
  175.     //夹点重定义
  176.     public class XhqGripOverrule : GripOverrule
  177.     {
  178.         public static XhqGripOverrule TheOverrule = new XhqGripOverrule();
  179.         public XhqGripOverrule()
  180.         {
  181.             SetXDataFilter(XhqHelper.RegAppName);
  182.         }
  183.         public override void GetGripPoints(Entity entity, Point3dCollection gripPoints, IntegerCollection snapModes, IntegerCollection geometryIds)
  184.         {
  185.             MText mtxt = (MText)entity;
  186.             Point3d pt = mtxt.Location;
  187.             gripPoints.Add(pt + new Vector3d(0, XhqHelper.Radius, 0));
  188.             gripPoints.Add(pt + new Vector3d(XhqHelper.Radius, 0, 0));
  189.             gripPoints.Add(pt - new Vector3d(0, XhqHelper.Radius, 0));
  190.             gripPoints.Add(pt - new Vector3d(XhqHelper.Radius, 0, 0));
  191.             gripPoints.Add(XhqHelper.GetPoint(mtxt));
  192.         }
  193.         public override void MoveGripPointsAt(Entity entity, IntegerCollection indices, Vector3d offset)
  194.         {
  195.             MText mtxt = (MText)entity;
  196.             foreach (int index in indices)
  197.             {
  198.                 switch (index)
  199.                 {
  200.                     case 0:
  201.                     case 1:
  202.                     case 2:
  203.                     case 3:
  204.                         mtxt.Location += offset;
  205.                         break;
  206.                     case 4:
  207.                         XhqHelper.SetPoint(mtxt, XhqHelper.GetPoint(mtxt) + offset);
  208.                         break;
  209.                 }
  210.             }
  211.         }
  212.     }
  213.     //捕捉重定义
  214.     public class XhqOsnapOverrule : OsnapOverrule
  215.     {
  216.         public static XhqOsnapOverrule TheOverrule = new XhqOsnapOverrule();
  217.         public XhqOsnapOverrule()
  218.         {
  219.             SetXDataFilter(XhqHelper.RegAppName);
  220.         }
  221.         public override void GetObjectSnapPoints(Entity entity, ObjectSnapModes snapMode, System.IntPtr gsSelectionMark, Point3d pickPoint, Point3d lastPoint, Matrix3d viewTransform, Point3dCollection snapPoints, IntegerCollection geometryIds, Matrix3d insertionMat)
  222.         {
  223.             MText mtxt = (MText)entity;
  224.             switch (snapMode)
  225.             {
  226.                 case ObjectSnapModes.ModeEnd:
  227.                     snapPoints.Add(XhqHelper.GetPoint(mtxt));
  228.                     break;
  229.                 case ObjectSnapModes.ModeQuad:
  230.                     Point3d pt = mtxt.Location;
  231.                     snapPoints.Add(pt + new Vector3d(0, XhqHelper.Radius, 0));
  232.                     snapPoints.Add(pt + new Vector3d(XhqHelper.Radius, 0, 0));
  233.                     snapPoints.Add(pt + new Vector3d(0, -XhqHelper.Radius, 0));
  234.                     snapPoints.Add(pt + new Vector3d(-XhqHelper.Radius, 0, 0));
  235.                     break;
  236.             }
  237.         }
  238.     }
  239.     //
  240.     public class XhqTransformOverrule : TransformOverrule
  241.     {
  242.         public static XhqTransformOverrule TheOverrule = new XhqTransformOverrule();
  243.         public XhqTransformOverrule()
  244.         {
  245.             SetXDataFilter(XhqHelper.RegAppName);
  246.         }
  247.         public override void Explode(Entity entity, DBObjectCollection entitySet)
  248.         {
  249.             base.Explode(entity, entitySet);
  250.             MText mtxt = (MText)entity;
  251.             entitySet.Add(new Circle(mtxt.Location, mtxt.Normal, XhqHelper.Radius));
  252.             Point3d pt1 = XhqHelper.GetPoint(mtxt);
  253.             Vector3d vec = mtxt.Location - pt1;
  254.             if (vec.Length > XhqHelper.Radius)
  255.             {
  256.                 vec = vec / vec.Length * XhqHelper.Radius;
  257.                 entitySet.Add(new Line(pt1, mtxt.Location - vec));
  258.             }
  259.         }
  260.     }
  261.     //MText->序号球
  262.     public class XhqDrawOverrule : DrawableOverrule
  263.     {
  264.         public static XhqDrawOverrule TheOverrule = new XhqDrawOverrule();
  265.         public XhqDrawOverrule()
  266.         {
  267.             SetXDataFilter(XhqHelper.RegAppName);
  268.         }
  269.         public override bool WorldDraw(Drawable drawable, WorldDraw wd)
  270.         {
  271.             MText mtxt = (MText)drawable;
  272.             wd.Geometry.Circle(mtxt.Location, XhqHelper.Radius, new Vector3d(0, 0, 1));
  273.             Point3d pt1 = XhqHelper.GetPoint(mtxt);
  274.             Vector3d vec = mtxt.Location - pt1;
  275.             if (vec.Length > XhqHelper.Radius)
  276.             {
  277.                 vec = vec / vec.Length * XhqHelper.Radius;
  278.                 wd.Geometry.WorldLine(pt1, mtxt.Location - vec);
  279.             }
  280.             return base.WorldDraw(drawable, wd);
  281.         }
  282.     }
  283.     #endregion
  284. }

本帖子中包含更多资源

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

x
发表于 2022-7-14 17:18 | 显示全部楼层
雪山飞狐_lzh 发表于 2009-5-14 23:31
来自于Autodesk开发者网络课程上的例子,会变色的温度计
当你移动当中三个空心的圆圈时,对应的温度数值会 ...

这个例子 现在加载好像没有反应
发表于 2018-9-28 15:45 | 显示全部楼层
终于找到,我想要的东西了感谢感谢
发表于 2009-5-14 11:26 | 显示全部楼层
我正在弄标号,可惜我弄得的CAD2008
 楼主| 发表于 2009-5-14 23:31 | 显示全部楼层
本帖最后由 作者 于 2009-5-15 7:49:14 编辑

来自于Autodesk开发者网络课程上的例子,会变色的温度计
当你移动当中三个空心的圆圈时,对应的温度数值会自动变化。

用netload命令加载下面的文件体验一下
命令TestOn,然后选择直线
  1. using System;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.GraphicsInterface;
  7. using Autodesk.AutoCAD.Runtime;
  8. [assembly: CommandClass(typeof(abc.TestOverrule))]
  9. [assembly: ExtensionApplication(typeof(abc.TestOverrule))]
  10. namespace abc
  11. {
  12.     #region "HelperClass"
  13.     // Global helper class (singleton). Contains central definitions of some global constants,
  14.     // and a few helper functions
  15.     public class HelperClass
  16.     {
  17.         const String mExtDictName = "SGP_MyDict";
  18.         // Defines Dictionary name for the Extension Dictionary demo
  19.         const String mXRecName = "SGP_MyDATA";
  20.         // Defines Dictionary name for the Extension Dictionary demo
  21.         private static HelperClass mMe;
  22.         // Name of our dictionary in extension dictionary
  23.         public String DictionaryName
  24.         {
  25.             get
  26.             {
  27.                 return mExtDictName;
  28.             }
  29.         }
  30.         // Name of our XRecord
  31.         public String XRecordName
  32.         {
  33.             get
  34.             {
  35.                 return mXRecName;
  36.             }
  37.         }
  38.         // Protected constructor - to enforce singleton behavior
  39.         protected HelperClass()
  40.         {
  41.         }
  42.         // static function to retrieve one and only instance of singleton
  43.         public static HelperClass GetSingleton
  44.         {
  45.             get
  46.             {
  47.                 if (mMe == null)
  48.                 {
  49.                     mMe = new HelperClass();
  50.                 }
  51.                 return mMe;
  52.             }
  53.         }
  54.         // Retrieve data (as resbuf) from or Xrecord.
  55.         // Returns null object if there's a problem
  56.         public ResultBuffer GetXRecordData(DBObject obj)
  57.         {
  58.             Xrecord xRec = null;
  59.             ObjectId id = obj.ExtensionDictionary;
  60.             // Make sure we have an ext dict befoore proceeding
  61.             if (id.IsValid)
  62.             {
  63.                 // Retrieve data using a transaction
  64.                 Database db = Application.DocumentManager.MdiActiveDocument.Database;
  65.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  66.                 {
  67.                     DBDictionary extDict = (DBDictionary)tr.GetObject(id, OpenMode.ForRead, false);
  68.                     if (extDict.Contains(DictionaryName))
  69.                     {
  70.                         // We're assuming that if my dictionary exists, then so will the XRecord in it.
  71.                         ObjectId dictId = extDict.GetAt((String)DictionaryName);
  72.                         DBDictionary myDict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForRead);
  73.                         xRec = (Xrecord)tr.GetObject(myDict.GetAt((String)XRecordName),
  74.                             OpenMode.ForRead);
  75.                     }
  76.                 }
  77.             }
  78.             if (xRec == null)
  79.             {
  80.                 return null;
  81.             }
  82.             else
  83.             {
  84.                 return xRec.Data;
  85.             }
  86.         }
  87.         // Modifies data in our XRecord.
  88.         // (creates ou rdictionary and XRecoird if it doesn't already exist)
  89.         public void SetXRecordData(DBObject obj, ResultBuffer myData)
  90.         {
  91.             Database db = Application.DocumentManager.MdiActiveDocument.Database;
  92.             using (Transaction tr = db.TransactionManager.StartTransaction())
  93.             {
  94.                 DBDictionary myDict = default(DBDictionary);
  95.                 Xrecord xRec = null;
  96.                 ObjectId id = obj.ExtensionDictionary;
  97.                 if (id == ObjectId.Null)
  98.                 {
  99.                     obj.CreateExtensionDictionary();
  100.                     id = obj.ExtensionDictionary;
  101.                 }
  102.                 DBDictionary extDict = (DBDictionary)tr.GetObject(id, OpenMode.ForWrite);
  103.                 if (extDict.Contains(DictionaryName))
  104.                 {
  105.                     ObjectId dictId = extDict.GetAt((String)DictionaryName);
  106.                     myDict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForWrite);
  107.                 }
  108.                 else
  109.                 {
  110.                     myDict = new DBDictionary();
  111.                     extDict.SetAt((String)DictionaryName, myDict);
  112.                     tr.AddNewlyCreatedDBObject(myDict, true);
  113.                 }
  114.                 if (myDict.Contains(XRecordName))
  115.                 {
  116.                     xRec = (Xrecord)tr.GetObject(myDict.GetAt((String)XRecordName),
  117.                         OpenMode.ForWrite);
  118.                 }
  119.                 else
  120.                 {
  121.                     xRec = new Xrecord();
  122.                     myDict.SetAt((String)XRecordName, xRec);
  123.                     tr.AddNewlyCreatedDBObject(xRec, true);
  124.                 }
  125.                 xRec.Data = myData;
  126.                 tr.Commit();
  127.             }
  128.         }
  129.     }
  130.     #endregion
  131.     // Grip overrule to add our custom grips to the line
  132.     public class MyGripOverrule : GripOverrule
  133.     {
  134.         public class MyGrip : GripData
  135.         {
  136.             private int mGripNum;
  137.             public int Ordinal
  138.             {
  139.                 get
  140.                 {
  141.                     return mGripNum;
  142.                 }
  143.                 set
  144.                 {
  145.                     mGripNum = value;
  146.                 }
  147.             }
  148.             // Call this to tell the grip to move itself
  149.             public void Move(Vector3d vec)
  150.             {
  151.                 GripPoint = GripPoint + vec;
  152.             }
  153.             public override bool ViewportDraw(ViewportDraw worldDraw, ObjectId entityId,
  154.                 GripData.DrawType type, Point3d? imageGripPoint, int gripSizeInPixels)
  155.             {
  156.                 Point2d unit = worldDraw.Viewport.GetNumPixelsInUnitSquare(GripPoint);
  157.                 worldDraw.Geometry.Circle(GripPoint, 1.5 * gripSizeInPixels / unit.X,
  158.                     worldDraw.Viewport.ViewDirection);
  159.                 return true;
  160.             }
  161.         }
  162.         // Array to hold our 3 grips
  163.         GripData[] mGripData = new GripData[3];
  164.         public override void GetGripPoints(Entity entity, GripDataCollection grips,
  165.             double curViewUnitSize, int gripSize, Vector3d curViewDir, GetGripPointsFlags bitFlags)
  166.         {
  167.             ResultBuffer rb = HelperClass.GetSingleton.GetXRecordData(entity);
  168.             // We assume entity is a line
  169.             Line myLine = (Line)entity;
  170.             // Set grip positions to represent temperatures (we're using Celsius)
  171.             // min temperature
  172.             int temp = (int)rb.AsArray()[1].Value;
  173.             double pos = myLine.StartParam + (temp / 100.0) * (myLine.EndParam - myLine.StartParam);
  174.             Point3d pt = myLine.GetPointAtParameter(pos);
  175.             MyGrip grip = new MyGrip();
  176.             grip.Ordinal = 0;
  177.             grip.GripPoint = pt;
  178.             mGripData[0] = grip;
  179.             // max temperature
  180.             temp = (int)rb.AsArray()[2].Value;
  181.             pos = myLine.StartParam + (temp / 100.0) * (myLine.EndParam - myLine.StartParam);
  182.             pt = myLine.GetPointAtParameter(pos);
  183.             grip = new MyGrip();
  184.             grip.Ordinal = 1;
  185.             grip.GripPoint = pt;
  186.             mGripData[1] = grip;
  187.             // current temperature
  188.             temp = (int)rb.AsArray()[3].Value;
  189.             pos = myLine.StartParam + (temp / 100.0) * (myLine.EndParam - myLine.StartParam);
  190.             pt = myLine.GetPointAtParameter(pos);
  191.             grip = new MyGrip();
  192.             grip.Ordinal = 2;
  193.             grip.GripPoint = pt;
  194.             mGripData[2] = grip;
  195.             // Add our grips to the list
  196.             foreach (MyGrip g in mGripData)
  197.             {
  198.                 grips.Add(g);
  199.             }
  200.             // Get the standard line grip points as well
  201.             base.GetGripPoints(entity, grips, curViewUnitSize, gripSize, curViewDir, bitFlags);
  202.             Point3d qq1 = grips[0].GripPoint;
  203.             Point3d qq2 = grips[1].GripPoint;
  204.         }
  205.         public override void MoveGripPointsAt(Entity entity, GripDataCollection grips,
  206.             Vector3d offset, MoveGripPointsFlags bitFlags)
  207.         {
  208.             // We only take action when we get this call on a database resident entity
  209.             // Dragging operation makes shallow clone of line,
  210.             // and setting clomeMeForDragging to false is generally a bad idea.
  211.             // (If you do set clone me for dragging to false, then don't call bae class overriden methods).
  212.             if (entity.Id.IsValid)
  213.             {
  214.                 // Cast to a Line so we can access properties
  215.                 Line myLine = (Line)entity;
  216.                 Vector3d lineDir = (myLine.EndPoint - myLine.StartPoint);
  217.                 lineDir = lineDir.GetNormal();
  218.                 // Direction of Line
  219.                 double offsetDist = lineDir.DotProduct(offset);
  220.                 // Component of mouse translation along like
  221.                 // Iterate through list of all grips being moved
  222.                 foreach (GripData g in grips)
  223.                 {
  224.                     if (g is MyGrip)
  225.                     {
  226.                         MyGrip grip = (MyGrip)g;
  227.                         // Cast to our grip type
  228.                         // Make sure offset never takes grip beyond either end of line
  229.                         if (offsetDist >= 0)
  230.                         {
  231.                             if (offsetDist > (myLine.EndPoint - grip.GripPoint).Length)
  232.                             {
  233.                                 offsetDist = (myLine.EndPoint - grip.GripPoint).Length;
  234.                             }
  235.                         }
  236.                         else
  237.                         {
  238.                             if (-offsetDist > (myLine.StartPoint - grip.GripPoint).Length)
  239.                             {
  240.                                 offsetDist = -(myLine.StartPoint - grip.GripPoint).Length;
  241.                             }
  242.                         }
  243.                         lineDir = lineDir * offsetDist;
  244.                         // retrieve stored data and edit the changed value
  245.                         ResultBuffer rb = HelperClass.GetSingleton.GetXRecordData(entity);
  246.                         TypedValue[] typeValue = rb.AsArray();
  247.                         String val1 = (String)typeValue[0].Value;
  248.                         int[] intVal = new int[3];
  249.                         intVal[0] = (int)typeValue[1].Value;
  250.                         // min
  251.                         intVal[1] = (int)typeValue[2].Value;
  252.                         // max
  253.                         intVal[2] = (int)typeValue[3].Value;
  254.                         // current
  255.                         // Tell grip to move itself long the line
  256.                         grip.Move(lineDir);
  257.                         // Calculate new temperature from grip position along the line
  258.                         double newParam = myLine.GetParameterAtPoint(grip.GripPoint);
  259.                         int newTemp = (int)(100 * (newParam - myLine.StartParam) / (myLine.EndParam - myLine.StartParam));
  260.                         // Don't let min temp value rise above max temp
  261.                         // And don't let max temp go below min temp
  262.                         if (grip.Ordinal == 0)
  263.                         {
  264.                             if (newTemp < intVal[1])
  265.                             {
  266.                                 intVal[0] = newTemp;
  267.                             }
  268.                             else
  269.                             {
  270.                                 intVal[0] = intVal[1] - 1;
  271.                             }
  272.                         }
  273.                         else if (grip.Ordinal == 1)
  274.                         {
  275.                             if (newTemp > intVal[0])
  276.                             {
  277.                                 intVal[1] = newTemp;
  278.                             }
  279.                             else
  280.                             {
  281.                                 intVal[1] = intVal[0] + 1;
  282.                             }
  283.                         }
  284.                         else
  285.                         {
  286.                             intVal[2] = newTemp;
  287.                         }
  288.                         // Create new resbuf with new data and put back in Xrecord
  289.                         ResultBuffer newRb = new ResultBuffer(new TypedValue((int)DxfCode.Text, val1),
  290.                             new TypedValue((int)DxfCode.Int32, intVal[0]),
  291.                             new TypedValue((int)DxfCode.Int32, intVal[1]),
  292.                             new TypedValue((int)DxfCode.Int32, intVal[2]));
  293.                         HelperClass.GetSingleton.SetXRecordData(myLine, newRb);
  294.                     }
  295.                 }
  296.             }
  297.             // Remove our grips from the list befroe calling base class function
  298.             // (Doesn't seem to like my grips)
  299.             for (int i = grips.Count - 1; i >= 0; i += -1)
  300.             {
  301.                 if (grips[i] is MyGrip)
  302.                 {
  303.                     grips.Remove(grips[i]);
  304.                 }
  305.             }
  306.             // If any grips left, then we call base class function
  307.             if (grips.Count > 0)
  308.             {
  309.                 base.MoveGripPointsAt(entity, grips, offset, bitFlags);
  310.             }
  311.         }
  312.     }
  313.     #region "Simple DrawableOverrule "
  314.     // This overrule adds our custom graphhics to the Line
  315.     // We're going to turn our Line into a Thermometer
  316.     public class MyDrawOverrule : DrawableOverrule
  317.     {
  318.         const int mSize = 30;
  319.         // Universal scaling constant - so I don't have to edit every calculation
  320.         // if I want the thermometer thicker or thinner
  321.         // This is the function that gets called to add/replace an entity's WorldDraw graphics
  322.         public override bool WorldDraw(Drawable drawable, WorldDraw wd)
  323.         {
  324.             // Is it a line? (It should be)
  325.             if (!(drawable is Line))
  326.             {
  327.                 return base.WorldDraw(drawable, wd);
  328.             }
  329.             Line myLine = (Line)drawable;
  330.             Point3dCollection pts = new Point3dCollection();
  331.             // Read Xrecord values to populate prompt defauls
  332.             ResultBuffer resbuf = HelperClass.GetSingleton.GetXRecordData(myLine);
  333.             TypedValue[] typeValue = resbuf.AsArray();
  334.             // Room name
  335.             String myText = (String)typeValue[0].Value;
  336.             // Min temp
  337.             int lowerTemp = (int)typeValue[1].Value;
  338.             // max temp  
  339.             int upperTemp = (int)typeValue[2].Value;
  340.             // Current temp
  341.             int curTemp = (int)typeValue[3].Value;
  342.             double curPos = curTemp / 100.0;
  343.             Vector3d perpVec = (myLine.EndPoint - myLine.StartPoint).CrossProduct(myLine.Normal).GetNormal();
  344.             double startParam = myLine.GetParameterAtPoint(myLine.StartPoint);
  345.             double endParam = myLine.GetParameterAtPoint(myLine.EndPoint);
  346.             var oldColIndex = wd.SubEntityTraits.Color;
  347.             FillType oldFillType = wd.SubEntityTraits.FillType;
  348.             double posParam = 0;
  349.             IntPtr gsMarker = default(IntPtr);
  350.             // Draw thermometer body
  351.             wd.SubEntityTraits.FillType = FillType.FillNever;
  352.             // right body edge
  353.             pts.Clear();
  354.             pts.Add(myLine.StartPoint + perpVec * myLine.Length * 2.5 / mSize);
  355.             pts.Add(myLine.EndPoint + perpVec * myLine.Length * 2.5 / mSize);
  356.             gsMarker = (System.IntPtr)1;
  357.             wd.Geometry.Polyline(pts, myLine.Normal, gsMarker);
  358.             // left body edge
  359.             pts.Clear();
  360.             pts.Add(myLine.EndPoint - perpVec * myLine.Length * 2.5 / mSize);
  361.             pts.Add(myLine.StartPoint - perpVec * myLine.Length * 2.5 / mSize);
  362.             gsMarker = (System.IntPtr)2;
  363.             wd.Geometry.Polyline(pts, myLine.Normal, gsMarker);
  364.             // top body edge
  365.             wd.Geometry.CircularArc(myLine.EndPoint - perpVec * myLine.Length * 2.5 / mSize,
  366.                 myLine.EndPoint + (myLine.EndPoint - myLine.StartPoint) * 2.5 / mSize,
  367.                 myLine.EndPoint + perpVec * myLine.Length * 2.5 / mSize, ArcType.ArcSimple);
  368.             // bottom body edge
  369.             double theta = Math.PI / 6;
  370.             double rad = (myLine.Length * 2.5 / mSize) / Math.Sin(theta);
  371.             double a = (myLine.Length * 2.5 / mSize) / Math.Tan(theta);
  372.             Point3d bowlCenter = myLine.StartPoint + (myLine.StartPoint - myLine.EndPoint).GetNormal() * a;
  373.             wd.Geometry.CircularArc(myLine.StartPoint + perpVec * myLine.Length * 2.5 / mSize,
  374.                 myLine.StartPoint + (myLine.StartPoint - myLine.EndPoint).GetNormal() * (rad + a),
  375.                 myLine.StartPoint - perpVec * myLine.Length * 2.5 / mSize, ArcType.ArcSimple);
  376.             // Draw upper temperature marker (in red)
  377.             wd.SubEntityTraits.Color = 1;
  378.             posParam = startParam + (endParam - startParam) * (upperTemp / 100.0);
  379.             pts.Clear();
  380.             pts.Add(myLine.GetPointAtParameter(posParam) - perpVec * myLine.Length * 3 / mSize);
  381.             pts.Add(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 3 / mSize);
  382.             gsMarker = (System.IntPtr)3;
  383.             wd.Geometry.Polyline(pts, myLine.Normal, gsMarker);
  384.             wd.Geometry.Text(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 4 / mSize,
  385.                 myLine.Normal, perpVec, myLine.Length * 1.2 / mSize, 1, 0,
  386.                 "Max. Temp = " + upperTemp.ToString());
  387.             // Draw lower temperature marker (in blue)
  388.             wd.SubEntityTraits.Color = 5;
  389.             posParam = startParam + (endParam - startParam) * (lowerTemp / 100.0);
  390.             pts.Clear();
  391.             pts.Add(myLine.GetPointAtParameter(posParam) - perpVec * myLine.Length * 3 / mSize);
  392.             pts.Add(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 3 / mSize);
  393.             gsMarker = (System.IntPtr)3;
  394.             wd.Geometry.Polyline(pts, myLine.Normal, gsMarker);
  395.             wd.Geometry.Text(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 4 / mSize,
  396.                 myLine.Normal, perpVec, myLine.Length * 1.2 / mSize, 1, 0,
  397.                 "Min. Temp = " + lowerTemp.ToString());
  398.             // Draw current temperature marker in different color depending on position w.r.t. min and max temps
  399.             short colIndex = 0;
  400.             if (curTemp <= lowerTemp)
  401.             {
  402.                 colIndex = 5;
  403.                 // Blue
  404.             }
  405.             else if (curTemp >= upperTemp)
  406.             {
  407.                 colIndex = 1;
  408.                 // Red
  409.             }
  410.             else
  411.             {
  412.                 colIndex = 94;
  413.                 // Dark green
  414.             }
  415.             // Draw current Temperature marker
  416.             wd.SubEntityTraits.Color = colIndex;
  417.             posParam = startParam + (endParam - startParam) * (curTemp / 100.0);
  418.             pts.Clear();
  419.             pts.Add(myLine.GetPointAtParameter(posParam) - perpVec * myLine.Length * 3 / mSize);
  420.             pts.Add(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 3 / mSize);
  421.             gsMarker = (System.IntPtr)4;
  422.             wd.Geometry.Polyline(pts, myLine.Normal, gsMarker);
  423.             wd.Geometry.Text(myLine.GetPointAtParameter(posParam) + perpVec * myLine.Length * 4 / mSize,
  424.                 myLine.Normal, perpVec, myLine.Length * 1.2 / mSize, 1, 0,
  425.                 myText + " Temp = " + curTemp.ToString());
  426.             // We want to draw filled primitives (polygon and circle) to
  427.             // represent the mercury in the thermometer
  428.             wd.SubEntityTraits.FillType = FillType.FillAlways;
  429.             // drawable mercury - line first, then bowl
  430.             pts.Clear();
  431.             Vector3d offset = perpVec * myLine.Length / mSize;
  432.             Point3d pt1 = myLine.StartPoint + offset;
  433.             pts.Add(bowlCenter + offset);
  434.             pts.Add(bowlCenter - offset);
  435.             pts.Add(myLine.GetPointAtParameter(posParam) - offset);
  436.             pts.Add(myLine.GetPointAtParameter(posParam) + offset);
  437.             wd.Geometry.Polygon(pts);
  438.             // mercury bowl
  439.             theta = Math.PI / 6;
  440.             rad = 1.5 * (offset.Length) / Math.Sin(theta);
  441.             a = (offset.Length) / Math.Tan(theta);
  442.             wd.Geometry.Circle(bowlCenter, rad, myLine.Normal);
  443.             // Set old subentitytrait values, then call overriden class worlddraw fn
  444.             wd.SubEntityTraits.FillType = oldFillType;
  445.             wd.SubEntityTraits.Color = oldColIndex;
  446.             return base.WorldDraw(drawable, wd);
  447.         }
  448.     }
  449.     #endregion
  450.     #region "Implementation of the commands"
  451.     public class TestOverrule : IExtensionApplication
  452.     {
  453.         // Setup some global variables
  454.         static MyDrawOverrule mDrawOverrule;
  455.         // One and only instance of this DrawableOverrule
  456.         static MyGripOverrule mGripOverrule;
  457.         // One and only instance of this TransformOverrule
  458.         // Called when DLL is loaded by AutoCAD.
  459.         public void Initialize()
  460.         {
  461.             // Instantiate our global Overrule and set it to overrule lines with my data attached
  462.             mDrawOverrule = new MyDrawOverrule();
  463.             Overrule.AddOverrule(RXObject.GetClass(typeof(Line)), mDrawOverrule, false);
  464.             mDrawOverrule.SetExtensionDictionaryEntryFilter(HelperClass.GetSingleton.DictionaryName);
  465.             // Instantiate our global Overrule and set it to overrule lines with my data attached
  466.             mGripOverrule = new MyGripOverrule();
  467.             Overrule.AddOverrule(RXObject.GetClass(typeof(Line)), mGripOverrule, false);
  468.             mGripOverrule.SetExtensionDictionaryEntryFilter(HelperClass.GetSingleton.DictionaryName);
  469.             // Turn overruling on
  470.             Overrule.Overruling = true;
  471.         }
  472.         // Clean up after ourselves.
  473.         public void Terminate()
  474.         {
  475.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(Line)), mDrawOverrule);
  476.             mDrawOverrule = null;
  477.             Overrule.RemoveOverrule(RXObject.GetClass(typeof(Line)), mGripOverrule);
  478.             mDrawOverrule = null;
  479.         }
  480.         // Toggles all overrules on and off.
  481.         [CommandMethod("testoff")]
  482.         public void ToggleOverrule()
  483.         {
  484.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  485.             Overrule.Overruling = !Overrule.Overruling;
  486.             ed.WriteMessage("\n*** Overrule is now " + Overrule.Overruling.ToString() + " ***\n");
  487.             ed.Regen();
  488.         }
  489.         // Demo of Extension Dictionary filter.
  490.         // There's also an Xdata filter, but we won't demonstrate it here - its basically the same).
  491.         // This command needs tidying up to use HelperClass functions for XData access. (Currently does its own thing).
  492.         [CommandMethod("teston")]
  493.         public void AddXDictFilter()
  494.         {
  495.             // Select a line
  496.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  497.             PromptEntityOptions opts = new PromptEntityOptions("\nSelect a line to add Extension dictionary to:");
  498.             opts.SetRejectMessage("\nSorry dude! That's not a line\n");
  499.             opts.AddAllowedClass(typeof(Line), true);
  500.             PromptEntityResult res = ed.GetEntity(opts);
  501.             // Only continue if a circle was selected
  502.             if (res.Status != PromptStatus.OK)
  503.             {
  504.                 return;
  505.             }
  506.             // Open circle and make sure it has our dictionary in its extension dictionary
  507.             ObjectId objId = res.ObjectId;
  508.             Database db = objId.Database;
  509.             using (Transaction tr = db.TransactionManager.StartTransaction())
  510.             {
  511.                 Entity ent = (Entity)tr.GetObject(objId, OpenMode.ForRead);
  512.                 ObjectId extId = ent.ExtensionDictionary;
  513.                 // Create ext dict if necessary
  514.                 if (extId == ObjectId.Null)
  515.                 {
  516.                     ent.UpgradeOpen();
  517.                     ent.CreateExtensionDictionary();
  518.                     extId = ent.ExtensionDictionary;
  519.                 }
  520.                 // Open ext dict
  521.                 DBDictionary extDict = (DBDictionary)tr.GetObject(extId, OpenMode.ForWrite);
  522.                 // make sure we clone data when entity is cloned for dragging
  523.                 extDict.TreatElementsAsHard = true;
  524.                 // If it doesn't contain our dictionary, we add one
  525.                 PromptIntegerOptions temp1Opts = new PromptIntegerOptions("\nEnter Lower Temperature:");
  526.                 PromptIntegerOptions temp2Opts = new PromptIntegerOptions("\nEnter Upper Temperature:");
  527.                 PromptIntegerOptions temp3Opts = new PromptIntegerOptions("\nEnter Current Temperature:");
  528.                 PromptStringOptions nameOpts = new PromptStringOptions("\nEnter Name:");
  529.                 temp1Opts.LowerLimit = 0;
  530.                 temp1Opts.UpperLimit = 100;
  531.                 temp2Opts.LowerLimit = 0;
  532.                 temp2Opts.UpperLimit = 100;
  533.                 temp3Opts.LowerLimit = 0;
  534.                 temp1Opts.UpperLimit = 100;
  535.                 ObjectId xRecObjID;
  536.                 Xrecord xRec;
  537.                 DBDictionary myDict;
  538.                 if (!extDict.Contains(HelperClass.GetSingleton.XRecordName))
  539.                 {
  540.                     // If dict is not present, then we add it and set up default Xrec to be edited later
  541.                     extDict.UpgradeOpen();
  542.                     myDict = new DBDictionary();
  543.                     // make sure we clone data when entity is cloned for dragging
  544.                     myDict.TreatElementsAsHard = true;
  545.                     extDict.SetAt(HelperClass.GetSingleton.DictionaryName, myDict);
  546.                     tr.AddNewlyCreatedDBObject(myDict, true);
  547.                     temp1Opts.DefaultValue = 20;
  548.                     temp2Opts.DefaultValue = 30;
  549.                     temp3Opts.DefaultValue = 25;
  550.                     nameOpts.DefaultValue = "San Rafael";
  551.                     xRec = new Xrecord();
  552.                     xRec.Data = new ResultBuffer(new TypedValue((int)DxfCode.Text, nameOpts.DefaultValue),
  553.                         new TypedValue((int)DxfCode.Int32, temp1Opts.DefaultValue),
  554.                         new TypedValue((int)DxfCode.Int32, temp2Opts.DefaultValue),
  555.                         new TypedValue((int)DxfCode.Int32, temp3Opts.DefaultValue));
  556.                     xRecObjID = myDict.SetAt(HelperClass.GetSingleton.XRecordName, xRec);
  557.                     tr.AddNewlyCreatedDBObject(xRec, true);
  558.                 }
  559.                 else
  560.                 {
  561.                     // If dict exists, then we extract values from XRecord to populate default values from prompt
  562.                     // We're assuming that if my dictionary exists, then so will the XRecord in it.
  563.                     ObjectId dictId = extDict.GetAt(HelperClass.GetSingleton.DictionaryName);
  564.                     myDict = (DBDictionary)tr.GetObject(dictId, OpenMode.ForWrite, false);
  565.                     temp1Opts.DefaultValue = 20;
  566.                     temp1Opts.DefaultValue = 30;
  567.                     xRecObjID = myDict.GetAt(HelperClass.GetSingleton.XRecordName);
  568.                     xRec = (Xrecord)tr.GetObject(xRecObjID, OpenMode.ForRead, false);
  569.                 }
  570.                 // xRec now points to our XRecord, which is open for write.
  571.                 // Read Xrecord values to populate prompt defauls
  572.                 TypedValue[] typeValue = xRec.Data.AsArray();
  573.                 TypedValue val1 = typeValue[0];
  574.                 // Room name
  575.                 TypedValue val2 = typeValue[1];
  576.                 // Min temp
  577.                 TypedValue val3 = typeValue[2];
  578.                 // Max temp
  579.                 TypedValue val4 = typeValue[3];
  580.                 // Current temp
  581.                 nameOpts.DefaultValue = (String)val1.Value;
  582.                 temp1Opts.DefaultValue = (int)val2.Value;
  583.                 temp2Opts.DefaultValue = (int)val3.Value;
  584.                 temp3Opts.DefaultValue = (int)val4.Value;
  585.                 // Prompt for new values
  586.                 PromptResult nameRes = ed.GetString(nameOpts);
  587.                 if (nameRes.Status == PromptStatus.OK)
  588.                 {
  589.                     val1 = new TypedValue((int)DxfCode.Text, nameRes.StringResult);
  590.                 }
  591.                 PromptIntegerResult temp1Res = ed.GetInteger(temp1Opts);
  592.                 if (temp1Res.Status == PromptStatus.OK)
  593.                 {
  594.                     val2 = new TypedValue((int)DxfCode.Int32, temp1Res.Value);
  595.                 }
  596.                 PromptIntegerResult temp2Res = ed.GetInteger(temp2Opts);
  597.                 if (temp2Res.Status == PromptStatus.OK)
  598.                 {
  599.                     val3 = new TypedValue((int)DxfCode.Int32, temp2Res.Value);
  600.                 }
  601.                 PromptIntegerResult temp3Res = ed.GetInteger(temp3Opts);
  602.                 if (temp3Res.Status == PromptStatus.OK)
  603.                 {
  604.                     val4 = new TypedValue((int)DxfCode.Int32, temp3Res.Value);
  605.                 }
  606.                 // Now set Xrecord contents to new values
  607.                 xRec.Data = new ResultBuffer(val1, val2, val3, val4);
  608.                 tr.Commit();
  609.             }
  610.             // Display new results
  611.             ed.Regen();
  612.         }
  613.     }
  614.     #endregion
  615. }

本帖子中包含更多资源

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

x
发表于 2009-5-14 23:51 | 显示全部楼层

问个问题,2010能不能存成低版本的,如果可以,存成低版本后,自定义实体会变成什么样子的,是否会消失?

 楼主| 发表于 2009-5-15 07:45 | 显示全部楼层

可以存成低版本的,但别说存低版本,就是不加载Dll,Overrule就会还原

注意Overrule是规则重定义,采取的手段只是在现有的实体上附加数据(XData或LData),然后由dll读取数据解释为重定义后形式

感觉这一版的Overrule要么是自动桌子的一次尝试,可能下一版本会推出真的自定义实体(严重怀疑)

要么就是内部开发人员在Net这块出现了分歧,不打算支持自定义实体(自己猜的)

不过现阶段真正的自定义实体可以用ObjectArx实现,然后NetArx去调用

Overrule的用途现在想到的只有图纸加密:重要的东西规则重定义,别人没有我的Dll就看不见,:)
发表于 2009-7-24 09:59 | 显示全部楼层

GripOverrule...需要引用什么才可以用?

我用的是2008!

 楼主| 发表于 2009-7-24 10:42 | 显示全部楼层
注意AutoCad要2010版本
发表于 2009-8-26 10:12 | 显示全部楼层

GripOverrule...需要引用什么才可以用?

发表于 2009-9-28 18:01 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
发表于 2009-10-22 16:57 | 显示全部楼层

飞狐兄,这些真是不错啊·但可惜的是必须要高版本的·

支持!

支持!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-3-29 22:00 , Processed in 0.206966 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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