2012년 11월 21일 수요일

[코드]TrueView - SendStringToExecute 사용 Ribbon (for AutoCAD R/LT/TV 2013)

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 Autodesk.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;

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

namespace iDwgField.Ribbon.Utils
{
    public class TextboxCommandHandler : ICommand
    {
#pragma warning disable 67
        public event EventHandler CanExecuteChanged;
#pragma warning restore 67

        public bool CanExecute(object parameter)
        {
            // Yes, we can execute
            return true;
        }

        public void Execute(object parameter)
        {
            // Dump the textbox contents to the command-line
            NotifyingTextBox tb = parameter as NotifyingTextBox;
            if (tb != null)
            {
                Document doc = AcadApp.DocumentManager.MdiActiveDocument;
                doc.SendStringToExecute(tb.GetTextWithoutNewlines() + "\n", true, false, true);
                tb.ClearText();
            }
        }
    }

    public class NotifyingTextBox : RibbonTextBox
    {
        double _baseHeight;
        double _baseWidth;
        double _heightPadding;
        double _widthPadding;
        bool _textChanging = false;

        public NotifyingTextBox(
            double width, double height,
            double widthPadding,
            double heightPadding
        )
        {
            // Set some member variables, some of which
            // we also use to set the TextBox dimensions
            _baseWidth = width;
            _baseHeight = height;
            _widthPadding = widthPadding;
            _heightPadding = heightPadding;

            Width = width;
            Height = height;
            MinWidth = width;
        }

        public string GetTextWithoutNewlines()
        {
            // Return the contained text without newline characters
            return TextValue.ReplaceNewlinesWithSpaces();
        }

        public void ClearText()
        {
            TextValue = "";
        }
    }

    public static class StringExtensions
    {
        const string newline = "\r\n";

        public static string InsertNewlineAtLastSpace(this string s)
        {
            // If the string contains a space, replace it
            // with a newline character, otherwise we simply
            // append the newline to the string
            string ret;
            if (s.Contains(" "))
            {
                int index = s.LastIndexOf(" ");
                ret = s.Substring(0, index) + newline + s.Substring(index + 1);
            }
            else
            {
                ret = s + newline;
            }
            return ret;
        }

        public static string ReplaceNewlinesWithSpaces(this string s)
        {
            // Replace all the newlines with spaces

            if (String.IsNullOrEmpty(s))
                return s;
            return s.Replace(newline, " ");
        }

        public static string GetLastLine(this string s)
        {
            // Return the last line of the text (or
            // the whole thing if there's no newline in it)
            string ret;
            if (s.Contains(newline))
            {
                ret = s.Substring(s.LastIndexOf(newline) + newline.Length);
            }
            else
            {
                ret = s;
            }
            return ret;
        }

        public static int GetLineCount(this string s)
        {
            return ((s.Length - s.Replace(newline, "").Length) / newline.Length) + 1;
        }
    }
}

public class Script
{
    private static ObjectId _idField;
    private static ObjectId _idFrom;
    private static ObjectId _idTo;
    private static RibbonTab _rt = null;

    public void RibbonUi()
    {
        RibbonControl rc = ComponentManager.Ribbon;
        foreach (RibbonTab tab in rc.Tabs)
        {
            if (tab.AutomationName == "iDwgField-Ribbon")
            {
                _rt = tab;
                break;
            }
        }
        // If we didn't find it, create a custom tab
        if (_rt == null)
        {
            _rt = new RibbonTab();
            _rt.Title = "리본메뉴테스트";
            _rt.Id = "ID_IDWGFIELD_RIBBONTAB";
            rc.Tabs.Add(_rt);
        }
        else
        {
            _rt.IsActive = true;
            return;
        }

        // Create our custom panel, add it to the ribbon tab
        RibbonPanelSource rps = new RibbonPanelSource();
        rps.Title = "리본메뉴테스트";
        RibbonPanel rp = new RibbonPanel();
        rp.Source = rps;
        _rt.Panels.Add(rp);

        // Create our custom textbox, add it to the panel
        iDwgField.Ribbon.Utils.NotifyingTextBox tb = new iDwgField.Ribbon.Utils.NotifyingTextBox(150, 15, 5, 5);
        tb.IsEmptyTextValid = true;
        tb.AcceptTextOnLostFocus = true;
        tb.InvokesCommand = true;
        tb.CommandHandler = new iDwgField.Ribbon.Utils.TextboxCommandHandler();
        rps.Items.Add(tb);

        // Set our tab to be active
        _rt.IsActive = true;
    }

    public void CloseUi()
    {
        if (_rt != null)
        {
            RibbonControl rc = ComponentManager.Ribbon;
            rc.Tabs.Remove(_rt);
            _rt = null;
        }
    }

    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(hField, 16);
            Handle hn1 = new Handle(ln1);
            _idField = db.GetObjectId(false, hn1, 0);

            long ln2 = Convert.ToInt64(hFrom, 16);
            if (ln2 > 0)
            {
                Handle hn2 = new Handle(ln2);
                _idFrom = db.GetObjectId(false, hn2, 0);
            }

            long ln3 = Convert.ToInt64(hTo, 16);
            if (ln3 > 0)
            {
                Handle hn3 = new Handle(ln3);
                _idTo = db.GetObjectId(false, hn3, 0);
            }

            RibbonUi();
        }
        catch (Autodesk.AutoCAD.Runtime.Exception e)
        {
            return e.Message;
        }

        return "Ribbon 로딩됨";
    }

    public string Open(string hField, string hFrom, string hTo)
    {
        RibbonUi();

        return Regen(hField, hFrom, hTo);
    }

    public void Exit(string msg)
    {
        CloseUi();
    }
}

댓글 없음:

댓글 쓰기