2012년 11월 27일 화요일

[코드] 타이머(for AutoCAD R/LT 2013) postID = "2109696018724171468"

using System;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;

using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using WinApp = System.Windows.Forms.Application;

namespace TransientSelection_6AA3989FA391470D9923
{
    public class TransientTimer : Transient
    {
        private Point3d _pos3d = new Point3d();
        private TextStyle _style;

        public TransientTimer()
        {
            _style = new TextStyle();
            _style.Font = new FontDescriptor("Calibri", false, true, 0, 0);
            _style.TextSize = 10;
        }
     
        public void SetPosition(Point3d pos3d)
          {
               _pos3d = pos3d;
          }
     
        protected override int SubSetAttributes(DrawableTraits traits)
        {
            traits.FillType = FillType.FillAlways;
            return (int)DrawableAttributes.None;
        }

        protected override void SubViewportDraw(ViewportDraw vd)
        {
            DrawText(vd.Geometry, "ViewportDraw");
        }

        protected override bool SubWorldDraw(WorldDraw wd)
        {
            DrawText(wd.Geometry,DateTime.Now.ToString("HH:mm:ss tt"));
            return true;
        }

        private void DrawText(Geometry g, string text)
        {
            if (g != null)
            {
                g.PushOrientationTransform(OrientationBehavior.Screen);
                g.PushPositionTransform(PositionBehavior.Screen, new Point2d(20,20));
                g.Text(
                    new Point3d(0, 0, 0), //_pos3d,  // Position
                    new Vector3d(0, 0, 1), // Normal
                    new Vector3d(1, 0, 0), // Direction
                    text,                  // Text
                    true,                  // Rawness
                    _style                 // TextStyle
                );
                g.PopModelTransform();
                g.PopModelTransform();
            }
        }

        protected override void OnDeviceInput(DeviceInputEventArgs e)
        {
            base.OnDeviceInput(e);
        }

        protected override void OnPointInput(PointInputEventArgs e)
        {
            base.OnPointInput(e);
        }
    }
}

public class Script
{
    private ObjectId _idField;
    private ObjectId _idFrom;
    private ObjectId _idTo;
    private TransientSelection_6AA3989FA391470D9923.TransientTimer _tb = null;
    private Point3d _pos3d = new Point3d();

    public string Open(string hField, string hFrom, string hTo)
    {
        _tb = new TransientSelection_6AA3989FA391470D9923.TransientTimer();
        Transient.CapturedDrawable = _tb;
        TransientManager.CurrentTransientManager.AddTransient(_tb, TransientDrawingMode.Main, 128, new IntegerCollection() );
        WinApp.Idle += new System.EventHandler(OnIdle);
        return Regen(hField, hFrom, hTo);
    }

    public void OnIdle(object sender, System.EventArgs e)
    {
        _tb.SetPosition(_pos3d);
       TransientManager.CurrentTransientManager.UpdateTransient(_tb, new IntegerCollection());
    }

    public string Regen(string hField, string hFrom, string hTo)
    {
        Document doc = AcadApp.DocumentManager.MdiActiveDocument;
        Editor ed = doc.Editor;
        try
        {
            Database db = doc.Database;

            long ln1 = Convert.ToInt64(hFrom, 16);
            Handle hn1 = new Handle(ln1);
            _idFrom= db.GetObjectId(false, hn1, 0);

            using(Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBObject txtObj = tr.GetObject(_idFrom, OpenMode.ForRead, false);
                if(txtObj !=null)
                {
                    if (txtObj.GetType() == typeof(MText))
                    {
                        _pos3d = ((MText)txtObj ).Location;
                    }
                    else
                    {
                        _pos3d = ((DBText)txtObj ).Position;
                    }
                }
            }
        }
        catch(Autodesk.AutoCAD.Runtime.Exception e)
        {
            return e.Message;
        }
        return "*";
    }

    public bool Exit(string msg)
    {
        WinApp.Idle -= new System.EventHandler(OnIdle);

        TransientManager.CurrentTransientManager.EraseTransient(_tb,
            new IntegerCollection());
        _tb.Dispose();
        _tb = null;

        return true;
    }

}

댓글 없음:

댓글 쓰기