2012년 11월 12일 월요일

[코드]계산기기능 (postID=3352286799413239524)


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;
 
public class MathParser
{
    public static double EvalExpression(char[] expr)
    {
        return parseSummands(expr, 0);
    }
    private static double parseSummands(char[] expr, int index)
    {
        double x = parseFactors(expr, ref index);
        while (true)
        {
            char op = expr[index];
            if (op != '+' && op != '-')
                return x;
            index++;
            double y = parseFactors(expr, ref index);
            if (op == '+')
                x += y;
            else
                x -= y;
        }
    }
    private static double parseFactors(char[] expr, ref int index)
    {
        double x = GetDouble(expr, ref index);
        while (true)
        {
            char op = expr[index];
            if (op != '/' && op != '*')
               return x;
            index++;
            double y = GetDouble(expr, ref index);
            if (op == '/')
                x /= y;
            else
                x *= y;
        }
    }
    private static double GetDouble(char[] expr, ref int index)
    {
        string dbl = "";
        while (((int)expr[index] >= 48 && (int)expr[index] < 57) || expr[index] == 46)
        {
            dbl = dbl + expr[index].ToString();
            index++;
            if (index == expr.Length)
            {
                index--;
                break;
            }
        }
        return double.Parse(dbl);
    }
}
 
public class Script
{
    private ObjectId _idField;
    private ObjectId _idFrom;
    private ObjectId _idTo;
 
    public string Open(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);
            }
        }
        catch(Autodesk.AutoCAD.Runtime.Exception e)
        {
            return e.Message;
        }
        return Regen(hField, hFrom, hTo);
    }
 
    public string Regen(string hField, string hFrom, string hTo)
    {
        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
                string evalStr = null;
                Entity fromEntity = tr.GetObject(_idFrom, OpenMode.ForRead, false) as Entity;
                if (fromEntity == null)
                {
                }
                if (fromEntity .GetType() == typeof(MText))
                {
                    evalStr  = ((MText)fromEntity ).Contents;
                }
                else
                {
                    evalStr  = ((DBText)fromEntity ).TextString;
                }
 
                string result = MathParser.EvalExpression(evalStr.ToCharArray()).ToString();  
 
                Entity toEntity = tr.GetObject(_idTo, OpenMode.ForWrite, false) as Entity;
                if (toEntity == null)
                {
                }
                if (toEntity .GetType() == typeof(MText))
                {
                    ((MText)toEntity ).Contents = result.ToString();
                }
                else
                {
                    ((DBText)toEntity ).TextString = result.ToString();
                }
                tr.Commit();
        }
        return "계산기";
    }
 
    public void Modified(string hnd)
    {
    }
 
    public void Erased(string isErasing)
    {
    }
 
    public void Copied(string newId)
    {
    }
 
    public void Exit(string msg)
    {
    }
}

댓글 없음:

댓글 쓰기